Example #1
0
async def search_user_by_name(name: str) -> List[schemas.User]:
    users = await User.async_all()
    res = []
    for item in users:
        u = schemas.User(**item)
        if name in u.name: res.append(u)
    return res
Example #2
0
async def create_user(**data):
    if 'name' not in data or 'password' not in data:
        raise ValueError('username or password are required.')
    data['password'] = get_pwd_hash(data.pop('password'))
    rv = await User.acreate(**data)
    data.pop('password')
    data.update(id=rv)
    return schemas.User(**data)
Example #3
0
async def get_current_user(token: str):
    credentials_exception = HTTPException(
        status_code=status.HTTP_401_UNAUTHORIZED,
        detail="Could not validate credentials",
        headers={"WWW-Authenticate": "Bearer"},
    )
    try:
        playload = jwt.decode(token,
                              config.JWT_SECRET,
                              algorithms=config.JWT_ALGORITHM)
        username: str = playload.get('username')
        if username is None:
            raise credentials_exception
        token_data = schemas.TokenData(username=username)
    except PyJWTError:
        raise credentials_exception
    user = await User.async_first(name=token_data.username)
    user = schemas.User(**user)
    if user is None:
        raise credentials_exception
    return user
Example #4
0
async def current_user(request: Request, token: str = Depends(oauth2_scheme)):
    credentials_exception = HTTPException(
        status_code=status.HTTP_401_UNAUTHORIZED,
        detail='Invalid authentication credentials',
        headers={"WWW-Authenticate": "Bearer"})
    try:
        playload = jwt.decode(token,
                              config.JWT_SECRET,
                              algorithms=config.JWT_ALGORITHM)
        username: str = playload.get('sub')
        if username is None:
            raise credentials_exception
        token_data = schemas.TokenData(username=username)
    except PyJWTError:
        raise credentials_exception
    user = await User.async_first(name=token_data.username)
    user = schemas.User(**user)
    if user is None:
        raise credentials_exception
    request.session['admin_user'] = dict(user)
    return user
Example #5
0
async def list_users(token: str = Depends(
    oauth2_scheme)) -> schemas.CommonResponse:
    users: list = await User.async_all()
    users = [schemas.User(**u) for u in users]
    return {'items': users, 'total': len(users)}