orra 0.0.2

Context-aware agent session management for any application
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
//! Authentication and OAuth2 support.
//!
//! Provides token management for provider authentication, including OAuth2
//! authorization code and client credentials flows. Handles token refresh
//! automatically when tokens expire.

use std::sync::Arc;
use std::time::{Duration, Instant};

use async_trait::async_trait;
use tokio::sync::RwLock;

// ---------------------------------------------------------------------------
// Token types
// ---------------------------------------------------------------------------

/// An access token with optional expiration.
#[derive(Debug, Clone)]
pub struct AccessToken {
    /// The token value.
    pub token: String,

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

    /// When this token expires, if known.
    pub expires_at: Option<Instant>,

    /// Refresh token for obtaining a new access token.
    pub refresh_token: Option<String>,

    /// Scopes granted by this token.
    pub scopes: Vec<String>,
}

impl AccessToken {
    /// Check if this token has expired (with a 30-second safety margin).
    pub fn is_expired(&self) -> bool {
        match self.expires_at {
            Some(expires_at) => Instant::now() + Duration::from_secs(30) >= expires_at,
            None => false,
        }
    }

    /// Get the Authorization header value.
    pub fn authorization_header(&self) -> String {
        format!("{} {}", self.token_type, self.token)
    }
}

// ---------------------------------------------------------------------------
// OAuth2 configuration
// ---------------------------------------------------------------------------

/// Configuration for an OAuth2 provider.
#[derive(Debug, Clone)]
pub struct OAuth2Config {
    /// Client ID.
    pub client_id: String,

    /// Client secret.
    pub client_secret: String,

    /// Authorization endpoint URL.
    pub auth_url: String,

    /// Token endpoint URL.
    pub token_url: String,

    /// Redirect URI for authorization code flow.
    pub redirect_uri: Option<String>,

    /// Requested scopes.
    pub scopes: Vec<String>,
}

// ---------------------------------------------------------------------------
// Token provider trait
// ---------------------------------------------------------------------------

/// Trait for obtaining and refreshing access tokens.
#[async_trait]
pub trait TokenProvider: Send + Sync {
    /// Get a valid access token. May refresh automatically if expired.
    async fn get_token(&self) -> Result<AccessToken, AuthError>;

    /// Force a token refresh.
    async fn refresh(&self) -> Result<AccessToken, AuthError>;

    /// Revoke the current token.
    async fn revoke(&self) -> Result<(), AuthError>;
}

// ---------------------------------------------------------------------------
// Static token provider (API keys)
// ---------------------------------------------------------------------------

/// Simple provider for static API keys that never expire.
pub struct StaticTokenProvider {
    token: AccessToken,
}

impl StaticTokenProvider {
    pub fn new(api_key: impl Into<String>) -> Self {
        Self {
            token: AccessToken {
                token: api_key.into(),
                token_type: "Bearer".into(),
                expires_at: None,
                refresh_token: None,
                scopes: Vec::new(),
            },
        }
    }

    pub fn with_token_type(mut self, token_type: impl Into<String>) -> Self {
        self.token.token_type = token_type.into();
        self
    }
}

#[async_trait]
impl TokenProvider for StaticTokenProvider {
    async fn get_token(&self) -> Result<AccessToken, AuthError> {
        Ok(self.token.clone())
    }

    async fn refresh(&self) -> Result<AccessToken, AuthError> {
        Ok(self.token.clone())
    }

    async fn revoke(&self) -> Result<(), AuthError> {
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// OAuth2 client credentials provider
// ---------------------------------------------------------------------------

/// OAuth2 client credentials flow. Suitable for server-to-server
/// authentication where no user interaction is needed.
pub struct ClientCredentialsProvider {
    config: OAuth2Config,
    client: reqwest::Client,
    cached_token: Arc<RwLock<Option<AccessToken>>>,
}

impl ClientCredentialsProvider {
    pub fn new(config: OAuth2Config) -> Self {
        Self {
            config,
            client: reqwest::Client::new(),
            cached_token: Arc::new(RwLock::new(None)),
        }
    }

    async fn fetch_token(&self) -> Result<AccessToken, AuthError> {
        let mut params = HashMap::new();
        params.insert("grant_type", "client_credentials".to_string());
        params.insert("client_id", self.config.client_id.clone());
        params.insert("client_secret", self.config.client_secret.clone());

        if !self.config.scopes.is_empty() {
            params.insert("scope", self.config.scopes.join(" "));
        }

        let response = self
            .client
            .post(&self.config.token_url)
            .form(&params)
            .send()
            .await
            .map_err(|e| AuthError::Request(e.to_string()))?;

        if !response.status().is_success() {
            let status = response.status().as_u16();
            let body = response.text().await.unwrap_or_default();
            return Err(AuthError::TokenEndpoint {
                status,
                message: body,
            });
        }

        let data: TokenResponse = response
            .json()
            .await
            .map_err(|e| AuthError::Parse(e.to_string()))?;

        let expires_at = data
            .expires_in
            .map(|secs| Instant::now() + Duration::from_secs(secs));

        Ok(AccessToken {
            token: data.access_token,
            token_type: data.token_type.unwrap_or_else(|| "Bearer".into()),
            expires_at,
            refresh_token: data.refresh_token,
            scopes: data
                .scope
                .map(|s| s.split_whitespace().map(String::from).collect())
                .unwrap_or_default(),
        })
    }
}

use std::collections::HashMap;

#[async_trait]
impl TokenProvider for ClientCredentialsProvider {
    async fn get_token(&self) -> Result<AccessToken, AuthError> {
        // Check cache first
        if let Some(token) = self.cached_token.read().await.as_ref() {
            if !token.is_expired() {
                return Ok(token.clone());
            }
        }

        // Fetch a new token
        let token = self.fetch_token().await?;
        *self.cached_token.write().await = Some(token.clone());
        Ok(token)
    }

    async fn refresh(&self) -> Result<AccessToken, AuthError> {
        let token = self.fetch_token().await?;
        *self.cached_token.write().await = Some(token.clone());
        Ok(token)
    }

    async fn revoke(&self) -> Result<(), AuthError> {
        *self.cached_token.write().await = None;
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// OAuth2 refresh token provider
// ---------------------------------------------------------------------------

/// OAuth2 provider that uses a refresh token to maintain access.
/// Useful when an initial authorization code flow has already been completed.
pub struct RefreshTokenProvider {
    config: OAuth2Config,
    client: reqwest::Client,
    cached_token: Arc<RwLock<Option<AccessToken>>>,
}

impl RefreshTokenProvider {
    pub fn new(config: OAuth2Config, initial_token: AccessToken) -> Self {
        Self {
            config,
            client: reqwest::Client::new(),
            cached_token: Arc::new(RwLock::new(Some(initial_token))),
        }
    }

    async fn refresh_with_token(&self, refresh_token: &str) -> Result<AccessToken, AuthError> {
        let mut params = HashMap::new();
        params.insert("grant_type", "refresh_token".to_string());
        params.insert("refresh_token", refresh_token.to_string());
        params.insert("client_id", self.config.client_id.clone());
        params.insert("client_secret", self.config.client_secret.clone());

        let response = self
            .client
            .post(&self.config.token_url)
            .form(&params)
            .send()
            .await
            .map_err(|e| AuthError::Request(e.to_string()))?;

        if !response.status().is_success() {
            let status = response.status().as_u16();
            let body = response.text().await.unwrap_or_default();
            return Err(AuthError::TokenEndpoint {
                status,
                message: body,
            });
        }

        let data: TokenResponse = response
            .json()
            .await
            .map_err(|e| AuthError::Parse(e.to_string()))?;

        let expires_at = data
            .expires_in
            .map(|secs| Instant::now() + Duration::from_secs(secs));

        Ok(AccessToken {
            token: data.access_token,
            token_type: data.token_type.unwrap_or_else(|| "Bearer".into()),
            expires_at,
            refresh_token: data.refresh_token.or(Some(refresh_token.to_string())),
            scopes: data
                .scope
                .map(|s| s.split_whitespace().map(String::from).collect())
                .unwrap_or_default(),
        })
    }
}

#[async_trait]
impl TokenProvider for RefreshTokenProvider {
    async fn get_token(&self) -> Result<AccessToken, AuthError> {
        if let Some(token) = self.cached_token.read().await.as_ref() {
            if !token.is_expired() {
                return Ok(token.clone());
            }
        }

        self.refresh().await
    }

    async fn refresh(&self) -> Result<AccessToken, AuthError> {
        let current = self.cached_token.read().await.clone();
        let refresh_token = current
            .and_then(|t| t.refresh_token)
            .ok_or(AuthError::NoRefreshToken)?;

        let token = self.refresh_with_token(&refresh_token).await?;
        *self.cached_token.write().await = Some(token.clone());
        Ok(token)
    }

    async fn revoke(&self) -> Result<(), AuthError> {
        *self.cached_token.write().await = None;
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Token response (OAuth2 standard format)
// ---------------------------------------------------------------------------

#[derive(Debug, serde::Deserialize)]
struct TokenResponse {
    access_token: String,
    token_type: Option<String>,
    expires_in: Option<u64>,
    refresh_token: Option<String>,
    scope: Option<String>,
}

// ---------------------------------------------------------------------------
// Errors
// ---------------------------------------------------------------------------

#[derive(Debug, thiserror::Error)]
pub enum AuthError {
    #[error("request failed: {0}")]
    Request(String),

    #[error("token endpoint error (status {status}): {message}")]
    TokenEndpoint { status: u16, message: String },

    #[error("failed to parse token response: {0}")]
    Parse(String),

    #[error("no refresh token available")]
    NoRefreshToken,

    #[error("token revoked")]
    Revoked,
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn static_token_never_expires() {
        let token = AccessToken {
            token: "sk-test-123".into(),
            token_type: "Bearer".into(),
            expires_at: None,
            refresh_token: None,
            scopes: Vec::new(),
        };
        assert!(!token.is_expired());
    }

    #[test]
    fn expired_token() {
        let token = AccessToken {
            token: "old".into(),
            token_type: "Bearer".into(),
            expires_at: Some(Instant::now() - Duration::from_secs(60)),
            refresh_token: None,
            scopes: Vec::new(),
        };
        assert!(token.is_expired());
    }

    #[test]
    fn token_expiring_within_margin() {
        // Token expires in 20 seconds, but we have a 30s safety margin
        let token = AccessToken {
            token: "soon".into(),
            token_type: "Bearer".into(),
            expires_at: Some(Instant::now() + Duration::from_secs(20)),
            refresh_token: None,
            scopes: Vec::new(),
        };
        assert!(token.is_expired());
    }

    #[test]
    fn valid_token() {
        let token = AccessToken {
            token: "fresh".into(),
            token_type: "Bearer".into(),
            expires_at: Some(Instant::now() + Duration::from_secs(3600)),
            refresh_token: None,
            scopes: Vec::new(),
        };
        assert!(!token.is_expired());
    }

    #[test]
    fn authorization_header() {
        let token = AccessToken {
            token: "abc123".into(),
            token_type: "Bearer".into(),
            expires_at: None,
            refresh_token: None,
            scopes: Vec::new(),
        };
        assert_eq!(token.authorization_header(), "Bearer abc123");
    }

    #[tokio::test]
    async fn static_provider_returns_token() {
        let provider = StaticTokenProvider::new("my-api-key");
        let token = provider.get_token().await.unwrap();
        assert_eq!(token.token, "my-api-key");
        assert_eq!(token.token_type, "Bearer");
    }

    #[tokio::test]
    async fn static_provider_refresh_returns_same() {
        let provider = StaticTokenProvider::new("key");
        let t1 = provider.get_token().await.unwrap();
        let t2 = provider.refresh().await.unwrap();
        assert_eq!(t1.token, t2.token);
    }

    #[tokio::test]
    async fn static_provider_with_custom_type() {
        let provider = StaticTokenProvider::new("key").with_token_type("Basic");
        let token = provider.get_token().await.unwrap();
        assert_eq!(token.token_type, "Basic");
        assert_eq!(token.authorization_header(), "Basic key");
    }

    #[test]
    fn oauth2_config_construction() {
        let config = OAuth2Config {
            client_id: "id".into(),
            client_secret: "secret".into(),
            auth_url: "https://auth.example.com/authorize".into(),
            token_url: "https://auth.example.com/token".into(),
            redirect_uri: Some("https://localhost/callback".into()),
            scopes: vec!["read".into(), "write".into()],
        };

        assert_eq!(config.client_id, "id");
        assert_eq!(config.scopes.len(), 2);
    }

    #[test]
    fn auth_error_display() {
        let err = AuthError::TokenEndpoint {
            status: 401,
            message: "invalid_client".into(),
        };
        let msg = err.to_string();
        assert!(msg.contains("401"));
        assert!(msg.contains("invalid_client"));
    }

    #[test]
    fn token_response_deserialization() {
        let json = r#"{
            "access_token": "abc",
            "token_type": "Bearer",
            "expires_in": 3600,
            "scope": "read write"
        }"#;

        let resp: TokenResponse = serde_json::from_str(json).unwrap();
        assert_eq!(resp.access_token, "abc");
        assert_eq!(resp.expires_in, Some(3600));
        assert_eq!(resp.scope.as_deref(), Some("read write"));
    }

    #[test]
    fn token_response_minimal() {
        let json = r#"{"access_token": "xyz"}"#;
        let resp: TokenResponse = serde_json::from_str(json).unwrap();
        assert_eq!(resp.access_token, "xyz");
        assert!(resp.token_type.is_none());
        assert!(resp.expires_in.is_none());
    }
}