diff --git a/README.md b/README.md index 68c02e34..ddf644d8 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,9 @@ Makes working with AWS Cognito easier for Python developers. - [Get User Object](#get-user-object) - [Get User](#get-user) - [Get Users](#get-users) + - [Get Group Object](#get-group-object) + - [Get Group](#get-group) + - [Get Groups](#get-groups) - [Check Token](#check-token) - [Logout](#logout) - [Cognito SRP Utility](#cognito-srp-utility) `warrant.aws_srp.AWSSRP` @@ -338,6 +341,49 @@ user = u.get_users(attr_map={"given_name":"first_name","family_name":"last_name" ##### Arguments - **attr_map:** Dictionary map from Cognito attributes to attribute names we would like to show to our users +#### Get Group object + +Returns an instance of the specified group_class. + +```python +u = Cognito('your-user-pool-id', 'your-client-id') + +group_data = {'GroupName': 'user_group', 'Description': 'description', + 'Precedence': 1} + +group_obj = u.get_group_obj(group_data) +``` + +##### Arguments +- **group_data:** Dictionary with group's attributes. + +#### Get Group + +Get all of the group's attributes. Returns an instance of the group_class. +Requires developer credentials. + +```python +from warrant import Cognito + +u = Cognito('your-user-pool-id','your-client-id') + +group = u.get_group(group_name='some_group_name') +``` + +##### Arguments +- **group_name:** Name of a group + +#### Get Groups + +Get a list of groups in the user pool. Requires developer credentials. + +```python +from warrant import Cognito + +u = Cognito('your-user-pool-id','your-client-id') + +groups = u.get_groups() +``` #### Check Token diff --git a/warrant/__init__.py b/warrant/__init__.py index 3187d311..8d1b1424 100644 --- a/warrant/__init__.py +++ b/warrant/__init__.py @@ -1,6 +1,7 @@ import ast import boto3 import datetime +import re import requests from envs import env @@ -9,6 +10,7 @@ from .aws_srp import AWSSRP from .exceptions import TokenVerificationException + def cognito_to_dict(attr_list, attr_map=None): if attr_map is None: attr_map = {} @@ -35,6 +37,22 @@ def dict_to_cognito(attributes, attr_map=None): return [{'Name': key, 'Value': value} for key, value in attributes.items()] +def camel_to_snake(camel_str): + """ + :param camel_str: string + :return: string converted from a CamelCase to a snake_case + """ + s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', camel_str) + return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower() + +def snake_to_camel(snake_str): + """ + :param snake_str: string + :return: string converted from a snake_case to a CamelCase + """ + components = snake_str.split('_') + return ''.join(x.title() for x in components) + class UserObj(object): @@ -55,7 +73,7 @@ def __init__(self, username, attribute_list, cognito_obj, metadata=None, attr_ma self._metadata = {} if metadata is None else metadata def __repr__(self): - return '<{class_name}: {uni} >'.format( + return '<{class_name}: {uni}>'.format( class_name=self.__class__.__name__, uni=self.__unicode__()) def __unicode__(self): @@ -86,9 +104,34 @@ def delete(self,admin=False): self._cognito.delete_user() +class GroupObj(object): + + def __init__(self, group_data, cognito_obj): + """ + :param group_data: a dictionary with information about a group + :param cognito_obj: an instance of the Cognito class + """ + self._data = group_data + self._cognito = cognito_obj + self.group_name = self._data.pop('GroupName', None) + self.description = self._data.pop('Description', None) + self.creation_date = self._data.pop('CreationDate', None) + self.last_modified_date = self._data.pop('LastModifiedDate', None) + self.role_arn = self._data.pop('RoleArn', None) + self.precedence = self._data.pop('Precedence', None) + + def __unicode__(self): + return self.group_name + + def __repr__(self): + return '<{class_name}: {uni}>'.format( + class_name=self.__class__.__name__, uni=self.__unicode__()) + + class Cognito(object): user_class = UserObj + group_class = GroupObj def __init__( self, user_pool_id, client_id,user_pool_region=None, @@ -181,6 +224,14 @@ def get_user_obj(self, username=None, attribute_list=None, metadata=None, cognito_obj=self, metadata=metadata,attr_map=attr_map) + def get_group_obj(self, group_data): + """ + Instantiates the self.group_class + :param group_data: a dictionary with information about a group + :return: an instance of the self.group_class + """ + return self.group_class(group_data=group_data, cognito_obj=self) + def switch_session(self,session): """ Primarily used for unit testing so we can take advantage of the @@ -538,3 +589,24 @@ def _set_attributes(self, response, attribute_dict): if status_code == 200: for k, v in attribute_dict.items(): setattr(self, k, v) + + def get_group(self, group_name): + """ + Get a group by a name + :param group_name: name of a group + :return: instance of the self.group_class + """ + response = self.client.get_group(GroupName=group_name, + UserPoolId=self.user_pool_id) + return self.get_group_obj(response.get('Group')) + + def get_groups(self): + """ + Returns all groups for a user pool. Returns instances of the + self.group_class. + :return: list of instances + """ + response = self.client.list_groups(UserPoolId=self.user_pool_id) + return [self.get_group_obj(group_data) + for group_data in response.get('Groups')] + diff --git a/warrant/tests/tests.py b/warrant/tests/tests.py index 892e4380..bab0d2a8 100644 --- a/warrant/tests/tests.py +++ b/warrant/tests/tests.py @@ -3,7 +3,7 @@ from mock import patch from envs import env -from warrant import Cognito, UserObj, TokenVerificationException +from warrant import Cognito, UserObj, GroupObj, TokenVerificationException from warrant.aws_srp import AWSSRP @@ -33,6 +33,20 @@ def test_init(self): self.assertEqual(u.user_status,self.user_metadata.get('user_status')) +class GroupObjTestCase(unittest.TestCase): + + def setUp(self): + self.cognito_user_pool_id = env('COGNITO_USER_POOL_ID') + self.app_id = env('COGNITO_APP_ID') + self.group_data = {'GroupName': 'test_group', 'Precedence': 1} + self.cognito_obj = Cognito(self.cognito_user_pool_id, self.app_id) + + def test_init(self): + group = GroupObj(group_data=self.group_data, cognito_obj=self.cognito_obj) + self.assertEqual(group.group_name, 'test_group') + self.assertEqual(group.precedence, 1) + + class CognitoAuthTestCase(unittest.TestCase): def setUp(self):