Purpose
A Library for setting up login routes in a Chalice app.
Basic Usage
Below is an example of a basic application making use of a Cognito User Pool.
First set up a new Chalice app:
$ chalice new-project test-auth $ cd test-auth
Next we add chalice-cognito-auth as a dependency:
$ echo "chalice-cognito-auth" >> requirements.txt
Now update the app.py file to configure a default user pool handler.
from chalice import Chalice
import chalice_cognito_auth
app = Chalice(app_name='test-auth')
app.experimental_feature_flags.update([
'BLUEPRINTS',
])
user_pool_handler = chalice_cognito_auth.default_user_pool_handler()
app.register_blueprint(user_pool_handler.blueprint)
@app.route('/whoami', authorizer=user_pool_handler.auth)
def index():
return {
'username': user_pool_handler.current_user
}
This will create a UserPoolHandler object using the environment variables APP_CLIENT_ID for the Cognito Userpool application client id. POOL_ID for the ID of the Cognito Userpool itself. And AWS_REGION for the region. AWS_REGION is set by the AWS Lambda runtime, but the other two we need to set ourselves. Update the file .chalice/config.json to look something like the following:
{
"version": "2.0",
"app_name": "test-auth",
"environment_variables": {
"APP_CLIENT_ID": "...client id here...",
"POOL_ID": "...pool id here..."
},
"stages": {
"dev": {
"api_gateway_stage": "api"
}
}
}
Substitute the client id and pool id values for ones that match an existing cognito user pool you have and can use for testing.
Now deploy the application using:
$ chalice deploy Creating deployment package. Updating policy for IAM role: test-auth-dev Updating lambda function: test-auth-dev Updating lambda function: test-auth-dev-UserPoolAuth Updating rest API Resources deployed: - Lambda ARN: arn:aws:lambda:us-west-2:...:function:test-auth-dev - Lambda ARN: arn:aws:lambda:us-west-2:...:function:test-auth-dev-UserPoolAuth - Rest API URL: https://id.execute-api.us-west-2.amazonaws.com/api/
Now that it has been deployed we can access the API using the Rest API URL. chalice-cognito-auth injects a login route which accepts a POST request with a JSON payload containing the two keys username and password. Make sure your configured userpool has a user in it that can be used for testing and send something like the following:
$ curl -X POST -H Content-Type:application/json https://id.execute-api.us-west-2.amazonaws.com/api/login -d '{"username":"StealthyCoin", "password": "secret"}'
{"id_token":"...","refresh_token":"...","access_token":"...","token_type":"Bearer"}
The above JSON response contains all the tokens needed to send authorized requests. To test our authorizer we will use the whoami route which simply takes a request and either rejects it if unauthorized, or sends back the username associated with the request. To do this we will send a GET request with an Authorization header with the value of our id_token from the result JSON above.
In my case:
$ curl -H Authorization:...id token here... https://id.execute-api.us-west-2.amazonaws.com/api/whoami
{"username":"StealthyCoin"}
Which sends back JSON object with the username that goes with my id token.
To check that a requset with a bad authorization token is rejected, run the following curl command:
$ curl -H Authorization:foobar https://id.execute-api.us-west-2.amazonaws.com/api/whoami
{"Message":"User is not authorized to access this resource"}
Metadata
Release files for chalice-cognito-auth 2.5.1.post3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| chalice-cognito-auth-2.5.1.post3.tar.gz | 9.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| chalice_cognito_auth-2.5.1.post3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 19.0 kB
Release files / chalice-cognito-auth-2.5.1.post3.tar.gz
| Download URL | chalice-cognito-auth-2.5.1.post3.tar.gz |
|---|---|
| Size | 9.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e50a8442ac4ffa406f103a5f273aafb088ccd3a410974bba0f090550b89c58f1
|
|
BLAKE2b-256 checksum How to use checksums |
534918d0ae1a6cac6708ce9e4443ab099d46b6a92d29bd8d1f2469fccd3eb937
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.7.9
|
Release files / chalice_cognito_auth-2.5.1.post3-py3-none-any.whl
| Download URL | chalice_cognito_auth-2.5.1.post3-py3-none-any.whl |
|---|---|
| Size | 9.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
48db7fd0d90ccfc07b19a1de62aa95c1f25dcf672fce27f98c33b35ab131ba9a
|
|
BLAKE2b-256 checksum How to use checksums |
0ecd32b8cb5c39ff396d5236a7907ac459367e3e0531ea087042f1ef8fc1a40e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.7.9
|
Security for chalice-cognito-auth 2.5.1.post3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.