cognitopy


Namecognitopy JSON
Version 1.1.6 PyPI version JSON
download
home_pagehttps://github.com/DaniMG95/cognitopy
SummaryPython package to use aws cognito in a simple way
upload_time2024-12-01 18:04:23
maintainerNone
docs_urlNone
authorDaniel Muñoz Gonzalez
requires_python<4.0.0,>=3.10.0
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![PyPI version](https://img.shields.io/pypi/v/cognitopy.svg?style=plastic)](https://pypi.org/project/cognitopy/)
![PyPI - Downloads](https://img.shields.io/pypi/dm/cognitopy?style=plastic)

# cognitopy
This is a package that will allow you to use the aws Cognito technology, so for now we are going to allow the management of users, authentication and creation of groups by Roles.  
The potential of this package is the ease of management of all these functionalities and only creating an object with 3 parameters.

## Installation
```bash
pip install cognitopy
```

## Variables for using the admin functions

The cognito admin functions require that we have the aws, access key and secret access key credentials defined as system environment variables.

```python
import os

os.environ["AWS_ACCESS_KEY_ID"] = 'XXXXXXXXXXXXXXXXXXXXXXXX'
os.environ["AWS_SECRET_ACCESS_KEY"] = 'XXXXXXXXXXXXXXXXXXXXXXXX'
```

## Usage
To define the cognitopy object it is necessary to give it the userpool_id, the client_id and the client_secret information.  
The secret_hash parameter is set to False by default and indicates that for requests it is necessary to provide the secret_hash.
```python
from cognitopy import CognitoPy

COGNITO_USERPOOL_ID = 'XXX-XXX-XXXXXX'
COGNITO_APP_CLIENT_ID = 'XXXXXXXXXXXXXXXXXXXXXXXX'
COGNITO_APP_CLIENTE_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

cognitopy = CognitoPy(
    userpool_id=COGNITO_USERPOOL_ID, client_id=COGNITO_APP_CLIENT_ID, client_secret=COGNITO_APP_CLIENTE_SECRET,
    secret_hash=True
)
```

Now I will explain the different functions that we can use in this version, with an example.  
All these examples are in the directory example.

### Using context manager
It will allow us to use the cognitopy object in a context manager, so that we do not have to worry about closing the connection.
```python
with CognitoPy(userpool_id=COGNITO_USERPOOL_ID, client_id=COGNITO_APP_CLIENT_ID,
               client_secret=COGNITO_APP_CLIENTE_SECRET) as cognito:
    cognito.register(username="XXXXX@mail.to", password="XXXXXXX8", user_attributes={})
```

### Register a new user
It will register a user in our cognito service and send us a confirmation message.
```python
id_user = cognito.register(username='XXXXX@mail.to', password='XXXXXXX8', user_attributes={})
print(id_user)
```

### Confirm a new user
It is responsible for confirming the user from the number received by mail.
```python
cognito.confirm_register(username='XXXXX@mail.to', confirmation_code='820850')
```

### Resend confirm code
It allows us to receive a confirmation code again, when we have previously requested to change password or register.
```python
cognito.resend_confirmation_code(username='XXXXX@mail.to')
```

### Login a user
It will return the access token and refresh token of a confirmed user.
```python
tokens = cognito.login(username='XXXXX@mail.to', password='XXXXXXX')
print(tokens['access_token'], tokens['refresh_token'])
```

### Refresh access token
It will renew the user's access token.
```python
access_token = cognito.renew_access_token(access_token='XXXXXXXXX', refresh_token='XXXXXXXXX')
print(access_token)
```

### Check if access token is expired
Check if the access token has expired.
```python
is_expired = cognito.check_expired_token(access_token='XXXXXXXXX')
print(is_expired)
```

### Forgot password
Allows us to change our password by sending us a confirmation code.
```python
cognito.initiate_forgot_password(username='XXXXX@mail.to')
```

### Confirm forgot password
Change the password of a user from the confirmation code received.
```python
cognito.confirm_forgot_password(username='XXXXX@mail.to', confirmation_code='YYYYY', password='XXXXXXX')
```

### Delete user
Delete the user from his access token.
```python
cognito.delete_user(access_token='XXXXXXXXX')
```

### Change password
Change the password from your access token.
```python
cognito.change_password(access_token='XXXXXXXXX', previous_password='XXXXXXX', proposed_password="XXXXXXX")
```

### Get user information
We obtain basic user information from the user's access token.
```python
data_user = cognito.get_info_user_by_token(access_token='XXXXXXXXX')
print(data_user['username'], data_user['groups'])
```

### Admin delete user
We remove a user from our service from the administrator credentials.
```python
cognito.admin_delete_user(username='XXXXX@mail.to')
```

### Admin create group
We create a group from our service from the administrator credentials.
precedence: A non-negative integer value that specifies the precedence of this group relative to the other groups that a user can belong to in the user pool. Zero is the highest precedence value. Groups with lower Precedence values take precedence over groups with higher or null Precedence values.
role_arn: The role Amazon Resource Name (ARN) for the group.
```python
cognito.admin_create_group(group_name='test_group', description='test group', precedence=1)
```

### Admin delete group
We remove a group from our service from the administrator credentials.
```python
cognito.admin_delete_group(group_name='test_group')
```

### Admin add user to group
We add a user to group from our service from the administrator credentials.
```python
cognito.admin_add_user_to_group(username='XXXXX@mail.to', group_name='test_group')
```

### Admin remove user from group
We remove a user to group from our service from the administrator credentials.
```python
cognito.admin_remove_user_from_group(username='XXXXX@mail.to', group_name='test_group')
```


### Admin confirm user registration
We confirm a user register from the administrator credentials.
```python
cognito.admin_confirm_register(username="XXXXX@mail.to")
```


### Admin create user
We create a user from the administrator credentials.  
message_action = MessageAction.SUPPRESS | MessageAction.RESEND  
Set to RESEND to resend the invitation message to a user that already exists and reset the expiration limit on the user’s account. Set to SUPPRESS to suppress sending the message.  
desired_delivery = [DesiredDelivery.EMAIL | DesiredDelivery.SMS] or [DesiredDelivery.EMAIL, DesiredDelivery.SMS]  
Specify EMAIL if email will be used to send the welcome message. Specify SMS if the phone number will be used.  
optional temporary_password  
```python
cognito.admin_create_user(username="XXXXX@mail.to", force_alias=True, user_attributes={},
                          message_action=MessageAction.SUPPRESS, desired_delivery=[DesiredDelivery.EMAIL],
                          temporary_password="XXXXXXX")
```

### Admin disable user
We disable a user from the administrator credentials.
```python
cognito.admin_disable_user(username="XXXXX@mail.to")
```


### Admin enable user
We enabled a user from the administrator credentials.
```python
cognito.admin_enable_user(username="XXXXX@mail.to")
```

### Admin get user
We get info about a user from the administrator credentials.
```python
data_user = cognito.admin_get_user(username="XXXXX@mail.to")
print(data_user)
```


### Admin login
We login a user from the administrator credentials.
```python
tokens = cognito.admin_login(username="XXXXX@mail.to", password="XXXXXXX")
print(tokens)
```


### Admin renew access token
We renew access token a user from the administrator credentials.
```python
token = cognito.admin_renew_access_token(access_token="XXXXX", refresh_token="XXXXXXX")
print(token)
```


### Admin list groups for user
We list groups for user from the administrator credentials.
```python
groups = cognito.admin_list_groups_for_user(username="XXXXX@mail.to", limit=10)
print(groups)
groups = cognito.admin_list_groups_for_user(username="XXXXX@mail.to", limit=10, next_token=groups["NextToken"])
print(groups)
```


### Admin reset password
We reset password from the administrator credentials.  
After applying this function it will be necessary to launch the initiate_forgot_password function, since the user's password will be disabled.
```python
cognito.admin_reset_password(username="XXXXX@mail.to")
```


### Resolve challenge sms mfa
We resolve challenge sms mfa.
```python
tokens = cognito.resolve_challenge_challenge_sms_mfa(username="XXXXX@mail.to", session="XXXXXX", sms_mfa_code="XXXXXX")
print(tokens)
```


### Resolve challenge new password required
We resolve challenge new password required.
```python
tokens = cognito.resolve_challenge_new_password(username="XXXXX@mail.to", session="XXXXXX", new_password="XXXXXX")
print(tokens)
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/DaniMG95/cognitopy",
    "name": "cognitopy",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0.0,>=3.10.0",
    "maintainer_email": null,
    "keywords": null,
    "author": "Daniel Mu\u00f1oz Gonzalez",
    "author_email": "dani16595@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/47/b8/6909b7625dbb49be8623a2f052201f65ec1e41f92e3ec6e83d004557a775/cognitopy-1.1.6.tar.gz",
    "platform": null,
    "description": "[![PyPI version](https://img.shields.io/pypi/v/cognitopy.svg?style=plastic)](https://pypi.org/project/cognitopy/)\n![PyPI - Downloads](https://img.shields.io/pypi/dm/cognitopy?style=plastic)\n\n# cognitopy\nThis is a package that will allow you to use the aws Cognito technology, so for now we are going to allow the management of users, authentication and creation of groups by Roles.  \nThe potential of this package is the ease of management of all these functionalities and only creating an object with 3 parameters.\n\n## Installation\n```bash\npip install cognitopy\n```\n\n## Variables for using the admin functions\n\nThe cognito admin functions require that we have the aws, access key and secret access key credentials defined as system environment variables.\n\n```python\nimport os\n\nos.environ[\"AWS_ACCESS_KEY_ID\"] = 'XXXXXXXXXXXXXXXXXXXXXXXX'\nos.environ[\"AWS_SECRET_ACCESS_KEY\"] = 'XXXXXXXXXXXXXXXXXXXXXXXX'\n```\n\n## Usage\nTo define the cognitopy object it is necessary to give it the userpool_id, the client_id and the client_secret information.  \nThe secret_hash parameter is set to False by default and indicates that for requests it is necessary to provide the secret_hash.\n```python\nfrom cognitopy import CognitoPy\n\nCOGNITO_USERPOOL_ID = 'XXX-XXX-XXXXXX'\nCOGNITO_APP_CLIENT_ID = 'XXXXXXXXXXXXXXXXXXXXXXXX'\nCOGNITO_APP_CLIENTE_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'\n\ncognitopy = CognitoPy(\n    userpool_id=COGNITO_USERPOOL_ID, client_id=COGNITO_APP_CLIENT_ID, client_secret=COGNITO_APP_CLIENTE_SECRET,\n    secret_hash=True\n)\n```\n\nNow I will explain the different functions that we can use in this version, with an example.  \nAll these examples are in the directory example.\n\n### Using context manager\nIt will allow us to use the cognitopy object in a context manager, so that we do not have to worry about closing the connection.\n```python\nwith CognitoPy(userpool_id=COGNITO_USERPOOL_ID, client_id=COGNITO_APP_CLIENT_ID,\n               client_secret=COGNITO_APP_CLIENTE_SECRET) as cognito:\n    cognito.register(username=\"XXXXX@mail.to\", password=\"XXXXXXX8\", user_attributes={})\n```\n\n### Register a new user\nIt will register a user in our cognito service and send us a confirmation message.\n```python\nid_user = cognito.register(username='XXXXX@mail.to', password='XXXXXXX8', user_attributes={})\nprint(id_user)\n```\n\n### Confirm a new user\nIt is responsible for confirming the user from the number received by mail.\n```python\ncognito.confirm_register(username='XXXXX@mail.to', confirmation_code='820850')\n```\n\n### Resend confirm code\nIt allows us to receive a confirmation code again, when we have previously requested to change password or register.\n```python\ncognito.resend_confirmation_code(username='XXXXX@mail.to')\n```\n\n### Login a user\nIt will return the access token and refresh token of a confirmed user.\n```python\ntokens = cognito.login(username='XXXXX@mail.to', password='XXXXXXX')\nprint(tokens['access_token'], tokens['refresh_token'])\n```\n\n### Refresh access token\nIt will renew the user's access token.\n```python\naccess_token = cognito.renew_access_token(access_token='XXXXXXXXX', refresh_token='XXXXXXXXX')\nprint(access_token)\n```\n\n### Check if access token is expired\nCheck if the access token has expired.\n```python\nis_expired = cognito.check_expired_token(access_token='XXXXXXXXX')\nprint(is_expired)\n```\n\n### Forgot password\nAllows us to change our password by sending us a confirmation code.\n```python\ncognito.initiate_forgot_password(username='XXXXX@mail.to')\n```\n\n### Confirm forgot password\nChange the password of a user from the confirmation code received.\n```python\ncognito.confirm_forgot_password(username='XXXXX@mail.to', confirmation_code='YYYYY', password='XXXXXXX')\n```\n\n### Delete user\nDelete the user from his access token.\n```python\ncognito.delete_user(access_token='XXXXXXXXX')\n```\n\n### Change password\nChange the password from your access token.\n```python\ncognito.change_password(access_token='XXXXXXXXX', previous_password='XXXXXXX', proposed_password=\"XXXXXXX\")\n```\n\n### Get user information\nWe obtain basic user information from the user's access token.\n```python\ndata_user = cognito.get_info_user_by_token(access_token='XXXXXXXXX')\nprint(data_user['username'], data_user['groups'])\n```\n\n### Admin delete user\nWe remove a user from our service from the administrator credentials.\n```python\ncognito.admin_delete_user(username='XXXXX@mail.to')\n```\n\n### Admin create group\nWe create a group from our service from the administrator credentials.\nprecedence: A non-negative integer value that specifies the precedence of this group relative to the other groups that a user can belong to in the user pool. Zero is the highest precedence value. Groups with lower Precedence values take precedence over groups with higher or null Precedence values.\nrole_arn: The role Amazon Resource Name (ARN) for the group.\n```python\ncognito.admin_create_group(group_name='test_group', description='test group', precedence=1)\n```\n\n### Admin delete group\nWe remove a group from our service from the administrator credentials.\n```python\ncognito.admin_delete_group(group_name='test_group')\n```\n\n### Admin add user to group\nWe add a user to group from our service from the administrator credentials.\n```python\ncognito.admin_add_user_to_group(username='XXXXX@mail.to', group_name='test_group')\n```\n\n### Admin remove user from group\nWe remove a user to group from our service from the administrator credentials.\n```python\ncognito.admin_remove_user_from_group(username='XXXXX@mail.to', group_name='test_group')\n```\n\n\n### Admin confirm user registration\nWe confirm a user register from the administrator credentials.\n```python\ncognito.admin_confirm_register(username=\"XXXXX@mail.to\")\n```\n\n\n### Admin create user\nWe create a user from the administrator credentials.  \nmessage_action = MessageAction.SUPPRESS | MessageAction.RESEND  \nSet to RESEND to resend the invitation message to a user that already exists and reset the expiration limit on the user\u2019s account. Set to SUPPRESS to suppress sending the message.  \ndesired_delivery = [DesiredDelivery.EMAIL | DesiredDelivery.SMS] or [DesiredDelivery.EMAIL, DesiredDelivery.SMS]  \nSpecify EMAIL if email will be used to send the welcome message. Specify SMS if the phone number will be used.  \noptional temporary_password  \n```python\ncognito.admin_create_user(username=\"XXXXX@mail.to\", force_alias=True, user_attributes={},\n                          message_action=MessageAction.SUPPRESS, desired_delivery=[DesiredDelivery.EMAIL],\n                          temporary_password=\"XXXXXXX\")\n```\n\n### Admin disable user\nWe disable a user from the administrator credentials.\n```python\ncognito.admin_disable_user(username=\"XXXXX@mail.to\")\n```\n\n\n### Admin enable user\nWe enabled a user from the administrator credentials.\n```python\ncognito.admin_enable_user(username=\"XXXXX@mail.to\")\n```\n\n### Admin get user\nWe get info about a user from the administrator credentials.\n```python\ndata_user = cognito.admin_get_user(username=\"XXXXX@mail.to\")\nprint(data_user)\n```\n\n\n### Admin login\nWe login a user from the administrator credentials.\n```python\ntokens = cognito.admin_login(username=\"XXXXX@mail.to\", password=\"XXXXXXX\")\nprint(tokens)\n```\n\n\n### Admin renew access token\nWe renew access token a user from the administrator credentials.\n```python\ntoken = cognito.admin_renew_access_token(access_token=\"XXXXX\", refresh_token=\"XXXXXXX\")\nprint(token)\n```\n\n\n### Admin list groups for user\nWe list groups for user from the administrator credentials.\n```python\ngroups = cognito.admin_list_groups_for_user(username=\"XXXXX@mail.to\", limit=10)\nprint(groups)\ngroups = cognito.admin_list_groups_for_user(username=\"XXXXX@mail.to\", limit=10, next_token=groups[\"NextToken\"])\nprint(groups)\n```\n\n\n### Admin reset password\nWe reset password from the administrator credentials.  \nAfter applying this function it will be necessary to launch the initiate_forgot_password function, since the user's password will be disabled.\n```python\ncognito.admin_reset_password(username=\"XXXXX@mail.to\")\n```\n\n\n### Resolve challenge sms mfa\nWe resolve challenge sms mfa.\n```python\ntokens = cognito.resolve_challenge_challenge_sms_mfa(username=\"XXXXX@mail.to\", session=\"XXXXXX\", sms_mfa_code=\"XXXXXX\")\nprint(tokens)\n```\n\n\n### Resolve challenge new password required\nWe resolve challenge new password required.\n```python\ntokens = cognito.resolve_challenge_new_password(username=\"XXXXX@mail.to\", session=\"XXXXXX\", new_password=\"XXXXXX\")\nprint(tokens)\n```\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Python package to use aws cognito in a simple way",
    "version": "1.1.6",
    "project_urls": {
        "Documentation": "https://github.com/DaniMG95/cognitopy/blob/main/README.md",
        "Homepage": "https://github.com/DaniMG95/cognitopy",
        "Repository": "https://github.com/DaniMG95/cognitopy"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "381318b0bde2aa32f4a7f486cecc2faf530c9fc1783813375b2ddd71f968c00a",
                "md5": "d8e4622697f00226c1f714f887eb20d6",
                "sha256": "4c85021ba0ac4cc1826857da9dfd486d19760b8af741944f1f500512c5f5f509"
            },
            "downloads": -1,
            "filename": "cognitopy-1.1.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d8e4622697f00226c1f714f887eb20d6",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0.0,>=3.10.0",
            "size": 9968,
            "upload_time": "2024-12-01T18:04:21",
            "upload_time_iso_8601": "2024-12-01T18:04:21.532175Z",
            "url": "https://files.pythonhosted.org/packages/38/13/18b0bde2aa32f4a7f486cecc2faf530c9fc1783813375b2ddd71f968c00a/cognitopy-1.1.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "47b86909b7625dbb49be8623a2f052201f65ec1e41f92e3ec6e83d004557a775",
                "md5": "d0958226a98c2c99cab65981e5a11fb3",
                "sha256": "99b55c9ea85d4a12b3376182722b6ffc99582c9b693bd192e3dfd3c964c80e1a"
            },
            "downloads": -1,
            "filename": "cognitopy-1.1.6.tar.gz",
            "has_sig": false,
            "md5_digest": "d0958226a98c2c99cab65981e5a11fb3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0.0,>=3.10.0",
            "size": 11094,
            "upload_time": "2024-12-01T18:04:23",
            "upload_time_iso_8601": "2024-12-01T18:04:23.177371Z",
            "url": "https://files.pythonhosted.org/packages/47/b8/6909b7625dbb49be8623a2f052201f65ec1e41f92e3ec6e83d004557a775/cognitopy-1.1.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-01 18:04:23",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "DaniMG95",
    "github_project": "cognitopy",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "cognitopy"
}
        
Elapsed time: 0.93917s