This repository was archived by the owner on Jun 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtest_09_cookie_handler.py
More file actions
339 lines (304 loc) · 12.5 KB
/
test_09_cookie_handler.py
File metadata and controls
339 lines (304 loc) · 12.5 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
329
330
331
332
333
334
335
336
337
338
339
import pytest
from cryptojwt.jwk.hmac import SYMKey
from oidcop.cookie_handler import CookieHandler
from oidcop.cookie_handler import compute_session_state
KEYDEFS = [
{"type": "OCT", "kid": "sig", "use": ["sig"]},
{"type": "OCT", "kid": "enc", "use": ["enc"]},
]
class TestCookieSign(object):
@pytest.fixture(autouse=True)
def make_cookie_content_handler(self):
cookie_conf = {
"sign_key": SYMKey(k="ghsNKDDLshZTPn974nOsIGhedULrsqnsGoBFBLwUKuJhE2ch"),
}
self.cookie_handler = CookieHandler(**cookie_conf)
def test_init(self):
assert self.cookie_handler
def test_make_cookie_content(self):
_cookie_info = self.cookie_handler.make_cookie_content("oidcop", "value", "sso")
assert _cookie_info
assert set(_cookie_info.keys()) == {
"name", "value", "samesite", "httponly", "secure"
}
assert len(_cookie_info["value"].split("|")) == 3
def test_make_cookie_content_max_age(self):
_cookie_info = self.cookie_handler.make_cookie_content(
"oidcop", "value", "sso", max_age=3600
)
assert _cookie_info
assert set(_cookie_info.keys()) == {
'name', 'value', 'max-age', 'samesite', 'httponly', 'secure'
}
assert len(_cookie_info["value"].split("|")) == 3
def test_read_cookie_info(self):
_cookie_info = [self.cookie_handler.make_cookie_content("oidcop", "value", "sso")]
returned = [{"name": c["name"], "value": c["value"]} for c in _cookie_info]
_info = self.cookie_handler.parse_cookie("oidcop", returned)
assert len(_info) == 1
assert set(_info[0].keys()) == {"value", "type", "timestamp"}
assert _info[0]["value"] == "value"
assert _info[0]["type"] == "sso"
def test_mult_cookie(self):
_cookie = [
self.cookie_handler.make_cookie_content("oidcop", "value", "sso"),
self.cookie_handler.make_cookie_content("oidcop", "session_state", "session"),
]
assert len(_cookie) == 2
_c_info = self.cookie_handler.parse_cookie("oidcop", _cookie)
assert len(_c_info) == 2
assert _c_info[0]["value"] == "value"
assert _c_info[0]["type"] == "sso"
assert _c_info[1]["value"] == "session_state"
assert _c_info[1]["type"] == "session"
class TestCookieHandlerSignEnc(object):
@pytest.fixture(autouse=True)
def make_cookie_handler(self):
cookie_conf = {
"sign_key": SYMKey(k="ghsNKDDLshZTPn974nOsIGhedULrsqnsGoBFBLwUKuJhE2ch"),
"enc_key": SYMKey(k="NXi6HD473d_YS4exVRn7z9z23mGmvU641MuvKqH0o7Y"),
}
self.cookie_handler = CookieHandler(**cookie_conf)
def test_make_cookie_content(self):
_cookie_info = self.cookie_handler.make_cookie_content("oidcop", "value", "sso")
assert _cookie_info
assert set(_cookie_info.keys()) == {
'name', 'value', 'samesite', 'httponly', 'secure'
}
assert len(_cookie_info["value"].split("|")) == 4
def test_make_cookie_content_max_age(self):
_cookie_info = self.cookie_handler.make_cookie_content(
"oidcop", "value", "sso", max_age=3600
)
assert _cookie_info
assert set(_cookie_info.keys()) == {
'name', 'value', 'max-age', 'samesite', 'httponly', 'secure'
}
assert len(_cookie_info["value"].split("|")) == 4
def test_read_cookie_info(self):
_cookie_info = [self.cookie_handler.make_cookie_content("oidcop", "value", "sso")]
returned = [{"name": c["name"], "value": c["value"]} for c in _cookie_info]
_info = self.cookie_handler.parse_cookie("oidcop", returned)
assert len(_info) == 1
assert set(_info[0].keys()) == {"value", "type", "timestamp"}
assert _info[0]["value"] == "value"
assert _info[0]["type"] == "sso"
def test_mult_cookie(self):
_cookie = [
self.cookie_handler.make_cookie_content("oidcop", "value", "sso"),
self.cookie_handler.make_cookie_content("oidcop", "session_state", "session"),
]
assert len(_cookie) == 2
_c_info = self.cookie_handler.parse_cookie("oidcop", _cookie)
assert len(_c_info) == 2
assert _c_info[0]["value"] == "value"
assert _c_info[0]["type"] == "sso"
assert _c_info[1]["value"] == "session_state"
assert _c_info[1]["type"] == "session"
class TestCookieHandlerEnc(object):
@pytest.fixture(autouse=True)
def make_cookie_content_handler(self):
cookie_conf = {
"enc_key": SYMKey(k="NXi6HD473d_YS4exVRn7z9z23mGmvU641MuvKqH0o7Y"),
}
self.cookie_handler = CookieHandler(**cookie_conf)
def test_make_cookie_content(self):
_cookie_info = self.cookie_handler.make_cookie_content("oidcop", "value", "sso")
assert _cookie_info
assert set(_cookie_info.keys()) == {
'name', 'value', 'samesite', 'httponly', 'secure'
}
assert len(_cookie_info["value"].split("|")) == 4
def test_make_cookie_content_max_age(self):
_cookie_info = self.cookie_handler.make_cookie_content(
"oidcop", "value", "sso", max_age=3600
)
assert _cookie_info
assert set(_cookie_info.keys()) == {
'name', 'value', 'max-age', 'samesite', 'httponly', 'secure'
}
assert len(_cookie_info["value"].split("|")) == 4
def test_read_cookie_info(self):
_cookie_info = [self.cookie_handler.make_cookie_content("oidcop", "value", "sso")]
returned = [{"name": c["name"], "value": c["value"]} for c in _cookie_info]
_info = self.cookie_handler.parse_cookie("oidcop", returned)
assert len(_info) == 1
assert set(_info[0].keys()) == {"value", "type", "timestamp"}
assert _info[0]["value"] == "value"
assert _info[0]["type"] == "sso"
def test_mult_cookie(self):
_cookie = [
self.cookie_handler.make_cookie_content("oidcop", "value", "sso"),
self.cookie_handler.make_cookie_content("oidcop", "session_state", "session"),
]
assert len(_cookie) == 2
_c_info = self.cookie_handler.parse_cookie("oidcop", _cookie)
assert len(_c_info) == 2
assert _c_info[0]["value"] == "value"
assert _c_info[0]["type"] == "sso"
assert _c_info[1]["value"] == "session_state"
assert _c_info[1]["type"] == "session"
class TestCookieHandlerSignEncKeys(object):
@pytest.fixture(autouse=True)
def make_cookie_handler(self):
cookie_conf = {
"keys": {
"private_path": "private/cookie_jwks.json",
"key_defs": KEYDEFS,
"read_only": False,
}
}
self.cookie_handler = CookieHandler(**cookie_conf)
def test_make_cookie_content(self):
_cookie_info = self.cookie_handler.make_cookie_content("oidcop", "value", "sso")
assert _cookie_info
assert set(_cookie_info.keys()) == {
'name', 'value', 'samesite', 'httponly', 'secure'
}
assert len(_cookie_info["value"].split("|")) == 4
def test_make_cookie_content_max_age(self):
_cookie_info = self.cookie_handler.make_cookie_content(
"oidcop", "value", "sso", max_age=3600
)
assert _cookie_info
assert set(_cookie_info.keys()) == {
'name', 'value', 'max-age', 'samesite', 'httponly', 'secure'
}
assert len(_cookie_info["value"].split("|")) == 4
def test_read_cookie_info(self):
_cookie_info = [self.cookie_handler.make_cookie_content("oidcop", "value", "sso")]
returned = [{"name": c["name"], "value": c["value"]} for c in _cookie_info]
_info = self.cookie_handler.parse_cookie("oidcop", returned)
assert len(_info) == 1
assert set(_info[0].keys()) == {"value", "type", "timestamp"}
assert _info[0]["value"] == "value"
assert _info[0]["type"] == "sso"
def test_mult_cookie(self):
_cookie = [
self.cookie_handler.make_cookie_content("oidcop", "value", "sso"),
self.cookie_handler.make_cookie_content("oidcop", "session_state", "session"),
]
assert len(_cookie) == 2
_c_info = self.cookie_handler.parse_cookie("oidcop", _cookie)
assert len(_c_info) == 2
assert _c_info[0]["value"] == "value"
assert _c_info[0]["type"] == "sso"
assert _c_info[1]["value"] == "session_state"
assert _c_info[1]["type"] == "session"
def test_compute_session_state():
hv = compute_session_state("state", "salt", "client_id", "https://example.com/redirect")
assert hv == "d21113fbe4b54661ae45f3a3233b0f865ccc646af248274b6fa5664267540e29.salt"
#
# def test_create_session_cookie():
# kaka = create_session_cookie(
# "sess_man", "session_state", domain="example.com", path="/"
# )
#
# assert isinstance(kaka, SimpleCookie)
# assert {"sess_man"} == set(kaka.keys())
# morsel = kaka["sess_man"]
# assert morsel.value == "session_state"
# assert morsel["path"] == "/"
# assert morsel["domain"] == "example.com"
#
#
# def test_append_cookie():
# kaka1 = create_session_cookie(
# "sess_man", "session_state", domain="example.com", path="/"
# )
# kaka2 = create_session_cookie("foobar", "value", domain="example.com", path="/")
#
# kakor = append_cookie(kaka1, kaka2)
# assert {"sess_man", "foobar"} == set(kakor.keys())
#
#
# conf = {
# "issuer": "https://example.com/",
# "httpc_params": {"verify": False, "timeout": 1},
# "token_expires_in": 600,
# "grant_expires_in": 300,
# "refresh_token_expires_in": 86400,
# "verify_ssl": False,
# "endpoint": {"token": {"path": "token", "class": Token, "kwargs": {}}},
# "template_dir": "template",
# "keys": {
# "private_path": "own/jwks.json",
# "key_defs": KEYDEFS,
# "uri_path": "static/jwks.json",
# },
# }
#
# cookie_conf = {
# "sign_key": SYMKey(k="ghsNKDDLshZTPn974nOsIGhedULrsqnsGoBFBLwUKuJhE2ch"),
# "default_values": {
# "name": "oidc_op",
# "domain": "example.com",
# "path": "/",
# "max_age": 3600,
# },
# }
#
# client_id = "client_id"
# client_secret = "a_longer_client_secret"
# # Need to add the client_secret as a symmetric key bound to the client_id
# KEYJAR.add_symmetric(client_id, client_secret, ["sig"])
#
# endpoint_context = EndpointContext(conf, keyjar=KEYJAR)
# endpoint_context.cdb[client_id] = {"client_secret": client_secret}
# endpoint_context.cookie_handler = CookieHandler(**cookie_conf)
#
# enc_key = rndstr(32)
#
#
# def test_new_cookie():
# kaka = new_cookie(
# endpoint_context, "foobar", client_id="client_id", sid="sessionID"
# )
# assert isinstance(kaka, SimpleCookie)
# assert {"foobar"} == set(kaka.keys())
#
# val = endpoint_context.cookie_handler.get_cookie_value(kaka, "foobar")
# assert isinstance(val, tuple)
# b64val, ts, typ = val
# info = cookie_value(b64val)
# assert set(info.keys()) == {"client_id", "sid"}
#
#
# def test_cookie_default():
# _key = SYMKey(k="ghsNKDDLshZTPn974nOsIGhedULrsqnsGoBFBLwUKuJhE2ch")
# kaka = make_cookie("test", "data", sign_key=_key)
# assert kaka["test"]["secure"] is True
# assert kaka["test"]["httponly"] is True
# assert kaka["test"]["samesite"] is ""
#
#
# def test_cookie_http_only_false():
# _key = SYMKey(k="ghsNKDDLshZTPn974nOsIGhedULrsqnsGoBFBLwUKuJhE2ch")
# kaka = make_cookie("test", "data", sign_key=_key, http_only=False)
# assert kaka["test"]["secure"] is True
# assert kaka["test"]["httponly"] is False
# assert kaka["test"]["samesite"] is ""
#
#
# def test_cookie_not_secure():
# _key = SYMKey(k="ghsNKDDLshZTPn974nOsIGhedULrsqnsGoBFBLwUKuJhE2ch")
# kaka = make_cookie("test", "data", _key, secure=False)
# assert kaka["test"]["secure"] is False
# assert kaka["test"]["httponly"] is True
# assert kaka["test"]["samesite"] is ""
#
#
# def test_cookie_same_site_none():
# _key = SYMKey(k="ghsNKDDLshZTPn974nOsIGhedULrsqnsGoBFBLwUKuJhE2ch")
# kaka = make_cookie("test", "data", sign_key=_key, same_site="None")
# assert kaka["test"]["secure"] is True
# assert kaka["test"]["httponly"] is True
# assert kaka["test"]["samesite"] is "None"
#
#
# def test_cookie_enc():
# _key = SYMKey(k=enc_key)
# _enc_data = sign_enc_payload(json.dumps({"test": "data"}), timestamp=time.time(),
# enc_key=_key)
# _data, _timestamp = ver_dec_content(_enc_data.split('|'), enc_key=_key)
# assert json.loads(_data) == {"test": "data"}