This repository was archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_07_session.py
More file actions
328 lines (271 loc) · 8.56 KB
/
test_07_session.py
File metadata and controls
328 lines (271 loc) · 8.56 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import os
import time
import pytest
from cryptojwt.jws.jws import JWS
from cryptojwt.jws.utils import alg2keytype
from cryptojwt.key_bundle import KeyBundle
from cryptojwt.key_jar import KeyJar
from cryptojwt.key_jar import init_key_jar
from oidcmsg.exception import MessageException
from oidcmsg.exception import NotForMe
from oidcmsg.oidc import Claims
from oidcmsg.oidc import ClaimsRequest
from oidcmsg.oidc import IdToken
from oidcmsg.oidc import verified_claim_name
from oidcmsg.oidc.session import BACK_CHANNEL_LOGOUT_EVENT
from oidcmsg.oidc.session import BackChannelLogoutRequest
from oidcmsg.oidc.session import CheckSessionRequest
from oidcmsg.oidc.session import EndSessionRequest
from oidcmsg.oidc.session import EndSessionResponse
from oidcmsg.oidc.session import LogoutToken
from oidcmsg.time_util import utc_time_sans_frac
CLIENT_ID = "client_1"
ISS = "https://example.com"
IDTOKEN = IdToken(
iss=ISS,
sub="sub",
aud=CLIENT_ID,
exp=utc_time_sans_frac() + 300,
nonce="N0nce",
iat=time.time(),
)
KC_SYM_S = KeyBundle(
{"kty": "oct", "key": "abcdefghijklmnop".encode("utf-8"), "use": "sig", "alg": "HS256"}
)
NOW = utc_time_sans_frac()
KEYDEF = [
{"type": "EC", "crv": "P-256", "use": ["sig"]},
{"type": "EC", "crv": "P-256", "use": ["enc"]},
]
def full_path(local_file):
_dirname = os.path.dirname(os.path.abspath(__file__))
return os.path.join(_dirname, local_file)
CLI_KEY = init_key_jar(
public_path=full_path("pub_client.jwks"),
private_path=full_path("priv_client.jwks"),
key_defs=KEYDEF,
issuer_id=CLIENT_ID,
)
ISS_KEY = init_key_jar(
public_path=full_path("pub_iss.jwks"),
private_path=full_path("priv_iss.jwks"),
key_defs=KEYDEF,
issuer_id=ISS,
)
ISS_KEY.import_jwks_as_json(open(full_path("pub_client.jwks")).read(), CLIENT_ID)
CLI_KEY.import_jwks_as_json(open(full_path("pub_iss.jwks")).read(), ISS)
class TestEndSessionResponse(object):
def test_example(self):
esr = EndSessionResponse()
class TestEndSessionRequest(object):
def test_example(self):
_symkey = KC_SYM_S.get(alg2keytype("HS256"))
esreq = EndSessionRequest(
id_token_hint=IDTOKEN.to_jwt(key=_symkey, algorithm="HS256", lifetime=300),
redirect_url="http://example.org/jqauthz",
state="state0",
)
request = EndSessionRequest().from_urlencoded(esreq.to_urlencoded())
keyjar = KeyJar()
for _key in _symkey:
keyjar.add_symmetric("", _key.key)
keyjar.add_symmetric(ISS, _key.key)
keyjar.add_symmetric(CLIENT_ID, _key.key)
request.verify(keyjar=keyjar)
assert isinstance(request, EndSessionRequest)
assert set(request.keys()) == {
verified_claim_name("id_token_hint"),
"id_token_hint",
"redirect_url",
"state",
}
assert request["state"] == "state0"
assert request[verified_claim_name("id_token_hint")]["aud"] == ["client_1"]
class TestCheckSessionRequest(object):
def test_example(self):
_symkey = KC_SYM_S.get(alg2keytype("HS256"))
csr = CheckSessionRequest(
id_token=IDTOKEN.to_jwt(key=_symkey, algorithm="HS256", lifetime=300)
)
keyjar = KeyJar()
keyjar.add_kb("", KC_SYM_S)
with pytest.raises(ValueError):
assert csr.verify(keyjar=keyjar)
def test_example_1(self):
_symkey = KC_SYM_S.get(alg2keytype("HS256"))
csr = CheckSessionRequest(
id_token=IDTOKEN.to_jwt(key=_symkey, algorithm="HS256", lifetime=300)
)
keyjar = KeyJar()
keyjar.add_kb(ISS, KC_SYM_S)
assert csr.verify(keyjar=keyjar)
class TestClaimsRequest(object):
def test_example(self):
claims = {
"name": {"essential": True},
"nickname": None,
"email": {"essential": True},
"verified": {"essential": True},
"picture": None,
}
cr = ClaimsRequest(
userinfo=Claims(**claims), id_token=Claims(auth_time=None, acr={"values": ["2"]})
)
cr.verify()
_url = cr.to_urlencoded()
cr1 = ClaimsRequest().from_urlencoded(_url)
cr1.verify()
_js = cr.to_json()
cr1 = ClaimsRequest().from_json(_js)
cr1.verify()
def test_logout_token_1():
val = {
"iss": ISS,
"sub": "248289761001",
"aud": [CLIENT_ID],
"iat": NOW,
"jti": "bWJq",
"sid": "08a5019c-17e1-4977-8f42-65a12843ea02",
"events": {BACK_CHANNEL_LOGOUT_EVENT: {}},
}
lt = LogoutToken(**val)
assert lt.verify()
def test_logout_token_2():
val = {
"iss": ISS,
"sub": "248289761001",
"aud": [CLIENT_ID],
"iat": NOW,
"jti": "bWJq",
"events": {BACK_CHANNEL_LOGOUT_EVENT: {}},
}
lt = LogoutToken(**val)
assert lt.verify()
def test_logout_token_3():
val = {
"iss": ISS,
"aud": [CLIENT_ID],
"iat": NOW,
"jti": "bWJq",
"sid": "08a5019c-17e1-4977-8f42-65a12843ea02",
"events": {BACK_CHANNEL_LOGOUT_EVENT: {}},
}
lt = LogoutToken(**val)
assert lt.verify()
def test_logout_token_4():
val = {
"iss": ISS,
"aud": [CLIENT_ID],
"iat": NOW,
"jti": "bWJq",
"events": {BACK_CHANNEL_LOGOUT_EVENT: {}},
}
lt = LogoutToken(**val)
with pytest.raises(ValueError):
lt.verify()
def test_logout_token_5():
val = {
"iss": ISS,
"aud": [CLIENT_ID],
"iat": NOW,
"jti": "bWJq",
"events": {BACK_CHANNEL_LOGOUT_EVENT: {"foo": "bar"}},
}
lt = LogoutToken(**val)
with pytest.raises(ValueError):
lt.verify()
def test_logout_token_6():
val = {
"iss": ISS,
"aud": [CLIENT_ID],
"iat": NOW,
"jti": "bWJq",
"events": {"http://schemas.openid.net/event/foo": {}},
}
lt = LogoutToken(**val)
with pytest.raises(ValueError):
lt.verify()
def test_logout_token_7():
val = {
"iss": ISS,
"aud": [CLIENT_ID],
"iat": NOW,
"jti": "bWJq",
"events": {BACK_CHANNEL_LOGOUT_EVENT: {}, "http://schemas.openid.net/event/foo": {}},
}
lt = LogoutToken(**val)
with pytest.raises(ValueError):
lt.verify()
def test_logout_token_with_nonce():
val = {
"iss": ISS,
"aud": [CLIENT_ID],
"iat": NOW,
"jti": "bWJq",
"sid": "08a5019c-17e1-4977-8f42-65a12843ea02",
"events": {BACK_CHANNEL_LOGOUT_EVENT: {}},
"nonce": "1234567890",
}
lt = LogoutToken(**val)
with pytest.raises(MessageException):
lt.verify()
def test_logout_token_wrong_iat():
val = {
"iss": ISS,
"aud": [CLIENT_ID],
"iat": NOW + 10,
"jti": "bWJq",
"sid": "08a5019c-17e1-4977-8f42-65a12843ea02",
"events": {BACK_CHANNEL_LOGOUT_EVENT: {}},
}
lt = LogoutToken(**val)
with pytest.raises(ValueError):
lt.verify()
# Within allowed clock skew
lt.verify(skew=60)
def test_logout_token_wrong_aud():
val = {
"iss": ISS,
"aud": [CLIENT_ID],
"iat": NOW,
"jti": "bWJq",
"sid": "08a5019c-17e1-4977-8f42-65a12843ea02",
"events": {BACK_CHANNEL_LOGOUT_EVENT: {}},
}
lt = LogoutToken(**val)
with pytest.raises(NotForMe):
lt.verify(aud="deep_purple")
lt.verify(aud=CLIENT_ID)
def test_logout_token_wrong_iss():
val = {
"iss": ISS,
"aud": [CLIENT_ID],
"iat": NOW,
"jti": "bWJq",
"sid": "08a5019c-17e1-4977-8f42-65a12843ea02",
"events": {BACK_CHANNEL_LOGOUT_EVENT: {}},
}
lt = LogoutToken(**val)
with pytest.raises(NotForMe):
lt.verify(iss="deep_purple")
lt.verify(iss=ISS)
def test_back_channel_logout_request():
val = {
"iss": ISS,
"aud": [CLIENT_ID],
"iat": NOW,
"jti": "bWJq",
"sid": "08a5019c-17e1-4977-8f42-65a12843ea02",
"events": {BACK_CHANNEL_LOGOUT_EVENT: {}},
}
lt = LogoutToken(**val)
signer = JWS(lt.to_json(), alg="ES256")
_jws = signer.sign_compact(keys=ISS_KEY.get_signing_key(issuer_id=ISS))
bclr = BackChannelLogoutRequest(logout_token=_jws)
# This is how it is posted
_req = bclr.to_urlencoded()
_request = BackChannelLogoutRequest().from_urlencoded(_req)
assert "logout_token" in _request
_verified = _request.verify(keyjar=CLI_KEY, iss=ISS, aud=CLIENT_ID, skew=30)
assert _verified
assert set(_request.keys()) == {"logout_token", "__verified_logout_token"}