actix-web-middleware-cognito
Middleware for actix-web that helps you validate Cognito tokens.
Cognito validator
Before setting up the middleware we have to create a CognitoValidator
that will be built by receiving some variables from the environment:
- COGNITO_REGION: The region of the Cognito pool.
- COGNITO_POOLID: The Cognito pool id.
- COGNITO_CLIENTID: The client id of your app.
- COGNITO_ENABLED (optional): if not present or 0 no validation will be done.
- COGNITO_VERIFY_ACCESSTOKEN (optional): if not present or 0 idToken will be validated. If present, the accessToken will be validated instead.
Usage
Setting up the middleware:
// builidng the validator in order to be shared between all threads.
let cognito_validator =
new;
new
.bind
.unwrap_or_else
.run
.await
Extracting the token from the request
The library provides a CognitoInfo
extractor for you to get information about the Cognito token. If the token is invalid or you disable the middleware (by omitting the COGNITO_ENABLED
environment variable) you will always get a disabled CognitoInfo
, i.e. a CognitoInfo
with no token
.
async
Example
You can check the example
in the repo or run it: cargo run --example main
.