Skip to content

Commit 2335ecd

Browse files
authored
Merge pull request #116 from lionick/unbind_auth_user_info
Unbind authentication event lifetime from userinfo response
2 parents a78dabe + d74a312 commit 2335ecd

File tree

1 file changed

+25
-35
lines changed

1 file changed

+25
-35
lines changed

Diff for: src/idpyoidc/server/oidc/userinfo.py

+25-35
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ def do_response(
6464
client_id: Optional[str] = "",
6565
**kwargs,
6666
) -> dict:
67-
6867
if "error" in kwargs and kwargs["error"]:
6968
return Endpoint.do_response(self, response_args, request, **kwargs)
7069

@@ -126,44 +125,35 @@ def process_request(self, request=None, **kwargs):
126125
return self.error_cls(error="invalid_token", error_description="Invalid Token")
127126

128127
_grant = _session_info["grant"]
129-
token = _grant.get_token(request["access_token"])
130-
# should be an access token
131-
if token and token.token_class != "access_token":
132-
return self.error_cls(error="invalid_token", error_description="Wrong type of token")
128+
access_token = _grant.get_token(request["access_token"])
133129

134-
# And it should be valid
135-
if token.is_active() is False:
130+
# there must be a token
131+
if not access_token:
136132
return self.error_cls(error="invalid_token", error_description="Invalid Token")
137133

138-
allowed = True
139-
_auth_event = _grant.authentication_event
140-
# if the authentication is still active or offline_access is granted.
141-
if not _auth_event["valid_until"] >= utc_time_sans_frac():
142-
logger.debug(
143-
"authentication not valid: {} > {}".format(
144-
datetime.fromtimestamp(_auth_event["valid_until"]),
145-
datetime.fromtimestamp(utc_time_sans_frac()),
146-
)
147-
)
148-
allowed = False
134+
# the token must be an access_token
135+
if access_token.token_class != "access_token":
136+
return self.error_cls(error="invalid_token", error_description="Wrong type of token")
149137

150-
# This has to be made more finegrained.
151-
# if "offline_access" in session["authn_req"]["scope"]:
152-
# pass
138+
# the access_token must be valid
139+
if access_token.is_active() is False:
140+
return self.error_cls(error="invalid_token", error_description="Invalid Token")
141+
142+
# the access_token must contain the openid scope
143+
if "openid" not in access_token.scope:
144+
return self.error_cls(error="invalid_token", error_description="Invalid Token")
153145

154146
_cntxt = self.upstream_get("context")
155-
if allowed:
156-
_claims_restriction = _cntxt.claims_interface.get_claims(
157-
_session_info["branch_id"], scopes=token.scope, claims_release_point="userinfo"
158-
)
159-
info = _cntxt.claims_interface.get_user_claims(
160-
_session_info["user_id"],
161-
claims_restriction=_claims_restriction,
162-
client_id=_session_info["client_id"]
163-
)
164-
info["sub"] = _grant.sub
165-
if _grant.add_acr_value("userinfo"):
166-
info["acr"] = _grant.authentication_event["authn_info"]
147+
_claims_restriction = _cntxt.claims_interface.get_claims(
148+
_session_info["branch_id"], scopes=access_token.scope, claims_release_point="userinfo"
149+
)
150+
info = _cntxt.claims_interface.get_user_claims(
151+
_session_info["user_id"], claims_restriction=_claims_restriction,
152+
client_id=_session_info["client_id"]
153+
)
154+
info["sub"] = _grant.sub
155+
if _grant.add_acr_value("userinfo"):
156+
info["acr"] = _grant.authentication_event["authn_info"]
167157

168158
extra_claims = kwargs.get("extra_claims")
169159
if extra_claims:
@@ -173,7 +163,7 @@ def process_request(self, request=None, **kwargs):
173163
self.config["policy"] = _cntxt.cdb[request["client_id"]]["userinfo"]["policy"]
174164

175165
if "policy" in self.config:
176-
info = self._enforce_policy(request, info, token, self.config)
166+
info = self._enforce_policy(request, info, access_token, self.config)
177167

178168
return {"response_args": info, "client_id": _session_info["client_id"]}
179169

@@ -213,7 +203,7 @@ def parse_request(self, request, http_info=None, **kwargs):
213203
def _enforce_policy(self, request, response_info, token, config):
214204
policy = config["policy"]
215205
callable = policy["function"]
216-
kwargs = policy.get("kwargs", {})
206+
kwargs = policy.get("kwargs") or {}
217207

218208
if isinstance(callable, str):
219209
try:

0 commit comments

Comments
 (0)