rattler_networking 0.30.6

Authenticated requests in the conda ecosystem
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
//! Refresh logic for `Authentication::OAuth` credentials.

use std::fmt;

use serde::Deserialize;

use crate::{Authentication, AuthenticationStorage};

/// Standard OAuth 2.0 token response.
#[derive(Deserialize)]
struct TokenRefreshResponse {
    access_token: String,
    refresh_token: Option<String>,
    expires_in: Option<i64>,
}

/// Number of seconds before `expires_at` at which a token is considered
/// "about to expire"
const EXPIRY_SKEW_SECONDS: i64 = 300;

/// Maximum time to wait for the token endpoint to respond to a refresh request.
/// Without this, a hung authorization server would stall the request that
/// triggered the refresh indefinitely.
const REFRESH_REQUEST_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30);

fn now_unix_timestamp() -> i64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs() as i64
}

fn oauth_expires_at(auth: &Authentication) -> Option<i64> {
    match auth {
        Authentication::OAuth { expires_at, .. } => *expires_at,
        _ => None,
    }
}

fn needs_refresh(auth: &Authentication) -> bool {
    oauth_expires_at(auth).is_some_and(|exp| exp - now_unix_timestamp() < EXPIRY_SKEW_SECONDS)
}

fn is_expired(auth: &Authentication) -> bool {
    oauth_expires_at(auth).is_some_and(|exp| exp <= now_unix_timestamp())
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum OAuthRefreshFailure {
    MissingRefreshToken,
    ReauthenticationRequired { reason: String },
    Transient { reason: String },
    InvalidResponse { reason: String },
}

impl fmt::Display for OAuthRefreshFailure {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            OAuthRefreshFailure::MissingRefreshToken => {
                write!(
                    f,
                    "OAuth token is expired but no refresh token is available"
                )
            }
            OAuthRefreshFailure::ReauthenticationRequired { reason }
            | OAuthRefreshFailure::Transient { reason }
            | OAuthRefreshFailure::InvalidResponse { reason } => write!(f, "{reason}"),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct OAuthRefreshOutcome {
    authentication: Option<Authentication>,
    failure: Option<OAuthRefreshFailure>,
}

impl OAuthRefreshOutcome {
    pub(crate) fn into_authentication(self) -> Option<Authentication> {
        self.authentication
    }

    pub(crate) fn failure(&self) -> Option<&OAuthRefreshFailure> {
        self.failure.as_ref()
    }
}

fn auth_outcome(auth: Authentication) -> OAuthRefreshOutcome {
    OAuthRefreshOutcome {
        authentication: Some(auth),
        failure: None,
    }
}

fn stale_auth_fallback(auth: Authentication, failure: OAuthRefreshFailure) -> OAuthRefreshOutcome {
    if is_expired(&auth) {
        tracing::warn!(
            "OAuth access token is expired and could not be refreshed; not attaching it"
        );
        OAuthRefreshOutcome {
            authentication: None,
            failure: Some(failure),
        }
    } else {
        OAuthRefreshOutcome {
            authentication: Some(auth),
            failure: Some(failure),
        }
    }
}

/// Refresh an OAuth token if it is expired (or close to expiring) and
/// store the refreshed credential back to `storage`.
pub(crate) async fn maybe_refresh_oauth(
    storage: &AuthenticationStorage,
    auth: Authentication,
    matched_key: &str,
) -> OAuthRefreshOutcome {
    if !matches!(auth, Authentication::OAuth { .. }) {
        return auth_outcome(auth);
    }

    if !needs_refresh(&auth) {
        return auth_outcome(auth);
    }

    let refresh_lock = storage.oauth_refresh_lock(matched_key);
    let _refresh_guard = refresh_lock.lock().await;

    // Another request may have refreshed and stored credentials for this key
    // while we were waiting for the per-key refresh lock. Re-read storage so we
    // don't reuse a refresh token that has already been rotated.
    let auth = match storage.get(matched_key) {
        Ok(Some(current_auth)) => {
            if !matches!(current_auth, Authentication::OAuth { .. })
                || !needs_refresh(&current_auth)
            {
                return auth_outcome(current_auth);
            }
            current_auth
        }
        Ok(None) => auth,
        Err(e) => {
            tracing::warn!("Failed to re-read OAuth credentials before refresh: {e}");
            auth
        }
    };

    let Authentication::OAuth {
        access_token: _,
        ref refresh_token,
        expires_at: _,
        ref token_endpoint,
        ref revocation_endpoint,
        ref client_id,
    } = auth
    else {
        return auth_outcome(auth);
    };

    let Some(refresh_token_val) = refresh_token.as_deref() else {
        let failure = OAuthRefreshFailure::MissingRefreshToken;
        tracing::warn!("{failure}");
        return stale_auth_fallback(auth, failure);
    };

    tracing::debug!("OAuth token expired, attempting refresh");

    let client = reqwest::Client::new();
    let params = [
        ("grant_type", "refresh_token"),
        ("refresh_token", refresh_token_val),
        ("client_id", client_id),
    ];

    let response = match client
        .post(token_endpoint.as_str())
        .form(&params)
        .timeout(REFRESH_REQUEST_TIMEOUT)
        .send()
        .await
    {
        Ok(resp) => resp,
        Err(e) => {
            let failure = OAuthRefreshFailure::Transient {
                reason: format!("Failed to refresh OAuth token: {e}"),
            };
            tracing::warn!("{failure}");
            return stale_auth_fallback(auth, failure);
        }
    };

    if !response.status().is_success() {
        let status = response.status();
        let failure = match response.json::<serde_json::Value>().await {
            Ok(body) => {
                let error_code = body
                    .get("error")
                    .and_then(|v| v.as_str())
                    .unwrap_or("unknown");
                if error_code == "invalid_grant" {
                    OAuthRefreshFailure::ReauthenticationRequired {
                        reason: "refresh token is expired or revoked — please re-authenticate"
                            .to_string(),
                    }
                } else {
                    OAuthRefreshFailure::Transient {
                        reason: format!("OAuth token refresh failed with error code: {error_code}"),
                    }
                }
            }
            Err(_) => OAuthRefreshFailure::Transient {
                reason: format!("OAuth token refresh failed with HTTP {status}"),
            },
        };
        tracing::warn!("OAuth token refresh failed ({failure})");
        return stale_auth_fallback(auth, failure);
    }

    let token_response: TokenRefreshResponse = match response.json().await {
        Ok(body) => body,
        Err(e) => {
            let failure = OAuthRefreshFailure::InvalidResponse {
                reason: format!("Failed to read OAuth refresh response body: {e}"),
            };
            tracing::warn!("{failure}");
            return stale_auth_fallback(auth, failure);
        }
    };

    let new_expires_at = token_response.expires_in.map(|secs| {
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs() as i64
            + secs
    });

    let refreshed = Authentication::OAuth {
        access_token: token_response.access_token,
        refresh_token: token_response
            .refresh_token
            .or_else(|| refresh_token.clone()),
        expires_at: new_expires_at,
        token_endpoint: token_endpoint.clone(),
        revocation_endpoint: revocation_endpoint.clone(),
        client_id: client_id.clone(),
    };

    if let Err(e) = storage.store(matched_key, &refreshed) {
        tracing::warn!("Failed to store refreshed OAuth token: {e}");
    }

    auth_outcome(refreshed)
}

#[cfg(test)]
mod tests {
    use std::{
        collections::HashMap,
        sync::{
            Arc, Mutex,
            atomic::{AtomicUsize, Ordering},
        },
    };

    use axum::{Json, Router, extract::Form, http::StatusCode, routing::post};
    use futures::future::join_all;
    use serde_json::json;

    use super::*;
    use crate::authentication_storage::backends::memory::MemoryStorage;

    fn expired_oauth(token_endpoint: String) -> Authentication {
        Authentication::OAuth {
            access_token: "expired-access-token".to_string(),
            refresh_token: Some("refresh-token".to_string()),
            expires_at: Some(now_unix_timestamp() - 60),
            token_endpoint,
            revocation_endpoint: None,
            client_id: "client-id".to_string(),
        }
    }

    /// An OAuth token inside the refresh skew window (so `needs_refresh` is true)
    /// but not yet expired (so `is_expired` is false).
    fn near_expiry_oauth(token_endpoint: String) -> Authentication {
        Authentication::OAuth {
            access_token: "near-expiry-access-token".to_string(),
            refresh_token: Some("refresh-token".to_string()),
            expires_at: Some(now_unix_timestamp() + 60),
            token_endpoint,
            revocation_endpoint: None,
            client_id: "client-id".to_string(),
        }
    }

    fn auth_storage(host: &str, auth: &Authentication) -> AuthenticationStorage {
        let mut storage = AuthenticationStorage::empty();
        storage.add_backend(Arc::new(MemoryStorage::new()));
        storage.store(host, auth).unwrap();
        storage
    }

    /// Spawn a token endpoint whose handler receives the posted form parameters,
    /// so tests can assert on (and react to) the `refresh_token` that was sent.
    async fn spawn_token_endpoint_with_form(
        handler: impl Fn(HashMap<String, String>) -> (StatusCode, Json<serde_json::Value>)
        + Clone
        + Send
        + Sync
        + 'static,
    ) -> String {
        let router = Router::new().route(
            "/token",
            post({
                let handler = handler.clone();
                move |Form(form): Form<HashMap<String, String>>| {
                    let handler = handler.clone();
                    async move { handler(form) }
                }
            }),
        );
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(async move { axum::serve(listener, router).await.unwrap() });
        format!("http://{addr}/token")
    }

    async fn spawn_token_endpoint(
        handler: impl Fn() -> (StatusCode, Json<serde_json::Value>) + Clone + Send + Sync + 'static,
    ) -> String {
        let router = Router::new().route(
            "/token",
            post({
                let handler = handler.clone();
                move || {
                    let handler = handler.clone();
                    async move { handler() }
                }
            }),
        );
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(async move { axum::serve(listener, router).await.unwrap() });
        format!("http://{addr}/token")
    }

    #[tokio::test]
    async fn concurrent_refresh_is_single_flight() {
        let refresh_count = Arc::new(AtomicUsize::new(0));
        let token_endpoint = spawn_token_endpoint({
            let refresh_count = refresh_count.clone();
            move || {
                refresh_count.fetch_add(1, Ordering::SeqCst);
                (
                    StatusCode::OK,
                    Json(json!({
                        "access_token": "fresh-access-token",
                        "refresh_token": "rotated-refresh-token",
                        "expires_in": 3600,
                    })),
                )
            }
        })
        .await;

        let host = "repo.prefix.dev";
        let expired = expired_oauth(token_endpoint);
        let storage = auth_storage(host, &expired);

        let results =
            join_all((0..8).map(|_| maybe_refresh_oauth(&storage, expired.clone(), host))).await;

        assert_eq!(refresh_count.load(Ordering::SeqCst), 1);
        for result in results {
            assert_eq!(result.failure, None);
            assert!(matches!(
                result.authentication,
                Some(Authentication::OAuth { access_token, .. }) if access_token == "fresh-access-token"
            ));
        }
    }

    #[tokio::test]
    async fn expired_token_is_not_returned_after_refresh_failure() {
        let token_endpoint = spawn_token_endpoint(|| {
            (
                StatusCode::BAD_REQUEST,
                Json(json!({ "error": "invalid_grant" })),
            )
        })
        .await;

        let host = "repo.prefix.dev";
        let expired = expired_oauth(token_endpoint);
        let storage = auth_storage(host, &expired);

        let result = maybe_refresh_oauth(&storage, expired, host).await;

        assert_eq!(result.authentication, None);
        assert!(matches!(
            result.failure,
            Some(OAuthRefreshFailure::ReauthenticationRequired { .. })
        ));
    }

    /// The core property the coalescing gate protects: with refresh-token
    /// rotation enabled, concurrent refreshes must never send a stale refresh
    /// token to the authorization server. The endpoint here rotates its single
    /// valid refresh token on every successful refresh and rejects any other
    /// token with `invalid_grant` — exactly how a rotating server behaves. If
    /// two requests both refreshed (the bug), the second would arrive with the
    /// now-stale original token and be rejected.
    #[tokio::test]
    async fn concurrent_refresh_never_reuses_rotated_token() {
        let valid_refresh_token = Arc::new(Mutex::new("refresh-token".to_string()));
        let success_count = Arc::new(AtomicUsize::new(0));
        let invalid_grant_count = Arc::new(AtomicUsize::new(0));

        let token_endpoint = spawn_token_endpoint_with_form({
            let valid_refresh_token = valid_refresh_token.clone();
            let success_count = success_count.clone();
            let invalid_grant_count = invalid_grant_count.clone();
            move |form| {
                let presented = form.get("refresh_token").cloned().unwrap_or_default();
                let mut valid = valid_refresh_token.lock().unwrap();
                if presented == *valid {
                    // Rotate: the presented token is now spent.
                    let rotated =
                        format!("rotated-{}", success_count.fetch_add(1, Ordering::SeqCst));
                    *valid = rotated.clone();
                    (
                        StatusCode::OK,
                        Json(json!({
                            "access_token": "fresh-access-token",
                            "refresh_token": rotated,
                            "expires_in": 3600,
                        })),
                    )
                } else {
                    invalid_grant_count.fetch_add(1, Ordering::SeqCst);
                    (
                        StatusCode::BAD_REQUEST,
                        Json(json!({ "error": "invalid_grant" })),
                    )
                }
            }
        })
        .await;

        let host = "repo.prefix.dev";
        let expired = expired_oauth(token_endpoint);
        let storage = auth_storage(host, &expired);

        let results =
            join_all((0..8).map(|_| maybe_refresh_oauth(&storage, expired.clone(), host))).await;

        // Exactly one refresh hit the server and no stale token was ever presented.
        assert_eq!(success_count.load(Ordering::SeqCst), 1);
        assert_eq!(invalid_grant_count.load(Ordering::SeqCst), 0);
        for result in results {
            assert_eq!(result.failure, None);
            assert!(matches!(
                result.authentication,
                Some(Authentication::OAuth { access_token, .. }) if access_token == "fresh-access-token"
            ));
        }
    }

    #[tokio::test]
    async fn missing_refresh_token_yields_unauthenticated() {
        let host = "repo.prefix.dev";
        let expired = Authentication::OAuth {
            access_token: "expired-access-token".to_string(),
            refresh_token: None,
            expires_at: Some(now_unix_timestamp() - 60),
            token_endpoint: "http://127.0.0.1:0/token".to_string(),
            revocation_endpoint: None,
            client_id: "client-id".to_string(),
        };
        let storage = auth_storage(host, &expired);

        let result = maybe_refresh_oauth(&storage, expired, host).await;

        assert_eq!(result.authentication, None);
        assert!(matches!(
            result.failure,
            Some(OAuthRefreshFailure::MissingRefreshToken)
        ));
    }

    #[tokio::test]
    async fn transient_failure_on_expired_token_yields_unauthenticated() {
        let token_endpoint = spawn_token_endpoint(|| {
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(json!({ "error": "server_error" })),
            )
        })
        .await;

        let host = "repo.prefix.dev";
        let expired = expired_oauth(token_endpoint);
        let storage = auth_storage(host, &expired);

        let result = maybe_refresh_oauth(&storage, expired, host).await;

        // The token is expired, so a transient failure must not attach it.
        assert_eq!(result.authentication, None);
        assert!(matches!(
            result.failure,
            Some(OAuthRefreshFailure::Transient { .. })
        ));
    }

    #[tokio::test]
    async fn transient_failure_keeps_unexpired_token() {
        let token_endpoint = spawn_token_endpoint(|| {
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(json!({ "error": "server_error" })),
            )
        })
        .await;

        let host = "repo.prefix.dev";
        let near_expiry = near_expiry_oauth(token_endpoint);
        let storage = auth_storage(host, &near_expiry);

        let result = maybe_refresh_oauth(&storage, near_expiry, host).await;

        // The token is within the refresh-skew window but not yet expired, so a
        // transient failure should still attach the existing token and surface
        // the failure as non-fatal.
        assert!(matches!(
            result.authentication,
            Some(Authentication::OAuth { access_token, .. }) if access_token == "near-expiry-access-token"
        ));
        assert!(matches!(
            result.failure,
            Some(OAuthRefreshFailure::Transient { .. })
        ));
    }
}