capless / warrant

Python library for using AWS Cognito. With support for SRP.
Apache License 2.0
467 stars 190 forks source link

Returning Custom Attribute Value #140

Closed ghost closed 4 years ago

ghost commented 5 years ago

Hi, I'm wondering if you could directly return a user custom attribute as a text string from an API call. I'm using the following code to authenticate then grab the user object, but I can't find or think of a way to either convert it to a string or just return the string which contains the custom attribute.

from warrant import Cognito

user_pool_id = '<snip>'
app_client_id = '<snip>'
access_key='<snip>'
secret_key='<snip>'
user_pool_region='us-east-2'

u = Cognito(user_pool_id, app_client_id, user_pool_region=user_pool_region, access_key=access_key, secret_key=secret_key, username='<snip>')
u.admin_authenticate(password='<snip>')
accesstoken = u.access_token
refreshtoken = u.refresh_token
user = u.get_user_obj(username='<snip>', attribute_list=[{'endpointURL': 'string'}])
print(user)

What I get in my console is <UserObj: <snip>>

Any help is appreciated, I'm probably overlooking something simple.

piemadd commented 5 years ago

What you're getting back is a string of the user's information. Instead of doing print(user), try doing print(user["attribute_list"]). This will print out attribute_list instead of the entire user array. To continue further, you could set a varable for the attribute list with attribute_list = user["attribute_list"] and to then pull endpointURL you could use another print function or set a variable with endpointURL = attribute_list[endpointURL]. This will set a variable with just your endpointURL. Heres the code you would want to use the code below. Also, if you had other custom attributes besides the endpointURL, you could use the same method, but replacing endpointURL with whatever other attribute you would want to get.


from warrant import Cognito

user_pool_id = '<snip>'
app_client_id = '<snip>'
access_key='<snip>'
secret_key='<snip>'
user_pool_region='us-east-2'

u = Cognito(user_pool_id, app_client_id, user_pool_region=user_pool_region, access_key=access_key, secret_key=secret_key, username='<snip>')
u.admin_authenticate(password='<snip>')
accesstoken = u.access_token
refreshtoken = u.refresh_token
user = u.get_user_obj(username='<snip>', attribute_list=[{'endpointURL': 'string'}])
attribute_list = user[attribute_list]
enpointURL = attribute_list[endpointURL]
print(endpointURL)