Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(cognito): require the openid scope at userInfo under AUTH=true (#2272)

iot-rocket558af950f01e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8f9a095b..01ca8263 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,8 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
### Fixed
+- **Cognito — `/oauth2/userInfo` requires the `openid` scope under `AUTH=true`** — an access token without the `openid` scope, such as the token of a user pool API sign-in or of a Hosted UI grant of `email` alone, returned the user's attributes; with `AUTH=true` it now answers 401 `invalid_token` `Access token does not contain the 'openid' scope` once the token, its `username` and the user have passed the other checks. Contributed by @iot-rocket.
+- **Cognito — `/oauth2/userInfo` verifies the access token under `AUTH=true`** — the endpoint returned the user's attributes for any JWT carrying a user's `sub`, including an ID token, a token with an altered payload, a revoked or expired token and the token of a disabled or deleted user; with `AUTH=true` it now answers 401 `invalid_token` with AWS's `error_description` and `WWW-Authenticate` for each case. Contributed by @iot-rocket.
- **Cognito — `/oauth2/userInfo` verifies the access token under `AUTH=true`** — the endpoint returned the user's attributes for any JWT carrying a user's `sub`, including an ID token, a token with an altered payload, a revoked or expired token and the token of a disabled or deleted user; with `AUTH=true` it now answers 401 `invalid_token` with AWS's `error_description` and `WWW-Authenticate` for each case. Contributed by @iot-rocket.
- **Cognito Identity — `GetId` and `GetCredentialsForIdentity` check `Logins` under `AUTH=true`** — both calls took any token in `Logins`, `GetCredentialsForIdentity` vended authenticated credentials from the stored login when the request carried none, `GetId` created a new identity for a login that already had one, and a login given to an identity without one was not kept; with `AUTH=true` both calls refuse the tokens `GetOpenIdToken` refuses with the same `NotAuthorizedException` messages (and a Google login that is no JWT), `GetCredentialsForIdentity` refuses a request without `Logins` for an identity with a login, `GetId` returns the identity that holds the login, and `GetCredentialsForIdentity` and `GetOpenIdToken` link the login to an identity that had none, as AWS does. Contributed by @iot-rocket.
- **Cognito Identity — `GetOpenIdToken` checks `Logins` under `AUTH=true`** — the call issued a token for an identity with a login when the request carried no `Logins`, and for any token in `Logins`; with `AUTH=true` it now answers `NotAuthorizedException` as AWS does, with `Access to Identity '<identity id>' is forbidden.` without `Logins` and, for a user pool login, when the token is no ID token of a trusted app client, has a bad signature, expired more than five minutes ago or belongs to another user than the identity's login. Contributed by @iot-rocket.
diff --git a/ministack/services/cognito.py b/ministack/services/cognito.py
index ce087265..03d1e7f6 100644
--- a/ministack/services/cognito.py
+++ b/ministack/services/cognito.py
@@ -7319,7 +7319,11 @@ def _userinfo_token_error(token: str):
return "Value of username in token claims must be a String"
pool = _get_pool_unscoped(str(claims.get("iss", "")).rsplit("/", 1)[-1])
user = _user_from_token(token, pool) if pool else None
- return None if user and user.get("Enabled", True) else expired
+ if not user or not user.get("Enabled", True):
+ return expired
+ if "openid" not in str(claims.get("scope", "")).split():
+ return "Access token does not contain the 'openid' scope"
+ return None
def handle_oauth2_userinfo(method, path, headers, body, query_params):
diff --git a/tests/test_cognito.py b/tests/test_cognito.py
index 263eac53..ccc8e3d5 100644
--- a/tests/test_cognito.py
+++ b/tests/test_cognito.py
@@ -5536,6 +5536,29 @@ def test_oauth2_userinfo_refuses_revoked_tokens_and_disabled_users(auth_enabled,
assert _userinfo(kept["access_token"])[0] == 200
+def test_oauth2_userinfo_requires_the_openid_scope(auth_enabled, cognito_idp):
+ pid, cid, sign_in = _userinfo_pool(cognito_idp, "bob")
+
+ def api_token(username):
+ return cognito_idp.initiate_auth(ClientId=cid, AuthFlow="USER_PASSWORD_AUTH", AuthParameters={
+ "USERNAME": username, "PASSWORD": "TestPass1!"})["AuthenticationResult"]["AccessToken"]
+
+ disabled = api_token("bob")
+ cognito_idp.admin_disable_user(UserPoolId=pid, Username="bob")
+ # A disabled user's token gets the generic answer before the scope is looked at.
+ for token, message in [
+ (sign_in("testuser", scope="email")["access_token"], "Access token does not contain the 'openid' scope"),
+ (api_token("testuser"), "Access token does not contain the 'openid' scope"),
+ (disabled, "Access token is expired or user has globally signed out, disabled, or been deleted"),
+ ]:
+ if auth_enabled:
+ assert _userinfo(token)[::2] == (401, {"error": "invalid_token", "error_description": message})
+ else:
+ assert _userinfo(token)[0] == 200
+ assert _userinfo(sign_in("testuser", scope="openid")["access_token"])[0] == 200
+
+
def _userinfo_in_process(mod, token):
"""(status, WWW-Authenticate, body) of /oauth2/userInfo called in process."""
status, headers, body = mod.handle_oauth2_userinfo(
@@ -5551,7 +5574,9 @@ def test_oauth2_userinfo_refuses_an_expired_token_under_auth(monkeypatch):
with monkeypatch.context() as m:
m.setattr(mod.time, "time", lambda: issued)
expired = sign_in("carol")["AccessToken"]
- assert _userinfo_in_process(mod, sign_in("carol")["AccessToken"])[0] == 200
+ # API tokens lack the openid scope; expiry is checked first.
+ assert _userinfo_in_process(mod, sign_in("carol")["AccessToken"])[2] == {
+ "error": "invalid_token", "error_description": "Access token does not contain the 'openid' scope"}
assert _userinfo_in_process(mod, expired)[2] == {
"error": "invalid_token",
"error_description": "Access token is expired or user has globally signed out, disabled, or been deleted",