rama::http::layer::auth

Module add_authorization

Expand description

Add authorization to requests using the Authorization header.

§Example

use bytes::Bytes;

use rama_http::layer::validate_request::{ValidateRequestHeader, ValidateRequestHeaderLayer};
use rama_http::layer::auth::AddAuthorizationLayer;
use rama_http::{Body, Request, Response, StatusCode, header::AUTHORIZATION};
use rama_core::service::service_fn;
use rama_core::{Context, Service, Layer};
use rama_core::error::BoxError;


let mut client = (
    // Use basic auth with the given username and password
    AddAuthorizationLayer::basic("username", "password"),
).layer(service_that_requires_auth);

// Make a request, we don't have to add the `Authorization` header manually
let response = client
    .serve(Context::default(), Request::new(Body::default()))
    .await?;

assert_eq!(StatusCode::OK, response.status());

Structs§