From 2041fb5b041afbdd82a7c831cb102f0edec373b6 Mon Sep 17 00:00:00 2001 From: feijooso Date: Wed, 21 Feb 2024 00:34:16 -0300 Subject: [PATCH] refactor --- app/docker/tablas.sql | 2 +- app/exceptions/LoginException.py | 8 ++++ app/main.py | 1 - app/models/users.py | 2 +- app/repository/Users.py | 6 +-- app/schemas/Schemas.py | 2 +- app/service/Users.py | 79 ++++++++++++++++---------------- 7 files changed, 54 insertions(+), 46 deletions(-) create mode 100644 app/exceptions/LoginException.py diff --git a/app/docker/tablas.sql b/app/docker/tablas.sql index 998bc0c..4abc24b 100644 --- a/app/docker/tablas.sql +++ b/app/docker/tablas.sql @@ -4,7 +4,7 @@ CREATE TABLE IF NOT EXISTS dev.users ( id SERIAL PRIMARY KEY, name VARCHAR(255), email VARCHAR(255) UNIQUE NOT NULL, - genre VARCHAR(20), + gender VARCHAR(20), photo VARCHAR(255) ); diff --git a/app/exceptions/LoginException.py b/app/exceptions/LoginException.py new file mode 100644 index 0000000..61caf7e --- /dev/null +++ b/app/exceptions/LoginException.py @@ -0,0 +1,8 @@ +from fastapi import HTTPException, status +from typing import Optional + + +class AuthenticationError(HTTPException): + def __init__(self, message: Optional[str] = "Could not authenticate"): + status_code = status.HTTP_401_UNAUTHORIZED + super().__init__(status_code=status_code, detail=message) diff --git a/app/main.py b/app/main.py index 68aa8e9..8d14472 100644 --- a/app/main.py +++ b/app/main.py @@ -1,5 +1,4 @@ from fastapi import FastAPI -from fastapi import Body from controller.Users import UsersController from service.Users import UsersService from repository.Users import UsersRepository diff --git a/app/models/users.py b/app/models/users.py index afa7564..074df62 100644 --- a/app/models/users.py +++ b/app/models/users.py @@ -8,5 +8,5 @@ class User(Base): id = Column(Integer, primary_key=True, index=True, autoincrement=True) name = Column(String, nullable=True) email = Column(String, nullable=False) - genre = Column(String, nullable=True) + gender = Column(String, nullable=True) photo = Column(String, nullable=True) diff --git a/app/repository/Users.py b/app/repository/Users.py index 8659b21..84b0b8f 100644 --- a/app/repository/Users.py +++ b/app/repository/Users.py @@ -42,14 +42,14 @@ def get_all_users(self): def create_user(self, email: str, name: Optional[str] = None, - genre: Optional[str] = None, + gender: Optional[str] = None, photo: Optional[str] = None): user_data = {'email': email} if name is not None: user_data['name'] = name - if genre is not None: - user_data['genre'] = genre + if gender is not None: + user_data['gender'] = gender if photo is not None: user_data['photo'] = photo diff --git a/app/schemas/Schemas.py b/app/schemas/Schemas.py index ca34a78..8bbcd77 100644 --- a/app/schemas/Schemas.py +++ b/app/schemas/Schemas.py @@ -5,7 +5,7 @@ class UserSchema(BaseModel): id: int name: str email: str - genre: str + gender: str photo: str diff --git a/app/service/Users.py b/app/service/Users.py index aeff8f6..5f78cc9 100644 --- a/app/service/Users.py +++ b/app/service/Users.py @@ -1,36 +1,10 @@ from exceptions.UserException import UserNotFound +from exceptions.LoginException import AuthenticationError from repository.Users import UsersRepository import requests import os -def get_access_token(authorization_code): - token_url = "https://oauth2.googleapis.com/token" - payload = { - "client_id": os.environ["GOOGLE_CLIENT_ID"], - "client_secret": os.environ["GOOGLE_CLIENT_SECRET"], - "code": authorization_code, - "grant_type": "authorization_code", - "redirect_uri": "http://localhost:8000/auth/google/callback" - } - response = requests.post(token_url, data=payload) - if response.status_code == 200: - return response.json().get("access_token") - else: - return None - - -def get_user_info(access_token): - user_info_url = "https://www.googleapis.com/oauth2/v2/userinfo" - headers = {"Authorization": f"Bearer {access_token}"} - params = {"fields": "id,email,name,picture"} - response = requests.get(user_info_url, headers=headers, params=params) - if response.status_code == 200: - return response.json() - else: - return None - - class UsersService: def __init__(self, user_repository: UsersRepository): self.user_repository = user_repository @@ -49,20 +23,47 @@ def create_user(self, user_data: dict): return self.user_repository.create_user(email) def login(self, auth_code: str): - access_token = get_access_token(auth_code) - user_info = get_user_info(access_token) + access_token = self._get_access_token(auth_code) + if access_token is None: + raise AuthenticationError("Authentication code is invalid") - user = self.user_repository.get_user_by_email(user_info.get("email")) + user_info = self._get_user_info(access_token) + user = self.user_repository.get_user_by_email(user_info["email"]) if user is None: - user_data = {'email': user_info.get("email")} - if user_info.get("genre") is not None: - user_data['genre'] = user_info.get("genre") - if user_info.get("name") is not None: - user_data['name'] = user_info.get("name") - if user_info.get("picture") is not None: - user_data['photo'] = user_info.get("picture") - - user = self.user_repository.create_user(**user_data) + user = self.user_repository.create_user(**user_info) return user + + def _get_access_token(self, authorization_code): + token_url = "https://oauth2.googleapis.com/token" + payload = { + "client_id": os.environ["GOOGLE_CLIENT_ID"], + "client_secret": os.environ["GOOGLE_CLIENT_SECRET"], + "code": authorization_code, + "grant_type": "authorization_code", + "redirect_uri": "http://localhost:8000/auth/google/callback" + } + response = requests.post(token_url, data=payload) + if response.status_code == 200: + return response.json().get("access_token") + else: + return None + + def _get_user_info(self, access_token): + user_info_url = "https://www.googleapis.com/oauth2/v2/userinfo" + headers = {"Authorization": f"Bearer {access_token}"} + params = {"fields": "id,email,name,picture,gender"} + response = requests.get(user_info_url, headers=headers, params=params) + + if response.status_code != 200: + raise AuthenticationError() + + user_data = {'email': response.json().get("email")} + if response.json().get("gender") is not None: + user_data['gender'] = response.json().get("gender") + if response.json().get("name") is not None: + user_data['name'] = response.json().get("name") + if response.json().get("picture") is not None: + user_data['photo'] = response.json().get("picture") + return user_data