From 08758aa12fb956f637a307c99cd9ff0c17e6d70c Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 12 Oct 2023 23:01:51 +0200 Subject: [PATCH] access token ok --- app/dependencies.py | 6 +++--- app/routers/token.py | 6 ++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/dependencies.py b/app/dependencies.py index 20a9a91..1b7328a 100644 --- a/app/dependencies.py +++ b/app/dependencies.py @@ -12,11 +12,11 @@ from .models import users fake_users = [ # password foo {'id': 1, 'username': 'admin', 'password': '$2b$12$N.i74Kle18n5Toxhas.rVOjZreVC2WM34fCidNDyhSNgxVlbKwX7i', - 'permissions': ['items:read', 'items:write', 'users:read', 'users:write'] + 'roles': 'Admin' }, # password bar {'id': 2, 'username': 'client', 'password': '$2b$12$KUgpw1m0LF/s9NS1ZB5rRO2cA5D13MqRm56ab7ik2ixftXW/aqEyq', - 'permissions': ['items:read']} + 'roles':'User'} ] SECRET_KEY = "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7" @@ -35,7 +35,7 @@ def get_password_hash(password): def get_user(db, username: str): for user in db: - if username == user.username: + if username == user['username']: return users.UserInDB(**user) def authenticate_user(fake_db, username: str, password: str): diff --git a/app/routers/token.py b/app/routers/token.py index 1995ad9..9a889d9 100644 --- a/app/routers/token.py +++ b/app/routers/token.py @@ -1,7 +1,9 @@ +from datetime import datetime, timedelta + from typing import Annotated from fastapi import Depends, FastAPI, HTTPException, status, APIRouter 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 router = APIRouter() @@ -18,7 +20,7 @@ async def login_for_access_token( detail="Incorrect username or password", 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( data={"sub": user.username}, expires_delta=access_token_expires )