Skip to content

Commit

Permalink
Support for dict in Client.parse_response formats (#790)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Hultner authored Jul 7, 2021
1 parent a5e8986 commit 7cf85cd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
2 changes: 1 addition & 1 deletion pylama.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ ignore = D203,D212,E203
max_line_length = 120

[pylama:mccabe]
complexity = 29
complexity = 30
10 changes: 7 additions & 3 deletions src/oic/oauth2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -87,7 +88,7 @@

ENDPOINTS = ["authorization_endpoint", "token_endpoint", "token_revocation_endpoint"]

ENCODINGS = Literal["json", "urlencoded"]
ENCODINGS = Literal["json", "urlencoded", "dict"]


class ExpiredToken(PyoidcError):
Expand Down Expand Up @@ -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,
Expand All @@ -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 => "{}"'
Expand Down
14 changes: 14 additions & 0 deletions tests/test_oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit 7cf85cd

Please sign in to comment.