<commit_before>"""
    Assertion test module
    ~~~~~~~~~~~~~~~~~~~~~

    :copyright: (c) 2016 by <NAME>.
    :license: APACHE 2.0, see LICENSE for more details.
"""
import json
from aatest.events import EV_PROTOCOL_RESPONSE
from future.backports.urllib.parse import parse_qs
from oic.oauth2 import message


def get_provider_info(conv):
    _pi = conv.entity.provider_info
    if not _pi:
        _pi = conv.provider_info
    return _pi


def get_protocol_response(conv, cls):
    return conv.events.get_messages(EV_PROTOCOL_RESPONSE, cls)


def get_id_tokens(conv):
    res = []
    # In access token responses
    for inst, msg in get_protocol_response(conv, message.AccessTokenResponse):
        _dict = json.loads(msg)
        jwt = _dict["id_token"]
        idt = inst["id_token"]
        res.append((idt, jwt))

    # implicit, id_token in authorization response
    for inst, msg in get_protocol_response(conv, message.AuthorizationResponse):
        try:
            idt = inst["id_token"]
        except KeyError:
            pass
        else:
            _info = parse_qs(msg)
            jwt = _info["id_token"][0]
            res.append((idt, jwt))

    return res<commit_msg>The function get_protocol_response only returns a response instance.  Added two new functions:  - finds an id_token as the original JWS.  - gets the last authorization request<commit_after>"""
    Assertion test module
    ~~~~~~~~~~~~~~~~~~~~~

    :copyright: (c) 2016 by <NAME>.
    :license: APACHE 2.0, see LICENSE for more details.
"""
import json

from aatest.events import EV_PROTOCOL_RESPONSE
from aatest.events import EV_RESPONSE
from aatest.events import EV_REDIRECT_URL
from future.backports.urllib.parse import parse_qs

from oic.oic import message
from oic.oauth2 import AuthorizationRequest


def get_provider_info(conv):
    _pi = conv.entity.provider_info
    if not _pi:
        _pi = conv.provider_info
    return _pi


def get_protocol_response(conv, cls):
    return conv.events.get_messages(EV_PROTOCOL_RESPONSE, cls)


def get_id_tokens(conv):
    res = []
    # In access token responses
    for inst in get_protocol_response(conv, message.AccessTokenResponse):
        res.append(inst["id_token"])

    # implicit, id_token in authorization response
    for inst in get_protocol_response(conv, message.AuthorizationResponse):
        try:
            res.append(inst["id_token"])
        except KeyError:
            pass

    return res


def get_signed_id_tokens(conv):
    res = []
    for txt in conv.events.get_data(EV_RESPONSE):
        try:
            ent = json.loads(txt)
        except Exception:
            try:
                ent = parse_qs(txt)
            except:
                pass
            else:
                try:
                    res.append(ent['id_token'][0])
                except KeyError:
                    pass
        else:
            try:
                res.append(ent['id_token'])
            except KeyError:
                pass

    return res


def get_authorization_request(conv, cls):
    authz_req = conv.events.get_data(EV_REDIRECT_URL)[0].split('?')[1]
    return cls().from_urlencoded(authz_req)
{'fraction_non_alphanumeric': 0.09665192672141504, 'fraction_numerical': 0.005685407454200884, 'mean_word_length': 2.530657748049052, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1956319', 'n_tokens_mistral': 982, 'n_tokens_neox': 961, 'n_words': 245}