def execute(self): response = ApiGatewayResponse() if not VerifyRequest(signing_secret=self.signing_secret).execute( self.raw_body, self.headers): response.auth_error() return response body = json.loads(self.raw_body) type = body.get("type") if type == "url_verification": response_body = UrlVerification().execute(body) response.ok(response_body) elif type == "event_callback": event = body["event"] logging.info(f"Received event: {event['type']}") if event["type"] == "user_change": UpdateAllProfiles( user_link_store=self.user_link_store, user_token_store=self.user_token_store, ).execute(body) response.ok() elif event["type"] == "tokens_revoked": UserUninstall( user_link_store=self.user_link_store, user_token_store=self.user_token_store, ).execute(body) response.ok() else: logging.error("unsupported event_callback %s", event) response.ok() else: logging.error("event not supported %s", body) response.ok() return response
def execute(self, code, state): logging.debug( "received authorization_grant code '%s'", code, ) logging.debug( "received authorization_grant state '%s'", state, ) gateway_response = slack.authorisation_grant( client_id=self.__client_id, client_secret=self.__client_secret, code=code, redirect_uri=self.__redirect_uri, ) response = ApiGatewayResponse() if not gateway_response.success: logging.warning("returning auth error due to gateway failure") return response.auth_error() if gateway_response.scope == EXPECTED_SCOPE: body = RedirectUriPageRenderer( install_path="", redirect_uri_path="" ).render_success_page(app_id="fakeappid", team_id=None) user = User( team_id=gateway_response.team, user_id=gateway_response.user, token=gateway_response.token, ) self.__user_token_store.store(user) return response.ok_html(body) else: logging.warning( f"scope differs from expected scope {gateway_response.scope} != {EXPECTED_SCOPE}" ) return response.auth_error()