Skip to content

Commit e4dee66

Browse files
authored
refactor(oidc): include exception handling in authentication (#224)
1 parent f3f4692 commit e4dee66

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

app/Entirety/entirety/oidc.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22

3+
from django.contrib import messages
34
from mozilla_django_oidc.auth import OIDCAuthenticationBackend
45
from django.conf import settings
56
from jsonpath_ng import parse
@@ -19,8 +20,7 @@ def update_user(self, user, claims):
1920
def __set_user_values(self, user, claims):
2021

2122
path = settings.OIDC_TOKEN_ROLE_PATH
22-
parsed_result = parse(path).find(
23-
claims)
23+
parsed_result = parse(path).find(claims)
2424
if len(parsed_result) > 0:
2525
roles = parsed_result[0].value
2626

@@ -38,23 +38,26 @@ def __set_user_values(self, user, claims):
3838

3939
user.save()
4040

41-
logger.info(
42-
user.first_name
43-
+ " is accessing with roles "
44-
+ roles.__str__()
45-
)
41+
logger.info(user.first_name + " is accessing with roles " + roles.__str__())
4642
return user
4743

4844
def verify_claims(self, claims):
4945
logger.info(claims.get("given_name") + " is verifying claim")
5046
verified = super(CustomOIDCAB, self).verify_claims(claims)
5147
path = settings.OIDC_TOKEN_ROLE_PATH
52-
parsed_result = parse(path).find(
53-
claims)
48+
parsed_result = parse(path).find(claims)
5449
if len(parsed_result) > 0:
5550
value = parsed_result[0].value
5651
else:
5752
value = []
5853
is_user = settings.OIDC_USER_ROLE in value
5954

6055
return verified and is_user
56+
57+
def authenticate(self, request, **kwargs):
58+
try:
59+
user = super().authenticate(request, **kwargs)
60+
return user
61+
except Exception as e:
62+
messages.error(self.request, "Authentication Error: " + e.__str__())
63+
return None

0 commit comments

Comments
 (0)