Skip to content
This repository was archived by the owner on Jun 23, 2023. It is now read-only.

Commit 0bbdf9b

Browse files
committed
chore: coverage of user_authn.user increased
1 parent 571ee05 commit 0bbdf9b

2 files changed

Lines changed: 44 additions & 9 deletions

File tree

src/oidcop/user_authn/user.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,14 @@ def __call__(self, **kwargs):
169169
),
170170
OnlyForTestingWarning,
171171
)
172-
172+
if not self.server_get:
173+
raise Exception(
174+
f"{self.__class__.__name__} doesn't have a working server_get"
175+
)
173176
_context = self.server_get("endpoint_context")
174177
# Stores information need afterwards in a signed JWT that then
175178
# appears as a hidden input in the form
176179
jws = create_signed_jwt(_context.issuer, _context.keyjar, **kwargs)
177-
178180
_kwargs = self.kwargs.copy()
179181
for attr in ["policy", "tos", "logo"]:
180182
_uri = "{}_uri".format(attr)
@@ -220,8 +222,8 @@ def authenticated_as(self, client_id, cookie=None, authorization="", **kwargs):
220222
authorization = authorization[6:]
221223

222224
(user, pwd) = base64.b64decode(authorization).split(b":")
223-
user = unquote(user)
224-
self.verify_password(user, pwd)
225+
user = unquote(user.decode())
226+
self.verify_password(user, pwd.decode())
225227
res = {"uid": user}
226228
if cookie:
227229
res.update(self.cookie_info(cookie, client_id))
@@ -237,7 +239,7 @@ def __init__(self, ttl, symkey, server_get=None):
237239
if symkey is not None and symkey == "":
238240
msg = "SymKeyAuthn.symkey cannot be an empty value"
239241
raise ImproperlyConfigured(msg)
240-
self.symkey = symkey
242+
self.symkey = symkey.encode() if isinstance(symkey, str) else symkey
241243
self.ttl = ttl
242244

243245
def authenticated_as(self, client_id, cookie=None, authorization="", **kwargs):
@@ -252,7 +254,7 @@ def authenticated_as(self, client_id, cookie=None, authorization="", **kwargs):
252254
try:
253255
aesgcm = AESGCM(self.symkey)
254256
user = aesgcm.decrypt(iv, encmsg, None)
255-
except (AssertionError, KeyError):
257+
except (AssertionError, KeyError): # pragma: no-cover
256258
raise FailedAuthentication("Decryption failed")
257259

258260
res = {"uid": user}
@@ -278,8 +280,7 @@ def authenticated_as(self, client_id="", cookie=None, authorization="", **kwargs
278280
:param kwargs: extra key word arguments
279281
:return:
280282
"""
281-
282-
if self.fail:
283+
if self.fail: # pragma: no-cover
283284
raise self.fail()
284285

285286
res = {"uid": self.user}

tests/test_12_user_authn.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import base64
12
import os
23

34
import pytest
@@ -7,8 +8,12 @@
78
from oidcop.user_authn.authn_context import UNSPECIFIED
89
from oidcop.user_authn.user import NoAuthn
910
from oidcop.user_authn.user import UserPassJinja2
11+
from oidcop.user_authn.user import BasicAuthn
12+
from oidcop.user_authn.user import NoAuthn
13+
from oidcop.user_authn.user import SymKeyAuthn
1014
from oidcop.util import JSONDictDB
1115

16+
1217
KEYDEFS = [
1318
{"type": "RSA", "key": "", "use": ["sig"]},
1419
{"type": "EC", "crv": "P-256", "use": ["sig"]},
@@ -66,7 +71,7 @@ def create_endpoint_context(self):
6671
},
6772
},
6873
},
69-
"template_dir": "template",
74+
"template_dir": "tests/templates",
7075
}
7176
server = Server(conf)
7277
self.endpoint_context = server.endpoint_context
@@ -96,3 +101,32 @@ def test_authenticated_as_with_cookie(self):
96101
_info, _time_stamp = method.authenticated_as("client 12345", [_cookie])
97102
assert set(_info.keys()) == {"sub", "sid", "state", "client_id"}
98103
assert _info["sub"] == "diana"
104+
105+
def test_userpassjinja2(self):
106+
db = {
107+
"class": JSONDictDB,
108+
"kwargs": {"filename": full_path("passwd.json")},
109+
}
110+
template_handler = self.endpoint_context.template_handler
111+
sg = self.endpoint_context.session_manager.token_handler.handler['access_token'].kwargs['server_get']
112+
res = UserPassJinja2(db, template_handler, server_get=sg)
113+
res()
114+
assert 'page_header' in res.kwargs
115+
116+
117+
def test_basic_auth(self):
118+
sg = self.endpoint_context.session_manager.token_handler.handler['access_token'].kwargs['server_get']
119+
basic_auth = base64.b64encode(b'diana:krall').decode()
120+
ba = BasicAuthn(pwd={'diana': 'krall'},
121+
server_get=sg)
122+
ba.authenticated_as(
123+
client_id='', authorization=f"Basic {basic_auth}"
124+
)
125+
126+
def test_no_auth(self):
127+
sg = self.endpoint_context.session_manager.token_handler.handler['access_token'].kwargs['server_get']
128+
basic_auth = base64.b64encode(
129+
b'D\xfd\x8a\x85\xa6\xd1\x16\xe4\\6\x1e\x9ds~\xc3\t\x95\x99\x83\x91\x1f\xfb:iviviviv'
130+
)
131+
ba = SymKeyAuthn(symkey=b'0'*32, ttl=600, server_get=sg)
132+
ba.authenticated_as(client_id='', authorization=basic_auth)

0 commit comments

Comments
 (0)