Skip to content

Commit

Permalink
chore: Stop using the secret code (MFA setup code) as an access token,
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Oct 31, 2024
1 parent 6c6eb6d commit a20f147
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
13 changes: 6 additions & 7 deletions app/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,44 +111,43 @@ def respond_to_auth_challenge(
match challenge_name:
case "NEW_PASSWORD_REQUIRED":
return self.cognito.respond_to_auth_challenge(
Session=session,
ClientId=app_settings.cognito_client_id,
ChallengeName=challenge_name,
ChallengeResponses={
"USERNAME": username,
"NEW_PASSWORD": new_password,
"SECRET_HASH": secret_hash,
"NEW_PASSWORD": new_password,
},
Session=session,
)
case "MFA_SETUP":
associate_response = self.cognito.associate_software_token(Session=session)

verify_response = self.cognito.verify_software_token(
AccessToken=associate_response["SecretCode"],
Session=associate_response["Session"],
UserCode=mfa_code,
)

return self.cognito.respond_to_auth_challenge(
Session=verify_response["Session"],
ClientId=app_settings.cognito_client_id,
ChallengeName=challenge_name,
ChallengeResponses={
"USERNAME": username,
"NEW_PASSWORD": new_password,
"SECRET_HASH": secret_hash,
"NEW_PASSWORD": new_password,
},
Session=verify_response["Session"],
)
case "SOFTWARE_TOKEN_MFA":
return self.cognito.respond_to_auth_challenge(
Session=session,
ClientId=app_settings.cognito_client_id,
ChallengeName=challenge_name,
ChallengeResponses={
"USERNAME": username,
"SOFTWARE_TOKEN_MFA_CODE": mfa_code,
"SECRET_HASH": secret_hash,
"SOFTWARE_TOKEN_MFA_CODE": mfa_code,
},
Session=session,
)
case _:
raise HTTPException(
Expand Down
2 changes: 1 addition & 1 deletion app/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ResetPassword(BaseModel):
class SetupMFA(BaseModel):
temp_password: str
session: str
secret: str
secret: str | None = None


class AwardUpdate(BaseModel):
Expand Down
11 changes: 2 additions & 9 deletions app/routers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,10 @@ def setup_mfa(
payload: parsers.SetupMFA,
client: aws.Client = Depends(dependencies.get_aws_client),
) -> serializers.ResponseBase:
"""
Set up multi-factor authentication (MFA) for the user.
This endpoint allows users to set up MFA using a software token. It verifies the software
token with the provided secret, session, and temporary password.
"""
"""Set up multi-factor authentication (MFA) for the user."""
try:
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cognito-idp/client/verify_software_token.html
client.cognito.verify_software_token(
AccessToken=payload.secret, Session=payload.session, UserCode=payload.temp_password
)
client.cognito.verify_software_token(Session=payload.session, UserCode=payload.temp_password)
except client.cognito.exceptions.NotAuthorizedException:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
Expand Down

0 comments on commit a20f147

Please sign in to comment.