sa-token-rust 0.1.14

A powerful Rust authentication and authorization framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# JWT (JSON Web Token) Complete Guide

[中文]./JWT_GUIDE_zh-CN.md | English

---

## Overview

sa-token-rust provides complete JWT (JSON Web Token) functionality, supporting token generation, validation, refresh, and custom claims.

## Table of Contents

- [Features]#features
- [Quick Start]#quick-start
- [JWT Manager]#jwt-manager
- [JWT Claims]#jwt-claims
- [Integration with sa-token]#integration-with-sa-token
- [Algorithms]#algorithms
- [Advanced Usage]#advanced-usage
- [API Reference]#api-reference

## Features

- ✅ Multiple algorithm support (HS256, HS384, HS512, RS256, etc.)
- ✅ Custom claims
- ✅ Token validation
- ✅ Token refresh
- ✅ Expiration time management
- ✅ Issuer and audience verification
- ✅ Seamless integration with sa-token
- ✅ Fast user identification

## Quick Start

### 1. Standalone JWT Manager

```rust
use sa_token_core::{JwtManager, JwtClaims};

// Create JWT manager
let jwt_manager = JwtManager::new("your-secret-key");

// Create claims
let mut claims = JwtClaims::new("user_123");
claims.set_expiration(3600); // 1 hour

// Generate token
let token = jwt_manager.generate(&claims)?;

// Validate token
let decoded_claims = jwt_manager.validate(&token)?;
println!("User ID: {}", decoded_claims.login_id);
```

### 2. Using JWT with sa-token

```rust
use sa_token_core::{SaTokenConfig, SaTokenManager, StpUtil};
use sa_token_core::config::TokenStyle;
use std::sync::Arc;

// Configure to use JWT tokens
let config = SaTokenConfig::builder()
    .token_style(TokenStyle::Jwt)
    .jwt_secret_key("my-secret-key")
    .jwt_algorithm("HS256")
    .timeout(7200)
    .build_config();

let manager = SaTokenManager::new(storage, config);
StpUtil::init_manager(manager);

// Login generates JWT token
let token = StpUtil::login("user_123").await?;
```

## JWT Manager

### Creating JWT Manager

```rust
use sa_token_core::{JwtManager, JwtAlgorithm};

// Default (HS256)
let manager = JwtManager::new("secret-key");

// With custom algorithm
let manager = JwtManager::with_algorithm("secret-key", JwtAlgorithm::HS512);

// With issuer and audience
let manager = JwtManager::new("secret-key")
    .set_issuer("my-app")
    .set_audience("web-users");
```

### Generating Tokens

```rust
let mut claims = JwtClaims::new("user_123");
claims.set_expiration(3600);

let token = jwt_manager.generate(&claims)?;
```

### Validating Tokens

```rust
// Full validation (signature + expiration)
let claims = jwt_manager.validate(&token)?;

// Decode without validation (unsafe - for debugging only)
let claims = jwt_manager.decode_without_validation(&token)?;
```

### Refreshing Tokens

```rust
// Extend token validity by 2 hours
let new_token = jwt_manager.refresh(&token, 7200)?;
```

## JWT Claims

### Standard Claims

```rust
let mut claims = JwtClaims::new("user_123");

// Set expiration (seconds from now)
claims.set_expiration(3600);

// Set specific expiration time
claims.set_expiration_at(datetime);

// Set issuer
claims.set_issuer("my-application");

// Set audience
claims.set_audience("web-app");

// Set JWT ID
claims.set_jti("unique-id-123");
```

### sa-token Extensions

```rust
// Set login type
claims.set_login_type("admin");

// Set device identifier
claims.set_device("mobile-ios");
```

### Custom Claims

```rust
use serde_json::json;

// Add custom claims
claims.add_claim("role", json!("admin"));
claims.add_claim("permissions", json!(["read", "write"]));
claims.add_claim("metadata", json!({
    "department": "IT",
    "level": 5
}));

// Retrieve custom claims
let role = claims.get_claim("role");
```

### Checking Expiration

```rust
// Check if expired
if claims.is_expired() {
    println!("Token has expired");
}

// Get remaining time
if let Some(seconds) = claims.remaining_time() {
    println!("Token valid for {} seconds", seconds);
}
```

## Integration with sa-token

### Configuration

```rust
use sa_token_core::SaTokenConfig;
use sa_token_core::config::TokenStyle;

let config = SaTokenConfig::builder()
    // Set token style to JWT
    .token_style(TokenStyle::Jwt)
    
    // Required: JWT secret key
    .jwt_secret_key("your-secret-key-min-32-chars")
    
    // Optional: Algorithm (default: HS256)
    .jwt_algorithm("HS256")
    
    // Optional: Issuer
    .jwt_issuer("my-application")
    
    // Optional: Audience
    .jwt_audience("web-users")
    
    // Token timeout
    .timeout(7200)
    
    .build_config();
```

### Usage

Once configured, all sa-token operations automatically use JWT:

```rust
// Login - generates JWT token
let token = StpUtil::login("user_123").await?;

// Logout
StpUtil::logout(&token).await?;

// Validate
let is_valid = StpUtil::is_login(&token).await;
```

## Algorithms

Supported JWT algorithms:

| Algorithm | Description | Key Type |
|-----------|-------------|----------|
| HS256 | HMAC using SHA-256 | Symmetric (Secret) |
| HS384 | HMAC using SHA-384 | Symmetric (Secret) |
| HS512 | HMAC using SHA-512 | Symmetric (Secret) |
| RS256 | RSA using SHA-256 | Asymmetric (Public/Private) |
| RS384 | RSA using SHA-384 | Asymmetric (Public/Private) |
| RS512 | RSA using SHA-512 | Asymmetric (Public/Private) |
| ES256 | ECDSA using SHA-256 | Asymmetric (Public/Private) |
| ES384 | ECDSA using SHA-384 | Asymmetric (Public/Private) |

### Choosing an Algorithm

- **HS256/384/512**: Best for most applications, fast and simple
- **RS256/384/512**: When you need to distribute public keys for verification
- **ES256/384**: Modern alternative to RSA, smaller keys

## Advanced Usage

### 1. Token Validation with Custom Validation

```rust
let jwt_manager = JwtManager::new("secret")
    .set_issuer("expected-issuer")
    .set_audience("expected-audience");

// Validation will check issuer and audience
let claims = jwt_manager.validate(&token)?;
```

### 2. Quick User Identification

```rust
// Extract user ID without full validation (for logging, analytics)
let user_id = jwt_manager.extract_login_id(&token)?;
```

### 3. Multiple Algorithms

```rust
// Different managers for different purposes
let user_manager = JwtManager::with_algorithm("user-secret", JwtAlgorithm::HS256);
let admin_manager = JwtManager::with_algorithm("admin-secret", JwtAlgorithm::HS512);
```

### 4. Custom Token Lifetime

```rust
let mut claims = JwtClaims::new("user_123");

// Short-lived token (5 minutes)
claims.set_expiration(300);

// Long-lived token (30 days)
claims.set_expiration(2592000);

// Never expires (not recommended for production)
// Don't set expiration
```

## API Reference

### JwtManager

```rust
impl JwtManager {
    // Create new manager
    pub fn new(secret: impl Into<String>) -> Self;
    pub fn with_algorithm(secret: impl Into<String>, algorithm: JwtAlgorithm) -> Self;
    
    // Configure
    pub fn set_issuer(self, issuer: impl Into<String>) -> Self;
    pub fn set_audience(self, audience: impl Into<String>) -> Self;
    
    // Operations
    pub fn generate(&self, claims: &JwtClaims) -> SaTokenResult<String>;
    pub fn validate(&self, token: &str) -> SaTokenResult<JwtClaims>;
    pub fn refresh(&self, token: &str, extend_seconds: i64) -> SaTokenResult<String>;
    pub fn extract_login_id(&self, token: &str) -> SaTokenResult<String>;
    pub fn decode_without_validation(&self, token: &str) -> SaTokenResult<JwtClaims>;
}
```

### JwtClaims

```rust
impl JwtClaims {
    // Create
    pub fn new(login_id: impl Into<String>) -> Self;
    
    // Standard claims
    pub fn set_expiration(&mut self, seconds: i64) -> &mut Self;
    pub fn set_expiration_at(&mut self, datetime: DateTime<Utc>) -> &mut Self;
    pub fn set_issuer(&mut self, issuer: impl Into<String>) -> &mut Self;
    pub fn set_audience(&mut self, audience: impl Into<String>) -> &mut Self;
    pub fn set_jti(&mut self, jti: impl Into<String>) -> &mut Self;
    
    // sa-token extensions
    pub fn set_login_type(&mut self, login_type: impl Into<String>) -> &mut Self;
    pub fn set_device(&mut self, device: impl Into<String>) -> &mut Self;
    
    // Custom claims
    pub fn add_claim(&mut self, key: impl Into<String>, value: Value) -> &mut Self;
    pub fn get_claim(&self, key: &str) -> Option<&Value>;
    
    // Utilities
    pub fn is_expired(&self) -> bool;
    pub fn remaining_time(&self) -> Option<i64>;
}
```

## Security Best Practices

1. **Use Strong Secrets**: Minimum 32 characters for HMAC algorithms
2. **Rotate Keys**: Periodically change your secret keys
3. **Set Expiration**: Always set reasonable expiration times
4. **Validate Everything**: Use full validation, not just decoding
5. **HTTPS Only**: Always transmit JWTs over HTTPS
6. **Store Securely**: Never expose secrets in code or logs
7. **Handle Errors**: Properly handle validation errors
8. **Avoid Sensitive Data**: Don't store sensitive information in claims (they're just base64-encoded)

## Error Handling

```rust
use sa_token_core::SaTokenError;

match jwt_manager.validate(&token) {
    Ok(claims) => {
        // Token is valid
        println!("User: {}", claims.login_id);
    }
    Err(SaTokenError::TokenExpired) => {
        // Token has expired
        println!("Please login again");
    }
    Err(SaTokenError::InvalidToken(msg)) => {
        // Invalid token (signature, format, etc.)
        println!("Invalid token: {}", msg);
    }
    Err(e) => {
        // Other errors
        println!("Error: {:?}", e);
    }
}
```

## Examples

Run the JWT example:

```bash
cargo run --example jwt_example
```

See `examples/jwt_example.rs` for comprehensive examples covering:
- Standalone JWT usage
- Integration with sa-token
- Token refresh
- Multiple algorithms
- Custom claims
- Quick user identification

## References

- [JWT.io]https://jwt.io/ - JWT introduction and debugger
- [RFC 7519]https://tools.ietf.org/html/rfc7519 - JWT specification
- [jsonwebtoken crate]https://docs.rs/jsonwebtoken/ - Underlying library

## Next Steps

- [StpUtil API Reference]./StpUtil.md
- [Event Listeners]./EVENT_LISTENER.md
- [Permission Matching]./PermissionMatching.md