Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cognitoidp.admin_respond_to_auth_challenge #7136

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions IMPLEMENTATION_COVERAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1180,7 +1180,7 @@

## cognito-idp
<details>
<summary>59% implemented</summary>
<summary>60% implemented</summary>

- [X] add_custom_attributes
- [X] admin_add_user_to_group
Expand All @@ -1201,7 +1201,7 @@
- [ ] admin_list_user_auth_events
- [X] admin_remove_user_from_group
- [X] admin_reset_user_password
- [ ] admin_respond_to_auth_challenge
- [X] admin_respond_to_auth_challenge
- [X] admin_set_user_mfa_preference
- [X] admin_set_user_password
- [ ] admin_set_user_settings
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/services/cognito-idp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ cognito-idp
- [ ] admin_list_user_auth_events
- [X] admin_remove_user_from_group
- [X] admin_reset_user_password
- [ ] admin_respond_to_auth_challenge
- [X] admin_respond_to_auth_challenge
- [X] admin_set_user_mfa_preference
- [X] admin_set_user_password
- [ ] admin_set_user_settings
Expand Down
38 changes: 38 additions & 0 deletions moto/cognitoidp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1483,13 +1483,39 @@ def admin_initiate_auth(
# We shouldn't get here due to enum validation of auth_flow
return None # type: ignore[return-value]

def admin_respond_to_auth_challenge(
self,
session: str,
client_id: str,
challenge_name: str,
challenge_responses: Dict[str, str],
) -> Dict[str, Any]:
"""
Responds to an authentication challenge, as an administrator.

The only differences between this admin endpoint and public endpoint are not relevant and so we can safely call
the public endpoint to do the work:
- The admin endpoint requires a user pool id along with a session; the public endpoint searches across all pools
- ContextData is passed in; we don't use it

ref: https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminRespondToAuthChallenge.html
"""
return self.respond_to_auth_challenge(
session, client_id, challenge_name, challenge_responses
)

def respond_to_auth_challenge(
self,
session: str,
client_id: str,
challenge_name: str,
challenge_responses: Dict[str, str],
) -> Dict[str, Any]:
"""
Responds to an authentication challenge, from public client.

ref: https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_RespondToAuthChallenge.html
"""
if challenge_name == "PASSWORD_VERIFIER":
session = challenge_responses.get("PASSWORD_CLAIM_SECRET_BLOCK") # type: ignore[assignment]

Expand Down Expand Up @@ -2180,6 +2206,18 @@ def get_user(self, access_token: str) -> CognitoIdpUser:
backend = self._find_backend_by_access_token(access_token)
return backend.get_user(access_token)

def admin_respond_to_auth_challenge(
self,
session: str,
client_id: str,
challenge_name: str,
challenge_responses: Dict[str, str],
) -> Dict[str, Any]:
backend = self._find_backend_for_clientid(client_id)
return backend.admin_respond_to_auth_challenge(
session, client_id, challenge_name, challenge_responses
)

def respond_to_auth_challenge(
self,
session: str,
Expand Down
11 changes: 11 additions & 0 deletions moto/cognitoidp/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,17 @@ def admin_initiate_auth(self) -> str:

return json.dumps(auth_result)

def admin_respond_to_auth_challenge(self) -> str:
session = self._get_param("Session")
client_id = self._get_param("ClientId")
challenge_name = self._get_param("ChallengeName")
challenge_responses = self._get_param("ChallengeResponses")
auth_result = region_agnostic_backend.admin_respond_to_auth_challenge(
session, client_id, challenge_name, challenge_responses
)

return json.dumps(auth_result)

def respond_to_auth_challenge(self) -> str:
session = self._get_param("Session")
client_id = self._get_param("ClientId")
Expand Down
12 changes: 8 additions & 4 deletions tests/test_cognitoidp/test_cognitoidp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1532,7 +1532,8 @@ def test_group_in_access_token():

# This sets a new password and logs the user in (creates tokens)
new_password = "P2$Sword"
result = conn.respond_to_auth_challenge(
result = conn.admin_respond_to_auth_challenge(
UserPoolId=user_pool_id,
Session=result["Session"],
ClientId=client_id,
ChallengeName="NEW_PASSWORD_REQUIRED",
Expand Down Expand Up @@ -1585,7 +1586,8 @@ def test_group_in_id_token():

# This sets a new password and logs the user in (creates tokens)
new_password = "P2$Sword"
result = conn.respond_to_auth_challenge(
result = conn.admin_respond_to_auth_challenge(
UserPoolId=user_pool_id,
Session=result["Session"],
ClientId=client_id,
ChallengeName="NEW_PASSWORD_REQUIRED",
Expand Down Expand Up @@ -2749,7 +2751,8 @@ def authentication_flow(conn, auth_flow):

# This sets a new password and logs the user in (creates tokens)
new_password = "P2$Sword"
result = conn.respond_to_auth_challenge(
result = conn.admin_respond_to_auth_challenge(
UserPoolId=user_pool_id,
Session=result["Session"],
ClientId=client_id,
ChallengeName="NEW_PASSWORD_REQUIRED",
Expand Down Expand Up @@ -4388,7 +4391,8 @@ def test_admin_initiate_auth_when_token_totp_enabled():
assert result["Session"] != ""

# Respond to challenge with TOTP
result = conn.respond_to_auth_challenge(
result = conn.admin_respond_to_auth_challenge(
UserPoolId=user_pool_id,
ClientId=client_id,
ChallengeName="SOFTWARE_TOKEN_MFA",
Session=result["Session"],
Expand Down