forked from bcgov/cthub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Start creating a custom auth user * Swap custom user model * work on custom user model * Add user to application * tidy stuff * fix details page
- Loading branch information
Naomi
authored
Apr 13, 2022
1 parent
4e26226
commit 0f12c91
Showing
23 changed files
with
343 additions
and
252 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,71 @@ | ||
import base64 | ||
from keycloak import KeycloakOpenID | ||
from django.conf import settings | ||
from django.core.exceptions import ObjectDoesNotExist | ||
from django.contrib.auth.models import User | ||
from django.contrib.auth import get_user_model | ||
from rest_framework.authentication import TokenAuthentication | ||
from rest_framework.exceptions import AuthenticationFailed | ||
|
||
import logging | ||
|
||
|
||
log = logging.getLogger('KeycloakAuthentication') | ||
log = logging.getLogger("KeycloakAuthentication") | ||
|
||
ITVRUser = get_user_model() | ||
|
||
|
||
def base64_decode(data: str) -> str: | ||
""" | ||
We can check the identity provider of the token and then | ||
verify or pass on the request. | ||
""" | ||
|
||
data = data.encode("ascii") | ||
|
||
rem = len(data) % 4 | ||
|
||
if rem > 0: | ||
data += b"=" * (4 - rem) | ||
return base64.urlsafe_b64decode(data).decode("utf-8") | ||
|
||
|
||
class KeycloakAuthentication(TokenAuthentication): | ||
keyword = 'Bearer' | ||
keyword = "Bearer" | ||
|
||
def authenticate_credentials(self, token): | ||
keycloak_openid = KeycloakOpenID( | ||
server_url=settings.KEYCLOAK_URL, | ||
client_id=settings.KEYCLOAK_CLIENT_ID, | ||
realm_name=settings.KEYCLOAK_REALM | ||
realm_name=settings.KEYCLOAK_REALM, | ||
) | ||
|
||
# Decode the token from the front-end | ||
KEYCLOAK_PUBLIC_KEY = \ | ||
"-----BEGIN PUBLIC KEY-----\n" + \ | ||
keycloak_openid.public_key() + \ | ||
"\n-----END PUBLIC KEY-----" | ||
KEYCLOAK_PUBLIC_KEY = ( | ||
"-----BEGIN PUBLIC KEY-----\n" | ||
+ keycloak_openid.public_key() | ||
+ "\n-----END PUBLIC KEY-----" | ||
) | ||
|
||
options = { | ||
'verify_signature': True, | ||
'verify_aud': True, | ||
'verify_exp': True | ||
} | ||
options = {"verify_signature": True, "verify_aud": True, "verify_exp": True} | ||
|
||
try: | ||
token_info = keycloak_openid.decode_token( | ||
token, | ||
key=KEYCLOAK_PUBLIC_KEY, | ||
options=options | ||
token, key=KEYCLOAK_PUBLIC_KEY, options=options | ||
) | ||
except Exception: | ||
raise AuthenticationFailed( | ||
'Invalid Token' | ||
) | ||
|
||
username = token_info.get('preferred_username') | ||
raise AuthenticationFailed("Invalid Token") | ||
|
||
# TODO make a ticket to improve this | ||
user = None | ||
try: | ||
user = User.objects.get(username=username) | ||
except ObjectDoesNotExist: | ||
log.warn( | ||
'KeycloakAuthentication user does not exist' | ||
) | ||
user, created = ITVRUser.objects.get_or_create( | ||
username=token_info.get("sub"), | ||
identity_provider=token_info.get("identity_provider"), | ||
defaults={ | ||
"display_name": token_info.get("display_name"), | ||
"email": token_info.get("email"), | ||
}, | ||
) | ||
|
||
if user is None: | ||
user = User.objects.create_user(username=username) | ||
if created: | ||
log.debug("Created user") | ||
log.debug(user) | ||
|
||
return user, token |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 0 additions & 49 deletions
49
django/api/migrations/0002_goelectricrebateapplication_application_type_and_more.py
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.