ironflow-auth-proxy 0.1.21

Auth proxy for ironflow agent pods: swaps an opaque per-step token for the real Claude credential
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
//! The real proxy router on a real TCP port, relaying to a fake Anthropic API
//! (wiremock) through the real reqwest client.

use std::future::Future;
use std::net::{SocketAddr, TcpListener as StdTcpListener};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use axum::serve;
use reqwest::{Client, Response, StatusCode};
use serde_json::{Value, from_slice, json};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};
use tokio::spawn;
use tokio::time::timeout;
use url::Url;
use wiremock::matchers::{header, header_exists, method, path, query_param};
use wiremock::{Mock, MockServer, Request, ResponseTemplate};

use ironflow_auth_proxy::{AuthProxyConfig, AuthProxyState, router};
use ironflow_core::auth_proxy::{AuthProxyRegistry, CredentialKind, ProxyCredential, TokenRequest};

const ADMIN_KEY: &str = "0123456789abcdef0123456789abcdef";
const OAUTH: &str = "sk-ant-oat01-test";
const API_KEY: &str = "sk-ant-api03-test";

fn now() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_secs()
}

async fn within_timeout(test: impl Future<Output = ()>) {
    timeout(Duration::from_secs(10), test)
        .await
        .expect("test timed out");
}

struct Proxy {
    base: String,
    addr: SocketAddr,
    state: AuthProxyState,
    http: Client,
}

impl Proxy {
    /// Start the proxy relaying to `upstream`, with its own in-memory registry.
    async fn start(upstream: &str) -> Self {
        Self::start_with_registry(upstream, AuthProxyRegistry::default()).await
    }

    /// Start a proxy replica relaying to `upstream` over `registry`.
    async fn start_with_registry(upstream: &str, registry: AuthProxyRegistry) -> Self {
        let config = AuthProxyConfig::new(ADMIN_KEY).with_upstream(Url::parse(upstream).unwrap());
        let state = AuthProxyState::with_registry(config, registry).unwrap();
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        let app = router(state.clone());
        spawn(async move {
            serve(listener, app).await.unwrap();
        });
        Self {
            base: format!("http://{addr}"),
            addr,
            state,
            http: Client::new(),
        }
    }

    /// Issue a token through the admin API.
    async fn issue(&self, kind: CredentialKind, value: &str) -> (String, String) {
        let request = TokenRequest {
            run_id: "run-1".to_string(),
            step: "review".to_string(),
            expires_at: now() + 600,
            credential: ProxyCredential::new(kind, value.to_string()),
        };
        let resp = self
            .http
            .post(format!("{}/admin/v1/tokens", self.base))
            .bearer_auth(ADMIN_KEY)
            .json(&request)
            .send()
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::CREATED);
        let body: Value = resp.json().await.unwrap();
        (
            body["id"].as_str().unwrap().to_string(),
            body["token"].as_str().unwrap().to_string(),
        )
    }

    async fn post_messages(&self, token: &str) -> Response {
        self.http
            .post(format!("{}/v1/messages", self.base))
            .bearer_auth(token)
            .json(&json!({"model": "claude-sonnet-4-5", "messages": []}))
            .send()
            .await
            .unwrap()
    }
}

async fn assert_auth_error(resp: Response, status: StatusCode, kind: &str) {
    assert_eq!(resp.status(), status);
    let body: Value = resp.json().await.unwrap();
    assert_eq!(body["type"], "error");
    assert_eq!(body["error"]["type"], kind);
}

/// An upstream that must never be reached.
async fn silent_upstream() -> MockServer {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .respond_with(ResponseTemplate::new(200))
        .expect(0)
        .mount(&server)
        .await;
    server
}

#[tokio::test]
async fn unknown_token_returns_401() {
    within_timeout(async {
        let upstream = silent_upstream().await;
        let proxy = Proxy::start(&upstream.uri()).await;
        let resp = proxy.post_messages("invalide").await;
        assert_auth_error(resp, StatusCode::UNAUTHORIZED, "authentication_error").await;
    })
    .await;
}

#[tokio::test]
async fn missing_token_returns_401() {
    within_timeout(async {
        let upstream = silent_upstream().await;
        let proxy = Proxy::start(&upstream.uri()).await;
        let resp = proxy
            .http
            .post(format!("{}/v1/messages", proxy.base))
            .body("{}")
            .send()
            .await
            .unwrap();
        assert_auth_error(resp, StatusCode::UNAUTHORIZED, "authentication_error").await;
    })
    .await;
}

#[tokio::test]
async fn expired_token_returns_401() {
    within_timeout(async {
        let upstream = silent_upstream().await;
        let proxy = Proxy::start(&upstream.uri()).await;
        // Issued in the past, expired ten seconds ago.
        let issued_at = now() - 100;
        let request = TokenRequest {
            run_id: "run-1".to_string(),
            step: "review".to_string(),
            expires_at: issued_at + 90,
            credential: ProxyCredential::new(CredentialKind::OauthToken, OAUTH.to_string()),
        };
        let issued = proxy
            .state
            .registry()
            .issue(request, issued_at)
            .await
            .unwrap();
        let resp = proxy.post_messages(&issued.token).await;
        assert_auth_error(resp, StatusCode::UNAUTHORIZED, "authentication_error").await;
        assert!(proxy.state.registry().is_empty().await.unwrap());
    })
    .await;
}

#[tokio::test]
async fn revoked_token_returns_401() {
    within_timeout(async {
        let upstream = silent_upstream().await;
        let proxy = Proxy::start(&upstream.uri()).await;
        let (id, token) = proxy.issue(CredentialKind::OauthToken, OAUTH).await;

        let resp = proxy
            .http
            .delete(format!("{}/admin/v1/tokens/{id}", proxy.base))
            .bearer_auth(ADMIN_KEY)
            .send()
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::NO_CONTENT);
        let again = proxy
            .http
            .delete(format!("{}/admin/v1/tokens/{id}", proxy.base))
            .bearer_auth(ADMIN_KEY)
            .send()
            .await
            .unwrap();
        assert_eq!(again.status(), StatusCode::NOT_FOUND);

        let resp = proxy.post_messages(&token).await;
        assert_auth_error(resp, StatusCode::UNAUTHORIZED, "authentication_error").await;
    })
    .await;
}

#[tokio::test]
async fn token_issued_by_one_replica_is_relayed_by_another() {
    within_timeout(async {
        let upstream = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/messages"))
            .and(header("authorization", format!("Bearer {OAUTH}").as_str()))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({"id": "msg_1"})))
            .expect(1)
            .mount(&upstream)
            .await;
        let registry = AuthProxyRegistry::default();
        let a = Proxy::start_with_registry(&upstream.uri(), registry.clone()).await;
        let b = Proxy::start_with_registry(&upstream.uri(), registry).await;
        assert_ne!(a.addr, b.addr);

        let (id, token) = a.issue(CredentialKind::OauthToken, OAUTH).await;
        let resp = b.post_messages(&token).await;
        assert_eq!(resp.status(), StatusCode::OK);
        let body: Value = resp.json().await.unwrap();
        assert_eq!(body["id"], "msg_1");

        let revoke = b
            .http
            .delete(format!("{}/admin/v1/tokens/{id}", b.base))
            .bearer_auth(ADMIN_KEY)
            .send()
            .await
            .unwrap();
        assert_eq!(revoke.status(), StatusCode::NO_CONTENT);

        let resp = a.post_messages(&token).await;
        assert_auth_error(resp, StatusCode::UNAUTHORIZED, "authentication_error").await;
    })
    .await;
}

#[tokio::test]
async fn revoke_run_drops_every_token_of_the_run() {
    within_timeout(async {
        let upstream = silent_upstream().await;
        let proxy = Proxy::start(&upstream.uri()).await;
        let (_, first) = proxy.issue(CredentialKind::OauthToken, OAUTH).await;
        let (_, second) = proxy.issue(CredentialKind::ApiKey, API_KEY).await;

        let resp = proxy
            .http
            .delete(format!("{}/admin/v1/runs/run-1/tokens", proxy.base))
            .bearer_auth(ADMIN_KEY)
            .send()
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        let body: Value = resp.json().await.unwrap();
        assert_eq!(body["revoked"], 2);

        for token in [first, second] {
            let resp = proxy.post_messages(&token).await;
            assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
        }
    })
    .await;
}

#[tokio::test]
async fn valid_oauth_token_relays_with_real_credential() {
    within_timeout(async {
        let upstream = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/messages"))
            .and(query_param("beta", "true"))
            .and(header("authorization", format!("Bearer {OAUTH}").as_str()))
            .and(header("anthropic-version", "2023-06-01"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({"id": "msg_1"})))
            .expect(1)
            .mount(&upstream)
            .await;
        let proxy = Proxy::start(&upstream.uri()).await;
        let (_, token) = proxy.issue(CredentialKind::OauthToken, OAUTH).await;

        let resp = proxy
            .http
            .post(format!("{}/v1/messages?beta=true", proxy.base))
            .bearer_auth(&token)
            .header("anthropic-version", "2023-06-01")
            .header("anthropic-beta", "claude-code-20250219")
            .json(&json!({"model": "claude-sonnet-4-5", "messages": []}))
            .send()
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        let body: Value = resp.json().await.unwrap();
        assert_eq!(body["id"], "msg_1");

        let received: Vec<Request> = upstream.received_requests().await.unwrap();
        assert_eq!(received.len(), 1);
        let headers = &received[0].headers;
        let beta = headers.get("anthropic-beta").unwrap().to_str().unwrap();
        assert!(beta.contains("oauth-2025-04-20"), "{beta}");
        assert!(beta.contains("claude-code-20250219"), "{beta}");
        assert!(headers.get("x-api-key").is_none());
        for (_, value) in headers {
            let value = value.to_str().unwrap_or_default();
            assert!(!value.contains(&token), "opaque token forwarded upstream");
        }
        let sent: Value = from_slice(&received[0].body).unwrap();
        assert_eq!(sent["model"], "claude-sonnet-4-5");
    })
    .await;
}

#[tokio::test]
async fn valid_api_key_token_relays_with_x_api_key() {
    within_timeout(async {
        let upstream = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/messages/count_tokens"))
            .and(header("x-api-key", API_KEY))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({"input_tokens": 3})))
            .expect(1)
            .mount(&upstream)
            .await;
        let proxy = Proxy::start(&upstream.uri()).await;
        let (_, token) = proxy.issue(CredentialKind::ApiKey, API_KEY).await;

        let resp = proxy
            .http
            .post(format!("{}/v1/messages/count_tokens", proxy.base))
            .header("x-api-key", &token)
            .body("{}")
            .send()
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);

        let received = upstream.received_requests().await.unwrap();
        assert!(received[0].headers.get("authorization").is_none());
    })
    .await;
}

#[tokio::test]
async fn sse_response_is_streamed_with_ratelimit_headers() {
    within_timeout(async {
        let events = concat!(
            "event: message_start\ndata: {\"type\":\"message_start\"}\n\n",
            "event: content_block_delta\ndata: {\"type\":\"content_block_delta\"}\n\n",
            "event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n",
        );
        let upstream = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/messages"))
            .and(header_exists("authorization"))
            .respond_with(
                ResponseTemplate::new(200)
                    .insert_header("anthropic-ratelimit-unified-status", "allowed")
                    .insert_header("anthropic-ratelimit-unified-5h-utilization", "0.42")
                    .insert_header("request-id", "req_123")
                    .set_body_raw(events, "text/event-stream"),
            )
            .mount(&upstream)
            .await;
        let proxy = Proxy::start(&upstream.uri()).await;
        let (_, token) = proxy.issue(CredentialKind::OauthToken, OAUTH).await;

        let resp = proxy.post_messages(&token).await;
        assert_eq!(resp.status(), StatusCode::OK);
        let headers = resp.headers();
        assert_eq!(headers["content-type"], "text/event-stream");
        assert_eq!(headers["anthropic-ratelimit-unified-status"], "allowed");
        assert_eq!(
            headers["anthropic-ratelimit-unified-5h-utilization"],
            "0.42"
        );
        assert_eq!(headers["request-id"], "req_123");
        assert_eq!(resp.text().await.unwrap(), events);
    })
    .await;
}

#[tokio::test]
async fn path_outside_api_returns_403() {
    within_timeout(async {
        let upstream = MockServer::start().await;
        Mock::given(method("GET"))
            .respond_with(ResponseTemplate::new(200))
            .expect(0)
            .mount(&upstream)
            .await;
        let proxy = Proxy::start(&upstream.uri()).await;
        let (_, token) = proxy.issue(CredentialKind::OauthToken, OAUTH).await;

        for target in [
            "/api/oauth/profile",
            "/admin/v1/tokens",
            "/",
            "/v1/%2e%2e/x",
        ] {
            let resp = proxy
                .http
                .get(format!("{}{target}", proxy.base))
                .bearer_auth(&token)
                .send()
                .await
                .unwrap();
            if target == "/admin/v1/tokens" {
                // The admin route exists for POST only: never relayed.
                assert_ne!(resp.status(), StatusCode::OK, "{target}");
                continue;
            }
            assert_auth_error(resp, StatusCode::FORBIDDEN, "permission_error").await;
        }
    })
    .await;
}

#[tokio::test]
async fn absolute_form_request_to_other_host_returns_403() {
    within_timeout(async {
        let upstream = silent_upstream().await;
        let proxy = Proxy::start(&upstream.uri()).await;
        let (_, token) = proxy.issue(CredentialKind::OauthToken, OAUTH).await;

        let mut stream = TcpStream::connect(proxy.addr).await.unwrap();
        let request = format!(
            "GET http://example.com/v1/messages HTTP/1.1\r\nHost: example.com\r\n\
             Authorization: Bearer {token}\r\nConnection: close\r\n\r\n"
        );
        stream.write_all(request.as_bytes()).await.unwrap();
        let mut raw = Vec::new();
        stream.read_to_end(&mut raw).await.unwrap();
        let answer = String::from_utf8_lossy(&raw);
        assert!(answer.starts_with("HTTP/1.1 403"), "{answer}");
        assert!(answer.contains("permission_error"), "{answer}");
    })
    .await;
}

#[tokio::test]
async fn put_method_returns_405() {
    within_timeout(async {
        let upstream = silent_upstream().await;
        let proxy = Proxy::start(&upstream.uri()).await;
        let (_, token) = proxy.issue(CredentialKind::OauthToken, OAUTH).await;

        let resp = proxy
            .http
            .put(format!("{}/v1/messages", proxy.base))
            .bearer_auth(&token)
            .body("{}")
            .send()
            .await
            .unwrap();
        assert_auth_error(
            resp,
            StatusCode::METHOD_NOT_ALLOWED,
            "invalid_request_error",
        )
        .await;
    })
    .await;
}

#[tokio::test]
async fn upstream_unreachable_returns_502() {
    within_timeout(async {
        // Bind then drop: nothing listens on the port any more.
        let port = {
            let listener = StdTcpListener::bind("127.0.0.1:0").unwrap();
            listener.local_addr().unwrap().port()
        };
        let proxy = Proxy::start(&format!("http://127.0.0.1:{port}")).await;
        let (_, token) = proxy.issue(CredentialKind::OauthToken, OAUTH).await;

        let resp = proxy.post_messages(&token).await;
        assert_auth_error(resp, StatusCode::BAD_GATEWAY, "api_error").await;
    })
    .await;
}

#[tokio::test]
async fn healthz_returns_ok() {
    within_timeout(async {
        let upstream = silent_upstream().await;
        let proxy = Proxy::start(&upstream.uri()).await;
        let resp = proxy
            .http
            .get(format!("{}/healthz", proxy.base))
            .send()
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        assert_eq!(resp.text().await.unwrap(), "ok");
    })
    .await;
}

#[tokio::test]
async fn admin_without_key_returns_401() {
    within_timeout(async {
        let upstream = silent_upstream().await;
        let proxy = Proxy::start(&upstream.uri()).await;
        let body = json!({
            "run_id": "run-1",
            "step": "review",
            "expires_at": now() + 600,
            "credential": {"kind": "oauth_token", "value": OAUTH}
        });

        let missing = proxy
            .http
            .post(format!("{}/admin/v1/tokens", proxy.base))
            .json(&body)
            .send()
            .await
            .unwrap();
        assert_auth_error(missing, StatusCode::UNAUTHORIZED, "authentication_error").await;

        let wrong = proxy
            .http
            .post(format!("{}/admin/v1/tokens", proxy.base))
            .bearer_auth("ifap_not_the_admin_key_0123456789abcdef")
            .json(&body)
            .send()
            .await
            .unwrap();
        assert_auth_error(wrong, StatusCode::UNAUTHORIZED, "authentication_error").await;

        let revoke = proxy
            .http
            .delete(format!("{}/admin/v1/runs/run-1/tokens", proxy.base))
            .send()
            .await
            .unwrap();
        assert_eq!(revoke.status(), StatusCode::UNAUTHORIZED);
        assert!(proxy.state.registry().is_empty().await.unwrap());
    })
    .await;
}

#[tokio::test]
async fn admin_issue_rejects_past_expiry_with_400() {
    within_timeout(async {
        let upstream = silent_upstream().await;
        let proxy = Proxy::start(&upstream.uri()).await;
        let past = json!({
            "run_id": "run-1",
            "step": "review",
            "expires_at": now() - 1,
            "credential": {"kind": "oauth_token", "value": OAUTH}
        });
        let resp = proxy
            .http
            .post(format!("{}/admin/v1/tokens", proxy.base))
            .bearer_auth(ADMIN_KEY)
            .json(&past)
            .send()
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
        let body: Value = resp.json().await.unwrap();
        assert_eq!(body["error"]["type"], "invalid_request_error");
        assert_eq!(body["error"]["message"], "expires_at is in the past");

        let garbage = proxy
            .http
            .post(format!("{}/admin/v1/tokens", proxy.base))
            .bearer_auth(ADMIN_KEY)
            .body(format!("{{\"credential\": \"{OAUTH}\"}}"))
            .send()
            .await
            .unwrap();
        assert_eq!(garbage.status(), StatusCode::BAD_REQUEST);
        let text = garbage.text().await.unwrap();
        assert!(!text.contains(OAUTH), "{text}");
        assert!(proxy.state.registry().is_empty().await.unwrap());
    })
    .await;
}