Skip to content

Instantly share code, notes, and snippets.

@Angeloem
Created August 6, 2022 11:25
Show Gist options
  • Save Angeloem/c3720f3ea3aff709a6baaf59db8f7870 to your computer and use it in GitHub Desktop.
Save Angeloem/c3720f3ea3aff709a6baaf59db8f7870 to your computer and use it in GitHub Desktop.
TokenAuthentication class, this is used to verify the token
import json
from django.contrib.auth.models import User
from rest_framework.authentication import BaseAuthentication
from pamoja_admin.shared.auth.enc_dec import decrypt
class TokenAuthentication(BaseAuthentication):
def authenticate(self, request):
headers = request.headers
token: str = headers.get('authorization', None)
if token is None:
return {}, None
token = token.split(' ')[1]
# try decrypting the token's second part
decrypted_token = decrypt(token)
# json load from the decrypted_token
user_profile = json.loads(decrypted_token)
# user id is in the user_profile dictionary
user = User.objects.get(id=user_profile['id'])
return user, None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment