22# encoding: utf-8
33
44import sys
5+ import urllib .parse
56from observable import Observable
67from functools import reduce
78from .auth import Auth
1314URL_PREFIX = '/restapi'
1415TOKEN_ENDPOINT = '/restapi/oauth/token'
1516REVOKE_ENDPOINT = '/restapi/oauth/revoke'
17+ AUTHORIZE_ENDPOINT = '/restapi/oauth/authorize'
1618API_VERSION = 'v1.0'
1719ACCESS_TOKEN_TTL = 3600 # 60 minutes
1820REFRESH_TOKEN_TTL = 604800 # 1 week
@@ -76,12 +78,20 @@ def logged_in(self):
7678 except :
7779 return False
7880
79- def login (self , username = '' , extension = '' , password = '' , code = '' , redirect_uri = '' ):
81+ def login_url (self , redirect_uri , state = '' , challenge = '' , challenge_method = 'S256' ):
82+ built_url = self .create_url ( AUTHORIZE_ENDPOINT , add_server = True )
83+ built_url += '?response_type=code&client_id=' + self ._key + '&redirect_uri=' + urllib .parse .quote (redirect_uri )
84+ if state :
85+ built_url += '&state=' + urllib .parse .quote (state )
86+ if challenge :
87+ built_url += '&code_challenge=' + urllib .parse .quote (challenge ) + '&code_challenge_method=' + challenge_method
88+ return built_url
89+
90+ def login (self , username = '' , extension = '' , password = '' , code = '' , redirect_uri = '' , jwt = '' , verifier = '' ):
8091 try :
81- if not code and not username and not password :
82- raise Exception ('Either code or username with password has to be provided' )
83-
84- if not code :
92+ if not code and not username and not password and not jwt :
93+ raise Exception ('Either code, or username with password, or jwt has to be provided' )
94+ if not code and not jwt :
8595 body = {
8696 'grant_type' : 'password' ,
8797 'username' : username ,
@@ -91,12 +101,19 @@ def login(self, username='', extension='', password='', code='', redirect_uri=''
91101 }
92102 if extension :
93103 body ['extension' ] = extension
104+ elif jwt :
105+ body = {
106+ 'grant_type' : 'urn:ietf:params:oauth:grant-type:jwt-bearer' ,
107+ 'assertion' : jwt
108+ }
94109 else :
95110 body = {
96111 'grant_type' : 'authorization_code' ,
97112 'redirect_uri' : redirect_uri if redirect_uri else self ._redirect_uri ,
98113 'code' : code
99114 }
115+ if verifier :
116+ body ['code_verifier' ] = verifier
100117 response = self ._request_token (TOKEN_ENDPOINT , body = body )
101118 self ._auth .set_data (response .json_dict ())
102119 self .trigger (Events .loginSuccess , response )
0 commit comments