Skip to content

Commit

Permalink
feat: Allow providing ClientMetadata in authentication (NabuCasa#108)
Browse files Browse the repository at this point in the history
* feat: Allow providing client metadata

* refactor: name argument client_metadata
  • Loading branch information
tobni authored Mar 14, 2022
1 parent c11db5a commit ddf6447
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions pycognito/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,11 @@ def admin_authenticate(self, password):
)
self._set_tokens(tokens)

def authenticate(self, password):
def authenticate(self, password, client_metadata=None):
"""
Authenticate the user using the SRP protocol
:param password: The user's passsword
:param client_metadata: Metadata you can provide for custom workflows that RespondToAuthChallenge triggers.
:return:
"""
aws = AWSSRP(
Expand All @@ -460,7 +461,7 @@ def authenticate(self, password):
client=self.client,
client_secret=self.client_secret,
)
tokens = aws.authenticate_user()
tokens = aws.authenticate_user(client_metadata=client_metadata)
self._set_tokens(tokens)

def new_password_challenge(self, password, new_password):
Expand Down
3 changes: 2 additions & 1 deletion pycognito/aws_srp.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def process_challenge(self, challenge_parameters):
)
return response

def authenticate_user(self, client=None):
def authenticate_user(self, client=None, client_metadata=None):
boto_client = self.client or client
auth_params = self.get_auth_params()
response = boto_client.initiate_auth(
Expand All @@ -257,6 +257,7 @@ def authenticate_user(self, client=None):
ClientId=self.client_id,
ChallengeName=self.PASSWORD_VERIFIER_CHALLENGE,
ChallengeResponses=challenge_response,
**dict(ClientMetadata=client_metadata) if client_metadata else {},
)

if tokens.get("ChallengeName") == self.NEW_PASSWORD_REQUIRED_CHALLENGE:
Expand Down
2 changes: 1 addition & 1 deletion tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from pycognito.utils import RequestsSrpAuth


def _mock_authenticate_user(_, client=None):
def _mock_authenticate_user(_, client=None, client_metadata=None):
return {
"AuthenticationResult": {
"TokenType": "admin",
Expand Down

0 comments on commit ddf6447

Please sign in to comment.