1717import json
1818import os
1919import time
20- import urllib
21- import urllib2
20+ from urllib .parse import urlencode
21+ from urllib .request import urlopen
22+ from urllib .error import URLError
23+ from urllib .request import Request
2224
2325from jwkest .jwk import KEYS
2426from jwkest .jws import JWS
@@ -42,7 +44,7 @@ class Client:
4244 def __init__ (self , config ):
4345 self .config = config
4446
45- print 'Getting ssl context for oauth server'
47+ print ( 'Getting ssl context for oauth server' )
4648 self .ctx = tools .get_ssl_context (self .config )
4749 self .__init_config ()
4850 self .client_data = None
@@ -51,14 +53,14 @@ def __init_config(self):
5153
5254 if 'issuer' in self .config :
5355 meta_data_url = self .config ['issuer' ] + '/.well-known/openid-configuration'
54- print 'Fetching config from: %s' % meta_data_url
55- meta_data = urllib2 . urlopen (meta_data_url , context = self .ctx )
56+ print ( 'Fetching config from: %s' % meta_data_url )
57+ meta_data = urlopen (meta_data_url , context = self .ctx )
5658 if meta_data :
5759 self .config .update (json .load (meta_data ))
5860 else :
59- print 'Unexpected response on discovery document: %s' % meta_data
61+ print ( 'Unexpected response on discovery document: %s' % meta_data )
6062 else :
61- print 'Found no issuer in config, can not perform discovery. All endpoint config needs to be set manually'
63+ print ( 'Found no issuer in config, can not perform discovery. All endpoint config needs to be set manually' )
6264
6365 # Mandatory settings
6466 if 'authorization_endpoint' not in self .config :
@@ -68,20 +70,20 @@ def __init_config(self):
6870
6971 self .read_credentials_from_file ()
7072 if 'client_id' not in self .config :
71- print 'Client is not registered.'
73+ print ( 'Client is not registered.' )
7274
7375 if 'scope' not in self .config :
7476 self .config ['scope' ] = 'openid'
7577
7678 def read_credentials_from_file (self ):
7779 if not os .path .isfile (REGISTERED_CLIENT_FILENAME ):
78- print 'Client is not dynamically registered'
80+ print ( 'Client is not dynamically registered' )
7981 return
8082
8183 try :
8284 registered_client = json .loads (open (REGISTERED_CLIENT_FILENAME ).read ())
8385 except Exception as e :
84- print 'Could not read credentials from file' , e
86+ print ( 'Could not read credentials from file' , e )
8587 return
8688 self .config ['client_id' ] = registered_client ['client_id' ]
8789 self .config ['client_secret' ] = registered_client ['client_secret' ]
@@ -94,8 +96,8 @@ def register(self):
9496 :raises: raises error when http call fails
9597 """
9698 if 'registration_endpoint' not in self .config :
97- print 'Authorization server does not support Dynamic Client Registration. Please configure client ' \
98- 'credentials manually '
99+ print ( 'Authorization server does not support Dynamic Client Registration. Please configure client ' \
100+ 'credentials manually ' )
99101 return
100102
101103 if 'client_id' in self .config :
@@ -108,7 +110,7 @@ def register(self):
108110 dcr_access_token = self .get_registration_token ()
109111
110112 if 'template_client' in self .config :
111- print 'Registering client using template_client: %s' % self .config ['template_client' ]
113+ print ( 'Registering client using template_client: %s' % self .config ['template_client' ])
112114 data = {
113115 'software_id' : self .config ['template_client' ]
114116 }
@@ -120,7 +122,7 @@ def register(self):
120122 }
121123
122124 if self .config ['debug' ]:
123- print 'Registering client with data:\n %s' % json .dumps (data )
125+ print ( 'Registering client with data:\n %s' % json .dumps (data ) )
124126
125127 register_response = self .__urlopen (self .config ['registration_endpoint' ], data = json .dumps (data ),
126128 context = self .ctx , token = dcr_access_token )
@@ -153,7 +155,7 @@ def revoke(self, token, token_type_hint="access_token"):
153155 :raises: raises error when http call fails
154156 """
155157 if 'revocation_endpoint' not in self .config :
156- print 'No revocation endpoint set'
158+ print ( 'No revocation endpoint set' )
157159 return
158160
159161 data = {
@@ -163,7 +165,7 @@ def revoke(self, token, token_type_hint="access_token"):
163165 'client_secret' : self .config ['client_secret' ]
164166 }
165167
166- self .__urlopen (self .config ['revocation_endpoint' ], urllib . urlencode (data ), context = self .ctx )
168+ self .__urlopen (self .config ['revocation_endpoint' ], urlencode (data ), context = self .ctx )
167169
168170 def refresh (self , refresh_token ):
169171 """
@@ -177,7 +179,7 @@ def refresh(self, refresh_token):
177179 'client_id' : self .config ['client_id' ],
178180 'client_secret' : self .config ['client_secret' ]
179181 }
180- token_response = self .__urlopen (self .config ['token_endpoint' ], urllib . urlencode (data ), context = self .ctx )
182+ token_response = self .__urlopen (self .config ['token_endpoint' ], urlencode (data ), context = self .ctx )
181183 return json .loads (token_response .read ())
182184
183185 def get_authn_req_url (self , session , acr , forceAuthN , scope , forceConsent , allowConsentOptionDeselection ,
@@ -247,15 +249,16 @@ def get_authn_req_url(self, session, acr, forceAuthN, scope, forceConsent, allow
247249 elif send_parameters_via == "request_uri" :
248250 request_args = None # TODO: Implement request URI support
249251
250- login_url = "%s%s%s" % (self .config ['authorization_endpoint' ], delimiter , urllib . urlencode (request_args ))
252+ login_url = "%s%s%s" % (self .config ['authorization_endpoint' ], delimiter , urlencode (request_args ))
251253
252- print "Redirect to %s" % login_url
254+ print ( "Redirect to %s" % login_url )
253255
254256 return login_url
255257
256258 def get_token (self , code , code_verifier ):
257259 """
258260 :param code: The authorization code to use when getting tokens
261+ :param code_verifier: The original code verifier sent with the authorization request
259262 :return the json response containing the tokens
260263 """
261264 data = {'client_id' : self .config ['client_id' ], "client_secret" : self .config ['client_secret' ],
@@ -266,9 +269,9 @@ def get_token(self, code, code_verifier):
266269
267270 # Exchange code for tokens
268271 try :
269- token_response = self .__urlopen (self .config ['token_endpoint' ], urllib . urlencode (data ), context = self .ctx )
270- except urllib2 . URLError as te :
271- print "Could not exchange code for tokens"
272+ token_response = self .__urlopen (self .config ['token_endpoint' ], urlencode (data ), context = self .ctx )
273+ except URLError as te :
274+ print ( "Could not exchange code for tokens" )
272275 raise te
273276 return json .loads (token_response .read ())
274277
@@ -294,14 +297,14 @@ def get_registration_token(self):
294297 }
295298
296299 try :
297- token_response = self .__urlopen (self .config ['token_endpoint' ], urllib . urlencode (data ), context = self .ctx )
298- except urllib2 . URLError as te :
299- print "Could not get DCR access token"
300+ token_response = self .__urlopen (self .config ['token_endpoint' ], urlencode (data ), context = self .ctx )
301+ except URLError as te :
302+ print ( "Could not get DCR access token" )
300303 raise te
301304
302305 json_response = json .loads (token_response .read ())
303306 if self .config ['debug' ]:
304- print 'Got DCR token response: %s ' % json_response
307+ print ( 'Got DCR token response: %s ' % json_response )
305308
306309 return json_response ['access_token' ]
307310
@@ -322,14 +325,17 @@ def __urlopen(self, url, data=None, context=None, token=None):
322325 if token :
323326 headers ['Authorization' ] = 'Bearer %s' % token
324327
325- request = urllib2 .Request (url , data , headers )
328+ if data is not None :
329+ data = data .encode ('utf-8' )
330+
331+ request = Request (url , data , headers )
326332
327333 if self .config ['debug' ]:
328- print 'Request url: ' + url
329- print 'Request headers:\n ' + json .dumps (headers )
330- print 'Request data:\n ' + json .dumps (data )
334+ print ( 'Request url: ' + url )
335+ print ( 'Request headers:\n ' + json .dumps (headers ) )
336+ print ( 'Request data:\n ' + json .dumps (data . decode () if data is not None else None ) )
331337
332- return urllib2 . urlopen (request , context = context )
338+ return urlopen (request , context = context )
333339
334340 def __authn_req_args (self , state , scope , code_challenge , code_challenge_method = "plain" ):
335341 """
0 commit comments