rpi-cli 0.3.8

Terminal coding-agent CLI (the `rpi` binary) built on the rpi-* library crates
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
//! OAuth 2.0 authentication flow for various providers.
//!
//! Implements device code flow and authorization code flow for:
//! - Anthropic (Claude Pro/Max)
//! - OpenAI
//! - GitHub Copilot
//! - OpenRouter
//! - And more...

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use thiserror::Error;

#[derive(Error, Debug)]
pub enum OAuthError {
    #[error("HTTP request failed: {0}")]
    HttpError(String),

    #[error("Invalid response: {0}")]
    InvalidResponse(String),

    #[error("Authentication timeout")]
    Timeout,

    #[error("User denied authorization")]
    Denied,

    #[error("Token expired")]
    TokenExpired,

    #[error("Unsupported provider: {0}")]
    UnsupportedProvider(String),
}

/// OAuth provider types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum OAuthProvider {
    Anthropic,
    OpenAI,
    GitHubCopilot,
    OpenRouter,
    Google,
    Azure,
}

impl std::fmt::Display for OAuthProvider {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            OAuthProvider::Anthropic => write!(f, "anthropic"),
            OAuthProvider::OpenAI => write!(f, "openai"),
            OAuthProvider::GitHubCopilot => write!(f, "github-copilot"),
            OAuthProvider::OpenRouter => write!(f, "openrouter"),
            OAuthProvider::Google => write!(f, "google"),
            OAuthProvider::Azure => write!(f, "azure"),
        }
    }
}

/// OAuth token information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OAuthToken {
    pub access_token: String,
    pub refresh_token: Option<String>,
    pub token_type: String,
    pub expires_at: Option<u64>,
    pub scope: Option<String>,
}

impl OAuthToken {
    /// Check if the token is expired
    pub fn is_expired(&self) -> bool {
        if let Some(expires_at) = self.expires_at {
            let now = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs();
            now >= expires_at
        } else {
            false
        }
    }

    /// Check if the token will expire soon (within the given duration)
    pub fn expires_soon(&self, within: Duration) -> bool {
        if let Some(expires_at) = self.expires_at {
            let now = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs();
            let threshold = within.as_secs();
            expires_at.saturating_sub(now) <= threshold
        } else {
            false
        }
    }
}

/// Device code flow response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeviceCodeResponse {
    pub device_code: String,
    pub user_code: String,
    pub verification_uri: String,
    #[serde(alias = "verification_uri_complete")]
    pub verification_uri_complete: Option<String>,
    pub expires_in: u64,
    pub interval: u64,
}

/// OAuth client configuration
#[derive(Debug, Clone)]
pub struct OAuthConfig {
    pub client_id: String,
    pub client_secret: Option<String>,
    pub auth_url: String,
    pub token_url: String,
    pub redirect_uri: Option<String>,
    pub scopes: Vec<String>,
}

/// OAuth client for handling authentication flows
pub struct OAuthClient {
    config: OAuthConfig,
    provider: OAuthProvider,
    http_client: reqwest::Client,
}

impl OAuthClient {
    /// Create a new OAuth client for the specified provider
    pub fn new(provider: OAuthProvider, config: OAuthConfig) -> Self {
        Self {
            config,
            provider,
            http_client: reqwest::Client::new(),
        }
    }

    /// Create an OAuth client with default configuration for a provider
    pub fn with_defaults(provider: OAuthProvider) -> Result<Self, OAuthError> {
        let config = match provider {
            OAuthProvider::Anthropic => OAuthConfig {
                client_id: "pi-cli".to_string(),
                client_secret: None,
                auth_url: "https://claude.ai/oauth/authorize".to_string(),
                token_url: "https://claude.ai/oauth/token".to_string(),
                redirect_uri: Some("http://localhost:8080/callback".to_string()),
                scopes: vec!["read".to_string(), "write".to_string()],
            },
            OAuthProvider::OpenAI => OAuthConfig {
                client_id: "pi-cli".to_string(),
                client_secret: None,
                auth_url: "https://auth.openai.com/authorize".to_string(),
                token_url: "https://auth.openai.com/oauth/token".to_string(),
                redirect_uri: Some("http://localhost:8080/callback".to_string()),
                scopes: vec!["openai.public".to_string()],
            },
            OAuthProvider::GitHubCopilot => OAuthConfig {
                client_id: "Iv1.b507a08c30306e6a".to_string(), // GitHub App client ID
                client_secret: None,
                auth_url: "https://github.com/login/oauth/authorize".to_string(),
                token_url: "https://github.com/login/oauth/access_token".to_string(),
                redirect_uri: Some("http://localhost:8080/callback".to_string()),
                scopes: vec![],
            },
            OAuthProvider::OpenRouter => OAuthConfig {
                client_id: "pi-cli".to_string(),
                client_secret: None,
                auth_url: "https://openrouter.ai/auth/authorize".to_string(),
                token_url: "https://openrouter.ai/auth/token".to_string(),
                redirect_uri: Some("http://localhost:8080/callback".to_string()),
                scopes: vec![],
            },
            OAuthProvider::Google => OAuthConfig {
                client_id: "pi-cli".to_string(),
                client_secret: None,
                auth_url: "https://accounts.google.com/o/oauth2/v2/auth".to_string(),
                token_url: "https://oauth2.googleapis.com/token".to_string(),
                redirect_uri: Some("http://localhost:8080/callback".to_string()),
                scopes: vec!["https://www.googleapis.com/auth/generative-language".to_string()],
            },
            OAuthProvider::Azure => OAuthConfig {
                client_id: "pi-cli".to_string(),
                client_secret: None,
                auth_url: "https://login.microsoftonline.com/common/oauth2/v2.0/authorize"
                    .to_string(),
                token_url: "https://login.microsoftonline.com/common/oauth2/v2.0/token".to_string(),
                redirect_uri: Some("http://localhost:8080/callback".to_string()),
                scopes: vec!["https://cognitiveservices.azure.com/.default".to_string()],
            },
        };

        Ok(Self::new(provider, config))
    }

    /// Start device code flow
    pub async fn start_device_code_flow(&self) -> Result<DeviceCodeResponse, OAuthError> {
        let mut params = HashMap::new();
        params.insert("client_id", self.config.client_id.as_str());

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

        let response = self
            .http_client
            .post(&self.config.auth_url)
            .form(&params)
            .send()
            .await
            .map_err(|e| OAuthError::HttpError(e.to_string()))?;

        if !response.status().is_success() {
            let status = response.status();
            let text = response.text().await.unwrap_or_default();
            return Err(OAuthError::HttpError(format!("{}: {}", status, text)));
        }

        let device_code: DeviceCodeResponse = response
            .json()
            .await
            .map_err(|e| OAuthError::InvalidResponse(e.to_string()))?;

        Ok(device_code)
    }

    /// Poll for token completion (device code flow)
    pub async fn poll_for_token(
        &self,
        device_code: &str,
        interval: Duration,
        timeout: Duration,
    ) -> Result<OAuthToken, OAuthError> {
        let start = std::time::Instant::now();

        loop {
            if start.elapsed() > timeout {
                return Err(OAuthError::Timeout);
            }

            tokio::time::sleep(interval).await;

            let mut params = HashMap::new();
            params.insert("client_id", self.config.client_id.as_str());
            params.insert("device_code", device_code);
            params.insert("grant_type", "urn:ietf:params:oauth:grant-type:device_code");

            let response = self
                .http_client
                .post(&self.config.token_url)
                .form(&params)
                .send()
                .await
                .map_err(|e| OAuthError::HttpError(e.to_string()))?;

            let status = response.status();
            let text = response.text().await.unwrap_or_default();

            if status.is_success() {
                // Parse successful token response
                let token_response: serde_json::Value = serde_json::from_str(&text)
                    .map_err(|e| OAuthError::InvalidResponse(e.to_string()))?;

                let access_token = token_response["access_token"]
                    .as_str()
                    .ok_or_else(|| OAuthError::InvalidResponse("missing access_token".to_string()))?
                    .to_string();

                let refresh_token = token_response["refresh_token"]
                    .as_str()
                    .map(|s| s.to_string());

                let token_type = token_response["token_type"]
                    .as_str()
                    .unwrap_or("Bearer")
                    .to_string();

                let expires_at = token_response["expires_in"].as_u64().map(|expires_in| {
                    SystemTime::now()
                        .duration_since(UNIX_EPOCH)
                        .unwrap()
                        .as_secs()
                        + expires_in
                });

                let scope = token_response["scope"].as_str().map(|s| s.to_string());

                return Ok(OAuthToken {
                    access_token,
                    refresh_token,
                    token_type,
                    expires_at,
                    scope,
                });
            }

            // Check for specific error codes
            if let Ok(error_response) = serde_json::from_str::<serde_json::Value>(&text) {
                if let Some(error) = error_response["error"].as_str() {
                    match error {
                        "authorization_pending" => continue,
                        "slow_down" => {
                            tokio::time::sleep(Duration::from_secs(5)).await;
                            continue;
                        }
                        "access_denied" => return Err(OAuthError::Denied),
                        "expired_token" => return Err(OAuthError::Timeout),
                        _ => {
                            return Err(OAuthError::InvalidResponse(format!(
                                "unknown error: {}",
                                error
                            )))
                        }
                    }
                }
            }
        }
    }

    /// Refresh an expired token
    pub async fn refresh_token(&self, refresh_token: &str) -> Result<OAuthToken, OAuthError> {
        let mut params = HashMap::new();
        params.insert("client_id", self.config.client_id.as_str());
        params.insert("refresh_token", refresh_token);
        params.insert("grant_type", "refresh_token");

        if let Some(secret) = &self.config.client_secret {
            params.insert("client_secret", secret.as_str());
        }

        let response = self
            .http_client
            .post(&self.config.token_url)
            .form(&params)
            .send()
            .await
            .map_err(|e| OAuthError::HttpError(e.to_string()))?;

        if !response.status().is_success() {
            let status = response.status();
            let text = response.text().await.unwrap_or_default();
            return Err(OAuthError::HttpError(format!("{}: {}", status, text)));
        }

        let token_response: serde_json::Value = response
            .json()
            .await
            .map_err(|e| OAuthError::InvalidResponse(e.to_string()))?;

        let access_token = token_response["access_token"]
            .as_str()
            .ok_or_else(|| OAuthError::InvalidResponse("missing access_token".to_string()))?
            .to_string();

        let new_refresh_token = token_response["refresh_token"]
            .as_str()
            .map(|s| s.to_string())
            .unwrap_or_else(|| refresh_token.to_string());

        let token_type = token_response["token_type"]
            .as_str()
            .unwrap_or("Bearer")
            .to_string();

        let expires_at = token_response["expires_in"].as_u64().map(|expires_in| {
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs()
                + expires_in
        });

        let scope = token_response["scope"].as_str().map(|s| s.to_string());

        Ok(OAuthToken {
            access_token,
            refresh_token: Some(new_refresh_token),
            token_type,
            expires_at,
            scope,
        })
    }

    /// Get the provider type
    pub fn provider(&self) -> OAuthProvider {
        self.provider
    }
}

/// OAuth token storage
pub struct OAuthTokenStore {
    tokens: HashMap<OAuthProvider, OAuthToken>,
}

impl OAuthTokenStore {
    /// Create a new token store
    pub fn new() -> Self {
        Self {
            tokens: HashMap::new(),
        }
    }

    /// Store a token
    pub fn store(&mut self, provider: OAuthProvider, token: OAuthToken) {
        self.tokens.insert(provider, token);
    }

    /// Retrieve a token
    pub fn get(&self, provider: OAuthProvider) -> Option<&OAuthToken> {
        self.tokens.get(&provider)
    }

    /// Remove a token
    pub fn remove(&mut self, provider: OAuthProvider) -> Option<OAuthToken> {
        self.tokens.remove(&provider)
    }

    /// Check if a token exists and is valid
    pub fn is_valid(&self, provider: OAuthProvider) -> bool {
        self.tokens
            .get(&provider)
            .map(|token| !token.is_expired())
            .unwrap_or(false)
    }
}

impl Default for OAuthTokenStore {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_oauth_token_expiry() {
        let token = OAuthToken {
            access_token: "test".to_string(),
            refresh_token: None,
            token_type: "Bearer".to_string(),
            expires_at: Some(
                SystemTime::now()
                    .duration_since(UNIX_EPOCH)
                    .unwrap()
                    .as_secs()
                    + 3600,
            ),
            scope: None,
        };

        assert!(!token.is_expired());
        assert!(!token.expires_soon(Duration::from_secs(1800)));
        assert!(token.expires_soon(Duration::from_secs(7200)));
    }

    #[test]
    fn test_oauth_token_store() {
        let mut store = OAuthTokenStore::new();
        let token = OAuthToken {
            access_token: "test".to_string(),
            refresh_token: None,
            token_type: "Bearer".to_string(),
            expires_at: None,
            scope: None,
        };

        store.store(OAuthProvider::Anthropic, token.clone());
        assert!(store.is_valid(OAuthProvider::Anthropic));
        assert!(!store.is_valid(OAuthProvider::OpenAI));

        let retrieved = store.get(OAuthProvider::Anthropic).unwrap();
        assert_eq!(retrieved.access_token, "test");

        store.remove(OAuthProvider::Anthropic);
        assert!(!store.is_valid(OAuthProvider::Anthropic));
    }
}