Add random SECRET_KEY to test
Changed the SECRET_KEY to a randomly generated one using the command `openssl rand -hex 32`. Additionally, added code to raise an exception when facing JWTError in `get_current_user`. Added a new user in `fake_users_db` who is currently disabled. Finally, changed the endpoint to show all users instead of `me`.
This commit is contained in:
parent
ddd795b2f4
commit
6d78aefa62
3 changed files with 27 additions and 10 deletions
|
|
@ -9,7 +9,9 @@ from ..models.token import TokenData
|
|||
from ..models.user import get_user, fake_users_db, User
|
||||
|
||||
|
||||
SECRET_KEY = "your_secret_key"
|
||||
# to get a string like this run:
|
||||
# openssl rand -hex 32
|
||||
SECRET_KEY = "698619adad2d916f1f32d264540976964b3c0d3828e0870a65add5800a8cc6b9"
|
||||
ALGORITHM = "HS256"
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES = 30
|
||||
|
||||
|
|
@ -37,6 +39,7 @@ def create_access_token(data: dict, expires_delta: timedelta = None):
|
|||
|
||||
def authenticate_user(fake_db, username: str, password: str):
|
||||
user = get_user(fake_db, username)
|
||||
|
||||
if not user:
|
||||
return False
|
||||
if not verify_password(password, user.hashed_password):
|
||||
|
|
@ -50,14 +53,16 @@ async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]):
|
|||
detail="Could not validate credentials",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
|
||||
try:
|
||||
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
||||
username: str = payload.get("sub")
|
||||
if username is None:
|
||||
raise credentials_exception
|
||||
token_data = TokenData(username=username)
|
||||
except JWTError:
|
||||
raise credentials_exception
|
||||
except JWTError as e:
|
||||
raise credentials_exception from e
|
||||
|
||||
user = get_user(fake_users_db, username=token_data.username)
|
||||
if user is None:
|
||||
raise credentials_exception
|
||||
|
|
|
|||
|
|
@ -13,12 +13,19 @@ class UserInDB(User):
|
|||
|
||||
|
||||
fake_users_db = {
|
||||
"johndoe": {
|
||||
"username": "johndoe",
|
||||
"full_name": "John Doe",
|
||||
"email": "johndoe@example.com",
|
||||
"hashed_password": "$2b$12$EixZaYVK1fsbw1ZfbX3OXePaWxn96p36WQoeG6Lruj3vjPGga31lW",
|
||||
"gustavo": {
|
||||
"username": "gustavo",
|
||||
"full_name": "Gustavo Schaedler",
|
||||
"email": "gustavopoa@gmail.com",
|
||||
"hashed_password": "$2b$12$f4R8IHUaVxVchhpWrwhckeJXnPalW1vUbJzcvb1KeovJcuMwE861K", #secret
|
||||
"disabled": False,
|
||||
},
|
||||
"gustavo_disabled": {
|
||||
"username": "gustavo_disabled",
|
||||
"full_name": "Gustavo Disabled",
|
||||
"email": "gustavo_disabled@gmail.com",
|
||||
"hashed_password": "$2b$12$f4R8IHUaVxVchhpWrwhckeJXnPalW1vUbJzcvb1KeovJcuMwE861K", #secret
|
||||
"disabled": True,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,8 +5,13 @@ from ..auth.auth import get_current_active_user
|
|||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/users/me/items/")
|
||||
@router.get("/users/all/")
|
||||
async def read_own_items(
|
||||
current_user: User = Depends(get_current_active_user)
|
||||
):
|
||||
return [{"item_id": "Foo", "owner": current_user.username}]
|
||||
return [
|
||||
{
|
||||
"item_id": "my_id",
|
||||
"owner": current_user.username
|
||||
}
|
||||
]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue