Skip to content

Commit

Permalink
Trocando python-jose por pyjwt na aula 08
Browse files Browse the repository at this point in the history
related #102
related #67
  • Loading branch information
dunossauro committed Feb 28, 2024
1 parent 0de8df2 commit c96b9c3
Show file tree
Hide file tree
Showing 4 changed files with 499 additions and 678 deletions.
2 changes: 1 addition & 1 deletion codigo_das_aulas/08/fast_zero/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


class Message(BaseModel):
detail: str
message: str


class UserSchema(BaseModel):
Expand Down
10 changes: 6 additions & 4 deletions codigo_das_aulas/08/fast_zero/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
from jose import JWTError, jwt
from jwt import DecodeError, ExpiredSignatureError, decode, encode
from passlib.context import CryptContext
from sqlalchemy import select
from sqlalchemy.orm import Session
Expand All @@ -23,7 +23,7 @@ def create_access_token(data: dict):
minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES
)
to_encode.update({'exp': expire})
encoded_jwt = jwt.encode(
encoded_jwt = encode(
to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM
)
return encoded_jwt
Expand All @@ -48,14 +48,16 @@ def get_current_user(
)

try:
payload = jwt.decode(
payload = decode(
token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]
)
username: str = payload.get('sub')
if not username:
raise credentials_exception
token_data = TokenData(username=username)
except JWTError:
except DecodeError:
raise credentials_exception
except ExpiredSignatureError:
raise credentials_exception

user = session.scalar(
Expand Down
Loading

0 comments on commit c96b9c3

Please sign in to comment.