Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yaxian/rocket-jwt
jwt authorization for rocket
https://github.com/yaxian/rocket-jwt
jwt rocket
Last synced: 6 days ago
JSON representation
jwt authorization for rocket
- Host: GitHub
- URL: https://github.com/yaxian/rocket-jwt
- Owner: Yaxian
- License: mit
- Created: 2022-04-12T13:45:45.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-08T15:39:44.000Z (6 months ago)
- Last Synced: 2024-10-29T21:09:34.599Z (2 months ago)
- Topics: jwt, rocket
- Language: Rust
- Homepage:
- Size: 25.4 KB
- Stars: 14
- Watchers: 1
- Forks: 10
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE-MIT
Awesome Lists containing this project
README
# rocket-jwt
jwt authorization for [email protected].
[email protected] see [v0.4](https://github.com/Yaxian/rocket-jwt/tree/v0.4).
```rust
#[macro_use]
extern crate rocket;use rocket_jwt::jwt;
static SECRET_KEY: &str = "secret_key";
#[jwt(SECRET_KEY)]
pub struct UserClaim {
id: String,
}#[jwt("secret", exp = 100)]
pub struct UserClaimExp {
id: String
}#[jwt("secret", leeway = 10)]
pub struct UserClaimLeeway {
id: String
}// get token from cookie, key is `token`
#[jwt("secret", cookie = "token")]
pub struct UserClaimCookie {
id: String
}// get token from request query, key is `token`
#[jwt("secret", query = "token")]
pub struct UserClaimQuery {
id: String
}#[get("/")]
fn index() -> String {
let user_claim = UserClaim {
id: format!("hello_rocket_jwt"),
};
let token = UserClaim::sign(user_claim);
println!("{:#?}", UserClaim::decode(token.clone()));
token
}#[get("/user_id")]
fn get_uer_id_from_jwt(user: UserClaim) -> String {
format!("user id is {}", user.id)
}#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![index, get_uer_id_from_jwt])
}```
### API
| attribute | type | description | default |
|----------|------|-------------|---|
| | String | jwt secret key, required | |
| exp | Int | token expire after seconds | 2592000 *(one month)* |
| leeway | Int | token expire leeway in seconds | 60 *(one minute)* |
| cookie | String | get token from cookie key, optional | |
| query | String | get token from query key, optional | |### Run example
```
cargo run --example rocket-jwt-demo
```1. get `jwt` token
```
curl http://localhost:8000
```2. use `jwt` token
```
curl -H "Authorization: Bearer YOUR_JWT_TOKEN" http://localhost:8000/user_id
```## License
[MIT](LICENSE-MIT)