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

register fails when using client_secret #17

Open
jkoscialkowski opened this issue Sep 2, 2020 · 2 comments
Open

register fails when using client_secret #17

jkoscialkowski opened this issue Sep 2, 2020 · 2 comments

Comments

@jkoscialkowski
Copy link

jkoscialkowski commented Sep 2, 2020

It seems impossible to register a user when using a client secret.

Steps to reproduce: have a user pool with no users and with an app client with a secret.

u = Cognito(user_pool_id='eu-central-1_...',
    client_id='...',
    client_secret='...')
u.set_base_attributes(email='[email protected]')
u.register('person', 'password')

Crashes with:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-22-d291537f4dbd> in <module>
      1 u.set_base_attributes(email='[email protected]')
----> 2 u.register('person', '...')

~/.local/lib/python3.8/site-packages/pycognito/__init__.py in register(self, username, password, attr_map)
    346             "UserAttributes": cognito_attributes,
    347         }
--> 348         self._add_secret_hash(params, "SecretHash")
    349         response = self.client.sign_up(**params)
    350 

~/.local/lib/python3.8/site-packages/pycognito/__init__.py in _add_secret_hash(self, parameters, key)
    690         """
    691         if self.client_secret is not None:
--> 692             secret_hash = AWSSRP.get_secret_hash(
    693                 self.username, self.client_id, self.client_secret
    694             )

~/.local/lib/python3.8/site-packages/pycognito/aws_srp.py in get_secret_hash(username, client_id, client_secret)
    199     @staticmethod
    200     def get_secret_hash(username, client_id, client_secret):
--> 201         message = bytearray(username + client_id, "utf-8")
    202         hmac_obj = hmac.new(bytearray(client_secret, "utf-8"), message, hashlib.sha256)
    203         return base64.standard_b64encode(hmac_obj.digest()).decode("utf-8")

TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

This is caused by https://github.com/pvizeli/pycognito/blob/6c173cf0bb26e40a941be188670c7c592b46eeb8/pycognito/__init__.py#L693 operating on self.username which is not initialized.
I think adding self.username = username around https://github.com/pvizeli/pycognito/blob/6c173cf0bb26e40a941be188670c7c592b46eeb8/pycognito/__init__.py#L334 would fix it without breaking any code using _add_secret_hash. Please let me know if this is OK for you and I will submit a PR.

@gavihs
Copy link

gavihs commented Sep 4, 2020

I am having the same issue

@dleber
Copy link

dleber commented Jul 22, 2021

I found I can work around this by providing the username when instantiating the class, i.e.

u = Cognito(USER_POOL_ID, CLIENT_ID, client_secret=CLIENT_SECRET, username=my_username)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants