示例#1
0
async def create_story(
        story: schemas.StoryCreate,
        request: Request,
        db: Session = Depends(get_db),
):
    token_data = await main.get_token_if_present(request)

    user = main.get_user_from_token(db, token_data)
    story_to_update = main.get_existing_story(user, token_data, db)

    if story_to_update:
        db_story = crud.update_story(db, story_to_update, story)
    else:
        db_story = crud.create_story(db=db, story=story, user=user)

    jsonStory = jsonable_encoder(schemas.Story.from_orm(db_story))

    # prepare response
    response = JSONResponse(jsonStory, status_code=200)
    if not token_data:
        access_token = main.create_access_token(data={"story_id": db_story.id})
        response.set_cookie(
            "Authorization",
            value=f"Bearer {access_token}",
            httponly=True,
        )
    return response
示例#2
0
async def login_for_access_token(
        form_data: OAuth2PasswordRequestForm = Depends()):
    user = authenticate_user(form_data.username, form_data.password)

    if not user:
        raise HTTPException(status_code=400,
                            detail='Incorrect username or password')

    not_allowed_scopes = set(form_data.scopes) - set(user.allowed_scopes)
    if len(not_allowed_scopes) > 0:
        raise HTTPException(
            status_code=400,
            detail='Not allowed scopes for this user requested: {}'.format(
                ', '.join(not_allowed_scopes)))

    access_token_expires = timedelta(
        minutes=int(config['AUTH']['token_expire_minutes']))
    access_token = create_access_token(
        data={
            'sub': user.username,
            'scopes': form_data.scopes
        },
        expires_delta=access_token_expires,
    )

    return {'access_token': access_token, 'token_type': 'bearer'}
示例#3
0
def test_update_travels(setup):
    db_story = models.Story()
    setup["db"].add(db_story)
    setup["db"].commit()
    db_travel = models.Travel(
        story_id=db_story.id,
        location="Bariloche, Argentina",
        date_of_return=date.today(),
    )
    setup["db"].add(db_travel)
    setup["db"].commit()
    data = [{
        "id": db_travel.id,
        "story_id": db_story.id,
        "date_of_return": date.today().strftime("%Y-%m-%d"),
        "location": "Argentina",
    }]
    access_token = main.create_access_token(data={"story_id": db_story.id})
    cookie = {"Authorization": f"Bearer {access_token}"}
    response = setup["app"].put(
        f"/api/stories/{db_story.id}/travels",
        data=json.dumps(data),
        cookies=cookie,
        headers=cookie,
    )
    assert response.status_code == 200
    parsed_response = response.json()
    for k in parsed_response[0]:
        assert parsed_response[0][k] == data[0][k]
示例#4
0
def test_update_close_contacts(setup):
    db_story = models.Story()
    setup["db"].add(db_story)
    setup["db"].commit()
    db_contact = models.CloseContact(
        story_id=db_story.id,
        email="*****@*****.**",
        phone_number="1122334455",
    )
    setup["db"].add(db_contact)
    setup["db"].commit()
    data = [{
        "id": db_contact.id,
        "story_id": db_story.id,
        "email": "*****@*****.**",
        "phone_number": "9988776655",
    }]
    access_token = main.create_access_token(data={"story_id": db_story.id})
    cookie = {"Authorization": f"Bearer {access_token}"}
    response = setup["app"].put(
        f"/api/stories/{db_story.id}/contacts",
        data=json.dumps(data),
        cookies=cookie,
        headers=cookie,
    )
    assert response.status_code == 200
    parsed_response = response.json()
    for k in parsed_response[0]:
        assert parsed_response[0][k] == data[0][k]
示例#5
0
文件: auth.py 项目: alex-wenzel/oasis
async def login_for_access_token(
        request: Request,
        form_data: OAuth2PasswordRequestForm = Depends(),
        db: Session = Depends(get_db),
):
    token_data = await main.get_token_if_present(request)
    user = crud.authenticate_user(form_data.username, form_data.password,
                                  token_data, db)
    if not user:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Incorrect email or password",
            headers={"WWW-Authenticate": "Bearer"},
        )

    access_token = main.create_access_token(data={"email": user.email})
    response = JSONResponse({}, status_code=200)
    response.set_cookie(
        "Authorization",
        value=f"Bearer {access_token}",
        httponly=True,
        # max_age=os.environ["COOKIE_EXPIRATION_SECONDS"],
        # expires=os.environ["COOKIE_EXPIRATION_SECONDS"],
    )
    return response
示例#6
0
文件: auth.py 项目: alex-wenzel/oasis
async def external_login(
        request: Request,
        form_data: OAuth2PasswordRequestForm = Depends(),
        db: Session = Depends(get_db),
):
    user = crud.get_user_by_email(db, email=form_data.username)

    if not user:
        new_user = schemas.UserCreate(email=form_data.username, password="")
        user = crud.create_user(db=db, user=new_user)

    access_token = main.create_access_token(data={"email": user.email})
    response = JSONResponse({}, status_code=200)
    response.set_cookie(
        "Authorization",
        value=f"Bearer {access_token}",
        httponly=True,
        # max_age=os.environ["COOKIE_EXPIRATION_SECONDS"],
        # expires=os.environ["COOKIE_EXPIRATION_SECONDS"],
    )
    return response
示例#7
0
def test_create_my_story(setup):
    db_story = models.Story()
    setup["db"].add(db_story)
    setup["db"].commit()
    data = {
        "story_id": db_story.id,
        "text": "A test my story",
    }

    access_token = main.create_access_token(data={"story_id": db_story.id})
    cookie = {"Authorization": f"Bearer {access_token}"}
    response = setup["app"].post(
        f"/api/stories/{db_story.id}/my_stories",
        data=json.dumps(data),
        cookies=cookie,
        headers=cookie,
    )
    print(response.reason)
    assert response.status_code == 200
    parsed_response = response.json()
    for k in parsed_response:
        if k != "id" and k != "updated_at" and k != "created_at":
            assert parsed_response[k] == data[k]