예제 #1
0
async def authorize_google(request: Request,
                           Authorize: AuthJWT = Depends(),
                           db: Session = Depends(get_db)):
    try:
        token = await oauth.google.authorize_access_token(request)
    except OAuthError as error:
        return HTMLResponse(f'<h1>{error.error}</h1>')
    user = await oauth.google.parse_id_token(request, token)
    account = db.query(Account).filter_by(email=user.email).first()
    if account is None:
        logging.warning('Creating a new user object for first-time login')
        new_account = Account(
            email=user.email,
            username=user.email.split('@')[0],
            first_name=user.given_name,
            last_name=user.family_name,
            oauth='google',
            profile_pic=user.picture,
            is_verified=True,
        )
        db.add(new_account)
        db.commit()
        db.refresh(new_account)
        create_account_settings(new_account.uuid, db)
        account = new_account
    return create_token_for_user(Authorize, str(account.uuid))
예제 #2
0
async def authorize_github(request: Request,
                           Authorize: AuthJWT = Depends(),
                           db: Session = Depends(get_db)):
    try:
        token = await oauth.github.authorize_access_token(request)
    except OAuthError as error:
        return HTMLResponse(f'<h1>{error.error}</h1>')
    resp = await oauth.github.get('user', token=token)
    user = resp.json()
    email_to_use = user['email'] or user['login'] + '@fakegithubemail.com'
    account = db.query(Account).filter_by(email=email_to_use).first()

    if account is None:
        new_account = Account(email=email_to_use,
                              username=user['login'],
                              first_name=user['name'],
                              last_name='no last name',
                              oauth='github',
                              profile_pic=user['avatar_url'],
                              city=None if user['location'] is None else
                              user['location'].split(', ')[0],
                              state=None if user['location'] is None else
                              user['location'].split(', ')[1],
                              is_verified=True)
        db.add(new_account)
        db.commit()
        db.refresh(new_account)
        create_account_settings(new_account.uuid, db)
        account = new_account
    return create_token_for_user(Authorize, str(account.uuid))
def update_password(uuid,
                    partial_account: AccountNewPasswordSchema,
                    Authorize: AuthJWT = Depends(),
                    db: Session = Depends(get_db)):
    try:
        Authorize.jwt_required()
        check_matching_user(uuid, Authorize)
        account = db.query(Account).filter_by(uuid=uuid).first()
        if partial_account.password is None:
            raise HTTPException(status_code=400, detail=f"Password is missing")
        else:
            check_valid_password(partial_account.password)
            account.password = encrypt_password(partial_account.password)
        db.merge(account)
        db.commit()
        db.refresh(account)
    except Exception as e:
        logging.warning(e)
        raise e
    return account