def post(self, name): bot = auth_util.get_bot(flask.request) force = api.payload.get('force', False) service = Service.get(name, parent=bot.key) task_util.sync_service(service, force=force) return WrapEntity(service)
def _oauth(request: flask.Request, user: Entity, dest: str): code = request.args.get('code') service = Service.get(SERVICE_NAME, parent=user.key) client = stravalib.client.Client() creds = client.exchange_code_for_token( client_id=config.strava_creds['client_id'], client_secret=config.strava_creds['client_secret'], code=code, ) Service.update_credentials(service, dict(creds)) task_util.sync_service(service) return flask.redirect(config.devserver_url + dest)
def redirect(user): service = Service.get(SERVICE_NAME, parent=user.key) code = flask.request.args.get('code') dest = flask.request.args.get('dest', '') auth = create_auth(callback_uri=get_callback_uri(dest)) creds_dict = create_creds_dict(auth.get_credentials(code)) Service.update_credentials(service, creds_dict) task_util.sync_service(service) return flask.redirect(config.devserver_url + dest)
def redirect(user): service = Service.get(SERVICE_NAME, parent=user.key) code = flask.request.args.get('code') dest = flask.request.args.get('dest', '') auth_client = fitbit.FitbitOauth2Client( client_id=config.fitbit_creds['client_id'], client_secret=config.fitbit_creds['client_secret'], redirect_uri=get_redirect_uri(dest), ) creds = auth_client.fetch_access_token(code) Service.update_credentials(service, creds) task_util.sync_service(service) return flask.redirect(config.devserver_url + dest)
def _oauth(user: Entity, dest: str, redirect_uri: str): """Step 2. Stores credentials.""" service = Service.get(SERVICE_NAME, parent=user.key) state = flask.session['state'] flow = google_auth_oauthlib.flow.Flow.from_client_config( config.gcp_web_creds, scopes=SCOPES, state=state) flow.redirect_uri = redirect_uri authorization_response = flask.request.url logging.debug('auth_response: %s', authorization_response) flow.fetch_token(authorization_response=authorization_response) creds = _credentials_to_dict(flow.credentials) logging.debug('creds: %s', creds) Service.update_credentials(service, creds) task_util.sync_service(Service.get(SERVICE_NAME, parent=user.key)) return flask.redirect(config.devserver_url + dest)
def get(self, name): bot = auth_util.get_bot(flask.request) service = Service.get(name, parent=bot.key) task_util.sync_service(service, force=True) return responses.OK
def _oauth(request: flask.Request, session: dict, user: Entity, dest: str, redirect_uri: str): # Retrieve the auth code and state from the request params if 'code' not in request.args: error = request.args["error"] if "error" in request.args else "" return flask.make_response( f"Something is wrong with the installation (error: {error})", 400) code = request.args['code'] # Verify the state parameter state_store = DatastoreOAuthStateStore(ds_util.client, STATE_EXPIRATION_SECONDS) if not state_store.consume(request.args["state"]): return flask.make_response( "Try the installation again (the state value is already expired)", 400) # Verify the state parameter # Complete the installation by calling oauth.v2.access API method client = WebClient() oauth_response = client.oauth_v2_access( client_id=config.slack_creds['client_id'], client_secret=config.slack_creds['client_secret'], redirect_uri=redirect_uri, code=code, ) # These seem to sometimes return None rather than being unset, so for maps, return {} installed_enterprise = oauth_response.get("enterprise", {}) or {} is_enterprise_install = oauth_response.get("is_enterprise_install") installed_team = oauth_response.get("team", {}) or {} installer = oauth_response.get("authed_user", {}) or {} incoming_webhook = oauth_response.get("incoming_webhook", {}) or {} bot_token = oauth_response.get("access_token") # NOTE: oauth.v2.access doesn't include bot_id in response bot_id = None enterprise_url = None if bot_token is not None: auth_test = client.auth_test(token=bot_token) bot_id = auth_test["bot_id"] if is_enterprise_install is True: enterprise_url = auth_test.get("url") installation = Installation( app_id=oauth_response.get("app_id"), enterprise_id=installed_enterprise.get("id"), enterprise_name=installed_enterprise.get("name"), enterprise_url=enterprise_url, team_id=installed_team.get("id"), team_name=installed_team.get("name"), bot_token=bot_token, bot_id=bot_id, bot_user_id=oauth_response.get("bot_user_id"), bot_scopes=oauth_response.get("scope"), # comma-separated string user_id=installer.get("id"), user_token=installer.get("access_token"), user_scopes=installer.get("scope"), # comma-separated string incoming_webhook_url=incoming_webhook.get("url"), incoming_webhook_channel=incoming_webhook.get("channel"), incoming_webhook_channel_id=incoming_webhook.get("channel_id"), incoming_webhook_configuration_url=incoming_webhook.get( "configuration_url"), is_enterprise_install=is_enterprise_install, token_type=oauth_response.get("token_type"), ) # Store the installation service = Service.get(SERVICE_NAME, parent=user.key) store = DatastoreInstallationStore(ds_util.client, parent=service.key) store.save(installation) task_util.sync_service(Service.get(SERVICE_NAME, parent=user.key)) return flask.redirect(config.devserver_url + dest)