access token ok

This commit is contained in:
Valentin CZERYBA 2023-10-12 23:01:51 +02:00
parent 6efe9fb5a7
commit 08758aa12f
2 changed files with 7 additions and 5 deletions

View File

@ -12,11 +12,11 @@ from .models import users
fake_users = [ fake_users = [
# password foo # password foo
{'id': 1, 'username': 'admin', 'password': '$2b$12$N.i74Kle18n5Toxhas.rVOjZreVC2WM34fCidNDyhSNgxVlbKwX7i', {'id': 1, 'username': 'admin', 'password': '$2b$12$N.i74Kle18n5Toxhas.rVOjZreVC2WM34fCidNDyhSNgxVlbKwX7i',
'permissions': ['items:read', 'items:write', 'users:read', 'users:write'] 'roles': 'Admin'
}, },
# password bar # password bar
{'id': 2, 'username': 'client', 'password': '$2b$12$KUgpw1m0LF/s9NS1ZB5rRO2cA5D13MqRm56ab7ik2ixftXW/aqEyq', {'id': 2, 'username': 'client', 'password': '$2b$12$KUgpw1m0LF/s9NS1ZB5rRO2cA5D13MqRm56ab7ik2ixftXW/aqEyq',
'permissions': ['items:read']} 'roles':'User'}
] ]
SECRET_KEY = "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7" SECRET_KEY = "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7"
@ -35,7 +35,7 @@ def get_password_hash(password):
def get_user(db, username: str): def get_user(db, username: str):
for user in db: for user in db:
if username == user.username: if username == user['username']:
return users.UserInDB(**user) return users.UserInDB(**user)
def authenticate_user(fake_db, username: str, password: str): def authenticate_user(fake_db, username: str, password: str):

View File

@ -1,7 +1,9 @@
from datetime import datetime, timedelta
from typing import Annotated from typing import Annotated
from fastapi import Depends, FastAPI, HTTPException, status, APIRouter from fastapi import Depends, FastAPI, HTTPException, status, APIRouter
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from ..dependencies import fake_users, authenticate_user from ..dependencies import fake_users, authenticate_user, ACCESS_TOKEN_EXPIRE_MINUTES, create_access_token
from ..models import token from ..models import token
router = APIRouter() router = APIRouter()
@ -18,7 +20,7 @@ async def login_for_access_token(
detail="Incorrect username or password", detail="Incorrect username or password",
headers={"WWW-Authenticate": "Bearer"}, headers={"WWW-Authenticate": "Bearer"},
) )
access_token_expires = timedelta(minutes=dependencies.ACCESS_TOKEN_EXPIRE_MINUTES) access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
access_token = create_access_token( access_token = create_access_token(
data={"sub": user.username}, expires_delta=access_token_expires data={"sub": user.username}, expires_delta=access_token_expires
) )