@@ -163,53 +163,69 @@ def get_auth_url(self, redirect_uri):
163163
164164 params = {
165165 "client_id" : self .client_id ,
166- "scope" : " " .join (self .scopes ),
167166 "response_type" : "code" ,
168167 "redirect_uri" : redirect_uri
169168 }
169+ if self .scopes is not None :
170+ params ["scope" ] = " " .join (self .scopes )
171+
170172 return "{}?{}" .format (self ._auth_server_url , urlencode (params ))
171173
172- def authenticate (self , code , redirect_uri , client_secret = None , resource = None ):
173- """Takes in a code string, gets the access token,
174- and creates session property bag
174+ def authenticate (self , redirect_uri , code = None , client_secret = None , resource = None ):
175+ """Takes in a gets the access token and creates a session.
175176
176177 Args:
177178 code (str):
178- The code provided by the oauth provider
179+ The code provided by the oauth provider.
180+ If provided, defaults to 'code flow' authorization.
181+ If not provided or None, then defaults to 'token flow'
182+ authorization.
179183 redirect_uri (str): The URI to redirect the callback
180184 to
181- client_secret (str): Defaults to None, the client
182- secret of your app
185+ client_secret (str): The client secret of your app. Only needed
186+ if code is not None
183187 resource (str): Defaults to None,The resource
184188 you want to access
185189 """
186-
190+ # First, default setup of common parameters
187191 params = {
188- "code" : code ,
189192 "client_id" : self .client_id ,
190- "redirect_uri" : redirect_uri ,
191- "grant_type" : "authorization_code" ,
192- "resource" : resource ,
193+ "redirect_uri" : redirect_uri
193194 }
194195
195- if client_secret is not None :
196- params ["client_secret" ] = client_secret
197196 if resource is not None :
198197 params ["resource" ] = resource
199198
200- headers = {"Content-Type" : "application/x-www-form-urlencoded" }
201- response = self ._http_provider .send (method = "POST" ,
202- headers = headers ,
203- url = self ._auth_token_url ,
204- data = params )
199+ response = None
200+
201+ # Fork based on whether a code was provided. If provided, then redeem the code.
202+ if code is not None :
203+ if client_secret is not None :
204+ params ["client_secret" ] = client_secret
205+ else :
206+ raise RuntimeError ("client_secret must be provided for 'code flow' authorization." )
207+ params ["code" ] = code
208+ params ["response_type" ] = "code"
209+ auth_url = self ._auth_token_url
210+ headers = {"Content-Type" : "application/x-www-form-urlencoded" }
211+ response = self ._http_provider .send (method = "POST" ,
212+ headers = headers ,
213+ url = auth_url ,
214+ data = params )
215+ else :
216+ params ["response_type" ] = "token"
217+ auth_url = self ._auth_server_url
218+ response = self ._http_provider .send (method = "GET" ,
219+ url = auth_url ,
220+ data = params )
205221
206222 rcont = json .loads (response .content )
207223 self ._session = self ._session_type (rcont ["token_type" ],
208224 rcont ["expires_in" ],
209225 rcont ["scope" ],
210226 rcont ["access_token" ],
211227 self .client_id ,
212- self ._auth_token_url ,
228+ self ._auth_token_url if code is not None else self . _auth_server_url ,
213229 redirect_uri ,
214230 rcont ["refresh_token" ] if "refresh_token" in rcont else None ,
215231 client_secret )
0 commit comments