def obtain_access_token_app(self, scope="general") -> str: key_search = self._client_id + scope token_cached = self._tokens_cached_app_user.get(key_search) if token_cached is not None and self.__is_valid(token_cached.token.get("expires_at")) and \ token_cached.token.get("access_token") is not None: return self.__format_to_header( token_cached.token.get("access_token")) try: auth = self.__build_http_basic() scopes = [scope] client = BackendApplicationClient(client_id=self._client_id, scope=scopes) oauth = OAuth2Session(client=client) token = oauth.fetch_token(token_url=self._environment.token_url, auth=auth) if len(self._tokens_cached_app_user ) + 1 > self.MAX_APP_USER_SIZE_CACHED: for key in self._tokens_cached_app_user: self._tokens_cached_app_user.pop(key) break self._tokens_cached_app_user[key_search] = CachedToken( self._client_id, self._client_secret, token) return self.__format_to_header(token.get("access_token")) except Exception as ex: raise ApiException( reason="error to obtain token app: {0}".format(ex))
def regenerate_access_token_app_user(self, user_key: str, secret_key: str, scope="spei_admin") -> str: try: auth = self.__build_http_basic() scopes = [scope] client = LegacyApplicationClient(client_id=self._client_id) oauth = OAuth2Session(client=client) token = oauth.fetch_token(token_url=self._environment.token_url, username=user_key, password=secret_key, auth=auth, scope=scopes) key_search = user_key + scope token_cached = self._tokens_cached_app_user.get(key_search) if token_cached is None and len( self._tokens_cached_app_user ) + 1 > self.MAX_APP_USER_SIZE_CACHED: for key in self._tokens_cached_app_user: self._tokens_cached_app_user.pop(key) break self._tokens_cached_app_user[key_search] = CachedToken( user_key, secret_key, token) return self.__format_to_header(token.get("access_token")) except Exception as ex: raise ApiException( reason="error to obtain token app user: {0}".format(ex))
def regenerate_access_token_app(self, scope="general"): try: auth = HTTPBasicAuth(self._client_id, self._client_secret) scopes = [scope] client = BackendApplicationClient(client_id=self._client_id, scope=scopes) oauth = OAuth2Session(client=client) token = oauth.fetch_token(token_url=self._environment.token_url, auth=auth) self._token_cached_app.token = token return self.__format_to_header(token.get("access_token")) except Exception as ex: raise ApiException( reason="error to obtain token app: {0}".format(ex))
def obtain_access_token_app_user(self, user_key, secret_key, scope="spei_admin"): key_search = user_key + scope token_cached = self.tokens_cached_app_user.get(key_search) if token_cached is not None and scope in token_cached.token.get("scope") and \ self.__is_expired(token_cached.token.get("expires_at")) and \ token_cached.token.get("access_token") is not None: return self.__format_to_header( token_cached.token.get("access_token")) try: auth = HTTPBasicAuth(self._client_id, self._client_secret) scopes = [scope] client = LegacyApplicationClient(client_id=self._client_id) oauth = OAuth2Session(client=client) token = oauth.fetch_token(token_url=self._environment.token_url, username=user_key, password=secret_key, auth=auth, scope=scopes) if len(self._tokens_cached_app_user ) + 1 > self.MAX_APP_USER_SIZE_CACHED: for key in self._tokens_cached_app_user: self._tokens_cached_app_user.pop(key) break self._tokens_cached_app_user[key_search] = CachedToken( user_key, secret_key, token) return self.__format_to_header(token.get("access_token")) except Exception as ex: raise ApiException( reason="error to obtain token app user: {0}".format(ex))