1+ from collections .abc import Generator
12from hashlib import sha512
23from typing import Iterable , Union
34
@@ -52,7 +53,7 @@ def __init__(self, authorization_url: str, token_url: str, **kwargs):
5253 :param code_field_name: Field name containing the code. code by default.
5354 :param username: Username in case basic authentication should be used to retrieve token.
5455 :param password: User password in case basic authentication should be used to retrieve token.
55- :param client: httpx.Client instance that will be used to request the token.
56+ :param headers: Additional headers to set when requesting or refreshing token.
5657 Use it to provide a custom proxying rule for instance.
5758 :param kwargs: all additional authorization parameters that should be put as query parameter
5859 in the authorization URL and as body parameters in the token URL.
@@ -80,7 +81,7 @@ def __init__(self, authorization_url: str, token_url: str, **kwargs):
8081 username = kwargs .pop ("username" , None )
8182 password = kwargs .pop ("password" , None )
8283 self .auth = (username , password ) if username and password else None
83- self .client = kwargs .pop ("client " , None )
84+ self .token_headers = kwargs .pop ("headers " , {} )
8485
8586 # As described in https://tools.ietf.org/html/rfc6749#section-4.1.2
8687 code_field_name = kwargs .pop ("code_field_name" , "code" )
@@ -136,7 +137,11 @@ def __init__(self, authorization_url: str, token_url: str, **kwargs):
136137 self .refresh_token ,
137138 )
138139
139- def request_new_token (self ) -> tuple :
140+ def request_new_token (
141+ self ,
142+ ) -> Generator [
143+ httpx .Request , httpx .Response , Union [tuple [str , str ], tuple [str , str , int ]]
144+ ]:
140145 # Request code
141146 state , code = authentication_responses_server .request_new_grant (
142147 self .code_grant_details
@@ -145,46 +150,30 @@ def request_new_token(self) -> tuple:
145150 # As described in https://tools.ietf.org/html/rfc6749#section-4.1.3
146151 self .token_data ["code" ] = code
147152
148- client = self .client or httpx .Client ()
149- self ._configure_client (client )
150- try :
151- # As described in https://tools.ietf.org/html/rfc6749#section-4.1.4
152- token , expires_in , refresh_token = request_new_grant_with_post (
153- self .token_url , self .token_data , self .token_field_name , client
154- )
155- finally :
156- # Close client only if it was created by this module
157- if self .client is None :
158- client .close ()
153+ # As described in https://tools.ietf.org/html/rfc6749#section-4.1.4
154+ token , expires_in , refresh_token = yield from request_new_grant_with_post (
155+ self .token_url , self .token_data , self .token_field_name , self .token_headers
156+ )
159157 # Handle both Access and Bearer tokens
160158 return (
161159 (self .state , token , expires_in , refresh_token )
162160 if expires_in
163161 else (self .state , token )
164162 )
165163
166- def refresh_token (self , refresh_token : str ) -> tuple :
167- client = self .client or httpx .Client ()
168- self ._configure_client (client )
169- try :
170- # As described in https://tools.ietf.org/html/rfc6749#section-6
171- self .refresh_data ["refresh_token" ] = refresh_token
172- token , expires_in , refresh_token = request_new_grant_with_post (
173- self .token_url ,
174- self .refresh_data ,
175- self .token_field_name ,
176- client ,
177- )
178- finally :
179- # Close client only if it was created by this module
180- if self .client is None :
181- client .close ()
164+ def refresh_token (
165+ self , refresh_token : str
166+ ) -> Generator [httpx .Request , httpx .Response , tuple [str , str , int , str ]]:
167+ # As described in https://tools.ietf.org/html/rfc6749#section-6
168+ self .refresh_data ["refresh_token" ] = refresh_token
169+ token , expires_in , refresh_token = yield from request_new_grant_with_post (
170+ self .token_url ,
171+ self .refresh_data ,
172+ self .token_field_name ,
173+ self .token_headers ,
174+ )
182175 return self .state , token , expires_in , refresh_token
183176
184- def _configure_client (self , client : httpx .Client ):
185- client .auth = self .auth
186- client .timeout = self .timeout
187-
188177
189178class OktaAuthorizationCode (OAuth2AuthorizationCode ):
190179 """
@@ -220,8 +209,7 @@ def __init__(self, instance: str, client_id: str, **kwargs):
220209 :param header_value: Format used to send the token value.
221210 "{token}" must be present as it will be replaced by the actual token.
222211 Token will be sent as "Bearer {token}" by default.
223- :param client: httpx.Client instance that will be used to request the token.
224- Use it to provide a custom proxying rule for instance.
212+ :param headers: Additional headers to set when requesting or refreshing token.
225213 :param kwargs: all additional authorization parameters that should be put as query parameter
226214 in the authorization URL.
227215 Usual parameters are:
@@ -276,8 +264,7 @@ def __init__(
276264 :param header_value: Format used to send the token value.
277265 "{token}" must be present as it will be replaced by the actual token.
278266 Token will be sent as "Bearer {token}" by default.
279- :param client: httpx.Client instance that will be used to request the token.
280- Use it to provide a custom proxying rule for instance.
267+ :param headers: Additional headers to set when requesting or refreshing token.
281268 :param kwargs: all additional authorization parameters that should be put as query parameter
282269 in the authorization URL.
283270 """
0 commit comments