kotoba-handler 0.1.22

Unified Web Handler for Kotoba ecosystem - integrates server, CLI, and WASM execution
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
//! Authentication Handler Module
//!
//! このモジュールは認証・認可機能を提供します。
//! JWTトークン、パスワードハッシュ、セッション管理などを含みます。

use crate::{HandlerError, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{SystemTime, UNIX_EPOCH};

/// 認証設定
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthConfig {
    pub jwt_secret: String,
    pub jwt_expiration_hours: u64,
    pub bcrypt_cost: u32,
    pub session_timeout_minutes: u64,
    pub allowed_origins: Vec<String>,
    pub enable_cors: bool,
}

/// ユーザークレデンシャル
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserCredentials {
    pub username: String,
    pub password: String,
}

/// JWTクレーム
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JwtClaims {
    pub sub: String,        // Subject (user ID)
    pub exp: u64,          // Expiration time
    pub iat: u64,          // Issued at
    pub iss: String,       // Issuer
    pub aud: String,       // Audience
    pub role: Option<String>, // User role
    pub permissions: Vec<String>, // User permissions
}

/// ユーザーロール
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum UserRole {
    Admin,
    User,
    Guest,
    Custom(String),
}

/// 認証ミドルウェア
pub struct AuthMiddleware {
    config: AuthConfig,
}

impl AuthMiddleware {
    /// 新しい認証ミドルウェアを作成
    pub fn new(config: AuthConfig) -> Self {
        Self { config }
    }

    /// JWTトークンを生成
    #[cfg(feature = "jsonwebtoken")]
    pub fn generate_jwt(&self, user_id: &str, role: Option<&str>) -> Result<String> {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map_err(|e| HandlerError::Jsonnet(format!("Time error: {}", e)))?
            .as_secs();

        let claims = JwtClaims {
            sub: user_id.to_string(),
            exp: now + (self.config.jwt_expiration_hours * 3600),
            iat: now,
            iss: "kotoba-auth".to_string(),
            aud: "kotoba-app".to_string(),
            role: role.map(|s| s.to_string()),
            permissions: vec![], // TODO: 実装する
        };

        jsonwebtoken::encode(
            &jsonwebtoken::Header::default(),
            &claims,
            &jsonwebtoken::EncodingKey::from_secret(self.config.jwt_secret.as_bytes()),
        )
        .map_err(|e| HandlerError::Jsonnet(format!("JWT encoding error: {}", e)))
    }

    /// JWTトークンを検証
    #[cfg(feature = "jsonwebtoken")]
    pub fn verify_jwt(&self, token: &str) -> Result<JwtClaims> {
        let validation = jsonwebtoken::Validation::default();

        let token_data = jsonwebtoken::decode::<JwtClaims>(
            token,
            &jsonwebtoken::DecodingKey::from_secret(self.config.jwt_secret.as_bytes()),
            &validation,
        )
        .map_err(|e| HandlerError::Jsonnet(format!("JWT decoding error: {}", e)))?;

        // トークンの有効期限をチェック
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map_err(|e| HandlerError::Jsonnet(format!("Time error: {}", e)))?
            .as_secs();

        if token_data.claims.exp < now {
            return Err(HandlerError::Jsonnet("JWT token has expired".to_string()));
        }

        Ok(token_data.claims)
    }

    /// パスワードをハッシュ化
    #[cfg(feature = "bcrypt")]
    pub fn hash_password(&self, password: &str) -> Result<String> {
        bcrypt::hash(password, self.config.bcrypt_cost)
            .map_err(|e| HandlerError::Jsonnet(format!("Password hashing error: {}", e)))
    }

    /// パスワードを検証
    #[cfg(feature = "bcrypt")]
    pub fn verify_password(&self, password: &str, hash: &str) -> Result<bool> {
        bcrypt::verify(password, hash)
            .map_err(|e| HandlerError::Jsonnet(format!("Password verification error: {}", e)))
    }

    /// CORSヘッダーを追加
    pub fn add_cors_headers(&self, response_headers: &mut HashMap<String, String>, origin: Option<&str>) {
        if !self.config.enable_cors {
            return;
        }

        response_headers.insert("Access-Control-Allow-Origin".to_string(),
            origin.unwrap_or("*").to_string());
        response_headers.insert("Access-Control-Allow-Methods".to_string(),
            "GET, POST, PUT, DELETE, OPTIONS".to_string());
        response_headers.insert("Access-Control-Allow-Headers".to_string(),
            "Content-Type, Authorization, X-Requested-With".to_string());
        response_headers.insert("Access-Control-Max-Age".to_string(), "86400".to_string());
    }

    /// Originが許可されているかチェック
    pub fn is_origin_allowed(&self, origin: &str) -> bool {
        if self.config.allowed_origins.is_empty() {
            return true; // すべてのオリジンを許可
        }

        self.config.allowed_origins.iter().any(|allowed| allowed == origin)
    }

    /// セッションを検証
    pub fn validate_session(&self, session_id: &str, user_id: &str) -> Result<bool> {
        // TODO: 実際のセッションストアとの統合
        // 現在はプレースホルダー

        // セッションIDの形式チェック
        if session_id.len() < 32 {
            return Ok(false);
        }

        // ユーザーIDの形式チェック
        if user_id.is_empty() {
            return Ok(false);
        }

        // TODO: セッションストアから有効性をチェック
        Ok(true)
    }

    /// 権限をチェック
    pub fn check_permission(&self, user_role: &UserRole, required_permission: &str) -> bool {
        match user_role {
            UserRole::Admin => true, // Adminはすべての権限を持つ
            UserRole::User => {
                // 一般ユーザーの権限
                matches!(required_permission,
                    "read_profile" | "update_profile" | "read_posts" | "create_posts")
            }
            UserRole::Guest => {
                // ゲストユーザーの権限
                matches!(required_permission, "read_posts" | "read_public")
            }
            UserRole::Custom(role) => {
                // カスタムロールの権限チェック
                match role.as_str() {
                    "editor" => matches!(required_permission,
                        "read_posts" | "create_posts" | "update_posts" | "delete_posts"),
                    "moderator" => matches!(required_permission,
                        "read_posts" | "update_posts" | "moderate_comments"),
                    _ => false,
                }
            }
        }
    }

    /// 設定を取得
    pub fn config(&self) -> &AuthConfig {
        &self.config
    }
}

/// セッションマネージャー
pub struct SessionManager {
    sessions: HashMap<String, SessionData>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionData {
    pub user_id: String,
    pub created_at: u64,
    pub expires_at: u64,
    pub data: HashMap<String, serde_json::Value>,
}

impl SessionManager {
    /// 新しいセッションマネージャーを作成
    pub fn new() -> Self {
        Self {
            sessions: HashMap::new(),
        }
    }

    /// 新しいセッションを作成
    pub fn create_session(&mut self, user_id: &str, timeout_minutes: u64) -> Result<String> {
        let session_id = uuid::Uuid::new_v4().to_string();
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map_err(|e| HandlerError::Jsonnet(format!("Time error: {}", e)))?
            .as_secs();

        let session_data = SessionData {
            user_id: user_id.to_string(),
            created_at: now,
            expires_at: now + (timeout_minutes * 60),
            data: HashMap::new(),
        };

        self.sessions.insert(session_id.clone(), session_data);
        Ok(session_id)
    }

    /// セッションを取得
    pub fn get_session(&self, session_id: &str) -> Option<&SessionData> {
        self.sessions.get(session_id)
    }

    /// セッションを検証
    pub fn validate_session(&self, session_id: &str) -> Result<bool> {
        if let Some(session) = self.sessions.get(session_id) {
            let now = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .map_err(|e| HandlerError::Jsonnet(format!("Time error: {}", e)))?
                .as_secs();

            Ok(session.expires_at > now)
        } else {
            Ok(false)
        }
    }

    /// セッションを更新
    pub fn update_session(&mut self, session_id: &str, data: HashMap<String, serde_json::Value>) -> Result<()> {
        if let Some(session) = self.sessions.get_mut(session_id) {
            session.data = data;
            Ok(())
        } else {
            Err(HandlerError::Jsonnet("Session not found".to_string()))
        }
    }

    /// セッションを削除
    pub fn delete_session(&mut self, session_id: &str) -> Result<()> {
        if self.sessions.remove(session_id).is_some() {
            Ok(())
        } else {
            Err(HandlerError::Jsonnet("Session not found".to_string()))
        }
    }

    /// 期限切れのセッションをクリーンアップ
    pub fn cleanup_expired_sessions(&mut self) -> Result<usize> {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map_err(|e| HandlerError::Jsonnet(format!("Time error: {}", e)))?
            .as_secs();

        let expired_count = self.sessions.len();
        self.sessions.retain(|_, session| session.expires_at > now);

        Ok(expired_count - self.sessions.len())
    }

    /// アクティブなセッション数を取得
    pub fn active_sessions_count(&self) -> usize {
        self.sessions.len()
    }
}

/// パスワードユーティリティ
pub struct PasswordUtils;

impl PasswordUtils {
    /// パスワード強度をチェック
    pub fn check_password_strength(password: &str) -> Result<(bool, Vec<String>)> {
        let mut issues = Vec::new();
        let mut is_strong = true;

        if password.len() < 8 {
            issues.push("Password must be at least 8 characters long".to_string());
            is_strong = false;
        }

        if !password.chars().any(|c| c.is_uppercase()) {
            issues.push("Password must contain at least one uppercase letter".to_string());
            is_strong = false;
        }

        if !password.chars().any(|c| c.is_lowercase()) {
            issues.push("Password must contain at least one lowercase letter".to_string());
            is_strong = false;
        }

        if !password.chars().any(|c| c.is_numeric()) {
            issues.push("Password must contain at least one number".to_string());
            is_strong = false;
        }

        if !password.chars().any(|c| !c.is_alphanumeric()) {
            issues.push("Password must contain at least one special character".to_string());
            is_strong = false;
        }

        Ok((is_strong, issues))
    }

    /// 安全なパスワードを生成
    pub fn generate_secure_password(length: usize) -> String {
        use rand::Rng;

        let charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\
                      abcdefghijklmnopqrstuvwxyz\
                      0123456789\
                      !@#$%^&*()_+-=[]{}|;:,.<>?";

        let mut rng = rand::thread_rng();
        let password: String = (0..length)
            .map(|_| {
                let idx = rng.gen_range(0..charset.len());
                charset.chars().nth(idx).unwrap()
            })
            .collect();

        password
    }
}

/// レートリミッター
pub struct RateLimiter {
    requests: HashMap<String, Vec<u64>>,
    max_requests: u32,
    window_seconds: u64,
}

impl RateLimiter {
    /// 新しいレートリミッターを作成
    pub fn new(max_requests: u32, window_seconds: u64) -> Self {
        Self {
            requests: HashMap::new(),
            max_requests,
            window_seconds,
        }
    }

    /// リクエストをチェック
    pub fn check_rate_limit(&mut self, identifier: &str) -> Result<bool> {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map_err(|e| HandlerError::Jsonnet(format!("Time error: {}", e)))?
            .as_secs();

        let window_start = now.saturating_sub(self.window_seconds);

        // 古いリクエストをクリーンアップ
        if let Some(requests) = self.requests.get_mut(identifier) {
            requests.retain(|&timestamp| timestamp > window_start);
        }

        // リクエスト数をチェック
        let current_requests = self.requests
            .get(identifier)
            .map(|requests| requests.len())
            .unwrap_or(0);

        if current_requests >= self.max_requests as usize {
            return Ok(false); // レート制限超過
        }

        // 新しいリクエストを追加
        self.requests
            .entry(identifier.to_string())
            .or_insert_with(Vec::new)
            .push(now);

        Ok(true)
    }

    /// 残りのリクエスト数を取得
    pub fn remaining_requests(&self, identifier: &str) -> u32 {
        let current_requests = self.requests
            .get(identifier)
            .map(|requests| requests.len())
            .unwrap_or(0);

        self.max_requests.saturating_sub(current_requests as u32)
    }

    /// ウィンドウのリセット時間を取得
    pub fn reset_time(&self, identifier: &str) -> Option<u64> {
        self.requests.get(identifier)
            .and_then(|requests| requests.first())
            .map(|first_request| first_request + self.window_seconds)
    }
}

/// OAuth 2.0 ヘルパー
#[cfg(feature = "jsonwebtoken")]
pub struct OAuthHelper {
    client_id: String,
    client_secret: String,
    redirect_uri: String,
}

#[cfg(feature = "jsonwebtoken")]
impl OAuthHelper {
    /// 新しいOAuthヘルパーを作成
    pub fn new(client_id: &str, client_secret: &str, redirect_uri: &str) -> Self {
        Self {
            client_id: client_id.to_string(),
            client_secret: client_secret.to_string(),
            redirect_uri: redirect_uri.to_string(),
        }
    }

    /// Google OAuth URLを生成
    pub fn generate_google_oauth_url(&self, state: &str) -> String {
        format!(
            "https://accounts.google.com/o/oauth2/v2/auth?\
             response_type=code&\
             client_id={}&\
             redirect_uri={}&\
             scope=openid%20email%20profile&\
             state={}",
            self.client_id,
            urlencoding::encode(&self.redirect_uri),
            urlencoding::encode(state)
        )
    }

    /// GitHub OAuth URLを生成
    pub fn generate_github_oauth_url(&self, state: &str) -> String {
        format!(
            "https://github.com/login/oauth/authorize?\
             client_id={}&\
             redirect_uri={}&\
             scope=user:email&\
             state={}",
            self.client_id,
            urlencoding::encode(&self.redirect_uri),
            urlencoding::encode(state)
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_password_strength_check() {
        // 弱いパスワード
        let (is_strong, issues) = PasswordUtils::check_password_strength("weak").unwrap();
        assert!(!is_strong);
        assert!(issues.len() > 0);

        // 強いパスワード
        let (is_strong, issues) = PasswordUtils::check_password_strength("StrongPass123!").unwrap();
        assert!(is_strong);
        assert_eq!(issues.len(), 0);
    }

    #[test]
    fn test_generate_secure_password() {
        let password = PasswordUtils::generate_secure_password(12);
        assert_eq!(password.len(), 12);

        // 生成されたパスワードが様々な文字を含むことを確認
        let has_upper = password.chars().any(|c| c.is_uppercase());
        let has_lower = password.chars().any(|c| c.is_lowercase());
        let has_digit = password.chars().any(|c| c.is_numeric());
        let has_special = password.chars().any(|c| !c.is_alphanumeric());

        assert!(has_upper || has_lower || has_digit || has_special);
    }

    #[test]
    fn test_user_role_permissions() {
        let auth = AuthMiddleware::new(AuthConfig {
            jwt_secret: "test".to_string(),
            jwt_expiration_hours: 24,
            bcrypt_cost: 4,
            session_timeout_minutes: 60,
            allowed_origins: vec![],
            enable_cors: false,
        });

        // Admin permissions
        assert!(auth.check_permission(&UserRole::Admin, "delete_users"));
        assert!(auth.check_permission(&UserRole::Admin, "read_system_logs"));

        // User permissions
        assert!(auth.check_permission(&UserRole::User, "read_profile"));
        assert!(auth.check_permission(&UserRole::User, "create_posts"));
        assert!(!auth.check_permission(&UserRole::User, "delete_users"));

        // Guest permissions
        assert!(auth.check_permission(&UserRole::Guest, "read_posts"));
        assert!(auth.check_permission(&UserRole::Guest, "read_public"));
        assert!(!auth.check_permission(&UserRole::Guest, "create_posts"));

        // Custom role permissions
        assert!(auth.check_permission(&UserRole::Custom("editor".to_string()), "update_posts"));
        assert!(!auth.check_permission(&UserRole::Custom("editor".to_string()), "delete_users"));
    }

    #[test]
    fn test_session_manager() {
        let mut session_manager = SessionManager::new();

        // セッションを作成
        let session_id = session_manager.create_session("user123", 60).unwrap();
        assert!(!session_id.is_empty());

        // セッションを取得
        let session = session_manager.get_session(&session_id).unwrap();
        assert_eq!(session.user_id, "user123");

        // セッションを検証
        assert!(session_manager.validate_session(&session_id).unwrap());

        // セッションを削除
        session_manager.delete_session(&session_id).unwrap();
        assert!(!session_manager.validate_session(&session_id).unwrap());
    }

    #[test]
    fn test_rate_limiter() {
        let mut limiter = RateLimiter::new(3, 60); // 1分間に3リクエスト

        let identifier = "user123";

        // 最初の3リクエストは成功
        assert!(limiter.check_rate_limit(identifier).unwrap());
        assert!(limiter.check_rate_limit(identifier).unwrap());
        assert!(limiter.check_rate_limit(identifier).unwrap());

        // 4番目のリクエストは失敗
        assert!(!limiter.check_rate_limit(identifier).unwrap());

        // 残りリクエスト数をチェック
        assert_eq!(limiter.remaining_requests(identifier), 0);
    }

    #[test]
    fn test_auth_config_creation() {
        let config = AuthConfig {
            jwt_secret: "my-secret-key".to_string(),
            jwt_expiration_hours: 24,
            bcrypt_cost: 12,
            session_timeout_minutes: 60,
            allowed_origins: vec!["https://example.com".to_string()],
            enable_cors: true,
        };

        assert_eq!(config.jwt_expiration_hours, 24);
        assert_eq!(config.bcrypt_cost, 12);
        assert!(config.enable_cors);
    }
}