Exemple #1
0
async def get_contact_by_attr(
    db: str, collection: str, filters: Dict = Body(..., example={"name": "test", "age": 35}), exact: bool = True,
    skip: int = 0, token: str = Depends(oauth2_scheme)
        ):
    """`filters` should be a dictionary of key-value pairs of attributes and attribute value"""
    if not exact:
        return await authorize(token, db, collection, read_contact_by_attr_contains, get_client(db, collection), filters, skip=skip)
    return await authorize(token, db, collection, read_contact_by_attr, get_client(db, collection), filters, skip=skip)
Exemple #2
0
def authenticate_user(db: str, collection: str, username: str, password: str):
    user = get_user(get_client(db, collection), username)
    if not user:
        return False
    if not verify_password(password, user.hashed_password):
        return False
    return user
Exemple #3
0
async def send_contacts_csv(
    db: str, collection: str, filters: Dict = Body(..., example={"name": "test"}), exact: bool = True,
    token: str = Depends(oauth2_scheme)
        ):
    email = await get_user_email(get_client(*USER_COLLECTION), token)
    if email:
        if not exact:
            return await csv_authorize(token, db, collection, read_contact_by_attr_contains, filters, email)
        return await csv_authorize(token, db, collection, read_contact_by_attr, filters, email)
    return {"success": "no email provided"}
Exemple #4
0
async def csv_authorize(token: Token, db: str, collection: str, func, filters,
                        email):
    user = await get_current_user(token)
    active_user = await get_current_active_user(user)
    if await check_authorization(active_user, db, collection):
        cursor = func(get_client(db, collection), filters, cursor=True)
        return await send_csv(cursor, email, collection, filters)
    return HTTPException(
        status_code=HTTP_401_UNAUTHORIZED,
        detail="Unauthorized database access",
        headers={"WWW-Authenticate": "Bearer"},
    )
Exemple #5
0
async def get_current_user(token: str = Depends(oauth2_scheme)):
    credentials_exception = HTTPException(
        status_code=HTTP_401_UNAUTHORIZED,
        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 PyJWTError:
        raise credentials_exception
    user = get_user(get_client(*USER_COLLECTION), username=token_data.username)
    if user is None:
        raise credentials_exception
    return user
Exemple #6
0
async def delete_contact_by_id(db: str, collection: str, _id: str, token: str = Depends(oauth2_scheme)):
    return await authorize(token, db, collection, delete_contact, get_client(db, collection), _id)
Exemple #7
0
async def put_contact(
    db: str, collection: str, _id: str, contact: UpdateContactModel, token: str = Depends(oauth2_scheme)
        ):
    return await authorize(token, db, collection, update_contact, get_client(db, collection), _id, contact)
Exemple #8
0
async def new_contact(db: str, collection: str, contact: ContactModel, token: str = Depends(oauth2_scheme)):
    return await authorize(token, db, collection, create_new_contact, get_client(db, collection), contact)
Exemple #9
0
async def get_cities_info(db: str, city: str = None, skip: int = 0, token: str = Depends(oauth2_scheme)):
    return await authorize(token, db, "city_info", get_city_data, get_client(db, "city_info"), city, skip)
Exemple #10
0
async def new_city_info(db: str, city: str, info: List[str] = Body(...), token: str = Depends(oauth2_scheme)):
    return await authorize(token, db, "city_info", new_city_data, get_client(db, "city_info"), city, info[0])
Exemple #11
0
async def city_names(db: str, collection: str, token: str = Depends(oauth2_scheme)):
    return await authorize(token, db, collection, get_cities, get_client(db, collection))