Exemple #1
0
def create_tokens(db: Session, user_id: int, at: str):
    """パスワード認証を行い、トークンを生成"""
    # ペイロード作成
    access_payload = {
        'token_type': 'access_token',
        'exp': datetime.utcnow() + timedelta(minutes=60),
        'user_id': user_id,
    }
    refresh_payload = {
        'token_type': 'refresh_token',
        'exp': datetime.utcnow() + timedelta(days=90),
        'user_id': user_id,
    }

    # トークン作成(本来は'SECRET_KEY123'はもっと複雑にする)
    access_token = jwt.encode(access_payload, random_key, algorithm='HS256')
    refresh_token = jwt.encode(refresh_payload, random_key, algorithm='HS256')

    # DBにリフレッシュトークンを保存
    # db.query(User).update({"refresh_token": refresh_token}).filter(User.id == user_id).values()
    # User.update(refresh_token=refresh_token).where(User.id == user_id).execute()
    user = schemas.User(id=user_id, foursquare_at=at)
    crud.create_user_item(db, user)


    return {'access_token': access_token, 'refresh_token': refresh_token, 'token_type': 'bearer'}
async def read_all_users(
        current_account: schemas.Account = Depends(get_current_account),
        db: Session = Depends(get_db)):
    if current_account.role_id != 0:
        raise HTTPException(status_code=403,
                            detail="Only Admin can access this api")

    db_user_list = db.query(models.User).all()
    user_list = []
    for db_user in db_user_list:
        user_list.append(
            schemas.UserAccount(account_id=db_user.account.account_id,
                                role_id=db_user.account.role_id,
                                email=db_user.account.email,
                                disabled=db_user.account.disabled,
                                info=schemas.User(
                                    user_id=db_user.user_id,
                                    name=db_user.name,
                                    date_of_birth=db_user.date_of_birth,
                                    address=db_user.address,
                                    gender_id=db_user.gender_id,
                                    job_id=db_user.job_id,
                                    phone_number=db_user.phone_number)))
    user_response = schemas.UserResponse(status="success",
                                         totalCount=len(user_list),
                                         perPage=len(user_list),
                                         data=user_list)
    return user_response
Exemple #3
0
def get_user_account(db_user: Session):
    return schemas.UserAccount(account_id=db_user.account.account_id,
                               role_id=db_user.account.role_id,
                               email=db_user.account.email,
                               disabled=db_user.account.disabled,
                               info=schemas.User(
                                   user_id=db_user.user_id,
                                   name=db_user.name,
                                   date_of_birth=db_user.date_of_birth,
                                   address=db_user.address,
                                   gender_id=db_user.gender_id,
                                   job_id=db_user.job_id,
                                   phone_number=db_user.phone_number))
def input_user(user_id: str,
               user_name: str,
               last_name: str,
               email: str,
               get_current_user: schemas.Admin_Login = Depends(
                   token_.get_current_user)):

    # Definition of the user with the given parameters and enter it to the database
    user = schemas.User(user_id=user_id,
                        user_name=user_name,
                        last_name=last_name,
                        email=email)
    user_data.append(user.dict())
    return user_data[-1]
async def read_user_information(
        current_account: schemas.Account = Depends(get_current_account),
        db: Session = Depends(get_db)):
    db_user = db.query(models.User).filter(
        models.User.account_id == current_account.account_id).first()
    return schemas.UserAccount(account_id=db_user.account.account_id,
                               role_id=db_user.account.role_id,
                               email=db_user.account.email,
                               disabled=db_user.account.disabled,
                               info=schemas.User(
                                   user_id=db_user.user_id,
                                   name=db_user.name,
                                   date_of_birth=db_user.date_of_birth,
                                   address=db_user.address,
                                   gender_id=db_user.gender_id,
                                   job_id=db_user.job_id,
                                   phone_number=db_user.phone_number))
Exemple #6
0
def login(*,
          username: str = Form(...),
          password: str = Form(...),
          sess: Session = Depends(middleware.get_db)):
    user: schemas.UserInDB = schemas.UserInDB(username=username,
                                              password="").from_db(sess)

    # Verify password
    if not user or not middleware.verify_password(password, user.password):
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Incorrect username or password",
            headers={"WWW-Authenticate": "Bearer"},
        )

    # Create token
    access_token_expires = timedelta(minutes=const.ACCESS_TOKEN_EXPIRE_MINUTES)
    access_token = middleware.create_access_token(
        data={"sub": user.username}, expires_delta=access_token_expires)

    return schemas.Token(access_token=access_token,
                         token_type="bearer",
                         user=schemas.User(**user.dict()))
Exemple #7
0
def bootstrap_mock_data():
    faker = Faker()

    # Create mock users
    for i in range(5):
        user = schemas.User(username=f"user{i}", name=faker.name())
        users_db.append(user)

    # Create mock profiles
    for i in range(10):
        profile = schemas.Profile(
            profile_id=i,
            owner=random.choice(users_db),
            bio=schemas.ProfileBio(name=faker.name(),
                                   address=faker.address(),
                                   company=faker.company()),
            contact_details=schemas.ProfileContactDetails(
                email=faker.email(), phone_number=faker.phone_number()),
            technical_details=schemas.ProfileTechnicalDetails(
                msisdn=faker.msisdn(), user_agent=faker.user_agent()),
            comments=schemas.ProfileComments(
                bio_comments=faker.paragraph(),
                general_comments=faker.paragraph()))
        profiles_db[i] = profile
async def create_user(user: schemas.UserCreate):
    fake_hashed_password = user.password + "notreallyhashed"
    db_user = users.insert().values(email=user.email,
                                    hashed_password=fake_hashed_password)
    user_id = await database.execute(db_user)
    return schemas.User(**user.dict(), id=user_id)