Crates.io | rocket-apitoken |
lib.rs | rocket-apitoken |
version | 0.1.0 |
source | src |
created_at | 2024-12-02 04:50:58.307878 |
updated_at | 2024-12-02 04:50:58.307878 |
description | A very simple API Authorization module for Rocket web applications |
homepage | |
repository | https://github.com/kvinwang/rocket-apitoken |
max_upload_size | |
id | 1468252 |
size | 6,177 |
A very simple API Authorization module for Rocket web applications
This module provides a simple token-based authorization system for Rocket web applications. It supports both enabled and disabled states, and validates Bearer tokens against a predefined set.
use rocket;
use rocket_apitoken::{ApiToken, Authorized};
#[post("/<method>?<json>", data = "<data>")]
async fn protected_endpoint(_auth: Authorized, /* other params */) {
// If this executes, the request was authorized
// ...
}
#[launch]
fn rocket() -> _ {
let tokens = vec!["secret-token".to_string()];
rocket::build()
.manage(ApiToken::new(tokens, true))
.mount("/api", routes![protected_endpoint])
}
ApiToken
instance with a list of valid tokens and enabled state.manage()
Authorized
guard in your route handlersWhen enabled, requests must include a valid token in the Authorization header. When disabled, all requests are authorized automatically.
This project is licensed under the MIT License - see the LICENSE file for details.