Created
          August 6, 2022 11:25 
        
      - 
      
- 
        Save Angeloem/c3720f3ea3aff709a6baaf59db8f7870 to your computer and use it in GitHub Desktop. 
    TokenAuthentication class, this is used to verify the token
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | 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