Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Arturo Montejo Ráez
/
WBT2425_0
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
20
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
c542e353
authored
Mar 24, 2025
by
Arturo Montejo Ráez
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
working on authorization
parent
6ecbec33
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
109 additions
and
4 deletions
.gitignore
fastapi/auth.py
fastapi/main.py
fastapi/requirements.txt
fastapi/user/crud.py
.gitignore
View file @
c542e353
__pycache__/
__pycache__/
.env/
.
v
env/
fastapi/auth.py
0 → 100644
View file @
c542e353
# This is the file where authentication and authorization is implemented
# This file is imported in main.py and used as middleware
#
from
datetime
import
datetime
,
timedelta
,
timezone
from
requests
import
Session
from
fastapi
import
HTTPException
,
Depends
from
fastapi.security
import
OAuth2PasswordBearer
,
OAuth2PasswordRequestForm
import
jwt
from
jwt.exceptions
import
InvalidTokenError
from
passlib.context
import
CryptContext
from
pydantic
import
BaseModel
from
user.schemas
import
UserBase
from
user.crud
import
get_user_by_email
#
# This is the secret key used to hash the password
# to get a string like this run:
# openssl rand -hex 32
#
SECRET_KEY
=
""
# This is the password hashing algorithm
ALGORITHM
=
"HS256"
# This is the expiration time of the token in minutes
ACCESS_TOKEN_EXPIRE_MINUTES
=
30
#
# DTOs for token and credentials
#
class
Token
(
BaseModel
):
access_token
:
str
token_type
:
str
class
TokenData
(
BaseModel
):
username
:
str
role
:
str
class
UserCredentials
(
BaseModel
):
email
:
str
password
:
str
# Password hashing
pwd_context
=
CryptContext
(
schemes
=
[
"bcrypt"
],
deprecated
=
"auto"
)
def
verify_password
(
plain_password
,
hashed_password
):
return
pwd_context
.
verify
(
plain_password
,
hashed_password
)
def
get_password_hash
(
password
):
return
pwd_context
.
hash
(
password
)
# Token creation
def
create_access_token
(
data
:
dict
,
expires_delta
:
timedelta
|
None
=
None
):
to_encode
=
data
.
copy
()
if
expires_delta
:
expire
=
datetime
.
now
(
timezone
.
utc
)
+
expires_delta
else
:
expire
=
datetime
.
now
(
timezone
.
utc
)
+
timedelta
(
minutes
=
15
)
to_encode
.
update
({
"exp"
:
expire
})
encoded_jwt
=
jwt
.
encode
(
to_encode
,
SECRET_KEY
,
algorithm
=
ALGORITHM
)
return
encoded_jwt
#
# Authentication
#
def
authenticate_user
(
db
:
Session
,
email
:
str
,
password
:
str
):
user
=
get_user_by_email
(
db
,
email
)
if
not
user
:
return
False
if
not
verify_password
(
password
,
user
.
password
):
return
False
return
user
def
login_user
(
db
,
email
:
str
,
password
:
str
):
user
=
authenticate_user
(
db
,
email
,
password
)
if
not
user
:
raise
HTTPException
(
status_code
=
400
,
detail
=
"Incorrect username or password"
)
access_token_expires
=
timedelta
(
minutes
=
ACCESS_TOKEN_EXPIRE_MINUTES
)
access_token
=
create_access_token
(
data
=
{
"sub"
:
user
.
email
,
"role"
:
user
.
role
,
"id"
:
user
.
id
},
expires_delta
=
access_token_expires
)
return
Token
(
access_token
=
access_token
,
token_type
=
"bearer"
)
fastapi/main.py
View file @
c542e353
...
@@ -90,4 +90,13 @@ def update_loan(user_id: int, book_id: int, start_date: date, loan: loan_schemas
...
@@ -90,4 +90,13 @@ def update_loan(user_id: int, book_id: int, start_date: date, loan: loan_schemas
@app.delete
(
"/loan/{user_id}/{book_id}/{start_date}"
)
@app.delete
(
"/loan/{user_id}/{book_id}/{start_date}"
)
def
delete_loan
(
user_id
:
int
,
book_id
:
int
,
start_date
:
date
,
db
:
Session
=
Depends
(
get_db
)):
def
delete_loan
(
user_id
:
int
,
book_id
:
int
,
start_date
:
date
,
db
:
Session
=
Depends
(
get_db
)):
return
loan_crud
.
delete_loan
(
db
,
user_id
,
book_id
,
start_date
)
return
loan_crud
.
delete_loan
(
db
,
user_id
,
book_id
,
start_date
)
\ No newline at end of file
# Authentication
from
auth
import
login_user
,
Token
from
fastapi.security
import
OAuth2PasswordRequestForm
@app.post
(
"/login"
,
response_model
=
Token
)
def
login
(
form_data
:
OAuth2PasswordRequestForm
=
Depends
(),
db
:
Session
=
Depends
(
get_db
)):
return
login_user
(
db
,
form_data
.
username
,
form_data
.
password
)
fastapi/requirements.txt
0 → 100644
View file @
c542e353
fastapi
[standard]
sqlalchemy
pymysql
passlib
pyjwt
requests
fastapi/user/crud.py
View file @
c542e353
...
@@ -2,7 +2,7 @@ from sqlalchemy.orm import Session
...
@@ -2,7 +2,7 @@ from sqlalchemy.orm import Session
from
fastapi
import
HTTPException
from
fastapi
import
HTTPException
from
passlib.context
import
CryptContext
from
passlib.context
import
CryptContext
from
.models
import
UserDB
from
.models
import
UserDB
from
.schemas
import
UserCreate
,
User
from
.schemas
import
UserCreate
pwd_context
=
CryptContext
(
schemes
=
[
"bcrypt"
],
deprecated
=
"auto"
)
pwd_context
=
CryptContext
(
schemes
=
[
"bcrypt"
],
deprecated
=
"auto"
)
...
@@ -35,6 +35,10 @@ def get_user(db: Session, user_id: int):
...
@@ -35,6 +35,10 @@ def get_user(db: Session, user_id: int):
raise
HTTPException
(
status_code
=
404
,
detail
=
"User not found"
)
raise
HTTPException
(
status_code
=
404
,
detail
=
"User not found"
)
return
db_user
return
db_user
def
get_user_by_email
(
db
:
Session
,
email
:
str
):
db_user
=
db
.
query
(
UserDB
)
.
filter
(
UserDB
.
email
==
email
)
.
first
()
return
db_user
def
update_user
(
db
:
Session
,
user_id
:
int
,
user
:
UserCreate
):
def
update_user
(
db
:
Session
,
user_id
:
int
,
user
:
UserCreate
):
db_user
=
db
.
query
(
UserDB
)
.
filter
(
UserDB
.
id
==
user_id
)
.
first
()
db_user
=
db
.
query
(
UserDB
)
.
filter
(
UserDB
.
id
==
user_id
)
.
first
()
if
db_user
is
None
:
if
db_user
is
None
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment