Skip to content

Commit

Permalink
Merge pull request capless#23 from grantmcconnaughey/admin_create_user
Browse files Browse the repository at this point in the history
Add admin_create_user method
  • Loading branch information
bjinwright authored Apr 21, 2017
2 parents e441d20 + 3cd768d commit 10e4cbc
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions warrant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ def __init__(

def get_user_obj(self,username=None,attribute_list=[],metadata={},attr_map=dict()):
"""
Returns the specified
:param username: Username of the user
:param attribute_list: List of tuples that represent the user's
Returns the specified
:param username: Username of the user
:param attribute_list: List of tuples that represent the user's
attributes as returned by the admin_get_user or get_user boto3 methods
:param metadata: Metadata about the user
:param attr_map: Dictionary that maps the Cognito attribute names to
:param attr_map: Dictionary that maps the Cognito attribute names to
what we'd like to display to the users
:return:
:return:
"""
return self.user_class(username=username,attribute_list=attribute_list,
metadata=metadata,attr_map=attr_map)
Expand Down Expand Up @@ -235,7 +235,7 @@ def update_profile(self, attrs,attr_map=dict()):
"""
Updates User attributes
:param attrs: Dictionary of attribute name, values
:param attr_map: Dictionary map from Cognito attributes to attribute
:param attr_map: Dictionary map from Cognito attributes to attribute
names we would like to show to our users
"""
user_attrs = dict_to_cognito(attrs,attr_map)
Expand All @@ -246,16 +246,16 @@ def update_profile(self, attrs,attr_map=dict()):

def get_user(self,attr_map=dict()):
"""
Returns a UserObj (or whatever the self.user_class is) by using the
Returns a UserObj (or whatever the self.user_class is) by using the
user's access token.
:param attr_map: Dictionary map from Cognito attributes to attribute
:param attr_map: Dictionary map from Cognito attributes to attribute
names we would like to show to our users
:return:
:return:
"""
user = self.client.get_user(
AccessToken=self.access_token
)

user_metadata = {
'username': user.get('Username'),
'id_token': self.id_token,
Expand All @@ -268,10 +268,10 @@ def get_user(self,attr_map=dict()):

def get_users(self,attr_map=dict()):
"""
Returns all users for a user pool. Returns instances of the
Returns all users for a user pool. Returns instances of the
self.user_class.
:param attr_map:
:return:
:param attr_map:
:return:
"""
kwargs = {"UserPoolId":self.user_pool_id}

Expand All @@ -285,7 +285,7 @@ def get_users(self,attr_map=dict()):
def admin_get_user(self,attr_map=dict()):
"""
Get the user's details using admin super privileges.
:param attr_map: Dictionary map from Cognito attributes to attribute
:param attr_map: Dictionary map from Cognito attributes to attribute
names we would like to show to our users
:return: UserObj object
"""
Expand All @@ -303,6 +303,27 @@ def admin_get_user(self,attr_map=dict()):
attribute_list=user.get('UserAttributes'),
metadata=user_metadata,attr_map=attr_map)

def admin_create_user(self, username, temporary_password='', attr_map=dict(), **kwargs):
"""
Create a user using admin super privileges.
:param username: User Pool username
:param temporary_password: The temporary password to give the user.
Leave blank to make Cognito generate a temporary password for the user.
:param attr_map: Attribute map to Cognito's attributes
:param kwargs: Additional User Pool attributes
:return response: Response from Cognito
"""
response = self.client.admin_create_user(
UserPoolId=self.user_pool_id,
Username=username,
UserAttributes=dict_to_cognito(kwargs, attr_map),
TemporaryPassword=temporary_password,
)
kwargs.update(username=username)
self._set_attributes(response, kwargs)

response.pop('ResponseMetadata')
return response

def send_verification(self, attribute='email'):
"""
Expand Down

0 comments on commit 10e4cbc

Please sign in to comment.