Skip to content

Commit 8ab4a29

Browse files
committed
Merge branch 'develop' into stormpath-325
2 parents fb06362 + 2351419 commit 8ab4a29

File tree

2 files changed

+438
-46
lines changed

2 files changed

+438
-46
lines changed

‎stormpath/api_auth.py‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,127 @@ def _get_scheme_and_token(self, headers, http_method, uri, body, scopes, ttl):
521521
return None, None
522522

523523

524+
class StormpathTokenGrantAuthenticator(Authenticator):
525+
"""This class should authenticate using ID Site JWT.
526+
It gets authentication tokens for valid credentials.
527+
"""
528+
def authenticate(self, id_site_jwt, organization_name_key=None, account_store=None, url=None):
529+
"""Method that authenticates with ID Site JWT.
530+
531+
:param account_store: If this parameter is set, token
532+
generation is targeted against this account store.
533+
:param url: url that is used for authentication. If this
534+
parameter is not specified, default url
535+
(APP_ID/oauth/token) is used.
536+
537+
:rtype: :class:`stormpath.api_auth.PasswordAuthenticationResult`
538+
:returns: result if request is valid, `None` otherwise.
539+
"""
540+
if not url:
541+
url = self.app.href + '/oauth/token'
542+
543+
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
544+
data = {
545+
'grant_type': 'stormpath_token',
546+
'token': id_site_jwt
547+
}
548+
549+
if organization_name_key:
550+
if isinstance(organization_name_key, string_types):
551+
data['organizationNameKey'] = organization_name_key
552+
else:
553+
raise TypeError('Unsupported type for organization_name_key.')
554+
555+
if account_store:
556+
if isinstance(account_store, string_types):
557+
data['accountStore'] = account_store
558+
elif hasattr(account_store, 'href'):
559+
data['accountStore'] = account_store.href
560+
else:
561+
raise TypeError('Unsupported type for account_store.')
562+
563+
try:
564+
res = self.app._store.executor.request('POST', url, headers=headers,
565+
data=data)
566+
except StormpathError:
567+
return None
568+
569+
refresh_token = res['refresh_token'] if 'refresh_token' in res else None
570+
571+
return PasswordAuthenticationResult(self.app,
572+
res['stormpath_access_token_href'],
573+
res['access_token'],
574+
res['expires_in'],
575+
res['token_type'],
576+
refresh_token
577+
)
578+
579+
580+
class StormpathSocialGrantAuthenticator(Authenticator):
581+
"""This class should authenticate using provider_id and either the
582+
Authorization Code or the access token for that Social Provider.
583+
It gets authentication tokens for valid credentials.
584+
"""
585+
def authenticate(self, provider_id, code=None, access_token=None, account_store=None, url=None):
586+
"""Method that authenticates with provider_id and authorization code
587+
or access token using stormpath social grant type.
588+
589+
:param account_store: If this parameter is set, token
590+
generation is targeted against this account store.
591+
:param url: url that is used for authentication. If this
592+
parameter is not specified, default url
593+
(APP_ID/oauth/token) is used.
594+
595+
:rtype: :class:`stormpath.api_auth.PasswordAuthenticationResult`
596+
:returns: result if request is valid, `None` otherwise.
597+
"""
598+
if not url:
599+
url = self.app.href + '/oauth/token'
600+
601+
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
602+
data = {
603+
'grant_type': 'stormpath_social',
604+
'providerId': provider_id
605+
}
606+
607+
if code:
608+
if isinstance(code, string_types):
609+
data['code'] = account_store
610+
else:
611+
raise TypeError("Unsupported type for 'code'.")
612+
elif access_token:
613+
if isinstance(access_token, string_types):
614+
data['accessToken'] = access_token
615+
else:
616+
raise TypeError('Unsupported type for code.')
617+
else:
618+
raise TypeError("'code' or 'access_token' params are required.")
619+
620+
if account_store:
621+
if isinstance(account_store, string_types):
622+
data['accountStore'] = account_store
623+
elif hasattr(account_store, 'href'):
624+
data['accountStore'] = account_store.href
625+
else:
626+
raise TypeError('Unsupported type for account_store.')
627+
628+
try:
629+
res = self.app._store.executor.request('POST', url, headers=headers,
630+
data=data)
631+
except StormpathError:
632+
return None
633+
634+
refresh_token = res['refresh_token'] if 'refresh_token' in res else None
635+
636+
return PasswordAuthenticationResult(self.app,
637+
res['stormpath_access_token_href'],
638+
res['access_token'],
639+
res['expires_in'],
640+
res['token_type'],
641+
refresh_token
642+
)
643+
644+
524645
class PasswordGrantAuthenticator(Authenticator):
525646
"""This class should authenticate using login and password.
526647
It gets authentication tokens for valid credentials.

0 commit comments

Comments
 (0)