# Hacking Cognito

## Cognito

AWS Cognito is a managed service by AWS which helps you control access to AWS resource from your application. Cognito solves 2 major uses cases.

1. User Pools : Cognito lets you configure user management for your application which includes user Signin/Signup and Forget Passwords and hence stores all user data. The whole management is taken up by AWS and user just needs to create User Pools in cognito.
2. Fedrated indetity pools : Identity pools that allows authentiated and un-authenticated users to access AWS resource.

### Cognito User flow

<figure><img src="https://1804885456-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIo3S6x9y21ea77Yw303B%2Fuploads%2FGzkAQaZc6O5Fqx6CG3Ir%2Fdb055238bed34761a183182dfcaecb74.png?alt=media&#x26;token=2cd66835-a2bb-4d6d-9ce2-b3f7bc601da3" alt=""><figcaption><p>Authentication Flow</p></figcaption></figure>

This shows how congito works for end application. User first performs signin and recives access token from Cognito. User then passes these access token to Cognito and receives back Secret keys for access to AWS Service.

## User Pools

User Pools in Cognito allows you to manage users for your applications and helps you configure the Signin and Singup flow for users along with the redirect after successful authentication.

The below screenshot indicates a user pool called `securitylabs-articles` and also shows the user pool id.

<figure><img src="https://1804885456-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIo3S6x9y21ea77Yw303B%2Fuploads%2FUqAPd0Vw4FelBFuL5UAf%2Fd374a62b23cf4f61aebf81b17e3ca909.png?alt=media&#x26;token=3583726d-01c5-49d5-b410-f3c87cb1e510" alt=""><figcaption><p>User Pool Overview</p></figcaption></figure>

The `App Integration` tab contains App clients which has the `CLIENT_ID`.

<figure><img src="https://1804885456-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIo3S6x9y21ea77Yw303B%2Fuploads%2F9IckubYnvTSwEuUVp5EY%2F5d80e181b4424431a82b2dd3b49ab255.png?alt=media&#x26;token=c0a834e1-d6b8-4317-ae73-87e7dae57ba4" alt=""><figcaption><p>App Client List</p></figcaption></figure>

## Fedrated Identity Pools

Fedrated Identity Pools controls the access for authenticated and unauthenticated users for the application.

The screenshot indicates the idenitity pool id.&#x20;

<figure><img src="https://1804885456-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIo3S6x9y21ea77Yw303B%2Fuploads%2FXLLNXZDcwoz7mOVeLUAa%2Fa4808cc684d0481e860cba6143899f6e.png?alt=media&#x26;token=a40499ca-86d1-4952-bd50-af214af1e3de" alt=""><figcaption><p>Fedrated Identity</p></figcaption></figure>

Any authenticated requests sent to application is logged in Identity Browser. As indicated in the screenshot below, we see that a user with identityid `us-east-2:85c7b6de-3a15-4b0a-a799-20df485734ab` recently logged in successfully into the application. &#x20;

<figure><img src="https://1804885456-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIo3S6x9y21ea77Yw303B%2Fuploads%2Fi8L9nK0jKNQeWzUQIFpj%2Feb5da13c610f4e9abc0780b0b00f0e5c.png?alt=media&#x26;token=09992655-f7d2-4903-894f-f4ef75fd30c3" alt=""><figcaption><p>Identity Pool</p></figcaption></figure>

Clicking on the identityId reveals the user pool, user was associated with.

<figure><img src="https://1804885456-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIo3S6x9y21ea77Yw303B%2Fuploads%2FxHSZ1BHxqm3rfqOU9dDi%2Ffe0bcd12ceb8476a9cf6b9c269e2d67f.png?alt=media&#x26;token=0417af05-316c-456b-ab1f-9f2b88041617" alt=""><figcaption><p>Identity Details</p></figcaption></figure>

### Cognito Authentication

Cognito allows authenticated and unauthenticated access to the application.

#### Unauthenticated Cognito Access

In authenticated Cognito access you just need the `IDENTITY_POOL_ID` which would allow you to fetch the STS tokens for unathenticated users.

```python
IDENTITY_POOL_ID = "us-east-2:aac74edd-4d2c-4b8a-bb87-2064fc9ccd5b"
client = boto3.client('cognito-identity',region_name='us-east-2')
_id = client.get_id(IdentityPoolId=IDENTITY_POOL_ID)['IdentityId']

credentials = client.get_credentials_for_identity(IdentityId=_id)

print(credentials)
```

<figure><img src="https://1804885456-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIo3S6x9y21ea77Yw303B%2Fuploads%2F818Hb3vPyYlVJ4KxgBEl%2F141a8b2b00464a9b84d508b688043250.png?alt=media&#x26;token=c096ebba-f64c-4728-b14a-ce719ddf307c" alt=""><figcaption><p>Credentials</p></figcaption></figure>

#### Authenticated Cognito Access

In AWS Cognito authentication is handled by AWS where once done, it returns back TokenId which can be later used to fetch the STS tokens associated with the logged in user.

In this example we would discuss the `USER_PASSWORD_AUTH` which allows applications to login the user via email and password.

For example, let ClientId for the User Pool be `ae90p5f6au1cqso7sbl0h0eae` and credentials be `securitylabs-articles@securitylabs.tech : vKEX@7Ti`. In such cases, we can initiate the authenitcation and obtain the TokenId.

```python
import boto3
client = boto3.client("cognito-idp", region_name="us-east-2")
response = client.initiate_auth(ClientId='1mf96jsi4jhs31qg2bq7p4lken',AuthFlow="USER_PASSWORD_AUTH",AuthParameters={"USERNAME":'securitylabs-articles@securitylabs.tech','PASSWORD':'vKEX@7Ti'})
tokenid = response['AuthenticationResult']['IdToken']
```

Once TokenId is obtained, we would move to fetch the `IdentityId` using the obtained TokenId. Lets assume the IdentityPoolId for the application is `us-east-2:aac74edd-4d2c-4b8a-bb87-2064fc9ccd5b` and Cognito User Pool Id is `us-east-2_rIJHISTX7`

```python
identity=boto3.client("cognito-identity",region_name="us-east-2")
identity_id = identity.get_id(IdentityPoolId='us-east-2:aac74edd-4d2c-4b8a-bb87-2064fc9ccd5b',Logins={'cognito-idp.ap-south-1.amazonaws.com/ap-south-1_zWBLhpAlk':tokenid})['IdentityId']
```

Once we have obtained the IdentityId, we can now fetch the STS tokens

```python
credentials = identity.get_credentials_for_identity(IdentityId='us-east-2:aac74edd-4d2c-4b8a-bb87-2064fc9ccd5b',Logins={'cognito-idp.ap-south-1.amazonaws.com/us-east-2_rIJHISTX7':tokenid})['Credentials']
access_key = credentials['AccessKeyId']
secret_key = credentials['SecretKey']
session_token = credentials['SessionToken']
```

We have now obtained the STS tokens for a authenticated user.
