Skip to content

Commit f55b996

Browse files
authored
Merge pull request #723 from OpenIDC/settings
Settings
2 parents 4d28a6a + 00a6af9 commit f55b996

File tree

17 files changed

+452
-71
lines changed

17 files changed

+452
-71
lines changed

doc/conf.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
extensions = [
44
'sphinx.ext.autodoc',
5+
'sphinx.ext.napoleon',
56
]
67

8+
autoclass_content = 'both' # Merge the __init__ docstring into the class docstring.
9+
autodoc_member_order = 'bysource' # Order by source ordering
10+
711
templates_path = ['_templates']
812

913
source_suffix = '.rst'

doc/contrib/settings.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Settings
2+
========
3+
4+
.. automodule:: oic.utils.settings
5+
:members:
6+
:show-inheritance:

doc/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Getting a copy is simple with Pip_:
2525
examples/tls
2626

2727
contrib/install
28+
contrib/settings
2829
contrib/testing
2930
contrib/documentation
3031

pylama.ini

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
[pylama]
2-
linters = pyflakes,eradicate,pycodestyle,mccabe,pep257
3-
# D10X - Ignore complains about missing docstrings - we want to enforce style but do not want to add all docstrings
2+
linters = pyflakes,eradicate,pycodestyle,mccabe
43
# D203/D204 and D212/D213 are mutually exclusive, pick one
54
# E203 is not PEP8 compliant in pycodestyle
6-
ignore = D100,D101,D102,D103,D104,D105,D106,D107,D203,D212,E203
5+
ignore = D203,D212,E203
76

87
[pylama:pycodestyle]
98
max_line_length = 120

setup.cfg

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[pydocstyle]
2+
convention = google
3+
add-ignore =
4+
D1, # Ignore missing docstrings
5+
D212, # Multiline docstring not on first line
6+
D413, # No blank line after last section
7+
add-select =
8+
D213 # Multiline docstring should start on first line

src/oic/extension/client.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import hashlib
22
import logging
3+
import warnings
34

45
from jwkest import b64e
56

@@ -13,6 +14,7 @@
1314
from oic.oauth2.message import ErrorResponse
1415
from oic.utils.http_util import SUCCESSFUL
1516
from oic.utils.sanitize import sanitize
17+
from oic.utils.settings import OauthClientSettings
1618

1719
logger = logging.getLogger(__name__)
1820

@@ -36,17 +38,26 @@ def __init__(
3638
client_id=None,
3739
client_authn_method=None,
3840
keyjar=None,
39-
verify_ssl=True,
41+
verify_ssl=None,
4042
config=None,
4143
message_factory=ExtensionMessageFactory,
44+
settings=None,
4245
):
46+
self.settings = settings or OauthClientSettings()
47+
if verify_ssl is not None:
48+
warnings.warn(
49+
"`verify_ssl` is deprecated, please use `settings` instead if you need to set a non-default value.",
50+
DeprecationWarning,
51+
stacklevel=2,
52+
)
53+
self.settings.verify_ssl = verify_ssl
4354
super().__init__(
4455
client_id=client_id,
4556
client_authn_method=client_authn_method,
4657
keyjar=keyjar,
47-
verify_ssl=verify_ssl,
4858
config=config,
4959
message_factory=message_factory,
60+
settings=self.settings,
5061
)
5162
self.allow = {}
5263
self.request2endpoint.update(

src/oic/oauth2/__init__.py

Lines changed: 76 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import warnings
23
from typing import Any # noqa - This is used for MyPy
34
from typing import Dict
45
from typing import List # noqa - This is used for MyPy
@@ -53,6 +54,9 @@
5354
from oic.utils.keyio import KeyJar
5455
from oic.utils.sdb import SessionBackend # noqa
5556
from oic.utils.sdb import session_update
57+
from oic.utils.settings import OauthClientSettings
58+
from oic.utils.settings import OauthServerSettings
59+
from oic.utils.settings import PyoidcSettings
5660
from oic.utils.time_util import utc_time_sans_frac
5761

5862
__author__ = "rohe0002"
@@ -170,35 +174,61 @@ def __init__(
170174
client_id=None,
171175
client_authn_method=None,
172176
keyjar=None,
173-
verify_ssl=True,
177+
verify_ssl=None,
174178
config=None,
175179
client_cert=None,
176-
timeout=5,
180+
timeout=None,
177181
message_factory: Type[MessageFactory] = OauthMessageFactory,
182+
settings: PyoidcSettings = None,
178183
):
179184
"""
180185
Initialize the instance.
181186
187+
Keyword Args:
188+
settings
189+
Instance of :class:`OauthClientSettings` with configuration options.
190+
Currently used settings are:
191+
- verify_ssl
192+
- client_cert
193+
- timeout
194+
182195
:param client_id: The client identifier
183196
:param client_authn_method: Methods that this client can use to
184197
authenticate itself. It's a dictionary with method names as
185198
keys and method classes as values.
186199
:param keyjar: The keyjar for this client.
187-
:param verify_ssl: Whether the SSL certificate should be verified.
188-
:param client_cert: A client certificate to use.
200+
:param verify_ssl: Whether the SSL certificate should be verified. Deprecated in favor of settings.
201+
:param client_cert: A client certificate to use. Deprecated in favor of settings.
189202
:param timeout: Timeout for requests library. Can be specified either as
190203
a single integer or as a tuple of integers. For more details, refer to
191-
``requests`` documentation.
204+
``requests`` documentation. Deprecated in favor of settings.
192205
:param: message_factory: Factory for message classes, should inherit from OauthMessageFactory
193206
:return: Client instance
207+
194208
"""
195-
PBase.__init__(
196-
self,
197-
verify_ssl=verify_ssl,
198-
keyjar=keyjar,
199-
client_cert=client_cert,
200-
timeout=timeout,
201-
)
209+
self.settings = settings or OauthClientSettings()
210+
if verify_ssl is not None:
211+
warnings.warn(
212+
"`verify_ssl` is deprecated, please use `settings` instead if you need to set a non-default value.",
213+
DeprecationWarning,
214+
stacklevel=2,
215+
)
216+
self.settings.verify_ssl = verify_ssl
217+
if client_cert is not None:
218+
warnings.warn(
219+
"`client_cert` is deprecated, please use `settings` instead if you need to set a non-default value.",
220+
DeprecationWarning,
221+
stacklevel=2,
222+
)
223+
self.settings.client_cert = client_cert
224+
if timeout is not None:
225+
warnings.warn(
226+
"`timeout` is deprecated, please use `settings` instead if you need to set a non-default value.",
227+
DeprecationWarning,
228+
stacklevel=2,
229+
)
230+
self.settings.timeout = timeout
231+
PBase.__init__(self, keyjar=keyjar, settings=self.settings)
202232

203233
self.sso_db = None # type: Optional[SessionBackend]
204234
self.client_id = client_id
@@ -1119,19 +1149,44 @@ class Server(PBase):
11191149

11201150
def __init__(
11211151
self,
1122-
verify_ssl: bool = True,
1152+
verify_ssl: bool = None,
11231153
keyjar: KeyJar = None,
11241154
client_cert: Union[str, Tuple[str, str]] = None,
1125-
timeout: int = 5,
1155+
timeout: float = None,
11261156
message_factory: Type[MessageFactory] = OauthMessageFactory,
1157+
settings: PyoidcSettings = None,
11271158
):
1128-
"""Initialize the server."""
1129-
super().__init__(
1130-
verify_ssl=verify_ssl,
1131-
keyjar=keyjar,
1132-
client_cert=client_cert,
1133-
timeout=timeout,
1134-
)
1159+
"""
1160+
Initialize the server.
1161+
1162+
Keyword Args:
1163+
settings
1164+
Instance of :class:`OauthServerSettings` with configuration options.
1165+
1166+
"""
1167+
self.settings = settings or OauthServerSettings()
1168+
if verify_ssl is not None:
1169+
warnings.warn(
1170+
"`verify_ssl` is deprecated, please use `settings` instead if you need to set a non-default value.",
1171+
DeprecationWarning,
1172+
stacklevel=2,
1173+
)
1174+
self.settings.verify_ssl = verify_ssl
1175+
if client_cert is not None:
1176+
warnings.warn(
1177+
"`client_cert` is deprecated, please use `settings` instead if you need to set a non-default value.",
1178+
DeprecationWarning,
1179+
stacklevel=2,
1180+
)
1181+
self.settings.client_cert = client_cert
1182+
if timeout is not None:
1183+
warnings.warn(
1184+
"`timeout` is deprecated, please use `settings` instead if you need to set a non-default value.",
1185+
DeprecationWarning,
1186+
stacklevel=2,
1187+
)
1188+
self.settings.timeout = timeout
1189+
super().__init__(keyjar=keyjar, settings=self.settings)
11351190
self.message_factory = message_factory
11361191

11371192
@staticmethod

src/oic/oauth2/base.py

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import copy
22
import http.cookiejar as cookielib
33
import logging
4+
import warnings
45
from http.cookies import CookieError
56
from http.cookies import SimpleCookie
67

@@ -10,6 +11,7 @@
1011
from oic.oauth2.util import set_cookie
1112
from oic.utils.keyio import KeyJar
1213
from oic.utils.sanitize import sanitize
14+
from oic.utils.settings import PyoidcSettings
1315

1416
__author__ = "roland"
1517

@@ -19,10 +21,22 @@
1921
class PBase(object):
2022
"""Class for OAuth2 clients and servers."""
2123

22-
def __init__(self, verify_ssl=True, keyjar=None, client_cert=None, timeout=5):
24+
def __init__(
25+
self,
26+
verify_ssl=None,
27+
keyjar=None,
28+
client_cert=None,
29+
timeout=None,
30+
settings: PyoidcSettings = None,
31+
):
2332
"""
2433
Initialize the instance.
2534
35+
Keyword Args:
36+
settings
37+
Instance of :class:`PyoidcSettings` with configuration options.
38+
39+
Note that the following params are deprecated in favor of settings.
2640
:param verify_ssl: Control TLS server certificate validation. If set to
2741
True the certificate is validated against the global settings,
2842
if set to False, no validation is performed. If set to a filename
@@ -37,17 +51,41 @@ def __init__(self, verify_ssl=True, keyjar=None, client_cert=None, timeout=5):
3751
:param timeout: Timeout for requests library. Can be specified either as
3852
a single integer or as a tuple of integers. For more details, refer to
3953
``requests`` documentation.
54+
4055
"""
41-
self.keyjar = keyjar or KeyJar(verify_ssl=verify_ssl)
56+
self.settings = settings or PyoidcSettings()
57+
if verify_ssl is not None:
58+
warnings.warn(
59+
"`verify_ssl` is deprecated, please use `settings` instead if you need to set a non-default value.",
60+
DeprecationWarning,
61+
stacklevel=2,
62+
)
63+
self.settings.verify_ssl = verify_ssl
64+
if client_cert is not None:
65+
warnings.warn(
66+
"`client_cert` is deprecated, please use `settings` instead if you need to set a non-default value.",
67+
DeprecationWarning,
68+
stacklevel=2,
69+
)
70+
self.settings.client_cert = client_cert
71+
if timeout is not None:
72+
warnings.warn(
73+
"`timeout` is deprecated, please use `settings` instead if you need to set a non-default value.",
74+
DeprecationWarning,
75+
stacklevel=2,
76+
)
77+
self.settings.timeout = timeout
78+
79+
self.keyjar = keyjar or KeyJar(verify_ssl=self.settings.verify_ssl)
4280

4381
self.cookiejar = cookielib.FileCookieJar()
4482

4583
# Additional args for the requests library calls
4684
self.request_args = {
4785
"allow_redirects": False,
48-
"cert": client_cert,
49-
"verify": verify_ssl,
50-
"timeout": timeout,
86+
"cert": self.settings.client_cert,
87+
"verify": self.settings.verify_ssl,
88+
"timeout": self.settings.timeout,
5189
}
5290

5391
# Event collector, for tracing
@@ -84,7 +122,7 @@ def http_request(self, url, method="GET", **kwargs):
84122

85123
if self.cookiejar:
86124
_kwargs["cookies"] = self._cookies()
87-
logger.debug("SENT {} COOKIES".format(len(_kwargs["cookies"])))
125+
logger.debug("SENT %s COOKIES", len(_kwargs["cookies"])) # type: ignore
88126

89127
if self.req_callback is not None:
90128
_kwargs = self.req_callback(method, url, **_kwargs)

0 commit comments

Comments
 (0)