async def show(username: str = Cookie(None), db: Session = Depends(deps.get_db)): if username: spotify_data = crud.spotify.get(db_session=db, id=username) token = cred.refresh_user_token(spotify_data.refresh_token) with spotify.token_as(token): user = spotify.current_user() response = HTMLResponse(content="ALL DONE, Hello " + user.display_name + "!") response.set_cookie("username", user.id) return response return Response(status_code=400)
async def get(self, request): username = '******' if request.user.is_authenticated: username = request.user.display_name try: username = request.cookies['test_cookies'] except KeyError: pass template = app.get_template('home.html') content = template.render(request=request, username=username) resp = HTMLResponse(content) resp.set_cookie('test_cookies', username, expires=86400) return resp
async def oauth_callback( code: str, request: Request, db: Manager = Depends(get_db), redis: PickleRedis = Depends(get_redis), ): try: resp = await requests.post( 'https://bgm.tv/oauth/access_token', data={ 'code': code, 'client_id': config.BgmTvAutoTracker.APP_ID, 'grant_type': 'authorization_code', 'redirect_uri': config.BgmTvAutoTracker.callback_url, 'client_secret': config.BgmTvAutoTracker.APP_SECRET, }, ) r = AuthResponse.parse_raw(resp.text) auth_time = dateutil.parser.parse(resp.headers['Date']).timestamp() user_info_resp = await requests.get( f'https://api.bgm.tv/user/{r.user_id}' ) user_info = UserInfo.parse_raw(user_info_resp.text) await db.execute( db_models.UserToken.upsert( user_id=r.user_id, scope=r.scope or '', token_type=r.token_type, expires_in=r.expires_in, auth_time=auth_time, access_token=r.access_token, refresh_token=r.refresh_token, username=user_info.username, nickname=user_info.nickname, usergroup=user_info.usergroup ) ) session = await new_session(user_id=r.user_id, redis=redis) response = templates.TemplateResponse( 'callback.html', {'request': request, 'data': json.dumps(r.dict())} ) html_response = HTMLResponse(content=response.body) html_response.set_cookie(cookie_scheme.model.name, session.api_key) return html_response except json.decoder.JSONDecodeError: return RedirectResponse('./auth') except ValidationError: return RedirectResponse('./auth')
async def get_login(api_key: APIKey = Depends(get_api_key)): html_content = """ <html> <head> <title>Assignment 3.1</title> </head> <body style="background-color:Black"> <h1 style="background-color:#DAF7A6;text-align:center">Access Granted!</h1> <h3 style="text-align:center;color:#DAF7A6">Access key set in: COOKIE</h3> </body> </html> """ response = HTMLResponse(content=html_content) response.set_cookie( API_KEY_NAME, value=api_key, domain=COOKIE_DOMAIN, httponly=True, max_age=1800, expires=1800, ) return response