Advertisement
jrujano

Untitled

Jul 29th, 2021
1,056
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.82 KB | None | 0 0
  1.  
  2. import requests
  3.  
  4. class AuditLogUserMixin(object):
  5.  
  6.  
  7.   def perform_authentication(self, request):
  8.    
  9.     auth = self.request.META.get("HTTP_AUTHORIZATION", None)
  10.  
  11.     if not auth:
  12.       exception = {"code": "authorization_header_missing",
  13.                    "description":
  14.                      "Authorization header is expected"}
  15.  
  16.       raise AuthenticationFailed(detail=exception)
  17.  
  18.     parts = auth.split()
  19.  
  20.     if parts[0].lower() != "bearer":
  21.       exception = {"code": "invalid_header",
  22.                    "description":
  23.                      "Authorization header must start with"
  24.                      " Bearer"}
  25.       raise AuthenticationFailed(detail=exception)
  26.  
  27.     elif len(parts) == 1:
  28.       exception = {"code": "invalid_header",
  29.                    "description": "Token not found"}
  30.       raise AuthenticationFailed(detail=exception)
  31.  
  32.     elif len(parts) > 2:
  33.       exception = {"code": "invalid_header",
  34.                    "description":
  35.                      "Authorization header must be"
  36.                      " Bearer token"}
  37.  
  38.       raise AuthenticationFailed(detail=exception)
  39.  
  40.  
  41.    
  42.     url = "https://oauth2.googleapis.com/tokeninfo?id_token={}".format(token_auth=parts[1])
  43.  
  44.     payload = {}
  45.     headers = {
  46.       'authorization': self.request.META.get("HTTP_AUTHORIZATION", None)
  47.     }
  48.  
  49.     response = requests.request("GET", url, headers=headers, data=payload)
  50.  
  51.     if response.status_code == 200:
  52.       .....
  53.     else:
  54.       exception = {"code": "invalid_header",
  55.                     "description": response.text
  56.                     }
  57.       raise AuthenticationFailed(detail=exception)
  58.  
  59.  
  60.    
  61.  
  62.     log_user_logged_in_success(sender=request.user.__class__, request=request, user=request.user)
  63.  
  64.  
  65. #CONSUMIR MI CLASSS
  66. class UserCreate(AuditLogUserMixin, generics.CreateAPIView):
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement