11import sys
22try :
3+ import urllib .parse
34 from urllib .parse import urlparse
45except ImportError :
56 from urlparse import urlparse
@@ -144,7 +145,8 @@ def login_url(self, redirect_uri, state='', challenge='', challenge_method='S256
144145
145146 def login (self , username = '' , extension = '' , password = '' , code = '' , redirect_uri = '' , jwt = '' , verifier = '' ):
146147 """
147- Logs in the user using various authentication methods.
148+ Authenticates a user or application. Call without arguments to use the
149+ client ID and secret configured on the SDK for the client credentials grant.
148150
149151 Args:
150152 username (str, optional): The username for authentication. Required if password is provided. Default is ''.
@@ -162,7 +164,8 @@ def login(self, username='', extension='', password='', code='', redirect_uri=''
162164 Exception: If the login attempt fails or invalid parameters are provided.
163165
164166 Note:
165- - This method supports multiple authentication flows including password-based, authorization code, and JWT.
167+ - This method supports password, authorization code, JWT, and client credentials grants.
168+ - Call `login()` again when a client credentials token expires.
166169 - It checks for the presence of required parameters and raises an exception if necessary.
167170 - Deprecation warning is issued for username-password login; recommend using JWT or OAuth instead.
168171 - Constructs the appropriate request body based on the provided parameters.
@@ -171,37 +174,47 @@ def login(self, username='', extension='', password='', code='', redirect_uri=''
171174 - Triggers the loginSuccess or loginError event based on the outcome of the login attempt.
172175 """
173176 try :
174- if not code and not username and not password and not jwt :
175- raise Exception ('Either code, or username with password, or jwt has to be provided' )
176177 if username and password :
177178 warnings .warn ("username-password login will soon be deprecated. Please use jwt or OAuth instead." )
178- if not code and not jwt :
179- body = {
180- 'grant_type' : 'password' ,
181- 'username' : username ,
182- 'password' : password ,
183- 'access_token_ttl' : ACCESS_TOKEN_TTL ,
184- 'refresh_token_ttl' : REFRESH_TOKEN_TTL
185- }
186- if extension :
187- body ['extension' ] = extension
188- elif jwt :
179+ if jwt :
189180 body = {
190181 'grant_type' : 'urn:ietf:params:oauth:grant-type:jwt-bearer' ,
191182 'assertion' : jwt
192183 }
193- else :
184+ elif code :
194185 body = {
195186 'grant_type' : 'authorization_code' ,
196187 'redirect_uri' : redirect_uri if redirect_uri else self ._redirect_uri ,
197188 'code' : code
198189 }
199190 if verifier :
200191 body ['code_verifier' ] = verifier
192+ elif username or password :
193+ body = {
194+ 'grant_type' : 'password' ,
195+ 'username' : username ,
196+ 'password' : password ,
197+ 'access_token_ttl' : ACCESS_TOKEN_TTL ,
198+ 'refresh_token_ttl' : REFRESH_TOKEN_TTL
199+ }
200+ if extension :
201+ body ['extension' ] = extension
202+ elif extension or redirect_uri or verifier :
203+ raise Exception ('Either code, or username with password, or jwt has to be provided' )
204+ else :
205+ if not self ._key or not self ._secret :
206+ raise ValueError ('Client credentials login requires a client ID and client secret' )
207+ body = {'grant_type' : 'client_credentials' }
201208
202209 built_url = self .create_url ( TOKEN_ENDPOINT , add_server = True )
203210 response = self ._request_token ( built_url , body = body )
204- self ._auth .set_data (response .json_dict ())
211+ auth_data = response .json_dict ()
212+ if body ['grant_type' ] == 'client_credentials' :
213+ if not isinstance (auth_data , dict ) or not auth_data .get ('access_token' ):
214+ raise ValueError ('Malformed OAuth response: missing access token' )
215+ # Use fresh defaults so stored user credentials are cleared on success.
216+ auth_data = Auth ().set_data (auth_data ).data ()
217+ self ._auth .set_data (auth_data )
205218 self .trigger (Events .loginSuccess , response )
206219 return response
207220 except Exception as e :
0 commit comments