Esempio n. 1
0
def validate(provider: schemas.Provider,
             token: str,
             request: Request,
             db: Session = Depends(get_db)):
    """
    Exchange a (provider) token for a COTACOL specific JWT access_token.
    """
    token_exception = HTTPException(
        status_code=status.HTTP_401_UNAUTHORIZED,
        detail="Could not validate token",
    )

    try:
        res = get_provider(provider).validate(token)
    except Exception:
        raise token_exception

    user = crud.create_user_for_provider(db, res, provider)

    if not user:
        raise token_exception

    access_token, expires_at, scopes = generate_access_token(user)

    return {
        "token_type": "Bearer",
        "access_token": access_token,
        "expires_at": expires_at,
        "scopes": scopes
    }
Esempio n. 2
0
def test_update_user(client, staff_user):
    access_token, _, _ = generate_access_token(staff_user)
    response = client.patch(
        "/v1/users/me/", json={"bookmarks": [34, 35]}, headers={"Authorization": f"Bearer {access_token}"},
    )
    data = response.json()
    assert response.status_code == status.HTTP_200_OK
    assert data["bookmarks"] == [34, 35]
Esempio n. 3
0
def test_update_climb(client, climb, staff_user):
    access_token, _, _ = generate_access_token(staff_user)
    response = client.patch(
        f"/v1/climbs/{climb.id}/",
        json={"name": "Kop"},
        headers={"Authorization": f"Bearer {access_token}"},
    )
    data = response.json()
    assert response.status_code == status.HTTP_200_OK
    assert data["id"] == climb.id
    assert data["name"] == "Kop"
Esempio n. 4
0
async def authorize(provider: schemas.Provider,
                    redirect_uri: str,
                    request: Request,
                    db: Session = Depends(get_db)):
    oauth_client = oauth.create_client(provider)
    token = await oauth_client.authorize_access_token(request)
    # user = await oauth_client.parse_id_token(request, token)
    user = crud.create_user_for_provider(db, token)
    access_token, expires_at, scopes = generate_access_token(user)
    data = {
        "access_token": access_token,
        "refresh_token": token["refresh_token"],
        "expires_at": expires_at,
        "scopes": scopes
    }

    return RedirectResponse(f"{redirect_uri}?{urlencode(data)}")
Esempio n. 5
0
def test_get_user(client, staff_user):
    access_token, _, _ = generate_access_token(staff_user)
    response = client.get("/v1/users/me/", headers={"Authorization": f"Bearer {access_token}"})
    assert response.status_code == status.HTTP_200_OK
    assert response.json()["username"] == "staff"