Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit 3a67789

Browse files
committed
Added httpc_params.
1 parent 84efd7b commit 3a67789

6 files changed

Lines changed: 72 additions & 77 deletions

File tree

flask_rp/application.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,38 @@
1212

1313

1414
def init_oidc_rp_handler(app):
15-
rp_keys_conf = app.config.get('RP_KEYS')
16-
if rp_keys_conf is None:
17-
rp_keys_conf = app.config.get('OIDC_KEYS')
18-
1915
verify_ssl = app.config.get('VERIFY_SSL')
16+
httpc_params = {"verify": verify_ssl}
17+
18+
_cert = app.config.get("CLIENT_CERT")
19+
_key = app.config.get("CLIENT_KEY")
20+
if _cert and _key:
21+
httpc_params["cert"] = (_cert, _key)
22+
elif _cert:
23+
httpc_params["cert"] = _cert
24+
2025
hash_seed = app.config.get('HASH_SEED')
2126
if not hash_seed:
2227
hash_seed = "BabyHoldOn"
2328

29+
rp_keys_conf = app.config.get('RP_KEYS')
30+
if rp_keys_conf is None:
31+
rp_keys_conf = app.config.get('OIDC_KEYS')
32+
2433
if rp_keys_conf:
2534
_kj = init_key_jar(**rp_keys_conf)
2635
_path = rp_keys_conf['public_path']
27-
# replaces ./ and / from the begin of the string
36+
# removes ./ and / from the begin of the string
2837
_path = re.sub('^(.)/', '', _path)
2938
else:
3039
_kj = KeyJar()
3140
_path = ''
32-
_kj.verify_ssl = verify_ssl
41+
_kj.httpc_params = httpc_params
3342

3443
rph = RPHandler(base_url=app.config.get('BASEURL'),
35-
hash_seed=hash_seed,
36-
keyjar=_kj, jwks_path=_path,
44+
hash_seed=hash_seed, keyjar=_kj, jwks_path=_path,
3745
client_configs=app.config.get('CLIENTS'),
38-
services=app.config.get('SERVICES'),
39-
verify_ssl=verify_ssl)
46+
services=app.config.get('SERVICES'), httpc_params=httpc_params)
4047

4148
return rph
4249

flask_rp/conf.yaml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@ BASEURL: "https://127.0.0.1:8090"
55
SERVER_CERT: "certs/cert.pem"
66
SERVER_KEY: "certs/key.pem"
77
CA_BUNDLE: ''
8+
# If you want the clients cert to be verified
9+
#VERIFY_USER: optional
810

911
# This is just for testing an local usage. In all other cases it MUST be True
1012
VERIFY_SSL: false
1113

14+
# Client side
15+
#CLIENT_CERT: "certs/client.crt"
16+
#CLIENT_KEY: "certs/client.key"
17+
1218
KEYDEFS: &keydef
1319
-
1420
"type": "RSA"
@@ -63,12 +69,13 @@ SERVICES: &id002
6369
kwargs: {}
6470

6571
CLIENTS:
72+
"":
73+
client_preferences: *id001
74+
redirect_uris: None
75+
services: *id002
6676
flop:
6777
client_preferences: *id001
6878
issuer: https://127.0.0.1:5000/
69-
# keys:
70-
# url:
71-
# 'https://127.0.0.1:5000' : https://127.0.0.1:5000/static/jwks.json
7279
jwks_uri: https://127.0.0.1:8090/static/jwks.json
7380
redirect_uris: ['https://127.0.0.1:8090/authz_cb/flop']
7481
services: *id002
@@ -79,5 +86,6 @@ CLIENTS:
7986
code_challenge_length: 64
8087
code_challenge_method: S256
8188

89+
8290
# Whether an attempt to fetch the userinfo should be made
8391
USERINFO: true

src/oidcrp/__init__.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,11 @@ def dynamic_provider_info_discovery(client):
114114
class RPHandler(object):
115115
def __init__(self, base_url='', hash_seed="", keyjar=None, verify_ssl=True,
116116
services=None, client_configs=None, client_authn_factory=None,
117-
client_cls=None, state_db=None, http_lib=None, **kwargs):
117+
client_cls=None, state_db=None, http_lib=None, httpc_params=None,
118+
**kwargs):
119+
118120
self.base_url = base_url
119121
self.hash_seed = as_bytes(hash_seed)
120-
self.verify_ssl = verify_ssl
121122
self.keyjar = keyjar
122123

123124
if state_db:
@@ -143,6 +144,13 @@ def __init__(self, base_url='', hash_seed="", keyjar=None, verify_ssl=True,
143144
self.issuer2rp = {}
144145
self.hash2issuer = {}
145146
self.httplib = http_lib
147+
if not httpc_params:
148+
self.httpc_params = {'verify': verify_ssl}
149+
else:
150+
self.httpc_params = httpc_params
151+
152+
if not self.keyjar.httpc_params:
153+
self.keyjar.httpc_params = self.httpc_params
146154

147155
def state2issuer(self, state):
148156
"""
@@ -192,10 +200,9 @@ def init_client(self, issuer):
192200

193201
try:
194202
client = self.client_cls(
195-
state_db=self.state_db,
196-
client_authn_factory=self.client_authn_factory,
197-
verify_ssl=self.verify_ssl, services=_services,
198-
config=_cnf, httplib=self.httplib)
203+
state_db=self.state_db, client_authn_factory=self.client_authn_factory,
204+
services=_services, config=_cnf, httplib=self.httplib,
205+
httpc_params=self.httpc_params)
199206
except Exception as err:
200207
logger.error('Failed initiating client: {}'.format(err))
201208
message = traceback.format_exception(*sys.exc_info())

src/oidcrp/http.py

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,48 +17,21 @@
1717

1818

1919
class HTTPLib(object):
20-
def __init__(self, ca_certs=None, verify_ssl=True, client_cert=None):
20+
def __init__(self, httpc_params=None):
2121
"""
2222
A base class for OAuth2 clients and servers
2323
24-
:param ca_certs: the path to a CA_BUNDLE file or directory with
25-
certificates of trusted CAs
26-
:param verify_ssl: If True then the server SSL certificate is not
27-
verfied
28-
:param client_cert: local cert to use as client side certificate, as a
29-
single file (containing the private key and the certificate) or as
30-
a tuple of both file's path
24+
:param httpc_params: Default arguments to be used for HTTP requests
3125
"""
3226

3327
self.request_args = {"allow_redirects": False}
28+
if httpc_params:
29+
self.request_args.update(httpc_params)
3430

3531
self.cookiejar = FileCookieJar()
36-
self.ca_certs = ca_certs
37-
38-
if ca_certs:
39-
if verify_ssl is False:
40-
raise ValueError(
41-
'conflict: ca_certs defined, but verify_ssl is False')
42-
43-
# Instruct requests to verify certificate against the CA cert
44-
# bundle located at the path given by `ca_certs`.
45-
self.request_args["verify"] = ca_certs
46-
47-
elif verify_ssl:
48-
# Instruct requests to verify server certificates against the
49-
# default CA bundle provided by 'certifi'. See
50-
# http://docs.python-requests.org/en/master/user/advanced/#ca
51-
# -certificates
52-
self.request_args["verify"] = True
53-
54-
else:
55-
# Instruct requests to not perform server cert verification.
56-
self.request_args["verify"] = False
5732

5833
self.events = None
5934
self.req_callback = None
60-
if client_cert:
61-
self.request_args['cert'] = client_cert
6235

6336
def _cookies(self):
6437
"""

src/oidcrp/oauth2/__init__.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
__author__ = 'Roland Hedberg'
2020

21+
from oidcrp.util import has_method
22+
2123
logger = logging.getLogger(__name__)
2224

2325
Version = "2.0"
@@ -31,38 +33,35 @@ class ExpiredToken(Exception):
3133

3234

3335
class Client(object):
34-
def __init__(self, state_db, ca_certs=None, client_authn_factory=None,
35-
keyjar=None, verify_ssl=True, config=None, client_cert=None,
36-
httplib=None, services=None, jwks_uri=''):
36+
def __init__(self, state_db, client_authn_factory=None,
37+
keyjar=None, verify_ssl=True, config=None,
38+
httplib=None, services=None, jwks_uri='', httpc_params=None):
3739
"""
3840
39-
:param ca_certs: Certificates used to verify HTTPS certificates
4041
:param client_authn_factory: Factory that this client can use to
4142
initiate a client authentication class.
4243
:param keyjar: A py:class:`oidcmsg.key_jar.KeyJar` instance
43-
:param verify_ssl: Whether the SSL certificate should be verified.
4444
:param config: Configuration information passed on to the
4545
:py:class:`oidcservice.service_context.ServiceContext`
4646
initialization
47-
:param client_cert: Certificate used by the HTTP client
4847
:param httplib: A HTTP client to use
4948
:param services: A list of service definitions
5049
:param jwks_uri: A jwks_uri
50+
:param httpc_params: HTTP request arguments
5151
:return: Client instance
5252
"""
5353

5454
self.session_interface = StateInterface(state_db)
55-
self.http = httplib or HTTPLib(ca_certs=ca_certs,
56-
verify_ssl=verify_ssl,
57-
client_cert=client_cert)
55+
self.http = httplib or HTTPLib(httpc_params)
5856

5957
if not keyjar:
6058
keyjar = KeyJar()
6159
keyjar.verify_ssl = verify_ssl
6260

6361
self.events = None
6462
self.service_context = ServiceContext(keyjar, config=config,
65-
jwks_uri=jwks_uri)
63+
jwks_uri=jwks_uri,
64+
httpc_params=httpc_params)
6665
if self.service_context.client_id:
6766
self.client_id = self.service_context.client_id
6867

@@ -77,7 +76,6 @@ def __init__(self, state_db, ca_certs=None, client_authn_factory=None,
7776
do_add_ons(config['add_ons'], self.service)
7877

7978
self.service_context.service = self.service
80-
8179
self.verify_ssl = verify_ssl
8280

8381
def do_request(self, request_type, response_body_type="", request_args=None,
@@ -149,7 +147,7 @@ def service_request(self, service, url, method="GET", body=None,
149147
:param body: A message body if any
150148
:param response_body_type: The expected format of the body of the
151149
return message
152-
:param http_args: Arguments for the HTTP client
150+
:param httpc_params: Arguments for the HTTP client
153151
:return: A cls or ResponseMessage instance or the HTTP response
154152
instance if no response body was expected.
155153
"""
@@ -159,8 +157,12 @@ def service_request(self, service, url, method="GET", body=None,
159157

160158
logger.debug(REQUEST_INFO.format(url, method, body, headers))
161159

162-
response = self.get_response(service, url, method, body, response_body_type, headers,
163-
**kwargs)
160+
if has_method(service, "get_response"):
161+
response = service.get_response(url, method, body, response_body_type, headers,
162+
**kwargs)
163+
else:
164+
response = self.get_response(service, url, method, body, response_body_type, headers,
165+
**kwargs)
164166

165167
if 'error' in response:
166168
pass

src/oidcrp/oidc/__init__.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,15 @@ class FetchException(Exception):
6161

6262

6363
class RP(oauth2.Client):
64-
def __init__(self, state_db, ca_certs=None, client_authn_factory=None,
65-
keyjar=None, verify_ssl=True, config=None, client_cert=None,
66-
httplib=None, services=None):
64+
def __init__(self, state_db, client_authn_factory=None,
65+
keyjar=None, verify_ssl=True, config=None,
66+
httplib=None, services=None, httpc_params=None):
6767

6868
_srvs = services or DEFAULT_SERVICES
6969

70-
oauth2.Client.__init__(self, state_db, ca_certs,
71-
client_authn_factory=client_authn_factory,
72-
keyjar=keyjar, verify_ssl=verify_ssl,
73-
config=config, client_cert=client_cert,
74-
httplib=httplib, services=_srvs)
70+
oauth2.Client.__init__(self, state_db, client_authn_factory=client_authn_factory,
71+
keyjar=keyjar, verify_ssl=verify_ssl, config=config,
72+
httplib=httplib, services=_srvs, httpc_params=httpc_params)
7573

7674
def fetch_distributed_claims(self, userinfo, callback=None):
7775
"""
@@ -90,20 +88,20 @@ def fetch_distributed_claims(self, userinfo, callback=None):
9088
if "endpoint" in spec:
9189
if "access_token" in spec:
9290
cauth = BearerHeader()
93-
http_args = cauth.construct(
91+
httpc_params = cauth.construct(
9492
service=self.service['userinfo'],
95-
access_token= spec['access_token'])
93+
access_token=spec['access_token'])
9694
_resp = self.http.send(spec["endpoint"], 'GET',
97-
**http_args)
95+
**httpc_params)
9896
else:
9997
if callback:
10098
token = callback(spec['endpoint'])
10199
cauth = BearerHeader()
102-
http_args = cauth.construct(
100+
httpc_params = cauth.construct(
103101
service=self.service['userinfo'],
104102
access_token=token)
105103
_resp = self.http.send(
106-
spec["endpoint"], 'GET', **http_args)
104+
spec["endpoint"], 'GET', **httpc_params)
107105
else:
108106
_resp = self.http.send(spec["endpoint"], 'GET')
109107

0 commit comments

Comments
 (0)