Spaces:
Sleeping
Sleeping
File size: 10,543 Bytes
13ba451 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
from datetime import timedelta, datetime
from request import RequestAuth as req
from response import ResponseAuth as res
import requests
import json, re
from auth.authentication import signJWT
from firebase_admin import credentials, auth, exceptions
import firebase_admin
from repository import UserLoginRepository, UserRepository, UserInfoRepository, OTPRepository
import service.OTPService
from function import support_function as sf
from dotenv import load_dotenv
import os
from response import ResponseUser as res
load_dotenv()
CLIENT_ID_GOOGLE = os.getenv('CLIENT_ID')
API_SIGN_UP_FIREBASE_PATH = os.getenv('API_SIGN_UP_FIREBASE')
regex = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b'
def get_user1(email):
try:
user = auth.get_user_by_email(email)
return user
except exceptions.FirebaseError as e:
return None
def check_email(email):
if isinstance(email, str) and re.fullmatch(regex, email):
return True
else:
return False
from pathlib import Path
try:
if not firebase_admin._apps:
json_path = Path(__file__).resolve().parent / 'app' / 'firebase_certificate.json'
cred = credentials.Certificate(str(json_path))
fred = firebase_admin.initialize_app(cred)
except:
if not firebase_admin._apps:
cred = credentials.Certificate("firebase_certificate.json")
fred = firebase_admin.initialize_app(cred)
def sign_up_with_email_and_password(email, password, username=None, return_secure_token=True):
try:
rest_api_url = "https://identitytoolkit.googleapis.com/v1/accounts:signUp"
payload = {
"email": email,
"password": password,
"returnSecureToken": return_secure_token
}
if username:
payload["displayName"] = username
payload = json.dumps(payload)
r = requests.post(rest_api_url, params={"key": API_SIGN_UP_FIREBASE_PATH}, data=payload)
try:
if r.status_code == 200:
response_data = r.json()
email = response_data.get('email')
display_name = response_data.get('displayName') # Lấy displayName nếu có
return email, display_name
except Exception as e:
pass
except Exception as e:
pass
def sign_in_with_email_and_password(email=None, password=None, return_secure_token=True):
rest_api_url = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword"
try:
payload = {
"returnSecureToken": return_secure_token
}
if email:
payload["email"] = email
if password:
payload["password"] = password
payload = json.dumps(payload)
r = requests.post(rest_api_url, params={"key": API_SIGN_UP_FIREBASE_PATH}, data=payload)
r.raise_for_status()
data = r.json()
if 'idToken' in data:
return data['email']
else:
return False
except requests.exceptions.RequestException as e:
print(f"Error signing in: {e}")
return False
async def login(request: req.RequestLoginEmail):
try:
email = request.email
password = request.password
check_email_fc = sf.check_email_empty_invalid(email)
if check_email_fc is not True:
return check_email_fc
if password is None:
return res.ReponseError(
status=400,
data=res.Message(message="Password is empty")
)
user_check = get_user1(email)
if user_check is None:
return res.ReponseError(
status=404,
data=res.Message(message="Email not exist")
)
user = sign_in_with_email_and_password(email, password)
if user:
check = signJWT(user)
if check is False:
return res.ReponseError(
status=500,
data=res.Message(message="Invalid authorization code.")
)
else:
access_token = check["access_token"]
refresh_token = check["refresh_token"]
expires_in = check["expires_in"]
session_id = check["session_id"]
return res.ResponseLoginEmail(
status=200,
data=res.DataLogin(access_token=access_token, refresh_token=refresh_token, expires_in=expires_in,
session_id=session_id)
)
else:
return res.ReponseError(
status=400,
data=res.Message(message="Passwords do not match")
)
except:
return res.ReponseError(
status=500,
data=res.Message(message="Server Error")
)
def verify_token_google(token):
from google.oauth2 import id_token
from google.auth.transport import requests
try:
CLIENT_ID = CLIENT_ID_GOOGLE
id_info = id_token.verify_oauth2_token(token, requests.Request(), CLIENT_ID)
check = id_info['email_verified']
if check is True:
return True
except ValueError as e:
return False
async def login_google(request: req.RequestLoginGoogle):
try:
email = request.email
token_google = request.token_google
check_google = verify_token_google(token_google)
if check_google is False:
return res.ReponseError(
status=400,
data =res.Message(message="Login google failed")
)
check_email_fc = sf.check_email_empty_invalid(email)
if check_email_fc is not True:
return check_email_fc
user = get_user1(email)
if user:
check = signJWT(email)
if check == False:
return res.ReponseError(
status=500,
data =res.Message(message="Invalid authorization code.")
)
else:
access_token = check["access_token"]
refresh_token = check["refresh_token"]
expires_in = check["expires_in"]
session_id = check["session_id"]
return res.ResponseLoginGoogle(
status= 200,
data = res.DataLogin(access_token=access_token,refresh_token=refresh_token,expires_in=expires_in,session_id=session_id)
)
else:
return res.ReponseError(
status= 404,
data = res.Message(message="Email not exist")
)
except:
return res.ReponseError(
status=500,
data=res.Message(message="Server Error")
)
async def sign_up(request: req.RequestRegister):
try:
email = request.email
password = request.password
confirm_password = request.confirm_password
username = request.username
check_email_fc = sf.check_email_empty_invalid(email)
if check_email_fc is not True:
return check_email_fc
if password is None or password == "":
return res.ReponseError(
status=400,
data =res.Message(message="password is empty")
)
if confirm_password is None or confirm_password == "":
return res.ReponseError(
status=400,
data =res.Message(message="confirm password is empty")
)
if confirm_password != password:
return res.ReponseError(status=400,
data =res.Message(message="password and confirm_password do not match"))
user_signup = get_user1(email)
if user_signup is not None:
return res.ReponseError(
status=400,
data =res.Message(message="Email exist")
)
user_info, display_name = sign_up_with_email_and_password(email, password, username)
if user_info:
return res.ResponseSignUp(
status=200,
data =res.DataSignUp(email=user_info)
)
else:
return res.ReponseError(
status=500,
data =res.Message(message="Internal Server Error")
)
except:
return res.ReponseError(
status=500,
data=res.Message(message="Server Error")
)
import pytz, datetime
import auth.authentication as auth123
from response import ResponseDefault as res1
def refresh_token(request: req.RequestRefreshTokenLogin):
try:
token = request.refresh_token
if token is None:
return res.ReponseError(
status=400,
data=res.Message(message="token is empty")
)
user_token = UserRepository.getUserIdByRefreshToken(token)
if user_token is None:
return res.ReponseError(
status=404,
data=res_login.Message(message="Not found refresh token")
)
email = UserRepository.getEmailUserById(user_token)
if email:
rf_token = token
result = auth123.get_refresh_token(token, email)
token_new = result["access_token"]
rf_token_new = result["refresh_token"]
seconds1 = result["expires_in"]
session_id = result["session_id"]
vn_timezone = pytz.timezone('Asia/Ho_Chi_Minh')
current_time = datetime.datetime.utcnow().replace(tzinfo=pytz.utc).astimezone(vn_timezone) + timedelta(
seconds=seconds1)
formatted_time = current_time.strftime('%Y-%m-%d %H:%M:%S ')
if rf_token == rf_token_new:
UserRepository.updateAccessToken(user_token, token_new, formatted_time)
else:
UserRepository.UpdateAccessTokenRefreshTokenById(user_token, token_new, rf_token_new, formatted_time)
user = token_new
if user == False:
return res.ReponseError(
status=400,
data=res.Message(message="Refresh token error")
)
return res.ResponseRefreshTokenLogin(
status=200,
data=res.DataRefreshToken(token_new=user, session_id=session_id)
)
except:
return res.ReponseError(
status=500,
data=res.Message(message="Server Error")
)
def check_token_is_valid(token):
try:
check = UserRepository.getEmailUserByAccessToken(token)
if check is None:
return False
return True
except:
return res.ReponseError(
status=500,
data=res.Message(message="Server Error")
) |