mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-07-15 18:07:02 +00:00
jira-5 google login and apple login
This commit is contained in:
@ -3,8 +3,13 @@
|
||||
from django.conf import settings
|
||||
from rest_framework import viewsets, status
|
||||
from rest_framework.response import Response
|
||||
|
||||
from templated_email import send_templated_mail
|
||||
import jwt
|
||||
from datetime import datetime
|
||||
from calendar import timegm
|
||||
from uuid import uuid4
|
||||
import secrets
|
||||
|
||||
def send_otp_email(recipient_email, otp):
|
||||
from_email = settings.EMAIL_FROM_ADDRESS
|
||||
recipient_list = [recipient_email]
|
||||
@ -36,3 +41,53 @@ def custom_error_response(detail, response_status):
|
||||
if not detail:
|
||||
detail = {}
|
||||
return Response({"error": detail, "status": "failed", "code": response_status})
|
||||
|
||||
|
||||
def get_user_data(attrs):
|
||||
"""
|
||||
used to decode token
|
||||
"""
|
||||
user_data = jwt.decode(jwt=attrs['token'], options={'verify_signature': False},
|
||||
algorithms=['RS256'])
|
||||
return user_data
|
||||
|
||||
|
||||
def generate_jwt_token(token_type: str, now_time: int, data: dict = dict):
|
||||
"""
|
||||
used to generate jwt token
|
||||
"""
|
||||
if type(data) == type:
|
||||
data = {}
|
||||
data.update({
|
||||
'token_type': token_type,
|
||||
'iss': 'your_site_url',
|
||||
'iat': timegm(datetime.utcnow().utctimetuple()),
|
||||
'jti': uuid4().hex
|
||||
})
|
||||
TOKEN_TYPE = ["access", "refresh"]
|
||||
if token_type == TOKEN_TYPE[1]:
|
||||
exp = now_time + settings.SIMPLE_JWT['REFRESH_TOKEN_LIFETIME']
|
||||
else:
|
||||
exp = now_time + settings.SIMPLE_JWT['ACCESS_TOKEN_LIFETIME']
|
||||
|
||||
data.update({
|
||||
"exp": timegm(exp.utctimetuple())
|
||||
})
|
||||
|
||||
|
||||
signing_key = secrets.token_hex(32)
|
||||
|
||||
return jwt.encode(payload=data, key=signing_key,
|
||||
algorithm='HS256')
|
||||
|
||||
|
||||
def get_token(data: dict = dict):
|
||||
""" create access and refresh token """
|
||||
now_time = datetime.utcnow()
|
||||
access = generate_jwt_token('access', now_time, data)
|
||||
refresh = generate_jwt_token('refresh', now_time, data)
|
||||
|
||||
return {
|
||||
'access': access,
|
||||
'refresh': refresh
|
||||
}
|
Reference in New Issue
Block a user