Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- class AuditLogUserMixin(object):
- def perform_authentication(self, request):
- auth = self.request.META.get("HTTP_AUTHORIZATION", None)
- if not auth:
- exception = {"code": "authorization_header_missing",
- "description":
- "Authorization header is expected"}
- raise AuthenticationFailed(detail=exception)
- parts = auth.split()
- if parts[0].lower() != "bearer":
- exception = {"code": "invalid_header",
- "description":
- "Authorization header must start with"
- " Bearer"}
- raise AuthenticationFailed(detail=exception)
- elif len(parts) == 1:
- exception = {"code": "invalid_header",
- "description": "Token not found"}
- raise AuthenticationFailed(detail=exception)
- elif len(parts) > 2:
- exception = {"code": "invalid_header",
- "description":
- "Authorization header must be"
- " Bearer token"}
- raise AuthenticationFailed(detail=exception)
- url = "https://oauth2.googleapis.com/tokeninfo?id_token={}".format(token_auth=parts[1])
- payload = {}
- headers = {
- 'authorization': self.request.META.get("HTTP_AUTHORIZATION", None)
- }
- response = requests.request("GET", url, headers=headers, data=payload)
- if response.status_code == 200:
- .....
- else:
- exception = {"code": "invalid_header",
- "description": response.text
- }
- raise AuthenticationFailed(detail=exception)
- log_user_logged_in_success(sender=request.user.__class__, request=request, user=request.user)
- #CONSUMIR MI CLASSS
- class UserCreate(AuditLogUserMixin, generics.CreateAPIView):
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement