vgi-forge-github 0.12.2

GitHub adapter for VGI git namespaces: per-community GitHub App auth, repo creation and bootstrap, role projection, device-flow account linking and verified webhooks.
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
//! App JWT, manifest registration, namespace binding and device-flow
//! account linking, against a mock GitHub.

mod common;

use std::collections::BTreeMap;

use common::*;
use serde_json::json;
use url::Url;
use vgi_forge::{
    BindCallback, BindRequest, BindStep, Forge, ForgeError, LinkCallback, LinkStep, NamespaceKind,
    Resource,
};
use vgi_forge_github::jwt::{AppClaims, app_jwt, app_jwt_at};
use vgi_forge_github::manifest::{
    APP_EVENTS, APP_PERMISSIONS, ManifestParams, app_manifest, exchange_code, registration_url,
};
use vgi_forge_github::{GitHubForge, InProcessKey};
use wiremock::matchers::{body_json, header, method, path};
use wiremock::{Mock, ResponseTemplate};

// ── JWT ──────────────────────────────────────────────────────────────────

#[tokio::test]
async fn app_jwt_is_rs256_backdated_and_short_lived() {
    let signer = InProcessKey::from_pem(&key().pkcs1_pem).unwrap();
    let now = 1_700_000_000;
    let jwt = app_jwt_at(&signer, CLIENT_ID, now).await.unwrap();
    let claims = verify_jwt(jwt.expose()).expect("signature verifies with the App's public key");
    assert_eq!(
        claims,
        serde_json::to_value(AppClaims::at(now, CLIENT_ID)).unwrap()
    );
    assert_eq!(claims["iat"], now - 60, "iat is backdated for clock drift");
    assert_eq!(claims["exp"], now + 540, "exp is nine minutes out");
    assert!(
        claims["exp"].as_u64().unwrap() - now <= 600,
        "GitHub caps exp at ten minutes"
    );
    assert_eq!(claims["iss"], CLIENT_ID);
}

#[tokio::test]
async fn both_pem_shapes_sign_verifiably() {
    for pem in [&key().pkcs1_pem, &key().pkcs8_pem] {
        let signer = InProcessKey::from_pem(pem).unwrap();
        let jwt = app_jwt(&signer, CLIENT_ID).await.unwrap();
        assert!(verify_jwt(jwt.expose()).is_some());
        assert_eq!(format!("{signer:?}"), "InProcessKey(<redacted>)");
        assert_eq!(format!("{jwt:?}"), "Secret(<redacted>)");
    }
}

// ── manifest ─────────────────────────────────────────────────────────────

fn params() -> ManifestParams {
    ManifestParams::new(
        "acme-vgi-bridge",
        "https://vtc.acme.example",
        "https://bridge.acme.example/webhook",
        "https://bridge.acme.example/manifest/callback",
    )
    .with_setup_url("https://bridge.acme.example/bind/callback")
}

#[test]
fn manifest_asks_for_exactly_the_reviewed_permissions() {
    let m = app_manifest(&params());
    assert_eq!(
        m["default_permissions"],
        json!({
            "administration": "write",
            "contents": "write",
            "actions_variables": "write",
            "metadata": "read",
            "members": "read",
            // §9: the org ruleset that makes verify-trust a required workflow.
            "organization_administration": "write",
            // §9: the check the bridge posts itself where there is none, and
            // the reads its trigger events need.
            "checks": "write",
            "pull_requests": "read",
            "merge_queues": "read",
        })
    );
    assert_eq!(m["public"], false);
    assert_eq!(
        m["hook_attributes"],
        json!({ "url": "https://bridge.acme.example/webhook", "active": true })
    );
    assert_eq!(
        m["redirect_url"],
        "https://bridge.acme.example/manifest/callback"
    );
    assert_eq!(
        m["callback_urls"],
        json!(["https://bridge.acme.example/manifest/callback"])
    );
    assert_eq!(m["setup_url"], "https://bridge.acme.example/bind/callback");
    assert_eq!(m["default_events"], json!(APP_EVENTS));
    for event in [
        "repository",
        "member",
        "membership",
        "organization",
        "repository_ruleset",
        "branch_protection_rule",
    ] {
        assert!(APP_EVENTS.contains(&event), "{event} feeds drift detection");
    }
    for event in ["pull_request", "merge_group", "check_run", "check_suite"] {
        assert!(
            APP_EVENTS.contains(&event),
            "{event} triggers the bridge's check"
        );
    }
    assert!(
        APP_EVENTS.is_sorted(),
        "exchange_code compares a sorted list"
    );
    assert_eq!(APP_PERMISSIONS.len(), 9, "no permission beyond §5.7's set");
}

#[test]
fn registration_url_targets_the_org_or_the_user() {
    let web = Url::parse("https://github.com").unwrap();
    assert_eq!(
        registration_url(&web, Some("acme"), "s1").as_str(),
        "https://github.com/organizations/acme/settings/apps/new?state=s1"
    );
    assert_eq!(
        registration_url(&web, None, "s1").as_str(),
        "https://github.com/settings/apps/new?state=s1"
    );
}

fn conversion(permissions: serde_json::Value) -> serde_json::Value {
    json!({
        "id": 1001,
        "slug": "acme-vgi-bridge",
        "owner": { "login": "acme" },
        "client_id": CLIENT_ID,
        "client_secret": "cs-secret-value",
        "webhook_secret": "wh-secret-value",
        "pem": key().pkcs1_pem,
        "permissions": permissions,
        "events": APP_EVENTS,
    })
}

#[tokio::test]
async fn manifest_code_exchange_returns_credentials_that_never_print() {
    let server = wiremock::MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/app-manifests/abc123/conversions"))
        // Bodyless, but it must still say so: GitHub answers 411 otherwise.
        .and(header("content-length", "0"))
        .respond_with(ResponseTemplate::new(201).set_body_json(conversion(json!({
            "administration": "write", "contents": "write", "actions_variables": "write",
            "checks": "write",
            "metadata": "read", "members": "read", "organization_administration": "write",
        }))))
        .expect(1)
        .mount(&server)
        .await;
    let base = Url::parse(&server.uri()).unwrap();
    let creds = exchange_code(&base, "abc123", "acme").await.unwrap();
    assert_eq!(creds.app_id, 1001);
    assert_eq!(creds.slug, "acme-vgi-bridge");
    assert_eq!(creds.client_id, CLIENT_ID);
    assert_eq!(creds.webhook_secret.expose(), "wh-secret-value");
    assert_eq!(creds.owner_login.as_deref(), Some("acme"));
    // The returned key is usable as the App's signer.
    InProcessKey::from_secret(&creds.pem).unwrap();

    let printed = format!("{creds:?}");
    for secret in ["cs-secret-value", "wh-secret-value", "PRIVATE KEY"] {
        assert!(
            !printed.contains(secret),
            "Debug leaked {secret}: {printed}"
        );
    }
}

#[tokio::test]
async fn manifest_exchange_refuses_an_app_with_more_than_the_reviewed_permissions() {
    let server = wiremock::MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/app-manifests/abc123/conversions"))
        .respond_with(ResponseTemplate::new(201).set_body_json(conversion(json!({
            "administration": "write", "contents": "write", "actions_variables": "write",
            "checks": "write",
            "metadata": "read", "members": "write", "secrets": "write",
            "organization_administration": "write", "organization_secrets": "write",
        }))))
        .mount(&server)
        .await;
    let base = Url::parse(&server.uri()).unwrap();
    let err = exchange_code(&base, "abc123", "acme")
        .await
        .unwrap_err()
        .to_string();
    assert!(
        err.contains("members:write")
            && err.contains("secrets:write")
            && err.contains("organization_secrets:write")
            && !err.contains("organization_administration"),
        "{err}"
    );
    assert!(
        exchange_code(&base, "../x", "acme").await.is_err(),
        "code must be alphanumeric"
    );
}

// ── bind ─────────────────────────────────────────────────────────────────

fn callback(state: &str, installation: &str, setup: &str) -> BTreeMap<String, String> {
    BTreeMap::from([
        ("state".to_string(), state.to_string()),
        ("installation_id".to_string(), installation.to_string()),
        ("setup_action".to_string(), setup.to_string()),
    ])
}

async fn mount_installation(server: &wiremock::MockServer, id: u64, login: &str, kind: &str) {
    Mock::given(method("GET"))
        .and(path(format!("/app/installations/{id}")))
        .and(ValidAppJwt)
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "id": id,
            "account": { "id": 500, "login": login, "type": kind },
            "permissions": {
                "administration": "write", "contents": "write", "actions_variables": "write",
            "checks": "write",
                "metadata": "read",
            },
            "suspended_at": null,
        })))
        .mount(server)
        .await;
}

#[tokio::test]
async fn bind_sends_the_admin_to_the_install_page_with_state() {
    let (_server, forge) = server_and_forge().await;
    let state = GitHubForge::new_state().unwrap();
    assert!(state.len() >= 43);
    let BindStep::Redirect { url } = forge
        .begin_bind(BindRequest::new(
            Resource::parse("github.com/newco").unwrap(),
            state.clone(),
        ))
        .await
        .unwrap()
    else {
        panic!("expected a redirect");
    };
    assert!(
        url.contains("/apps/acme-vgi/installations/new?state="),
        "{url}"
    );
    assert!(url.ends_with(&state));

    // A short or guessable state is refused before anyone is redirected.
    assert!(
        forge
            .begin_bind(BindRequest::new(
                Resource::parse("github.com/newco").unwrap(),
                "abc"
            ))
            .await
            .is_err()
    );
    // A resource on another forge is refused.
    assert!(
        forge
            .begin_bind(BindRequest::new(
                Resource::parse("codeberg.org/newco").unwrap(),
                state
            ))
            .await
            .is_err()
    );
}

#[tokio::test]
async fn complete_bind_records_installation_owner_and_kind() {
    let (server, forge) = server_and_forge().await;
    mount_installation(&server, 77, "NewCo", "Organization").await;
    // The bind also asks whether the org has org rulesets (§9): a token
    // with organization Administration only, and no repository.
    mount_token(
        &server,
        77,
        None,
        json!({ "organization_administration": "write" }),
        1,
    )
    .await;
    Mock::given(method("GET"))
        .and(path("/orgs/newco/rulesets"))
        .and(InstallationToken)
        .respond_with(ResponseTemplate::new(200).set_body_json(json!([])))
        .expect(1)
        .mount(&server)
        .await;
    let state = GitHubForge::new_state().unwrap();
    let ns = Resource::parse("github.com/newco").unwrap();
    let binding = forge
        .complete_bind(BindCallback::new(
            callback(&state, "77", "install"),
            state.clone(),
            ns.clone(),
        ))
        .await
        .unwrap();
    assert_eq!(binding.namespace.resource, ns);
    assert_eq!(binding.namespace.installation_id, Some(77));
    assert_eq!(binding.namespace.owner_id, Some(500));
    assert_eq!(binding.namespace.kind, NamespaceKind::Organization);
    // The mock installation lacks the members and org administration
    // permissions.
    // and everything the bridge-posted check needs, events included.
    assert_eq!(
        binding.missing_permissions,
        vec![
            "members:read".to_string(),
            "merge_queues:read".to_string(),
            "organization_administration:write".to_string(),
            "pull_requests:read".to_string(),
            "event:merge_group".to_string(),
            "event:pull_request".to_string(),
        ]
    );
    assert_eq!(forge.bridge_checks_ready(&ns), Some(false));
    forge.register_namespace(binding.namespace.clone()).unwrap();
    assert!(forge.capabilities(&binding.namespace).required_workflow);
    // Handed back as data for the bridge to persist, too.
    assert!(
        binding
            .capabilities
            .as_ref()
            .is_some_and(|c| c.required_workflow)
    );
}

#[tokio::test]
async fn complete_bind_rejects_wrong_state_owner_or_unapproved_installs() {
    let (server, forge) = server_and_forge().await;
    mount_installation(&server, 77, "someone-else", "User").await;
    Mock::given(method("GET"))
        .and(path("/app/installations/78"))
        .respond_with(ResponseTemplate::new(404).set_body_json(json!({ "message": "Not Found" })))
        .mount(&server)
        .await;
    let state = GitHubForge::new_state().unwrap();
    let ns = Resource::parse("github.com/newco").unwrap();
    let bind = |params, expected: &str| {
        forge.complete_bind(BindCallback::new(params, expected.to_string(), ns.clone()))
    };

    let other = GitHubForge::new_state().unwrap();
    let e = bind(callback(&other, "77", "install"), &state)
        .await
        .unwrap_err();
    assert!(
        matches!(e, ForgeError::BindRejected(ref m) if m.contains("state")),
        "{e}"
    );

    let e = bind(callback(&state, "77", "install"), &state)
        .await
        .unwrap_err();
    assert!(
        matches!(e, ForgeError::BindRejected(ref m) if m.contains("someone-else")),
        "{e}"
    );

    let e = bind(callback(&state, "78", "install"), &state)
        .await
        .unwrap_err();
    assert!(
        matches!(e, ForgeError::BindRejected(ref m) if m.contains("not an installation")),
        "{e}"
    );

    let e = bind(callback(&state, "77", "request"), &state)
        .await
        .unwrap_err();
    assert!(
        matches!(e, ForgeError::BindRejected(ref m) if m.contains("approved")),
        "{e}"
    );

    let e = bind(callback(&state, "x", "install"), &state)
        .await
        .unwrap_err();
    assert!(matches!(e, ForgeError::BindRejected(_)), "{e}");
}

// ── device flow ──────────────────────────────────────────────────────────

async fn mount_device_code(server: &wiremock::MockServer) {
    Mock::given(method("POST"))
        .and(path("/login/device/code"))
        .and(header("accept", "application/json"))
        .and(body_json(json!({ "client_id": CLIENT_ID })))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "device_code": "dev-code-1",
            "user_code": "WDJB-MJHT",
            "verification_uri": "https://github.com/login/device",
            "expires_in": 900,
            "interval": 5,
        })))
        .expect(1)
        .mount(server)
        .await;
}

fn poll_reply(body: serde_json::Value) -> ResponseTemplate {
    ResponseTemplate::new(200).set_body_json(body)
}

#[tokio::test]
async fn device_flow_polls_through_pending_and_slow_down_then_reads_the_user() {
    let (server, forge) = server_and_forge().await;
    mount_device_code(&server).await;
    let token_path = || path("/login/oauth/access_token");
    let grant = || {
        body_json(json!({
            "client_id": CLIENT_ID,
            "device_code": "dev-code-1",
            "grant_type": "urn:ietf:params:oauth:grant-type:device_code",
        }))
    };
    // Wiremock answers with the first matching mock that still has uses
    // left, so these play in order.
    Mock::given(method("POST"))
        .and(token_path())
        .and(grant())
        .respond_with(poll_reply(json!({ "error": "authorization_pending" })))
        .up_to_n_times(1)
        .expect(1)
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(token_path())
        .and(grant())
        .respond_with(poll_reply(json!({ "error": "slow_down", "interval": 10 })))
        .up_to_n_times(1)
        .expect(1)
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(token_path())
        .and(grant())
        .respond_with(poll_reply(
            json!({ "access_token": "ghu_user_token", "token_type": "bearer" }),
        ))
        .expect(1)
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .and(path("/user"))
        .and(header("authorization", "Bearer ghu_user_token"))
        .respond_with(
            ResponseTemplate::new(200).set_body_json(json!({ "id": 583231, "login": "Octocat" })),
        )
        .expect(1)
        .mount(&server)
        .await;

    let step = forge.begin_account_link("did:webvh:alice").await.unwrap();
    let LinkStep::DeviceCode {
        user_code,
        verification_uri,
        ..
    } = &step
    else {
        panic!("expected a device code");
    };
    assert_eq!(user_code, "WDJB-MJHT");
    assert_eq!(verification_uri, "https://github.com/login/device");

    let account = forge
        .complete_account_link(LinkCallback::from_device_step(&step).unwrap())
        .await
        .unwrap();
    assert_eq!(account.id, 583231);
    assert_eq!(account.login, "Octocat");
}

#[tokio::test]
async fn device_flow_stops_on_expiry_denial_or_deadline() {
    let (server, forge) = server_and_forge().await;
    let cb = |expires_in| LinkCallback::DeviceCode {
        device_code: "dev-code-1".into(),
        interval: 5,
        expires_in,
    };

    Mock::given(method("POST"))
        .and(path("/login/oauth/access_token"))
        .respond_with(poll_reply(json!({ "error": "expired_token" })))
        .up_to_n_times(1)
        .mount(&server)
        .await;
    let e = forge.complete_account_link(cb(900)).await.unwrap_err();
    assert!(
        matches!(e, ForgeError::LinkFailed(ref m) if m.contains("expired")),
        "{e}"
    );

    Mock::given(method("POST"))
        .and(path("/login/oauth/access_token"))
        .respond_with(poll_reply(json!({ "error": "access_denied" })))
        .up_to_n_times(1)
        .mount(&server)
        .await;
    let e = forge.complete_account_link(cb(900)).await.unwrap_err();
    assert!(
        matches!(e, ForgeError::LinkFailed(ref m) if m.contains("declined")),
        "{e}"
    );

    // Pending forever: the local deadline (expires_in) ends it.
    Mock::given(method("POST"))
        .and(path("/login/oauth/access_token"))
        .respond_with(poll_reply(json!({ "error": "authorization_pending" })))
        .mount(&server)
        .await;
    let e = forge.complete_account_link(cb(12)).await.unwrap_err();
    assert!(
        matches!(e, ForgeError::LinkFailed(ref m) if m.contains("expired")),
        "{e}"
    );
}

// ── review follow-ups ────────────────────────────────────────────────────

fn good_permissions() -> serde_json::Value {
    json!({
        "administration": "write", "contents": "write", "actions_variables": "write",
        "checks": "write", "pull_requests": "read", "merge_queues": "read",
        "metadata": "read", "members": "read", "organization_administration": "write",
    })
}

async fn exchange_with(
    reply: serde_json::Value,
    expected_owner: &str,
) -> vgi_forge::Result<String> {
    let server = wiremock::MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/app-manifests/abc123/conversions"))
        .respond_with(ResponseTemplate::new(201).set_body_json(reply))
        .mount(&server)
        .await;
    let base = Url::parse(&server.uri()).unwrap();
    exchange_code(&base, "abc123", expected_owner)
        .await
        .map(|c| c.slug)
}

#[tokio::test]
async fn manifest_exchange_checks_owner_events_visibility_and_requires_permissions() {
    assert!(
        exchange_with(conversion(good_permissions()), "ACME")
            .await
            .is_ok()
    );

    let e = exchange_with(conversion(good_permissions()), "newco")
        .await
        .unwrap_err();
    assert!(
        e.to_string()
            .contains("registered under `acme`, not `newco`"),
        "{e}"
    );

    let mut no_perms = conversion(good_permissions());
    no_perms.as_object_mut().unwrap().remove("permissions");
    assert!(
        exchange_with(no_perms, "acme").await.is_err(),
        "absent permissions fail closed"
    );

    let mut no_owner = conversion(good_permissions());
    no_owner.as_object_mut().unwrap().remove("owner");
    assert!(exchange_with(no_owner, "acme").await.is_err());

    let mut fewer_events = conversion(good_permissions());
    fewer_events["events"] = json!(["repository"]);
    let e = exchange_with(fewer_events, "acme").await.unwrap_err();
    assert!(e.to_string().contains("events"), "{e}");

    let mut public = conversion(good_permissions());
    public["public"] = json!(true);
    let e = exchange_with(public, "acme").await.unwrap_err();
    assert!(e.to_string().contains("public"), "{e}");
}

#[tokio::test]
async fn jwt_issuer_can_be_the_numeric_app_id() {
    let server = wiremock::MockServer::start().await;
    let forge = forge_with(&server, |cfg| cfg.with_app_id_issuer());
    Mock::given(method("GET"))
        .and(path("/app/installations/77"))
        .and(ValidAppJwtFor("1001"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "id": 77,
            "account": { "id": 500, "login": "newco", "type": "Organization" },
            "permissions": good_permissions(),
            "events": APP_EVENTS,
        })))
        .expect(1)
        .mount(&server)
        .await;
    let state = GitHubForge::new_state().unwrap();
    let ns = Resource::parse("github.com/newco").unwrap();
    let binding = forge
        .complete_bind(BindCallback::new(
            callback(&state, "77", "install"),
            state.clone(),
            ns,
        ))
        .await
        .unwrap();
    assert!(binding.missing_permissions.is_empty());
}

#[tokio::test]
async fn device_code_never_prints_and_the_user_token_is_revoked() {
    let server = wiremock::MockServer::start().await;
    let forge = forge_for(&server).with_client_secret(vgi_forge_github::Secret::new("cs-secret"));
    mount_device_code(&server).await;
    Mock::given(method("POST"))
        .and(path("/login/oauth/access_token"))
        .respond_with(poll_reply(
            json!({ "access_token": "ghu_user_token", "token_type": "bearer" }),
        ))
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .and(path("/user"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({ "id": 7, "login": "bob" })))
        .mount(&server)
        .await;
    use base64::Engine;
    let basic = base64::engine::general_purpose::STANDARD.encode(format!("{CLIENT_ID}:cs-secret"));
    Mock::given(method("DELETE"))
        .and(path(format!("/applications/{CLIENT_ID}/token")))
        .and(header("authorization", format!("Basic {basic}").as_str()))
        .and(body_json(json!({ "access_token": "ghu_user_token" })))
        .respond_with(ResponseTemplate::new(204))
        .expect(1)
        .mount(&server)
        .await;

    let step = forge.begin_account_link("did:webvh:bob").await.unwrap();
    let cb = LinkCallback::from_device_step(&step).unwrap();
    for printed in [format!("{step:?}"), format!("{cb:?}")] {
        assert!(!printed.contains("dev-code-1"), "{printed}");
        assert!(printed.contains("<redacted>"), "{printed}");
    }
    let account = forge.complete_account_link(cb).await.unwrap();
    assert_eq!(account.id, 7);
}

#[tokio::test]
async fn a_caller_cannot_stretch_polling_past_the_device_code_lifetime() {
    let (server, forge) = server_and_forge().await;
    // 900 s / 5 s per poll: at most 180 polls, however long the caller asks.
    Mock::given(method("POST"))
        .and(path("/login/oauth/access_token"))
        .respond_with(poll_reply(json!({ "error": "authorization_pending" })))
        .expect(1..=180)
        .mount(&server)
        .await;
    let e = forge
        .complete_account_link(LinkCallback::DeviceCode {
            device_code: "dev-code-1".into(),
            interval: 5,
            expires_in: u64::MAX,
        })
        .await
        .unwrap_err();
    assert!(
        matches!(e, ForgeError::LinkFailed(ref m) if m.contains("expired")),
        "{e}"
    );
}

#[tokio::test]
async fn a_member_bound_redirect_link_is_refused_by_the_device_flow_adapter() {
    let (server, forge) = server_and_forge().await;
    Mock::given(wiremock::matchers::any())
        .respond_with(ResponseTemplate::new(500))
        .expect(0)
        .mount(&server)
        .await;
    // GitHub links through the device flow; a redirect callback (which now
    // names the member it was started for) is not something it issues.
    let cb = LinkCallback::redirect(
        BTreeMap::from([("code".to_string(), "abc".to_string())]),
        "did:webvh:member.example",
    );
    let e = forge.complete_account_link(cb).await.unwrap_err();
    assert!(matches!(e, ForgeError::Unsupported { .. }), "{e:?}");
}