| Crates.io | tide-jsonwebtoken |
| lib.rs | tide-jsonwebtoken |
| version | 0.1.1 |
| created_at | 2023-10-24 12:55:02.450132+00 |
| updated_at | 2023-10-26 05:38:11.977166+00 |
| description | Tide (http-rs/tide) JWT Authorization Middleware |
| homepage | https://github.com/bitdinosaur-io/tide-jsonwebtoken |
| repository | https://github.com/bitdinosaur-io/tide-jsonwebtoken |
| max_upload_size | |
| id | 1012339 |
| size | 62,468 |
This Rust library offers a middleware for the Tide web server framework, focusing on API key authentication via JWT (JSON Web Tokens) using the tide_jsonwebtoken crate.
Add the required dependencies to your Cargo.toml:
[dependencies]
tide = "0.16" # Use the latest version
tide-jsonwebtoken = "0.1.0" # Use the latest version
async-std = { version = "1.12.0", features = ["attributes"] } # Use the latest version
Initialize the Middleware:
First, create an instance of the ApiKeyMiddleware:
let jwt = ApiKeyMiddleware::new("your-secret-key");
Set Up Tide Application:
Initialize the Tide application and apply the middleware to specific routes:
let mut app = tide::new();
app.at("/login").get(|_| async { Ok("Login route!") });
app.at("/name/:id")
.with(jwt)
.get(|req: Request<()>| async move {
Ok(format!("Hello, {}!", req.param("id").unwrap_or("0")))
});
Run the Server:
app.listen("127.0.0.1:8080").await?;
The middleware inspects the x-api-key header in incoming requests. If the JWT is validated, the request continues; otherwise, the middleware returns a 401 Unauthorized status. Potential error messages include:
API key missing: The request lacks the x-api-key header.Invalid API key: The supplied API key (JWT) is not valid.Pull requests are encouraged. For major adjustments, kindly initiate an issue first to deliberate on the desired changes.