Crates.io | actix-web-middleware-cognito |
lib.rs | actix-web-middleware-cognito |
version | 0.4.0-beta.19 |
source | src |
created_at | 2020-08-03 23:27:44.961404 |
updated_at | 2022-01-07 16:22:45.460287 |
description | Middleware for actix-web that helps you validate Cognito tokens |
homepage | |
repository | https://github.com/robertohuertasm/actix-web-middleware-cognito |
max_upload_size | |
id | 272689 |
size | 64,232 |
Middleware for actix-web that helps you validate Cognito tokens.
Before setting up the middleware we have to create a CognitoValidator
that will be built by receiving some variables from the environment:
Setting up the middleware:
// builidng the validator in order to be shared between all threads.
let cognito_validator =
Arc::new(CognitoValidator::create().expect("Cognito configuration error"));
HttpServer::new(move || {
// cognito middleware
let cognito = Cognito::new(cognito_validator.clone());
// set up the app
App::new()
.wrap(cognito)
.route("/", web::get().to(index))
})
.bind(format!("0.0.0.0:{}", PORT))
.unwrap_or_else(|_| panic!("🔥 Couldn't start the server at port {}", PORT))
.run()
.await
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 fn index(auth: CognitoInfo) -> impl Responder {
let msg = format!(
"User with id {} made this call with token {}",
auth.user.unwrap(),
auth.token.unwrap()
);
HttpResponse::Ok().body(msg)
}
You can check the example
in the repo or run it: cargo run --example main
.