async def save_to_cookies(response: web.HTTPSeeOther, key: str = "key", value: str = "value", http_only=True, lifetime: int = 300) -> web.HTTPSeeOther: """Save a given value to cookies.""" LOG.debug(f"Save a value for {key} to cookies.") response.set_cookie(key, value, domain=CONFIG.cookie["domain"], max_age=lifetime, secure=CONFIG.cookie["secure"], httponly=http_only) return response
async def _oAuth2Authorize(self, request: Request): query = request.query if query.get('client_id') != self.config.get( Setting.DEFAULT_DRIVE_CLIENT_ID) and query.get( 'client_id') != self._custom_drive_client_id: raise HTTPUnauthorized() if query.get('scope') != 'https://www.googleapis.com/auth/drive.file': raise HTTPUnauthorized() if query.get('response_type') != 'code': raise HTTPUnauthorized() if query.get('include_granted_scopes') != 'true': raise HTTPUnauthorized() if query.get('access_type') != 'offline': raise HTTPUnauthorized() if 'state' not in query: raise HTTPUnauthorized() if 'redirect_uri' not in query: raise HTTPUnauthorized() if query.get('prompt') != 'consent': raise HTTPUnauthorized() if query.get('redirect_uri') == 'urn:ietf:wg:oauth:2.0:oob': return json_response({"code": self._drive_auth_code}) url = URL(query.get('redirect_uri')).with_query({ 'code': self._drive_auth_code, 'state': query.get('state') }) raise HTTPSeeOther(str(url))
async def token(self, request: Request) -> None: self._global_info.setIngoreErrorsForNow(True) creds_deserialized = json.loads(str(base64.b64decode(request.query.get('creds').encode("utf-8")), 'utf-8')) creds = Creds.load(self._time, creds_deserialized) self._coord.saveCreds(creds) # Build the redirect url if 'host' in request.query: redirect = request.query.get('host') else: redirect = self._ha_source.getAddonUrl() if MIME_JSON in request.headers[hdrs.ACCEPT]: return web.json_response({'redirect': str(redirect)}) else: raise HTTPSeeOther(redirect)
async def authenticate(self, params: MultiDict): request = self.request token_bytes = request.headers.get('Authorization', '') token_bytes = token_bytes.lstrip('Bearer').strip() user_id = RSAJWT.decode(request.app['jwt_keys'][0], token_bytes) if user_id is None: reason = 'Invalid token' raise HTTPSeeOther('{}?{}'.format( request.app['config'].get('http_server.endpoints.login.path'), urlencode({ 'error': 'bigur_token_error', 'error_description': reason, 'next': ('{}?{}'.format(request.path, urlencode(query=params, doseq=True))), }))) return user_id
async def slugRedirect(self, request: Request): raise HTTPSeeOther("https://localhost:" + str(self.config.get(Setting.INGRESS_PORT)))
async def redirect(request): return HTTPSeeOther(repository_url, headers=headers)