Skip to content

Commit 475ef2a

Browse files
committed
Split get_deserialization_method into get_content_type and get_deserialization_method.
1 parent 8a6522f commit 475ef2a

File tree

4 files changed

+71
-43
lines changed

4 files changed

+71
-43
lines changed

src/idpyoidc/client/oauth2/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from idpyoidc.client.service import SUCCESSFUL
1515
from idpyoidc.client.service import Service
1616
from idpyoidc.client.util import do_add_ons
17+
from idpyoidc.client.util import get_content_type
1718
from idpyoidc.client.util import get_deserialization_method
1819
from idpyoidc.configure import Configuration
1920
from idpyoidc.context import OidcContext
@@ -254,12 +255,13 @@ def parse_request_response(self, service, reqresp, response_body_type="", state=
254255

255256
if reqresp.status_code in SUCCESSFUL:
256257
logger.debug('response_body_type: "{}"'.format(response_body_type))
257-
_deser_method = get_deserialization_method(reqresp)
258+
_content_type = get_content_type(reqresp)
259+
_deser_method = get_deserialization_method(_content_type)
258260

259-
if _deser_method != response_body_type:
261+
if _content_type != response_body_type:
260262
logger.warning(
261263
"Not the body type I expected: {} != {}".format(
262-
_deser_method, response_body_type
264+
_content_type, response_body_type
263265
)
264266
)
265267
if _deser_method in ["json", "jwt", "urlencoded"]:
@@ -282,7 +284,9 @@ def parse_request_response(self, service, reqresp, response_body_type="", state=
282284
elif 400 <= reqresp.status_code < 500:
283285
logger.error("Error response ({}): {}".format(reqresp.status_code, reqresp.text))
284286
# expecting an error response
285-
_deser_method = get_deserialization_method(reqresp)
287+
_content_type = get_content_type(reqresp)
288+
_deser_method = get_deserialization_method(_content_type)
289+
286290
if not _deser_method:
287291
_deser_method = "json"
288292

src/idpyoidc/client/util.py

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""Utilities"""
2-
import logging
3-
import secrets
42
from http.cookiejar import Cookie
53
from http.cookiejar import http2time
4+
import logging
5+
import secrets
66
from urllib.parse import parse_qs
77
from urllib.parse import urlsplit
88
from urllib.parse import urlunsplit
@@ -16,7 +16,6 @@
1616
from idpyoidc.defaults import BASECHR
1717
from idpyoidc.exception import UnSupported
1818
from idpyoidc.util import importer
19-
2019
from .exception import TimeFormatError
2120
from .exception import WrongContentType
2221

@@ -202,7 +201,7 @@ def verify_header(reqresp, body_type):
202201
logger.debug("resp.txt: %s" % (sanitize(reqresp.text),))
203202

204203
try:
205-
_ctype = reqresp.headers["content-type"]
204+
_ctype = get_content_type(reqresp)
206205
except KeyError:
207206
if body_type:
208207
return body_type
@@ -249,45 +248,54 @@ def verify_header(reqresp, body_type):
249248
return body_type
250249

251250

252-
def get_deserialization_method(reqresp):
253-
"""
254-
255-
:param reqresp: Class instance with attributes: ['status', 'text',
256-
'headers', 'url']
257-
:return: Verified body content type
258-
"""
251+
def get_content_type(reqresp) -> str:
259252
logger.debug("resp.headers: %s" % (sanitize(reqresp.headers),))
260253
logger.debug("resp.txt: %s" % (sanitize(reqresp.text),))
254+
ctype = reqresp.headers.get("content-type")
261255

262-
_ctype = reqresp.headers.get("content-type")
263-
if not _ctype:
256+
if not ctype:
264257
# let's try to detect the format
265258
try:
266259
reqresp.json()
267-
return "json"
260+
return "application/json"
268261
except Exception:
269262
try:
270263
_jwt = factory(reqresp.txt)
271-
return "jwt"
264+
return "application/jwt"
272265
except Exception:
273-
return "urlencoded" # reasonable default ??
274-
elif ';' in _ctype:
275-
for _typ in _ctype.split(";"):
266+
try:
267+
_info = parse_qs(reqresp.txt)
268+
return "application/x-www-form-urlencoded"
269+
except Exception:
270+
return "text/html" # reasonable default ??
271+
elif ';' in ctype:
272+
for _typ in ctype.split(";"):
276273
if _typ.startswith("application") or _typ.startswith("text"):
277-
_ctype = _typ
274+
ctype = _typ
278275
break
279276

280-
if match_to_("application/json", _ctype) or match_to_("application/jrd+json", _ctype):
277+
return ctype
278+
279+
280+
def get_deserialization_method(ctype):
281+
"""
282+
283+
:param reqresp: Class instance with attributes: ['status', 'text',
284+
'headers', 'url']
285+
:return: Verified body content type
286+
"""
287+
288+
if match_to_("application/json", ctype) or match_to_("application/jrd+json", ctype):
281289
deser_method = "json"
282-
elif match_to_("application/jwt", _ctype):
290+
elif match_to_("application/jwt", ctype):
283291
deser_method = "jwt"
284-
elif match_to_("application/jose", _ctype):
292+
elif match_to_("application/jose", ctype):
285293
deser_method = "jose"
286-
elif match_to_(URL_ENCODED, _ctype):
294+
elif match_to_(URL_ENCODED, ctype):
287295
deser_method = "urlencoded"
288-
elif match_to_("text/plain", _ctype) or match_to_("test/html", _ctype):
296+
elif match_to_("text/plain", ctype) or match_to_("test/html", ctype):
289297
deser_method = ""
290-
elif _ctype.startswith("application/") and _ctype.endswith("+jwt"):
298+
elif ctype.startswith("application/") and ctype.endswith("+jwt"):
291299
deser_method = "jwt"
292300
else:
293301
deser_method = "" # reasonable default ??

tests/test_client_05_util.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pytest
88

99
from idpyoidc.client.exception import WrongContentType
10+
from idpyoidc.client.util import get_content_type
1011
from idpyoidc.client.util import get_deserialization_method
1112
from idpyoidc.client.util import get_http_body
1213
from idpyoidc.client.util import get_http_url
@@ -139,28 +140,35 @@ def test_verify_header():
139140

140141
def test_get_deserialization_method_json():
141142
resp = FakeResponse("application/json")
142-
assert get_deserialization_method(resp) == "json"
143+
ctype = get_content_type(resp)
144+
assert get_deserialization_method(ctype) == "json"
143145

144146
resp = FakeResponse("application/json; charset=utf-8")
145-
assert get_deserialization_method(resp) == "json"
147+
ctype = get_content_type(resp)
148+
assert get_deserialization_method(ctype) == "json"
146149

147150
resp.headers["content-type"] = "application/jrd+json"
148-
assert get_deserialization_method(resp) == "json"
151+
ctype = get_content_type(resp)
152+
assert get_deserialization_method(ctype) == "json"
149153

150154

151155
def test_get_deserialization_method_jwt():
152156
resp = FakeResponse("application/jwt")
153-
assert get_deserialization_method(resp) == "jwt"
157+
ctype = get_content_type(resp)
158+
assert get_deserialization_method(ctype) == "jwt"
154159

155160

156161
def test_get_deserialization_method_urlencoded():
157162
resp = FakeResponse(URL_ENCODED)
158-
assert get_deserialization_method(resp) == "urlencoded"
163+
ctype = get_content_type(resp)
164+
assert get_deserialization_method(ctype) == "urlencoded"
159165

160166

161167
def test_get_deserialization_method_text():
162168
resp = FakeResponse("text/html")
163-
assert get_deserialization_method(resp) == ""
169+
ctype = get_content_type(resp)
170+
assert get_deserialization_method(ctype) == ""
164171

165172
resp = FakeResponse("text/plain")
166-
assert get_deserialization_method(resp) == ""
173+
ctype = get_content_type(resp)
174+
assert get_deserialization_method(ctype) == ""

tests/test_client_16_util.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from idpyoidc.client.exception import WrongContentType
1313
from idpyoidc.client.util import JSON_ENCODED
1414
from idpyoidc.client.util import URL_ENCODED
15+
from idpyoidc.client.util import get_content_type
1516
from idpyoidc.client.util import get_deserialization_method
1617
from idpyoidc.message.oauth2 import AccessTokenRequest
1718
from idpyoidc.message.oauth2 import AuthorizationRequest
@@ -145,31 +146,38 @@ def test_verify_header():
145146

146147
def test_get_deserialization_method_json():
147148
resp = FakeResponse("application/json")
148-
assert get_deserialization_method(resp) == "json"
149+
ctype = get_content_type(resp)
150+
assert get_deserialization_method(ctype) == "json"
149151

150152
resp = FakeResponse("application/json; charset=utf-8")
151-
assert get_deserialization_method(resp) == "json"
153+
ctype = get_content_type(resp)
154+
assert get_deserialization_method(ctype) == "json"
152155

153156
resp.headers["content-type"] = "application/jrd+json"
154-
assert get_deserialization_method(resp) == "json"
157+
ctype = get_content_type(resp)
158+
assert get_deserialization_method(ctype) == "json"
155159

156160

157161
def test_get_deserialization_method_jwt():
158162
resp = FakeResponse("application/jwt")
159-
assert get_deserialization_method(resp) == "jwt"
163+
ctype = get_content_type(resp)
164+
assert get_deserialization_method(ctype) == "jwt"
160165

161166

162167
def test_get_deserialization_method_urlencoded():
163168
resp = FakeResponse(URL_ENCODED)
164-
assert get_deserialization_method(resp) == "urlencoded"
169+
ctype = get_content_type(resp)
170+
assert get_deserialization_method(ctype) == "urlencoded"
165171

166172

167173
def test_get_deserialization_method_text():
168174
resp = FakeResponse("text/html")
169-
assert get_deserialization_method(resp) == ""
175+
ctype = get_content_type(resp)
176+
assert get_deserialization_method(ctype) == ""
170177

171178
resp = FakeResponse("text/plain")
172-
assert get_deserialization_method(resp) == ""
179+
ctype = get_content_type(resp)
180+
assert get_deserialization_method(ctype) == ""
173181

174182

175183
def test_verify_no_content_type():

0 commit comments

Comments
 (0)