Пример #1
0
def authenticate_user(user: JWTUser):
    if user.username == user_admin.username:
        if verify_password(user.password, user_admin.password):
            user.role = "admin"
            return user

    return None
Пример #2
0
async def authenticate_user(user:JWTUser):
    potential_users = await db_check_token_user(user)
    is_valid = False
    for db_user in potential_users:
        if verify_password(user.password, db_user["password"]):
            is_valid = True
    if is_valid:
        user.roles = "admin"
        return user
    return None
Пример #3
0
async def get_token(form_data: OAuth2PasswordRequestForm = Depends()):
    jwt_user_dict = {"username":form_data.username, "password":form_data.password}
    jwt_user = JWTUser(**jwt_user_dict)
    user = await authenticate_user(jwt_user)

    if user is None:
        raise HTTPException(status_code=HTTP_401_UNAUTHORIZED)

    jwt_token = create_token(user)
    return {"token":jwt_token}
Пример #4
0
async def authenticate_user(user: JWTUser):
    potential_users = await db_check_jwt_user(user)
    is_valid = False
    if(potential_users is None):
        return None
    for user1 in potential_users:
        is_valid = verify_password(user.password, user1["password"])

    if is_valid:
            user.role = "admin"
            return user
    return None
Пример #5
0
async def login_for_access_token(
        form_data: OAuth2PasswordRequestForm = Depends()):
    jwt_user_dict = {
        'username': form_data.username,
        'password': form_data.password
    }
    jwt_user = JWTUser(**jwt_user_dict)
    user = authenticate_user(jwt_user)

    if user is None:
        raise HTTPException(HTTP_401_UNAUTHORIZED)

    jwt_token = create_jwt_token(user)
    return {'token': jwt_token}
Пример #6
0
async def login_for_access_token(
        form_data: OAuth2PasswordRequestForm = Depends()):
    jwt_user_dict = {
        "username": form_data.username,
        "password": form_data.password
    }
    jwt_user = JWTUser(**jwt_user_dict)
    user = await authenticate_user(jwt_user)

    if user is None:
        raise HTTPException(status_code=HTTP_401_UNAUTHORIZED,
                            detail=TOKEN_INVALID_CREDENTIALS_MSG)

    jwt_token = create_jwt_token(user)
    return {"token": jwt_token}
Пример #7
0
async def get_access_token(response: Response,
                           form_data: OAuth2PasswordRequestForm = Depends()):
    user = JWTUser(
        **{
            "username": form_data.username,
            "password": form_data.password,
            "disabled": False,
            "role": "admin"
        })

    if authenticate_user(user) is not None:
        response.headers['Authorization'] = f"Bearer {create_jwt_token(user)}"
        return {"Message": "Authorization Successful"}
    else:
        raise HTTPException(HTTP_401_UNAUTHORIZED)
Пример #8
0
async def login_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
    redis_key = f"token:{form_data.username},{form_data.password}"
    user = await re.redis.get(redis_key)
    if not user:
        jwt_user_dict = {
            "username": form_data.username,
            "password": form_data.password
        }
        jwt_user = JWTUser(**jwt_user_dict)
        user = await authenticate_user(jwt_user)
        await re.redis.set(redis_key, pickle.dumps(user))
        if user is None:
            raise HTTPException(status_code=HTTP_401_UNAUTHORIZED)
    else:
        user = pickle.loads(user)
    jwt_token = create_jwt_token(user)
    return {"access_token": jwt_token}
Пример #9
0
def get_hashed_password(password):
    return my_ctx.hash(password)


def verify_password(password, hashed_password):
    try:
        return my_ctx.verify(password, hashed_password)
    except:
        return False


user_admin = JWTUser(
    **{
        "username": "******",
        "password": get_hashed_password("pass1"),
        "disabled": False,
        "role": "admin"
    })


# Authenticate users given username and password
def authenticate_user(user: JWTUser):
    if user.username == user_admin.username:
        if verify_password(user.password, user_admin.password):
            user.role = "admin"
            return user

    return None

Пример #10
0
from fastapi import Depends, HTTPException
from fastapi.security import OAuth2PasswordBearer
import time
from starlette.status import HTTP_401_UNAUTHORIZED
from utils.db_functions import db_check_token_user, db_check_jwt_username

pwd_context = CryptContext(schemes=["bcrypt"])
oauth_schema = OAuth2PasswordBearer(tokenUrl="/token")

jwt_user1 = {
    "username": '******',
    "password": "******",
    "disabled": False,
    "role": "personel",
}
fake_jwt_user1 = JWTUser(**jwt_user1)


def get_hashed_password(password):
    return pwd_context.hash(password)


def verify_password(plain_password, hashed_password):
    try:
        return pwd_context.verify(plain_password, hashed_password)
    except Exception as e:
        print(e)
        return False


#print(get_hashed_password("pass4"))
Пример #11
0
def authenticate_user(user: JWTUser):
    if fake_jwt_user1.username == user.username:
        if verify_password(user.password, fake_jwt_user1.password):
            user.role = "admin"
            return user
    return None
Пример #12
0
async def post_user(user: JWTUser):
    user.password = get_hashed_password(user.password)
    await db_insert_user(user)
    jwt_token = create_token(user)
    return {"token":jwt_token}