|
1 | 1 | # JWT Authentication |
2 | 2 |
|
3 | | -Authentication is critical for almost every API. This recipe demonstrates how to implement JSON Web Token (JWT) authentication using the `jsonwebtoken` crate and RustAPI's extractor pattern. |
| 3 | +Authentication is critical for almost every API. RustAPI provides a built-in, production-ready JWT authentication system via the `jwt` feature. |
4 | 4 |
|
5 | 5 | ## Dependencies |
6 | 6 |
|
7 | | -Add `jsonwebtoken` and `serde` to your `Cargo.toml`: |
| 7 | +Enable the `jwt` feature in your `Cargo.toml`: |
8 | 8 |
|
9 | 9 | ```toml |
10 | 10 | [dependencies] |
11 | | -jsonwebtoken = "9" |
| 11 | +rustapi-rs = { version = "0.1", features = ["jwt"] } |
12 | 12 | serde = { version = "1", features = ["derive"] } |
13 | 13 | ``` |
14 | 14 |
|
15 | 15 | ## 1. Define Claims |
16 | 16 |
|
17 | | -The standard JWT claims. You can add custom fields here (like `role`). |
| 17 | +Define your custom claims struct. It must be serializable and deserializable. |
18 | 18 |
|
19 | 19 | ```rust |
20 | 20 | use serde::{Deserialize, Serialize}; |
21 | 21 |
|
22 | | -#[derive(Debug, Serialize, Deserialize)] |
| 22 | +#[derive(Debug, Serialize, Deserialize, Clone)] |
23 | 23 | pub struct Claims { |
24 | | - pub sub: String, // Subject (User ID) |
25 | | - pub exp: usize, // Expiration time |
26 | | - pub role: String, // Custom claim: "admin", "user" |
| 24 | + pub sub: String, // Subject (User ID) |
| 25 | + pub role: String, // Custom claim: "admin", "user" |
| 26 | + pub exp: usize, // Required for JWT expiration validation |
27 | 27 | } |
28 | 28 | ``` |
29 | 29 |
|
30 | | -## 2. Configuration State |
| 30 | +## 2. Shared State |
31 | 31 |
|
32 | | -Store your keys in the application state. |
| 32 | +To avoid hardcoding secrets in multiple places, we'll store our secret key in the application state. |
33 | 33 |
|
34 | 34 | ```rust |
35 | | -use std::sync::Arc; |
36 | | -use jsonwebtoken::{EncodingKey, DecodingKey}; |
37 | | - |
38 | 35 | #[derive(Clone)] |
39 | | -pub struct AuthState { |
40 | | - pub encoder: EncodingKey, |
41 | | - pub decoder: DecodingKey, |
42 | | -} |
43 | | - |
44 | | -impl AuthState { |
45 | | - pub fn new(secret: &str) -> Self { |
46 | | - Self { |
47 | | - encoder: EncodingKey::from_secret(secret.as_bytes()), |
48 | | - decoder: DecodingKey::from_secret(secret.as_bytes()), |
49 | | - } |
50 | | - } |
| 36 | +pub struct AppState { |
| 37 | + pub secret: String, |
51 | 38 | } |
52 | 39 | ``` |
53 | 40 |
|
54 | | -## 3. The `AuthUser` Extractor |
| 41 | +## 3. The Handlers |
55 | 42 |
|
56 | | -This is where the magic happens. We create a custom extractor that: |
57 | | -1. Checks the `Authorization` header. |
58 | | -2. Decodes the token. |
59 | | -3. Validates expiration. |
60 | | -4. Returns the claims or rejects the request. |
| 43 | +We use the `AuthUser<T>` extractor to protect routes, and `State<T>` to access the secret for signing tokens during login. |
61 | 44 |
|
62 | 45 | ```rust |
63 | | -use rustapi::prelude::*; |
64 | | -use jsonwebtoken::{decode, Validation, Algorithm}; |
65 | | - |
66 | | -pub struct AuthUser(pub Claims); |
67 | | - |
68 | | -#[async_trait] |
69 | | -impl FromRequestParts<Arc<AuthState>> for AuthUser { |
70 | | - type Rejection = (StatusCode, Json<serde_json::Value>); |
71 | | - |
72 | | - async fn from_request_parts( |
73 | | - parts: &mut Parts, |
74 | | - state: &Arc<AuthState> |
75 | | - ) -> Result<Self, Self::Rejection> { |
76 | | - // 1. Get header |
77 | | - let auth_header = parts.headers.get("Authorization") |
78 | | - .ok_or((StatusCode::UNAUTHORIZED, Json(json!({"error": "Missing token"}))))?; |
79 | | - |
80 | | - let token = auth_header.to_str() |
81 | | - .map_err(|_| (StatusCode::UNAUTHORIZED, Json(json!({"error": "Invalid token format"}))))? |
82 | | - .strip_prefix("Bearer ") |
83 | | - .ok_or((StatusCode::UNAUTHORIZED, Json(json!({"error": "Invalid token type"}))))?; |
84 | | - |
85 | | - // 2. Decode |
86 | | - let token_data = decode::<Claims>( |
87 | | - token, |
88 | | - &state.decoder, |
89 | | - &Validation::new(Algorithm::HS256) |
90 | | - ).map_err(|e| (StatusCode::UNAUTHORIZED, Json(json!({"error": e.to_string()}))))?; |
91 | | - |
92 | | - Ok(AuthUser(token_data.claims)) |
93 | | - } |
94 | | -} |
95 | | -``` |
96 | | - |
97 | | -## 4. Usage in Handlers |
| 46 | +use rustapi_rs::prelude::*; |
| 47 | +use std::time::{SystemTime, UNIX_EPOCH}; |
98 | 48 |
|
99 | | -Now, securing an endpoint is as simple as adding an argument. |
100 | | - |
101 | | -```rust |
| 49 | +#[rustapi::get("/profile")] |
102 | 50 | async fn protected_profile( |
103 | | - AuthUser(claims): AuthUser |
| 51 | + // This handler will only be called if a valid token is present |
| 52 | + AuthUser(claims): AuthUser<Claims> |
104 | 53 | ) -> Json<String> { |
105 | 54 | Json(format!("Welcome back, {}! You are a {}.", claims.sub, claims.role)) |
106 | 55 | } |
107 | 56 |
|
108 | | -async fn login(State(state): State<Arc<AuthState>>) -> Json<String> { |
| 57 | +#[rustapi::post("/login")] |
| 58 | +async fn login(State(state): State<AppState>) -> Result<Json<String>> { |
109 | 59 | // In a real app, validate credentials first! |
| 60 | + use std::time::{SystemTime, UNIX_EPOCH}; |
| 61 | + |
| 62 | + let expiration = SystemTime::now() |
| 63 | + .duration_since(UNIX_EPOCH) |
| 64 | + .unwrap() |
| 65 | + .as_secs() + 3600; // Token expires in 1 hour (3600 seconds) |
| 66 | + |
110 | 67 | let claims = Claims { |
111 | 68 | sub: "user_123".to_owned(), |
112 | 69 | role: "admin".to_owned(), |
113 | | - exp: 10000000000, // Future timestamp |
| 70 | + exp: expiration as usize, |
114 | 71 | }; |
115 | 72 |
|
116 | | - let token = jsonwebtoken::encode( |
117 | | - &jsonwebtoken::Header::default(), |
118 | | - &claims, |
119 | | - &state.encoder |
120 | | - ).unwrap(); |
| 73 | + // We use the secret from our shared state |
| 74 | + let token = create_token(&claims, &state.secret)?; |
121 | 75 |
|
122 | | - Json(token) |
| 76 | + Ok(Json(token)) |
123 | 77 | } |
124 | 78 | ``` |
125 | 79 |
|
126 | | -## 5. Wiring it Up |
| 80 | +## 4. Wiring it Up |
| 81 | + |
| 82 | +Register the `JwtLayer` and the state in your application. |
127 | 83 |
|
128 | 84 | ```rust |
129 | | -#[tokio::main] |
130 | | -async fn main() { |
131 | | - let auth_state = Arc::new(AuthState::new("my_secret_key")); |
| 85 | +#[rustapi::main] |
| 86 | +async fn main() -> Result<()> { |
| 87 | + // In production, load this from an environment variable! |
| 88 | + let secret = "my_secret_key".to_string(); |
| 89 | + |
| 90 | + let state = AppState { |
| 91 | + secret: secret.clone(), |
| 92 | + }; |
132 | 93 |
|
133 | | - let app = RustApi::new() |
134 | | - .route("/login", post(login)) |
135 | | - .route("/profile", get(protected_profile)) |
136 | | - .with_state(auth_state); // Inject state |
| 94 | + // Configure JWT validation with the same secret |
| 95 | + let jwt_layer = JwtLayer::<Claims>::new(secret); |
137 | 96 |
|
138 | | - RustApi::serve("127.0.0.1:3000", app).await.unwrap(); |
| 97 | + RustApi::auto() |
| 98 | + .state(state) // Register the shared state |
| 99 | + .layer(jwt_layer) // Add the middleware |
| 100 | + .run("127.0.0.1:8080") |
| 101 | + .await |
139 | 102 | } |
140 | 103 | ``` |
141 | 104 |
|
142 | 105 | ## Bonus: Role-Based Access Control (RBAC) |
143 | 106 |
|
144 | | -Since we have the `role` in our claims, we can enforce permissions easily. |
| 107 | +Since we have the `role` in our claims, we can enforce permissions easily within the handler: |
145 | 108 |
|
146 | 109 | ```rust |
147 | | -async fn admin_only(AuthUser(claims): AuthUser) -> Result<String, StatusCode> { |
| 110 | +#[rustapi::get("/admin")] |
| 111 | +async fn admin_only(AuthUser(claims): AuthUser<Claims>) -> Result<String, StatusCode> { |
148 | 112 | if claims.role != "admin" { |
149 | 113 | return Err(StatusCode::FORBIDDEN); |
150 | 114 | } |
151 | 115 | Ok("Sensitive Admin Data".to_string()) |
152 | 116 | } |
153 | 117 | ``` |
| 118 | + |
| 119 | +## How It Works |
| 120 | + |
| 121 | +1. **`JwtLayer` Middleware**: Intercepts requests, looks for `Authorization: Bearer <token>`, validates the signature, and stores the decoded claims in the request extensions. |
| 122 | +2. **`AuthUser` Extractor**: Retrieves the claims from the request extensions. If the middleware failed or didn't run, or if the token was missing/invalid, the extractor returns a `401 Unauthorized` error. |
| 123 | + |
| 124 | +This separation allows you to have some public routes (where `JwtLayer` might just pass through) and some protected routes (where `AuthUser` enforces presence). Note that `JwtLayer` by default does *not* reject requests without tokens; it just doesn't attach claims. The *extractor* does the rejection. |
0 commit comments