From 7cf85cd41e7ce5b677213dc7e43f86eca3bd5842 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Hultn=C3=A9r?= Date: Wed, 7 Jul 2021 13:38:28 +0200 Subject: [PATCH] Support for dict in Client.parse_response formats (#790) * Support for dict in Client.parse_response formats * Added change to changelog * Added type guard and cast to Client.parse_response * Update pylama.ini Temporarily raise mccabe limits. --- CHANGELOG.md | 2 ++ pylama.ini | 2 +- src/oic/oauth2/__init__.py | 10 +++++++--- tests/test_oauth2.py | 14 ++++++++++++++ 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 536d754ff..3634f164d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,12 +11,14 @@ The format is based on the [KeepAChangeLog] project. - [#763] Drop python 3.5 support ### Added +- [#790] Support for dict in Client.parse_response formats - [#739] Better error message for providers which return HTTP Error 405 on userinfo - [#723] Add settings class to handle settings related to Client and Server [#723]: https://github.com/OpenIDC/pyoidc/pull/723/ [#739]: https://github.com/OpenIDC/pyoidc/pull/739/ [#763]: https://github.com/OpenIDC/pyoidc/pull/763/ +[#790]: https://github.com/OpenIDC/pyoidc/pull/790 ## 1.2.0 [2020-02-05] diff --git a/pylama.ini b/pylama.ini index 0a4e8cc4e..767dc7677 100644 --- a/pylama.ini +++ b/pylama.ini @@ -8,4 +8,4 @@ ignore = D203,D212,E203 max_line_length = 120 [pylama:mccabe] -complexity = 29 +complexity = 30 diff --git a/src/oic/oauth2/__init__.py b/src/oic/oauth2/__init__.py index 8330f0268..fbd5a94b4 100644 --- a/src/oic/oauth2/__init__.py +++ b/src/oic/oauth2/__init__.py @@ -7,6 +7,7 @@ from typing import Tuple from typing import Type from typing import Union +from typing import cast from urllib.parse import urlparse from jwkest import b64e @@ -87,7 +88,7 @@ ENDPOINTS = ["authorization_endpoint", "token_endpoint", "token_revocation_endpoint"] -ENCODINGS = Literal["json", "urlencoded"] +ENCODINGS = Literal["json", "urlencoded", "dict"] class ExpiredToken(PyoidcError): @@ -609,7 +610,7 @@ def get_urlinfo(info: str) -> str: def parse_response( self, response: Type[Message], - info: str = "", + info: Union[str, Dict] = "", sformat: ENCODINGS = "json", state: str = "", **kwargs, @@ -627,8 +628,11 @@ def parse_response( """ _r2e = self.response2error + if isinstance(info, dict) and sformat != "dict": + raise TypeError("If info is a dict sformat must be dict") + if sformat == "urlencoded": - info = self.get_urlinfo(info) + info = self.get_urlinfo(cast(str, info)) resp = response().deserialize(info, sformat, **kwargs) msg = 'Initial response parsing => "{}"' diff --git a/tests/test_oauth2.py b/tests/test_oauth2.py index beee8c5c3..499d66e31 100644 --- a/tests/test_oauth2.py +++ b/tests/test_oauth2.py @@ -145,6 +145,20 @@ def test_parse_authz_resp_query(self): assert self.client.grant["hij"].code == aresp["code"] assert self.client.grant["hij"].grant_expiration_time + def test_parse_authz_resp_dict(self): + code = "SplxlOBeZQQYbYS6WxSbIA" + state = "ghi" + resp = dict(code=code, state=state) + aresp = self.client.parse_response( + AuthorizationResponse, info=resp, sformat="dict" + ) + + assert aresp["code"] == code + assert aresp["state"] == state + + assert self.client.grant[state].code == aresp["code"] + assert self.client.grant[state].grant_expiration_time + def test_parse_authz_resp_query_multi_scope(self): code = "SplxlOBeZQQYbYS6WxSbIA" states = ["ghi", "hij", "klm"]