llm-registry-api 0.1.0

API layer for the LLM Registry - REST, GraphQL, and gRPC interfaces for model management
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
//! JWT token management
//!
//! This module provides JWT token generation, validation, and refresh functionality
//! for API authentication.

use chrono::{Duration, Utc};
use jsonwebtoken::{
    decode, encode, errors::Error as JwtError, Algorithm, DecodingKey, EncodingKey, Header,
    Validation,
};
use serde::{Deserialize, Serialize};
use std::fmt;
use thiserror::Error;
use uuid::Uuid;

/// JWT configuration
#[derive(Debug, Clone)]
pub struct JwtConfig {
    /// Secret key for signing tokens
    pub secret: String,

    /// Token expiration in seconds
    pub expiration_seconds: i64,

    /// Refresh token expiration in seconds
    pub refresh_expiration_seconds: i64,

    /// Token issuer
    pub issuer: String,

    /// Token audience
    pub audience: String,

    /// Algorithm for signing
    pub algorithm: Algorithm,
}

// Make config accessible through getters
impl JwtConfig {
    /// Get the issuer
    pub fn issuer(&self) -> &str {
        &self.issuer
    }

    /// Get the audience
    pub fn audience(&self) -> &str {
        &self.audience
    }

    /// Get expiration seconds
    pub fn expiration_seconds(&self) -> i64 {
        self.expiration_seconds
    }
}

impl Default for JwtConfig {
    fn default() -> Self {
        Self {
            secret: "change-me-in-production".to_string(),
            expiration_seconds: 3600,          // 1 hour
            refresh_expiration_seconds: 86400 * 7, // 7 days
            issuer: "llm-registry".to_string(),
            audience: "llm-registry-api".to_string(),
            algorithm: Algorithm::HS256,
        }
    }
}

impl JwtConfig {
    /// Create new JWT configuration
    pub fn new(secret: impl Into<String>) -> Self {
        Self {
            secret: secret.into(),
            ..Default::default()
        }
    }

    /// Set token expiration in seconds
    pub fn with_expiration(mut self, seconds: i64) -> Self {
        self.expiration_seconds = seconds;
        self
    }

    /// Set refresh token expiration in seconds
    pub fn with_refresh_expiration(mut self, seconds: i64) -> Self {
        self.refresh_expiration_seconds = seconds;
        self
    }

    /// Set issuer
    pub fn with_issuer(mut self, issuer: impl Into<String>) -> Self {
        self.issuer = issuer.into();
        self
    }

    /// Set audience
    pub fn with_audience(mut self, audience: impl Into<String>) -> Self {
        self.audience = audience.into();
        self
    }

    /// Set signing algorithm
    pub fn with_algorithm(mut self, algorithm: Algorithm) -> Self {
        self.algorithm = algorithm;
        self
    }

    /// Validate configuration
    pub fn validate(&self) -> Result<(), JwtConfigError> {
        if self.secret.is_empty() {
            return Err(JwtConfigError::EmptySecret);
        }

        if self.secret == "change-me-in-production" {
            tracing::warn!("Using default JWT secret - change this in production!");
        }

        if self.expiration_seconds <= 0 {
            return Err(JwtConfigError::InvalidExpiration);
        }

        if self.refresh_expiration_seconds <= 0 {
            return Err(JwtConfigError::InvalidExpiration);
        }

        if self.issuer.is_empty() {
            return Err(JwtConfigError::EmptyIssuer);
        }

        if self.audience.is_empty() {
            return Err(JwtConfigError::EmptyAudience);
        }

        Ok(())
    }
}

/// JWT configuration errors
#[derive(Debug, Error)]
pub enum JwtConfigError {
    #[error("JWT secret cannot be empty")]
    EmptySecret,

    #[error("JWT expiration must be positive")]
    InvalidExpiration,

    #[error("JWT issuer cannot be empty")]
    EmptyIssuer,

    #[error("JWT audience cannot be empty")]
    EmptyAudience,
}

/// JWT claims structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Claims {
    /// Subject (user ID)
    pub sub: String,

    /// Issuer
    pub iss: String,

    /// Audience
    pub aud: String,

    /// Expiration time (Unix timestamp)
    pub exp: i64,

    /// Issued at (Unix timestamp)
    pub iat: i64,

    /// Not before (Unix timestamp)
    pub nbf: i64,

    /// JWT ID (unique token identifier)
    pub jti: String,

    /// User email
    #[serde(skip_serializing_if = "Option::is_none")]
    pub email: Option<String>,

    /// User roles
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub roles: Vec<String>,

    /// Custom claims
    #[serde(flatten)]
    pub custom: serde_json::Value,
}

impl Claims {
    /// Create new claims with default values
    pub fn new(
        user_id: impl Into<String>,
        issuer: impl Into<String>,
        audience: impl Into<String>,
        expiration_seconds: i64,
    ) -> Self {
        let now = Utc::now();
        let exp = now + Duration::seconds(expiration_seconds);

        Self {
            sub: user_id.into(),
            iss: issuer.into(),
            aud: audience.into(),
            exp: exp.timestamp(),
            iat: now.timestamp(),
            nbf: now.timestamp(),
            jti: Uuid::new_v4().to_string(),
            email: None,
            roles: Vec::new(),
            custom: serde_json::json!({}),
        }
    }

    /// Add email to claims
    pub fn with_email(mut self, email: impl Into<String>) -> Self {
        self.email = Some(email.into());
        self
    }

    /// Add roles to claims
    pub fn with_roles(mut self, roles: Vec<String>) -> Self {
        self.roles = roles;
        self
    }

    /// Add a single role
    pub fn with_role(mut self, role: impl Into<String>) -> Self {
        self.roles.push(role.into());
        self
    }

    /// Add custom claims
    pub fn with_custom(mut self, key: impl Into<String>, value: serde_json::Value) -> Self {
        if let Some(obj) = self.custom.as_object_mut() {
            obj.insert(key.into(), value);
        }
        self
    }

    /// Check if token is expired
    pub fn is_expired(&self) -> bool {
        let now = Utc::now().timestamp();
        self.exp < now
    }

    /// Check if token is not yet valid
    pub fn is_not_yet_valid(&self) -> bool {
        let now = Utc::now().timestamp();
        self.nbf > now
    }

    /// Check if claims are valid
    pub fn validate(&self) -> Result<(), TokenError> {
        if self.is_expired() {
            return Err(TokenError::Expired);
        }

        if self.is_not_yet_valid() {
            return Err(TokenError::NotYetValid);
        }

        if self.sub.is_empty() {
            return Err(TokenError::InvalidClaims("Subject cannot be empty".to_string()));
        }

        Ok(())
    }

    /// Check if user has a specific role
    pub fn has_role(&self, role: &str) -> bool {
        self.roles.iter().any(|r| r == role)
    }

    /// Check if user has any of the specified roles
    pub fn has_any_role(&self, roles: &[&str]) -> bool {
        roles.iter().any(|role| self.has_role(role))
    }

    /// Check if user has all of the specified roles
    pub fn has_all_roles(&self, roles: &[&str]) -> bool {
        roles.iter().all(|role| self.has_role(role))
    }
}

impl fmt::Display for Claims {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Claims(sub={}, jti={})", self.sub, self.jti)
    }
}

/// Token errors
#[derive(Debug, Error)]
pub enum TokenError {
    #[error("Token has expired")]
    Expired,

    #[error("Token is not yet valid")]
    NotYetValid,

    #[error("Invalid token claims: {0}")]
    InvalidClaims(String),

    #[error("JWT error: {0}")]
    JwtError(#[from] JwtError),

    #[error("Invalid token format")]
    InvalidFormat,
}

/// JWT token pair (access + refresh)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenPair {
    /// Access token
    pub access_token: String,

    /// Refresh token
    pub refresh_token: String,

    /// Token type (always "Bearer")
    pub token_type: String,

    /// Expiration in seconds
    pub expires_in: i64,
}

impl TokenPair {
    /// Create a new token pair
    pub fn new(access_token: String, refresh_token: String, expires_in: i64) -> Self {
        Self {
            access_token,
            refresh_token,
            token_type: "Bearer".to_string(),
            expires_in,
        }
    }
}

/// JWT token manager
pub struct JwtManager {
    pub config: JwtConfig,
    encoding_key: EncodingKey,
    decoding_key: DecodingKey,
    validation: Validation,
}

impl JwtManager {
    /// Create a new JWT manager
    pub fn new(config: JwtConfig) -> Result<Self, JwtConfigError> {
        config.validate()?;

        let encoding_key = EncodingKey::from_secret(config.secret.as_bytes());
        let decoding_key = DecodingKey::from_secret(config.secret.as_bytes());

        let mut validation = Validation::new(config.algorithm);
        validation.set_issuer(&[&config.issuer]);
        validation.set_audience(&[&config.audience]);
        validation.validate_exp = true;
        validation.validate_nbf = true;

        Ok(Self {
            config,
            encoding_key,
            decoding_key,
            validation,
        })
    }

    /// Generate a new access token
    pub fn generate_token(&self, user_id: impl Into<String>) -> Result<String, TokenError> {
        let claims = Claims::new(
            user_id,
            &self.config.issuer,
            &self.config.audience,
            self.config.expiration_seconds,
        );

        let header = Header::new(self.config.algorithm);
        encode(&header, &claims, &self.encoding_key).map_err(TokenError::from)
    }

    /// Generate a new access token with custom claims
    pub fn generate_token_with_claims(&self, claims: Claims) -> Result<String, TokenError> {
        let header = Header::new(self.config.algorithm);
        encode(&header, &claims, &self.encoding_key).map_err(TokenError::from)
    }

    /// Generate a new refresh token
    pub fn generate_refresh_token(&self, user_id: impl Into<String>) -> Result<String, TokenError> {
        let claims = Claims::new(
            user_id,
            &self.config.issuer,
            &self.config.audience,
            self.config.refresh_expiration_seconds,
        )
        .with_role("refresh");

        let header = Header::new(self.config.algorithm);
        encode(&header, &claims, &self.encoding_key).map_err(TokenError::from)
    }

    /// Generate a token pair (access + refresh)
    pub fn generate_token_pair(&self, user_id: impl Into<String>) -> Result<TokenPair, TokenError> {
        let user_id = user_id.into();
        let access_token = self.generate_token(&user_id)?;
        let refresh_token = self.generate_refresh_token(&user_id)?;

        Ok(TokenPair::new(
            access_token,
            refresh_token,
            self.config.expiration_seconds,
        ))
    }

    /// Validate and decode a token
    pub fn validate_token(&self, token: &str) -> Result<Claims, TokenError> {
        let token_data = decode::<Claims>(token, &self.decoding_key, &self.validation)?;
        let claims = token_data.claims;
        claims.validate()?;
        Ok(claims)
    }

    /// Refresh an access token using a refresh token
    pub fn refresh_access_token(&self, refresh_token: &str) -> Result<TokenPair, TokenError> {
        let claims = self.validate_token(refresh_token)?;

        // Verify it's a refresh token
        if !claims.has_role("refresh") {
            return Err(TokenError::InvalidClaims(
                "Not a refresh token".to_string(),
            ));
        }

        // Generate new token pair
        self.generate_token_pair(&claims.sub)
    }

    /// Decode token without validation (use with caution)
    pub fn decode_unverified(&self, token: &str) -> Result<Claims, TokenError> {
        let token_data = decode::<Claims>(
            token,
            &self.decoding_key,
            &Validation::new(self.config.algorithm),
        )?;
        Ok(token_data.claims)
    }

    /// Extract token from Authorization header value
    pub fn extract_token_from_header(header_value: &str) -> Result<&str, TokenError> {
        let parts: Vec<&str> = header_value.split_whitespace().collect();

        if parts.len() != 2 {
            return Err(TokenError::InvalidFormat);
        }

        if parts[0].to_lowercase() != "bearer" {
            return Err(TokenError::InvalidFormat);
        }

        Ok(parts[1])
    }
}

impl fmt::Debug for JwtManager {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("JwtManager")
            .field("issuer", &self.config.issuer)
            .field("audience", &self.config.audience)
            .field("algorithm", &self.config.algorithm)
            .field("expiration_seconds", &self.config.expiration_seconds)
            .finish()
    }
}

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

    fn create_test_config() -> JwtConfig {
        JwtConfig::new("test-secret-key-for-testing")
            .with_issuer("test-issuer")
            .with_audience("test-audience")
            .with_expiration(3600)
    }

    #[test]
    fn test_jwt_config_validation() {
        let config = create_test_config();
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_jwt_config_empty_secret() {
        let config = JwtConfig {
            secret: String::new(),
            ..create_test_config()
        };
        assert!(matches!(config.validate(), Err(JwtConfigError::EmptySecret)));
    }

    #[test]
    fn test_claims_creation() {
        let claims = Claims::new("user123", "test-issuer", "test-audience", 3600);

        assert_eq!(claims.sub, "user123");
        assert_eq!(claims.iss, "test-issuer");
        assert_eq!(claims.aud, "test-audience");
        assert!(!claims.is_expired());
    }

    #[test]
    fn test_claims_with_roles() {
        let claims = Claims::new("user123", "test", "test", 3600)
            .with_role("admin")
            .with_role("user");

        assert!(claims.has_role("admin"));
        assert!(claims.has_role("user"));
        assert!(!claims.has_role("moderator"));
        assert!(claims.has_any_role(&["admin", "moderator"]));
        assert!(claims.has_all_roles(&["admin", "user"]));
        assert!(!claims.has_all_roles(&["admin", "moderator"]));
    }

    #[test]
    fn test_jwt_manager_creation() {
        let config = create_test_config();
        let manager = JwtManager::new(config);
        assert!(manager.is_ok());
    }

    #[test]
    fn test_generate_and_validate_token() {
        let config = create_test_config();
        let manager = JwtManager::new(config).unwrap();

        let token = manager.generate_token("user123").unwrap();
        let claims = manager.validate_token(&token).unwrap();

        assert_eq!(claims.sub, "user123");
        assert_eq!(claims.iss, "test-issuer");
        assert_eq!(claims.aud, "test-audience");
    }

    #[test]
    fn test_generate_token_pair() {
        let config = create_test_config();
        let manager = JwtManager::new(config).unwrap();

        let pair = manager.generate_token_pair("user123").unwrap();

        assert!(!pair.access_token.is_empty());
        assert!(!pair.refresh_token.is_empty());
        assert_eq!(pair.token_type, "Bearer");
        assert_eq!(pair.expires_in, 3600);

        // Validate access token
        let access_claims = manager.validate_token(&pair.access_token).unwrap();
        assert_eq!(access_claims.sub, "user123");

        // Validate refresh token
        let refresh_claims = manager.validate_token(&pair.refresh_token).unwrap();
        assert_eq!(refresh_claims.sub, "user123");
        assert!(refresh_claims.has_role("refresh"));
    }

    #[test]
    fn test_refresh_access_token() {
        let config = create_test_config();
        let manager = JwtManager::new(config).unwrap();

        let pair = manager.generate_token_pair("user123").unwrap();
        let new_pair = manager.refresh_access_token(&pair.refresh_token).unwrap();

        assert!(!new_pair.access_token.is_empty());
        assert_ne!(pair.access_token, new_pair.access_token);
    }

    #[test]
    fn test_extract_token_from_header() {
        let header = "Bearer abc123xyz";
        let token = JwtManager::extract_token_from_header(header).unwrap();
        assert_eq!(token, "abc123xyz");
    }

    #[test]
    fn test_extract_token_invalid_format() {
        let header = "InvalidFormat";
        assert!(JwtManager::extract_token_from_header(header).is_err());
    }

    #[test]
    fn test_validate_invalid_token() {
        let config = create_test_config();
        let manager = JwtManager::new(config).unwrap();

        let result = manager.validate_token("invalid.token.here");
        assert!(result.is_err());
    }

    #[test]
    fn test_claims_with_email_and_custom() {
        let claims = Claims::new("user123", "test", "test", 3600)
            .with_email("user@example.com")
            .with_custom("org_id", serde_json::json!("org-456"));

        assert_eq!(claims.email, Some("user@example.com".to_string()));
        assert_eq!(claims.custom["org_id"], "org-456");
    }
}