Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
feijooso committed Feb 21, 2024
1 parent 6be7458 commit 2041fb5
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 46 deletions.
2 changes: 1 addition & 1 deletion app/docker/tablas.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);

Expand Down
8 changes: 8 additions & 0 deletions app/exceptions/LoginException.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 0 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
6 changes: 3 additions & 3 deletions app/repository/Users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion app/schemas/Schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class UserSchema(BaseModel):
id: int
name: str
email: str
genre: str
gender: str
photo: str


Expand Down
79 changes: 40 additions & 39 deletions app/service/Users.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

0 comments on commit 2041fb5

Please sign in to comment.