Projet

Général

Profil

0004-idp_oidc-render-templated-claims-in-user-info-creati.patch

Paul Marillonnet, 25 février 2020 12:40

Télécharger (4,92 ko)

Voir les différences:

Subject: [PATCH 4/4] idp_oidc: render templated claims in user-info-creation
 utilities (#37884)

 src/authentic2_idp_oidc/utils.py | 11 +++--
 tests/test_idp_oidc.py           | 72 ++++++++++++++++++++++++++++++++
 2 files changed, 80 insertions(+), 3 deletions(-)
src/authentic2_idp_oidc/utils.py
30 30

  
31 31
from authentic2 import hooks, crypto
32 32
from authentic2.attributes_ng.engine import get_attributes
33
from authentic2.utils.template import Template
33 34

  
34 35
from . import app_settings
35 36

  
......
196 197
        if not set(claim.get_scopes()).intersection(scope_set):
197 198
            continue
198 199
        claims_to_show.add(claim)
199
        if claim.value not in attributes:
200
            continue
201
        attribute_value = attributes[claim.value]
200
        if claim.value and ('{{' in claim.value or '{%' in claim.value):
201
            template = Template(claim.value)
202
            attribute_value = template.render(context=attributes)
203
        else:
204
            if claim.value not in attributes:
205
                continue
206
            attribute_value = attributes[claim.value]
202 207
        if attribute_value is None:
203 208
            continue
204 209
        user_info[claim.name] = normalize_claim_values(attribute_value)
tests/test_idp_oidc.py
1115 1115
    assert 'family_name' not in user_info
1116 1116

  
1117 1117

  
1118
def test_claim_templated(oidc_settings, normal_oidc_client, simple_user, app):
1119
    oidc_settings.A2_IDP_OIDC_SCOPES = ['openid', 'profile', 'email']
1120
    OIDCClaim.objects.filter(
1121
            client=normal_oidc_client, name='given_name').delete()
1122
    OIDCClaim.objects.filter(
1123
            client=normal_oidc_client, name='family_name').delete()
1124
    claim1 = OIDCClaim.objects.create(
1125
            client=normal_oidc_client,
1126
            name='given_name',
1127
            value='{{ django_user_first_name|add:"ounet" }}',
1128
            scopes='profile')
1129
    claim2 = OIDCClaim.objects.create(
1130
            client=normal_oidc_client,
1131
            name='family_name',
1132
            value='{{ "Von der "|add:django_user_last_name }}',
1133
            scopes='profile')
1134
    normal_oidc_client.authorization_flow = normal_oidc_client.FLOW_AUTHORIZATION_CODE
1135
    normal_oidc_client.authorization_mode = normal_oidc_client.AUTHORIZATION_MODE_NONE
1136
    normal_oidc_client.save()
1137

  
1138
    utils.login(app, simple_user)
1139

  
1140
    oidc_client = normal_oidc_client
1141
    redirect_uri = oidc_client.redirect_uris.split()[0]
1142

  
1143
    params = {
1144
        'client_id': oidc_client.client_id,
1145
        'scope': 'openid email profile',
1146
        'redirect_uri': redirect_uri,
1147
        'state': 'xxx',
1148
        'nonce': 'yyy',
1149
        'response_type': 'code',
1150
    }
1151

  
1152
    def sso():
1153
        authorize_url = make_url('oidc-authorize', params=params)
1154

  
1155
        response = app.get(authorize_url)
1156
        location = urlparse.urlparse(response['Location'])
1157
        query = urlparse.parse_qs(location.query)
1158
        code = query['code'][0]
1159

  
1160
        token_url = make_url('oidc-token')
1161
        response = app.post(token_url, params={
1162
            'grant_type': 'authorization_code',
1163
            'code': code,
1164
            'redirect_uri': oidc_client.redirect_uris.split()[0],
1165
        }, headers=client_authentication_headers(oidc_client))
1166
        access_token = response.json['access_token']
1167
        id_token = response.json['id_token']
1168

  
1169
        key = JWK(kty='oct', k=base64.b64encode(oidc_client.client_secret.encode('utf-8')))
1170
        jwt = JWT(jwt=id_token, key=key)
1171
        claims = json.loads(jwt.claims)
1172

  
1173
        user_info_url = make_url('oidc-user-info')
1174
        response = app.get(user_info_url, headers=bearer_authentication_headers(access_token))
1175
        return claims, response.json
1176

  
1177
    claims, user_info = sso()
1178

  
1179
    assert claims['given_name'].endswith('ounet')
1180
    assert claims['given_name'].startswith(simple_user.first_name)
1181
    assert claims['family_name'].startswith('Von der')
1182
    assert claims['family_name'].endswith(simple_user.last_name)
1183

  
1184
    assert user_info['given_name'].endswith('ounet')
1185
    assert user_info['given_name'].startswith(simple_user.first_name)
1186
    assert user_info['family_name'].startswith('Von der')
1187
    assert user_info['family_name'].endswith(simple_user.last_name)
1188

  
1189

  
1118 1190
def test_client_is_valid_redirect_uri():
1119 1191
    client = OIDCClient(redirect_uris='''http://example.com
1120 1192
http://example2.com/
1121
-