17
17
import json
18
18
import os
19
19
import 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
22
24
23
25
from jwkest .jwk import KEYS
24
26
from jwkest .jws import JWS
@@ -42,7 +44,7 @@ class Client:
42
44
def __init__ (self , config ):
43
45
self .config = config
44
46
45
- print 'Getting ssl context for oauth server'
47
+ print ( 'Getting ssl context for oauth server' )
46
48
self .ctx = tools .get_ssl_context (self .config )
47
49
self .__init_config ()
48
50
self .client_data = None
@@ -51,14 +53,14 @@ def __init_config(self):
51
53
52
54
if 'issuer' in self .config :
53
55
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 )
56
58
if meta_data :
57
59
self .config .update (json .load (meta_data ))
58
60
else :
59
- print 'Unexpected response on discovery document: %s' % meta_data
61
+ print ( 'Unexpected response on discovery document: %s' % meta_data )
60
62
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' )
62
64
63
65
# Mandatory settings
64
66
if 'authorization_endpoint' not in self .config :
@@ -68,20 +70,20 @@ def __init_config(self):
68
70
69
71
self .read_credentials_from_file ()
70
72
if 'client_id' not in self .config :
71
- print 'Client is not registered.'
73
+ print ( 'Client is not registered.' )
72
74
73
75
if 'scope' not in self .config :
74
76
self .config ['scope' ] = 'openid'
75
77
76
78
def read_credentials_from_file (self ):
77
79
if not os .path .isfile (REGISTERED_CLIENT_FILENAME ):
78
- print 'Client is not dynamically registered'
80
+ print ( 'Client is not dynamically registered' )
79
81
return
80
82
81
83
try :
82
84
registered_client = json .loads (open (REGISTERED_CLIENT_FILENAME ).read ())
83
85
except Exception as e :
84
- print 'Could not read credentials from file' , e
86
+ print ( 'Could not read credentials from file' , e )
85
87
return
86
88
self .config ['client_id' ] = registered_client ['client_id' ]
87
89
self .config ['client_secret' ] = registered_client ['client_secret' ]
@@ -94,8 +96,8 @@ def register(self):
94
96
:raises: raises error when http call fails
95
97
"""
96
98
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 ' )
99
101
return
100
102
101
103
if 'client_id' in self .config :
@@ -108,7 +110,7 @@ def register(self):
108
110
dcr_access_token = self .get_registration_token ()
109
111
110
112
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' ])
112
114
data = {
113
115
'software_id' : self .config ['template_client' ]
114
116
}
@@ -120,7 +122,7 @@ def register(self):
120
122
}
121
123
122
124
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 ) )
124
126
125
127
register_response = self .__urlopen (self .config ['registration_endpoint' ], data = json .dumps (data ),
126
128
context = self .ctx , token = dcr_access_token )
@@ -153,7 +155,7 @@ def revoke(self, token, token_type_hint="access_token"):
153
155
:raises: raises error when http call fails
154
156
"""
155
157
if 'revocation_endpoint' not in self .config :
156
- print 'No revocation endpoint set'
158
+ print ( 'No revocation endpoint set' )
157
159
return
158
160
159
161
data = {
@@ -163,7 +165,7 @@ def revoke(self, token, token_type_hint="access_token"):
163
165
'client_secret' : self .config ['client_secret' ]
164
166
}
165
167
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 )
167
169
168
170
def refresh (self , refresh_token ):
169
171
"""
@@ -177,7 +179,7 @@ def refresh(self, refresh_token):
177
179
'client_id' : self .config ['client_id' ],
178
180
'client_secret' : self .config ['client_secret' ]
179
181
}
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 )
181
183
return json .loads (token_response .read ())
182
184
183
185
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
247
249
elif send_parameters_via == "request_uri" :
248
250
request_args = None # TODO: Implement request URI support
249
251
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 ))
251
253
252
- print "Redirect to %s" % login_url
254
+ print ( "Redirect to %s" % login_url )
253
255
254
256
return login_url
255
257
256
258
def get_token (self , code , code_verifier ):
257
259
"""
258
260
:param code: The authorization code to use when getting tokens
261
+ :param code_verifier: The original code verifier sent with the authorization request
259
262
:return the json response containing the tokens
260
263
"""
261
264
data = {'client_id' : self .config ['client_id' ], "client_secret" : self .config ['client_secret' ],
@@ -266,9 +269,9 @@ def get_token(self, code, code_verifier):
266
269
267
270
# Exchange code for tokens
268
271
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" )
272
275
raise te
273
276
return json .loads (token_response .read ())
274
277
@@ -294,14 +297,14 @@ def get_registration_token(self):
294
297
}
295
298
296
299
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" )
300
303
raise te
301
304
302
305
json_response = json .loads (token_response .read ())
303
306
if self .config ['debug' ]:
304
- print 'Got DCR token response: %s ' % json_response
307
+ print ( 'Got DCR token response: %s ' % json_response )
305
308
306
309
return json_response ['access_token' ]
307
310
@@ -322,14 +325,17 @@ def __urlopen(self, url, data=None, context=None, token=None):
322
325
if token :
323
326
headers ['Authorization' ] = 'Bearer %s' % token
324
327
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 )
326
332
327
333
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 ) )
331
337
332
- return urllib2 . urlopen (request , context = context )
338
+ return urlopen (request , context = context )
333
339
334
340
def __authn_req_args (self , state , scope , code_challenge , code_challenge_method = "plain" ):
335
341
"""
0 commit comments