2016-05-22 5 views
4

Mein Code zu implementieren:versucht oauth/Token basierte Anmeldung mit stormpath

  from stormpath.api_auth import ApiRequestAuthenticator 
      import base64 

      application = app.stormpath_manager.application 
      account = application.accounts[0] # random account 
      new_api_key = account.api_keys.create() 
      authenticator = ApiRequestAuthenticator(application) 
      uri = 'dont_care' 
      http_method = 'GET' 
      headers = { 
       'Authorization': 'Basic ' + base64.b64encode(new_api_key.id + ":" + new_api_key.secret) 
      } 
      result = authenticator.authenticate(headers=headers, http_method=http_method, uri=uri, body={}, scopes=[]) 
      print result.api_key #<ApiKey href=https://api.stormpath.com/v1/apiKeys/bla_bla> 
      print result.account # [email protected] 
      print result.token # None 

Was bin ich fehlt? Mein Code auf this part of the documentation.

Antwort

0

Heyo basiert, ich bin der Autor dieser Bibliothek, würde so dachte ich, hier hop =)

Was Sie wollen, ist zu tun, so etwas wie dieses. Ich nehme an, Sie versuchen, eine Benutzername/Passwort-basierte Anmeldung im Gegensatz zu einem API-Key Login, richtig? (Beispiel: Sie haben ein Front-End-Client wie Angular oder eine mobile App diese Anforderungen zu machen?)

Wenn ja, können Sie erreichen, was Sie wollen, mit meinem Beispiel hier:

from os import environ 

from stormpath.api_auth import PasswordGrantAuthenticator 
from stormpath.client import Client 


STORMPATH_CLIENT_APIKEY_ID = environ['STORMPATH_CLIENT_APIKEY_ID'] 
STORMPATH_CLIENT_APIKEY_SECRET = environ['STORMPATH_CLIENT_APIKEY_SECRET'] 
STORMPATH_APPLICATION_NAME = environ['STORMPATH_APPLICATION_NAME'] 
STORMPATH_USER_EMAIL = environ['STORMPATH_USER_EMAIL'] 
STORMPATH_USER_PASSWORD = environ['STORMPATH_USER_PASSWORD'] 


client = Client(id=STORMPATH_CLIENT_APIKEY_ID, secret=STORMPATH_CLIENT_APIKEY_SECRET) 
application = client.applications.query(name=STORMPATH_APPLICATION_NAME)[0] 


authenticator = PasswordGrantAuthenticator(app=application) 
result = authenticator.authenticate(STORMPATH_USER_EMAIL, STORMPATH_USER_PASSWORD) 

if result: 
    print('Access Token: {}'.format(result.access_token)) 
    print('Refresh Token: {}'.format(result.refresh_token)) 
else: 
    print('Invalid credentials supplied.') 
+0

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

+1

Bitte überprüfen Sie und sagen Sie mir, wenn Sie Fehler in meiner Antwort finden – WebQube

+0

Ihre Lösung sieht gut aus. Sie könnten auch den spezifischen 'OAuthClientCredentialsRequestAuthenticator' verwenden, um ein bisschen effizienter zu sein = D @WebQube – rdegges

0

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 
Verwandte Themen