File: test_user_mixin.py

package info (click to toggle)
python-authlib 1.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,016 kB
  • sloc: python: 26,998; makefile: 53; sh: 14
file content (184 lines) | stat: -rw-r--r-- 5,747 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from unittest import TestCase
from unittest import mock

import pytest
from flask import Flask

from authlib.integrations.flask_client import OAuth
from authlib.jose import JsonWebKey
from authlib.jose.errors import InvalidClaimError
from authlib.oidc.core.grants.util import generate_id_token

from ..util import get_bearer_token
from ..util import read_key_file

secret_key = JsonWebKey.import_key("secret", {"kty": "oct", "kid": "f"})


class FlaskUserMixinTest(TestCase):
    def test_fetch_userinfo(self):
        app = Flask(__name__)
        app.secret_key = "!"
        oauth = OAuth(app)
        client = oauth.register(
            "dev",
            client_id="dev",
            client_secret="dev",
            fetch_token=get_bearer_token,
            userinfo_endpoint="https://i.b/userinfo",
        )

        def fake_send(sess, req, **kwargs):
            resp = mock.MagicMock()
            resp.json = lambda: {"sub": "123"}
            resp.status_code = 200
            return resp

        with app.test_request_context():
            with mock.patch("requests.sessions.Session.send", fake_send):
                user = client.userinfo()
                assert user.sub == "123"

    def test_parse_id_token(self):
        token = get_bearer_token()
        id_token = generate_id_token(
            token,
            {"sub": "123"},
            secret_key,
            alg="HS256",
            iss="https://i.b",
            aud="dev",
            exp=3600,
            nonce="n",
        )

        app = Flask(__name__)
        app.secret_key = "!"
        oauth = OAuth(app)
        client = oauth.register(
            "dev",
            client_id="dev",
            client_secret="dev",
            fetch_token=get_bearer_token,
            jwks={"keys": [secret_key.as_dict()]},
            issuer="https://i.b",
            id_token_signing_alg_values_supported=["HS256", "RS256"],
        )
        with app.test_request_context():
            assert client.parse_id_token(token, nonce="n") is None

            token["id_token"] = id_token
            user = client.parse_id_token(token, nonce="n")
            assert user.sub == "123"

            claims_options = {"iss": {"value": "https://i.b"}}
            user = client.parse_id_token(
                token, nonce="n", claims_options=claims_options
            )
            assert user.sub == "123"

            claims_options = {"iss": {"value": "https://i.c"}}
            with pytest.raises(InvalidClaimError):
                client.parse_id_token(token, "n", claims_options)

    def test_parse_id_token_nonce_supported(self):
        token = get_bearer_token()
        id_token = generate_id_token(
            token,
            {"sub": "123", "nonce_supported": False},
            secret_key,
            alg="HS256",
            iss="https://i.b",
            aud="dev",
            exp=3600,
        )

        app = Flask(__name__)
        app.secret_key = "!"
        oauth = OAuth(app)
        client = oauth.register(
            "dev",
            client_id="dev",
            client_secret="dev",
            fetch_token=get_bearer_token,
            jwks={"keys": [secret_key.as_dict()]},
            issuer="https://i.b",
            id_token_signing_alg_values_supported=["HS256", "RS256"],
        )
        with app.test_request_context():
            token["id_token"] = id_token
            user = client.parse_id_token(token, nonce="n")
            assert user.sub == "123"

    def test_runtime_error_fetch_jwks_uri(self):
        token = get_bearer_token()
        id_token = generate_id_token(
            token,
            {"sub": "123"},
            secret_key,
            alg="HS256",
            iss="https://i.b",
            aud="dev",
            exp=3600,
            nonce="n",
        )

        app = Flask(__name__)
        app.secret_key = "!"
        oauth = OAuth(app)
        alt_key = secret_key.as_dict()
        alt_key["kid"] = "b"
        client = oauth.register(
            "dev",
            client_id="dev",
            client_secret="dev",
            fetch_token=get_bearer_token,
            jwks={"keys": [alt_key]},
            issuer="https://i.b",
            id_token_signing_alg_values_supported=["HS256"],
        )
        with app.test_request_context():
            token["id_token"] = id_token
            with pytest.raises(RuntimeError):
                client.parse_id_token(token, "n")

    def test_force_fetch_jwks_uri(self):
        secret_keys = read_key_file("jwks_private.json")
        token = get_bearer_token()
        id_token = generate_id_token(
            token,
            {"sub": "123"},
            secret_keys,
            alg="RS256",
            iss="https://i.b",
            aud="dev",
            exp=3600,
            nonce="n",
        )

        app = Flask(__name__)
        app.secret_key = "!"
        oauth = OAuth(app)
        client = oauth.register(
            "dev",
            client_id="dev",
            client_secret="dev",
            fetch_token=get_bearer_token,
            jwks={"keys": [secret_key.as_dict()]},
            jwks_uri="https://i.b/jwks",
            issuer="https://i.b",
        )

        def fake_send(sess, req, **kwargs):
            resp = mock.MagicMock()
            resp.json = lambda: read_key_file("jwks_public.json")
            resp.status_code = 200
            return resp

        with app.test_request_context():
            assert client.parse_id_token(token, nonce="n") is None

            with mock.patch("requests.sessions.Session.send", fake_send):
                token["id_token"] = id_token
                user = client.parse_id_token(token, nonce="n")
                assert user.sub == "123"