Skip to content

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

fix(cognito): per-pool signing keys, S256-only PKCE, registered redirects (#2378)

Nahuel Nucerafc0b222d4518
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e4242175..9bd83816 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
### Fixed
+- **Cognito — signing keys, managed login and federated users** — every user pool signed with one shared key (`kid` `ministack-key-1`); each pool now has two keys of its own, one for access tokens and one for ID tokens, both published at its JWKS URI, and a token verifies only against its own pool. Managed login refuses PKCE `plain` and a `code_challenge` without `S256` (`error=invalid_request`), any `redirect_uri` the client didn't register, also for a client with no callback URLs, and a `grant_type` other than `authorization_code`, `refresh_token` or `client_credentials` (400 `unsupported_grant_type`). Adding an identity provider creates the group `<user pool id>_<provider name>`, and a federated user's new profile joins it and carries its `identities`, so the ID token has both claims. Reported by @cupidchan.
- **S3 — bucket policy writes through an access point** — under `AUTH=true` `PutBucketPolicy` and `DeleteBucketPolicy` sent through an access point alias or ARN changed the bucket's policy; they now get 403 `AccessDenied` with an explicit deny on the access point, while `GetBucketPolicy` through it and both writes on the bucket itself work as before. Contributed by @iot-rocket.
- **S3 — SigV2 requests through an access point** — under `AUTH=true` a SigV2 presigned URL for an access point ARN failed its signature check, because the check signed the access point host as a bucket, and a SigV2 `Authorization` header through an access point alias or ARN was served. The presigned URL is now checked on its path as the SDK signs it, and the header request gets 400 `InvalidRequest`. Contributed by @iot-rocket.
- **IoT — thing-group membership authorized on the group and the thing** — with `AUTH=true`, `AddThingToThingGroup` and `RemoveThingFromThingGroup` were authorized on `thinggroup/addThingToThingGroup` (the request path), so a policy granting exactly the thing group and the thing was denied; both calls are now checked on the thing group and then the thing named in the body (by name or ARN), and a denial names the resource that failed. Contributed by @iot-rocket.
diff --git a/ministack/services/cognito.py b/ministack/services/cognito.py
index 459a7ca9..77d1a6e3 100644
--- a/ministack/services/cognito.py
+++ b/ministack/services/cognito.py
@@ -71,8 +71,10 @@ import logging
import os
import re
import secrets
+import shutil
import string
import struct
+import threading
import time
import zlib
from datetime import datetime, timezone
@@ -124,9 +126,9 @@ try:
except ImportError:
fcntl = None
- def _load_rsa_key_from_disk():
+ def _load_rsa_key_from_disk(path=_RSA_KEY_PATH):
try:
- with open(_RSA_KEY_PATH, "rb") as _f:
+ with open(path, "rb") as _f:
return serialization.load_pem_private_key(_f.read(), password=None)
except Exception:
return None
@@ -146,14 +148,14 @@ try:
continue
continue
- def _persist_or_load_rsa_key(_key):
- os.makedirs(_STATE_DIR, exist_ok=True)
+ def _persist_or_load_rsa_key(_key, path=_RSA_KEY_PATH):
+ os.makedirs(os.path.dirname(path), exist_ok=True)
_pem = _key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption(),
)
- _tmp_path = f"{_RSA_KEY_PATH}.{os.getpid()}.{secrets.token_hex(8)}.tmp"
+ _tmp_path = f"{path}.{os.getpid()}.{secrets.token_hex(8)}.tmp"
try:
# A file lock gives process-safe creation/repair without relying
# on hard links, and the OS releases it if a process exits mid-repair.
@@ -163,26 +165,26 @@ try:
# Another process may have generated or repaired the shared
# key first. Reload that winner so pytest workers and the
# server expose the same JWKS.
- _disk_key = _load_rsa_key_from_disk()
+ _disk_key = _load_rsa_key_from_disk(path)
if _disk_key is not None:
return _disk_key
with open(_tmp_path, "xb") as _f:
_f.write(_pem)
_f.flush()
os.fsync(_f.fileno())
- os.replace(_tmp_path, _RSA_KEY_PATH)
+ os.replace(_tmp_path, path)
_tmp_path = None
return _key
# Platforms without fcntl still get a stable signing key in-process
# and best-effort atomic persistence across restarts.
- _disk_key = _load_rsa_key_from_disk()
+ _disk_key = _load_rsa_key_from_disk(path)
if _disk_key is not None:
return _disk_key
with open(_tmp_path, "xb") as _f:
_f.write(_pem)
_f.flush()
os.fsync(_f.fileno())
- os.replace(_tmp_path, _RSA_KEY_PATH)
+ os.replace(_tmp_path, path)
_tmp_path = None
return _key
finally:
@@ -192,9 +194,9 @@ try:
except FileNotFoundError:
pass
- def _persist_or_load_rsa_key_best_effort(_key):
+ def _persist_or_load_rsa_key_best_effort(_key, path=_RSA_KEY_PATH):
try:
- return _persist_or_load_rsa_key(_key)
+ return _persist_or_load_rsa_key(_key, path)
except Exception:
# Persistence is best-effort — if it fails, fall back to in-memory key.
return _key
@@ -237,9 +239,56 @@ except ImportError:
}
+# Each user pool has two keys of its own, one for access tokens and one for ID
+# tokens. They live in STATE_DIR beside the shared key so other processes sign alike.
+_POOL_KEYS_DIR = os.path.join(_STATE_DIR, "cognito-pool-keys")
+_POOL_KEY_USES = ("access", "id")
+_pool_keys: dict = {} # (pool id, use) -> (private key, JWK), a cache of the key files
+_pool_keys_lock = threading.Lock()
+
+
+def _pool_signing_key(pool_id: str, use: str):
+ """The private key and JWK the pool signs `use` tokens with, or (None, None) without cryptography."""
+ if _RSA_PRIVATE_KEY is None or not re.fullmatch(r"[\w-]+", pool_id or ""):
+ return None, None
+ with _pool_keys_lock:
+ cached = _pool_keys.get((pool_id, use))
+ if cached:
+ return cached
+ path = os.path.join(_POOL_KEYS_DIR, f"{pool_id}-{use}.pem")
+ key = _load_rsa_key_from_disk(path) if os.path.exists(path) else None
+ if key is None:
+ key = _persist_or_load_rsa_key_best_effort(
+ rsa.generate_private_key(public_exponent=65537, key_size=2048), path)
+ public = key.public_key()
+ der = public.public_bytes(serialization.Encoding.DER, serialization.PublicFormat.SubjectPublicKeyInfo)
+ numbers = public.public_numbers()
+ jwk = {
+ "kty": "RSA",
+ "alg": "RS256",
+ "use": "sig",
+ "kid": base64.b64encode(hashlib.sha256(der).digest()).decode(),
+ "n": _int_to_base64url(numbers.n, 256),
+ "e": _int_to_base64url(numbers.e, 3),
+ }
+ _pool_keys[(pool_id, use)] = (key, jwk)
+ return key, jwk
+
+
+def _drop_pool_keys(pool_id: str) -> None:
+ for use in _POOL_KEY_USES:
+ _pool_keys.pop((pool_id, use), None)
+ try:
+ os.unlink(os.path.join(_POOL_KEYS_DIR, f"{pool_id}-{use}.pem"))
+ except OSError:
+ pass
+
+
def well_known_jwks(pool_id: str):
"""Return JWKS JSON for /{poolId}/.well-known/jwks.json."""
- return 200, {"Content-Type": "application/json"}, json.dumps({"keys": [_JWKS_KEY]}).encode()
+ keys = [_pool_signing_key(pool_id, use)[1] for use in _POOL_KEY_USES]
+ keys = [k for k in keys if k] or [_JWKS_KEY]
+ return 200, {"Content-Type": "application/json"}, json.dumps({"keys": keys}).encode()
def well_known_openid_configuration(pool_id: str, region: str | None = None,
@@ -878,11 +927,14 @@ def _fake_token(sub: str, pool_id: str, client_id: str, token_type: str = "acces
if k == "identities":
# "Your user's ID token contains all of their associated
# providers in the identities claim" — as an array, while
- # the user attribute itself carries JSON text.
+ # the user attribute itself carries JSON text. The claim
+ # carries primary and dateCreated as strings.
try:
- claims[k] = json.loads(v)
+ parsed = json.loads(v)
except (TypeError, ValueError):
claims[k] = v
+ continue
+ claims[k] = [_identity_claim(i) for i in parsed] if isinstance(parsed, list) else parsed
continue
if k in ("email_verified", "phone_number_verified"):
# AWS carries the standard verified flags as JSON booleans in the ID token
@@ -920,22 +972,37 @@ def _fake_token(sub: str, pool_id: str, client_id: str, token_type: str = "acces
client_metadata=client_metadata,
scope=granted,
)
- return _signed_jwt(claims)
-
-
-def _signed_jwt(claims: dict) -> str:
- """A JWT over *claims*, signed with the RSA key when cryptography is available."""
+ return _signed_jwt(claims, pool_id, "id" if token_type == "id" else "access")
+
+
+def _identity_claim(identity):
+ """An identities entry as the ID token carries it, with primary and dateCreated as strings."""
+ if not isinstance(identity, dict):
+ return identity
+ out = dict(identity)
+ if "primary" in out:
+ out["primary"] = str(out["primary"]).lower()
+ if out.get("dateCreated") is not None:
+ out["dateCreated"] = str(out["dateCreated"])
+ return out
+
+
+def _signed_jwt(claims: dict, pool_id: str = "", use: str = "") -> str:
+ """A JWT over *claims*, signed with the pool's `use` key, else the shared key, when cryptography is available."""
+ key, jwk = _pool_signing_key(pool_id, use) if pool_id else (None, None)
+ if key is None:
+ key, jwk = _RSA_PRIVATE_KEY, _JWKS_KEY
header = base64.urlsafe_b64encode(
- json.dumps({"alg": "RS256", "kid": "ministack-key-1"}).encode()
+ json.dumps({"alg": "RS256", "kid": jwk["kid"]}).encode()
).rstrip(b"=").decode()
payload = base64.urlsafe_b64encode(
json.dumps(claims).encode()
).rstrip(b"=").decode()
signing_input = f"{header}.{payload}".encode()
- if _RSA_PRIVATE_KEY is not None:
+ if key is not None:
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
- sig_bytes = _RSA_PRIVATE_KEY.sign(signing_input, padding.PKCS1v15(), hashes.SHA256())
+ sig_bytes = key.sign(signing_input, padding.PKCS1v15(), hashes.SHA256())
sig = base64.urlsafe_b64encode(sig_bytes).rstrip(b"=").decode()
else:
sig = base64.urlsafe_b64encode(secrets.token_bytes(32)).rstrip(b"=").decode()
@@ -1296,13 +1363,27 @@ def _token_claims(token) -> dict:
def _signature_valid(token: str) -> bool:
- """Whether a JWT is signed with this emulator's signing key."""
+ """Whether a JWT is signed with the key its issuer signs with: its user pool's, else the shared one."""
from cryptography.exceptions import InvalidSignature
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
+ pool_id = str(_token_claims(token).get("iss", "")).rsplit("/", 1)[-1]
+ if _get_pool_unscoped(pool_id) is not None:
+ head = token.split(".", 1)[0]
+ try:
+ kid = json.loads(base64.urlsafe_b64decode(head + "=" * (-len(head) % 4))).get("kid")
+ except (ValueError, AttributeError):
+ return False
+ keys = [k for k, jwk in (_pool_signing_key(pool_id, use) for use in _POOL_KEY_USES)
+ if k is not None and jwk["kid"] == kid]
+ if not keys:
+ return False
+ key = keys[0]
+ else:
+ key = _RSA_PRIVATE_KEY
signing_input, _, sig = token.rpartition(".")
try:
- _RSA_PRIVATE_KEY.public_key().verify(
+ key.public_key().verify(
base64.urlsafe_b64decode(sig + "=" * (-len(sig) % 4)), signing_input.encode(),
padding.PKCS1v15(), hashes.SHA256())
except (InvalidSignature, ValueError):
@@ -2359,12 +2440,25 @@ def _cleanup_expired_codes():
def _verify_pkce(code_verifier: str, code_challenge: str, method: str) -> bool:
- if method == "S256":
- digest = hashlib.sha256(code_verifier.encode("ascii")).digest()
- computed = base64.urlsafe_b64encode(digest).rstrip(b"=").decode("ascii")
- return computed == code_challenge
- # plain
- return code_verifier == code_challenge
+ # Cognito supports only S256.
+ if method != "S256":
+ return False
+ digest = hashlib.sha256(code_verifier.encode("ascii")).digest()
+ computed = base64.urlsafe_b64encode(digest).rstrip(b"=").decode("ascii")
+ return computed == code_challenge
+
+
+def _pkce_error_redirect(redirect_uri: str, code_challenge: str, code_challenge_method: str, state: str):
+ """The invalid_request redirect for PKCE parameters Cognito refuses, or None."""
+ if not (code_challenge or code_challenge_method):
+ return None
+ if code_challenge and code_challenge_method == "S256":
+ return None
+ sep = "&" if "?" in redirect_uri else "?"
+ location = f"{redirect_uri}{sep}error=invalid_request"
+ if state:
+ location += f"&state={quote(state)}"
+ return 302, {"Location": location, "Cache-Control": "no-store"}, b""
def _qp(query_params: dict, key: str, default: str = "") -> str:
@@ -2901,6 +2995,7 @@ def _delete_user_pool(data):
if configured:
_pool_domain_map.pop(configured, None)
del _user_pools[pid]
+ _drop_pool_keys(pid)
return json_response({})
@@ -5578,6 +5673,7 @@ def _create_identity_provider(data):
"LastModifiedDate": now,
}
providers[provider_name] = provider
+ _provider_group(pool, pid, provider_name)
logger.info("Cognito: CreateIdentityProvider %s in pool %s", provider_name, pid)
return json_response({"IdentityProvider": provider})
@@ -5776,6 +5872,37 @@ def _federated_profile_username(provider_name, attribute_value):
return f"{provider_name}_{attribute_value}"
+def _provider_group(pool, pool_id, provider_name):
+ """The group `{user pool id}_{IdP name}` that holds the IdP's auto-created profiles."""
+ name = f"{pool_id}_{provider_name}"
+ group = pool["_groups"].get(name)
+ if group is None:
+ now = _now_epoch()
+ group = pool["_groups"][name] = {
+ "GroupName": name,
+ "UserPoolId": pool_id,
+ "CreationDate": now,
+ "LastModifiedDate": now,
+ "_members": [],
+ }
+ return group
+
+
+def _start_federated_profile(pool, pool_id, user, provider_name, user_id):
+ """Record a new IdP profile's own identity and add it to the IdP's group."""
+ _set_user_identities(user, [{
+ "userId": user_id,
+ "providerName": provider_name,
+ "providerType": _provider_type_for(pool, provider_name),
+ "issuer": None,
+ "primary": True,
+ "dateCreated": int(time.time() * 1000),
+ }])
+ group = _provider_group(pool, pool_id, provider_name)
+ group["_members"].append(user["Username"])
+ user["_groups"].append(group["GroupName"])
+
+
def _admin_link_provider_for_user(data):
pid = data.get("UserPoolId")
pool, err = _resolve_pool(pid)
@@ -6441,9 +6568,8 @@ def _oauth2_authorize_federation(query_params):
if not pool:
return error_response_json("ResourceNotFoundException", f"Client {client_id} not found.", 400)
- # Validate redirect_uri against CallbackURLs (skip if empty for dev convenience)
callback_urls = client.get("CallbackURLs", [])
- if callback_urls and redirect_uri not in callback_urls:
+ if redirect_uri not in callback_urls:
return error_response_json("InvalidParameterException",
f"redirect_uri {redirect_uri} is not in CallbackURLs.", 400)
invalid_scope = _invalid_scope_redirect(client, redirect_uri, _qp(query_params, "scope"), state)
@@ -6553,11 +6679,14 @@ def handle_oauth2_authorize(method, path, headers, query_params):
redirect_uri = client.get("DefaultRedirectURI", "")
if not redirect_uri:
return _oauth2_error("invalid_request", "redirect_uri is required.")
- if callback_urls and redirect_uri not in callback_urls:
+ if redirect_uri not in callback_urls:
return _oauth2_error("invalid_request", f"redirect_uri is not allowed: {redirect_uri}")
invalid_scope = _invalid_scope_redirect(client, redirect_uri, scope, state)
if invalid_scope:
return invalid_scope
+ pkce_error = _pkce_error_redirect(redirect_uri, code_challenge, code_challenge_method, state)
+ if pkce_error:
+ return pkce_error
if _use_choice_based_ui(client):
html_body = _username_page_html(
@@ -6700,6 +6829,7 @@ def _saml2_idp_response(body: bytes, query_params):
}
pool["_users"][username] = user
pool["EstimatedNumberOfUsers"] = len(pool["_users"])
+ _start_federated_profile(pool, pool_id, user, provider_name, name_id)
# Generate authorization code
_cleanup_expired_relay_codes()
@@ -6940,6 +7070,7 @@ def _oauth2_idp_response(method, body, query_params):
}
pool["_users"][username] = user
pool["EstimatedNumberOfUsers"] = len(pool["_users"])
+ _start_federated_profile(pool, pool_id, user, provider_name, name_id)
# Issue MiniStack auth code so the app's /oauth2/token call works.
_cleanup_expired_relay_codes()
@@ -6970,6 +7101,12 @@ def _oauth2_idp_response(method, body, query_params):
def _issue_auth_code_redirect(client_id, pool_id, redirect_uri, scope, username, nonce,
code_challenge, code_challenge_method, state):
"""Mint an authorization code and build the 302 redirect response for the Hosted UI."""
+ client = _find_pool_by_client_id(client_id)[2] or {}
+ if redirect_uri not in client.get("CallbackURLs", []):
+ return _oauth2_error("invalid_request", f"redirect_uri is not allowed: {redirect_uri}")
+ pkce_error = _pkce_error_redirect(redirect_uri, code_challenge, code_challenge_method, state)
+ if pkce_error:
+ return pkce_error
code = _generate_auth_code()
_authorization_codes[code] = {
"client_id": client_id,
@@ -7360,7 +7497,7 @@ def _oauth2_token(data, query_params, raw_body: bytes = b"", headers: dict | Non
if entry.get("code_challenge"):
if not code_verifier:
return _oauth2_error("invalid_grant", "code_verifier is required.")
- method = entry.get("code_challenge_method", "plain")
+ method = entry.get("code_challenge_method", "")
if not _verify_pkce(code_verifier, entry["code_challenge"], method):
return _oauth2_error("invalid_grant", "PKCE verification failed.")
@@ -7553,14 +7690,7 @@ def _oauth2_token(data, query_params, raw_body: bytes = b"", headers: dict | Non
}
return 200, {"Content-Type": "application/json"}, json.dumps(resp).encode()
- # ── fallback (legacy behaviour for unrecognised grant_type) ──
- pool_id, pool, client = _find_pool_by_client_id(cid)
- access_token = _fake_token(cid or new_uuid(), pool_id or "", cid or "", "access")
- return json_response({
- "access_token": access_token,
- "token_type": "Bearer",
- "expires_in": 3600,
- })
+ return 400, {"Content-Type": "application/json"}, json.dumps({"error": "unsupported_grant_type"}).encode()
async def handle_oauth2_token(method, path, headers, body, query_params):
@@ -8302,6 +8432,8 @@ def _apply_user_filter(users: list, filter_str: str) -> list:
def reset():
_user_pools.clear()
+ _pool_keys.clear()
+ shutil.rmtree(_POOL_KEYS_DIR, ignore_errors=True)
_pool_domain_map.clear()
_identity_pools.clear()
_identity_tags.clear()
diff --git a/tests/test_apigatewayv1.py b/tests/test_apigatewayv1.py
index a32ef936..392b319b 100644
--- a/tests/test_apigatewayv1.py
+++ b/tests/test_apigatewayv1.py
@@ -4857,16 +4857,18 @@ def _v1_signed_token(claims: dict) -> str:
from ministack.services import cognito as _cognito
- if _cognito._RSA_PRIVATE_KEY is None:
+ pool_id = str(claims.get("iss", "")).rsplit("/", 1)[-1]
+ key, jwk = _cognito._pool_signing_key(pool_id, "id" if claims.get("token_use") == "id" else "access")
+ if key is None:
pytest.skip("cryptography-backed Cognito signing key unavailable")
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
- header = {"alg": "RS256", "kid": "ministack-key-1"}
+ header = {"alg": "RS256", "kid": jwk["kid"]}
h = _b64.urlsafe_b64encode(json.dumps(header).encode()).rstrip(b"=").decode()
p = _b64.urlsafe_b64encode(json.dumps(claims).encode()).rstrip(b"=").decode()
- sig = _cognito._RSA_PRIVATE_KEY.sign(f"{h}.{p}".encode(), padding.PKCS1v15(), hashes.SHA256())
+ sig = key.sign(f"{h}.{p}".encode(), padding.PKCS1v15(), hashes.SHA256())
s = _b64.urlsafe_b64encode(sig).rstrip(b"=").decode()
return f"{h}.{p}.{s}"
diff --git a/tests/test_apigatewayv2.py b/tests/test_apigatewayv2.py
index 1c79cc5d..36c955a6 100644
--- a/tests/test_apigatewayv2.py
+++ b/tests/test_apigatewayv2.py
@@ -92,17 +92,19 @@ def _start_echo_server():
def _make_signed_token(claims: dict) -> str:
from ministack.services import cognito as _cognito
- if _cognito._RSA_PRIVATE_KEY is None:
+ pool_id = str(claims.get("iss", "")).rsplit("/", 1)[-1]
+ key, jwk = _cognito._pool_signing_key(pool_id, "id" if claims.get("token_use") == "id" else "access")
+ if key is None:
pytest.skip("cryptography-backed Cognito signing key unavailable")
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
- header = {"alg": "RS256", "kid": "ministack-key-1"}
+ header = {"alg": "RS256", "kid": jwk["kid"]}
h = base64.urlsafe_b64encode(json.dumps(header).encode()).rstrip(b"=").decode()
p = base64.urlsafe_b64encode(json.dumps(claims).encode()).rstrip(b"=").decode()
signed = f"{h}.{p}".encode()
- sig = _cognito._RSA_PRIVATE_KEY.sign(signed, padding.PKCS1v15(), hashes.SHA256())
+ sig = key.sign(signed, padding.PKCS1v15(), hashes.SHA256())
s = base64.urlsafe_b64encode(sig).rstrip(b"=").decode()
return f"{h}.{p}.{s}"
diff --git a/tests/test_cognito.py b/tests/test_cognito.py
index a15f3bb1..4c24fe2c 100644
--- a/tests/test_cognito.py
+++ b/tests/test_cognito.py
@@ -11658,7 +11658,7 @@ def test_cognito_federation_access_token_pretoken_event_has_user_attributes(cogn
access = _decode_jwt_claims(tokens["access_token"])
assert access["seen_sub"] == access["sub"]
- assert access["seen_groups"] == "admins"
+ assert access["seen_groups"] == f"{pid}_TestSAML,admins"
# Secret-hash checks run under AUTH=true; server tests need a MiniStack started with it.
@@ -12468,3 +12468,102 @@ def test_cognito_unsupported_login_and_forged_refresh_token_contract(auth_enable
assert exc.value.response["Error"]["Message"] == "Invalid Refresh Token"
assert cognito_idp.initiate_auth(ClientId=cid, AuthFlow="REFRESH_TOKEN_AUTH", AuthParameters={
"REFRESH_TOKEN": refresh})["AuthenticationResult"]["AccessToken"]
+
+
+def test_cognito_pools_sign_with_their_own_keys(cognito_idp):
+ """Each pool publishes two keys of its own, and its tokens carry their kids."""
+ published, kids = [], []
+ for name in ("own-keys-one", "own-keys-two"):
+ pid = cognito_idp.create_user_pool(PoolName=name)["UserPool"]["Id"]
+ cid = cognito_idp.create_user_pool_client(
+ UserPoolId=pid, ClientName="c", ExplicitAuthFlows=["ALLOW_ADMIN_USER_PASSWORD_AUTH", "ALLOW_REFRESH_TOKEN_AUTH"],
+ )["UserPoolClient"]["ClientId"]
+ cognito_idp.admin_create_user(UserPoolId=pid, Username="u", MessageAction="SUPPRESS")
+ cognito_idp.admin_set_user_password(UserPoolId=pid, Username="u", Password="Example-Password-1", Permanent=True)
+ result = cognito_idp.admin_initiate_auth(
+ UserPoolId=pid, ClientId=cid, AuthFlow="ADMIN_USER_PASSWORD_AUTH",
+ AuthParameters={"USERNAME": "u", "PASSWORD": "Example-Password-1"},
+ )["AuthenticationResult"]
+ with urllib.request.urlopen(f"{ENDPOINT}/{pid}/.well-known/jwks.json") as r:
+ keys = json.loads(r.read())["keys"]
+ assert len(keys) == 2
+ heads = [json.loads(base64.urlsafe_b64decode(h + "=" * (-len(h) % 4)))
+ for h in (result["IdToken"].split(".")[0], result["AccessToken"].split(".")[0])]
+ assert heads[0]["kid"] != heads[1]["kid"]
+ assert {h["kid"] for h in heads} == {k["kid"] for k in keys}
+ cognito_idp.get_user(AccessToken=result["AccessToken"])
+ published.append({k["n"] for k in keys})
+ kids.append(heads[0]["kid"])
+ assert not published[0] & published[1]
+ assert kids[0] != kids[1]
+
+
+def test_oauth2_refuses_plain_pkce_unregistered_redirect_and_unknown_grant():
+ cognito_idp = make_client('cognito-idp')
+ pool_id, client = _setup_pool_with_user(cognito_idp, generate_secret=False)
+ client_id = client['ClientId']
+ authorize = (f'{ENDPOINT}/oauth2/authorize?response_type=code&client_id={client_id}'
+ f'&redirect_uri=http://localhost:3000/callback&state=s&scope=openid')
+ for pkce in ('&code_challenge=v&code_challenge_method=plain', '&code_challenge=v'):
+ try:
+ _no_redirect_opener.open(authorize + pkce)
+ assert False, "Expected 302"
+ except urllib.error.HTTPError as e:
+ assert e.code == 302
+ assert e.headers['Location'] == 'http://localhost:3000/callback?error=invalid_request&state=s'
+ status, headers, _ = _post_form(f'{ENDPOINT}/login', {
+ 'username': 'testuser', 'password': 'TestPass1!', 'client_id': client_id,
+ 'redirect_uri': 'http://localhost:3000/callback', 'response_type': 'code',
+ 'code_challenge': 'v', 'code_challenge_method': 'plain',
+ }, follow_redirects=False)
+ assert status == 302 and 'code=' not in headers['location']
+
+ bare = cognito_idp.create_user_pool_client(
+ UserPoolId=pool_id, ClientName='bare', AllowedOAuthFlows=['code'], AllowedOAuthScopes=['openid'],
+ AllowedOAuthFlowsUserPoolClient=True, SupportedIdentityProviders=['COGNITO'],
+ )['UserPoolClient']['ClientId']
+ try:
+ _no_redirect_opener.open(f'{ENDPOINT}/oauth2/authorize?response_type=code&client_id={bare}'
+ f'&redirect_uri=https://elsewhere.example.org/cb&scope=openid')
+ assert False, "Expected 400"
+ except urllib.error.HTTPError as e:
+ assert e.code == 400
+ status, _, _ = _post_form(f'{ENDPOINT}/login', {
+ 'username': 'testuser', 'password': 'TestPass1!', 'client_id': bare,
+ 'redirect_uri': 'https://elsewhere.example.org/cb', 'response_type': 'code',
+ }, follow_redirects=False)
+ assert status == 400
+
+ status, _, body = _post_form(f'{ENDPOINT}/oauth2/token', {'grant_type': 'example', 'client_id': client_id})
+ assert status == 400
+ assert json.loads(body) == {'error': 'unsupported_grant_type'}
+
+
+def test_cognito_oidc_federated_user_has_identities_and_provider_group(cognito_idp):
+ """A new IdP profile joins `<pool id>_<IdP name>` and its ID token carries identities."""
+ token_url, _recorded, stop = _start_fake_oidc_idp({"sub": "fed-sub", "email": "[email protected]"})
+ try:
+ pid, cid = _setup_oidc_pool(cognito_idp, token_url)
+ group = f"{pid}_TestOIDC"
+ assert [g["GroupName"] for g in cognito_idp.list_groups(UserPoolId=pid)["Groups"]] == [group]
+ try:
+ _no_redirect_opener.open(
+ f"{ENDPOINT}/oauth2/authorize?response_type=code&client_id={cid}"
+ f"&redirect_uri=http://localhost:3000/callback&identity_provider=TestOIDC&state=s&scope=openid")
+ assert False, "Expected 302"
+ except urllib.error.HTTPError as e:
+ relay_state = _parse_qs(urlparse(e.headers.get("Location", "")).query)["state"][0]
+ try:
+ _no_redirect_opener.open(f"{ENDPOINT}/oauth2/idpresponse?code=idp-code&state={relay_state}")
+ assert False, "Expected 302 back to the app"
+ except urllib.error.HTTPError as e:
+ claims = _federation_id_claims(cid, e.headers.get("Location", ""))
+ assert claims["cognito:groups"] == [group]
+ [identity] = claims["identities"]
+ assert {k: identity[k] for k in ("userId", "providerName", "providerType", "issuer", "primary")} == {
+ "userId": "fed-sub", "providerName": "TestOIDC", "providerType": "OIDC", "issuer": None, "primary": "true"}
+ assert identity["dateCreated"].isdigit()
+ users = cognito_idp.list_users_in_group(UserPoolId=pid, GroupName=group)["Users"]
+ assert [u["Username"] for u in users] == ["TestOIDC_fed-sub"]
+ finally:
+ stop()