habe ich eigentlich ApiRequestAuthenticator
zu erreichen, dass
def get_api_key(self, app, user_data):
self.init_authenticator()
email = user_data['email']
if not hasattr(self, 'cloud_directory'):
self.cloud_directory = app.stormpath_helper.get_cloud_directory(APP_NAME + '-' + env)
if self.accounts.get(email) is None:
account = self.cloud_directory.accounts.search({'email': email})
if account is None:
raise Exception('failed to find account')
self.accounts[email] = account[0]
if self.api_keys.get(email) is None:
api_key = self.accounts[email].api_keys.create()
self.api_keys[email] = api_key
return self.api_keys[email]
# init authenticator to stormpath api to validate access tokens
def init_authenticator(self):
if not hasattr(self, 'application'):
self.application = self.app.stormpath_manager.application
if not hasattr(self, 'authenticator'):
self.authenticator = ApiRequestAuthenticator(self.application)
def get_client_access_token(self, app, user_data):
api_key = self.get_api_key(app, user_data)
uri = 'bla_dont_care.com?grant_type=client_credentials'
http_method = 'GET'
headers = {
'Authorization': 'Basic ' + base64.b64encode(api_key.id + ":" + api_key.secret)
}
result = self.authenticator.authenticate(headers=headers, http_method=http_method, uri=uri, body={}, scopes=[])
if result:
client_token = result.token.token
else:
raise Exception('Invalid or not authenticated request.')
und für die Anmeldung verwende ich:
def is_token_valid(request, authenticator):
uri = 'bla_dont_care.com'
# headers = {
# 'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2FwaS5zdG9ybXBhdGguY29tL3YxL2FwcGxpY2F0aW9ucy9WSmlYRFdUV25ZZE5nckZlOUJvSTMiLCJpYXQiOjE0NjM5ODYxNTAsInN1YiI6IjVHWk03REpWNTFRSkhDWk41WENQMDEwRjQiLCJleHAiOjE0NjM5ODk3NTAsInNjb3BlIjpudWxsfQ.MTxQ2AzhlCkOtws4cnwLdrUhLEUGHpMOIATbSX9AeGw'
# }
result = authenticator.authenticate(headers=request.headers, http_method='GET', uri=uri, body={}, scopes=[])
if result is None:
return False, None
is_valid = result.account is not None
return is_valid, result.account
dank für die schnelle Antwort !. Ich möchte tatsächlich ein tokenbasiertes Login gegen Benutzerpasswort implementieren. Das gelingt mir tatsächlich, aber jetzt kämpfe ich mit hoher Reaktionszeit (800ms). Ich werde das in einer anderen Frage schreiben. – WebQube
Bitte überprüfen Sie und sagen Sie mir, wenn Sie Fehler in meiner Antwort finden – WebQube
Ihre Lösung sieht gut aus. Sie könnten auch den spezifischen 'OAuthClientCredentialsRequestAuthenticator' verwenden, um ein bisschen effizienter zu sein = D @WebQube – rdegges