@coffre/conformance 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{idp-Dyaj6k5N.js → idp-B8u4UPA4.js} +373 -22
- package/dist/idp.d.ts +80 -2
- package/dist/idp.js +2 -2
- package/dist/main.js +160 -32
- package/package.json +4 -4
- package/src/checks/access.ts +13 -4
- package/src/checks/canaries.ts +1 -1
- package/src/checks/cli.ts +1 -1
- package/src/checks/environments.ts +11 -8
- package/src/checks/pages.ts +96 -0
- package/src/checks/people.ts +6 -5
- package/src/chrome.ts +9 -3
- package/src/harness.ts +15 -7
- package/src/idp/github-app.ts +259 -0
- package/src/idp/github.ts +38 -7
- package/src/idp/idp.ts +17 -2
- package/src/idp/index.ts +1 -0
- package/src/idp/people.ts +2 -2
- package/src/suite.ts +2 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { promisify } from "node:util";
|
|
2
|
+
import { createHash, createPublicKey, generateKeyPair, randomBytes } from "node:crypto";
|
|
2
3
|
import { createServer } from "node:http";
|
|
3
|
-
import { SignJWT, exportJWK, generateKeyPair } from "jose";
|
|
4
|
+
import { SignJWT, exportJWK, generateKeyPair as generateKeyPair$1, jwtVerify } from "jose";
|
|
4
5
|
//#region src/idp/http.ts
|
|
5
6
|
function sendJson(res, status, body, headers = {}) {
|
|
6
7
|
res.writeHead(status, {
|
|
@@ -79,12 +80,12 @@ const PERSONAS = Object.freeze([
|
|
|
79
80
|
{
|
|
80
81
|
email: "admin@acme.example",
|
|
81
82
|
name: "Ada Admin",
|
|
82
|
-
note: "
|
|
83
|
+
note: "owner of coffre"
|
|
83
84
|
},
|
|
84
85
|
{
|
|
85
86
|
email: "lead@acme.example",
|
|
86
87
|
name: "Lea Lead",
|
|
87
|
-
note: "owner"
|
|
88
|
+
note: "owner of market"
|
|
88
89
|
},
|
|
89
90
|
{
|
|
90
91
|
email: "dev@acme.example",
|
|
@@ -465,8 +466,8 @@ var AuthorizationServer = class {
|
|
|
465
466
|
//#endregion
|
|
466
467
|
//#region src/idp/github.ts
|
|
467
468
|
const WEB = "/github";
|
|
468
|
-
const API = "/github/api";
|
|
469
|
-
const DOCS = "https://docs.github.com/rest";
|
|
469
|
+
const API$1 = "/github/api";
|
|
470
|
+
const DOCS$1 = "https://docs.github.com/rest";
|
|
470
471
|
const TROUBLESHOOTING = "https://docs.github.com/apps/managing-oauth-apps/troubleshooting-oauth-app-access-token-request-errors";
|
|
471
472
|
/**
|
|
472
473
|
* Enough of GitHub's OAuth apps and REST API for coffre's GitHub sign-in:
|
|
@@ -513,17 +514,17 @@ var FakeGitHub = class {
|
|
|
513
514
|
},
|
|
514
515
|
{
|
|
515
516
|
method: "GET",
|
|
516
|
-
path: `${API}/user`,
|
|
517
|
+
path: `${API$1}/user`,
|
|
517
518
|
handler: async (req, res) => this.#user(req, res)
|
|
518
519
|
},
|
|
519
520
|
{
|
|
520
521
|
method: "GET",
|
|
521
|
-
path: `${API}/user/emails`,
|
|
522
|
+
path: `${API$1}/user/emails`,
|
|
522
523
|
handler: async (req, res) => this.#emails(req, res)
|
|
523
524
|
},
|
|
524
525
|
{
|
|
525
526
|
method: "GET",
|
|
526
|
-
path: new RegExp(`^${API}/user/memberships/orgs/[^/]+$`),
|
|
527
|
+
path: new RegExp(`^${API$1}/user/memberships/orgs/[^/]+$`),
|
|
527
528
|
handler: async (req, res, url) => this.#membership(req, res, decodeURIComponent(url.pathname.split("/").pop()))
|
|
528
529
|
}
|
|
529
530
|
];
|
|
@@ -538,7 +539,7 @@ var FakeGitHub = class {
|
|
|
538
539
|
const reply = (body) => {
|
|
539
540
|
if ((req.headers.accept ?? "").includes("application/json")) return sendJson(res, 200, body);
|
|
540
541
|
res.writeHead(200, { "content-type": "application/x-www-form-urlencoded; charset=utf-8" });
|
|
541
|
-
res.end(new URLSearchParams(body).toString());
|
|
542
|
+
res.end(new URLSearchParams(Object.entries(body).map(([key, value]) => [key, String(value)])).toString());
|
|
542
543
|
};
|
|
543
544
|
const fail = (error, description) => reply({
|
|
544
545
|
error,
|
|
@@ -557,18 +558,27 @@ var FakeGitHub = class {
|
|
|
557
558
|
}
|
|
558
559
|
const client = clientId ? this.#idp.client(clientId) : void 0;
|
|
559
560
|
if (!client || client.clientSecret !== clientSecret) return fail("incorrect_client_credentials", "The client_id and/or client_secret passed are incorrect.");
|
|
561
|
+
const app = this.#isApp(client.clientId);
|
|
560
562
|
const result = this.#authz.exchange("github", {
|
|
561
563
|
client,
|
|
562
564
|
code: params.get("code") ?? "",
|
|
563
565
|
redirectUri: params.get("redirect_uri") ?? void 0,
|
|
564
566
|
codeVerifier: params.get("code_verifier") ?? "",
|
|
565
|
-
accessTokenTtl: Infinity,
|
|
566
|
-
accessTokenPrefix: "gho_"
|
|
567
|
+
accessTokenTtl: app ? 28800 : Infinity,
|
|
568
|
+
accessTokenPrefix: app ? "ghu_" : "gho_"
|
|
567
569
|
});
|
|
568
570
|
if (!result.ok) {
|
|
569
571
|
if (result.error === "redirect_uri_mismatch") return fail("redirect_uri_mismatch", "The redirect_uri MUST match the registered callback URL for this application.");
|
|
570
572
|
return fail("bad_verification_code", "The code passed is incorrect or expired.");
|
|
571
573
|
}
|
|
574
|
+
if (app) return reply({
|
|
575
|
+
access_token: result.accessToken,
|
|
576
|
+
expires_in: 28800,
|
|
577
|
+
refresh_token: `ghr_${randomBytes(30).toString("hex")}`,
|
|
578
|
+
refresh_token_expires_in: 15897600,
|
|
579
|
+
token_type: "bearer",
|
|
580
|
+
scope: ""
|
|
581
|
+
});
|
|
572
582
|
reply({
|
|
573
583
|
access_token: result.accessToken,
|
|
574
584
|
token_type: "bearer",
|
|
@@ -599,7 +609,9 @@ var FakeGitHub = class {
|
|
|
599
609
|
#emails(req, res) {
|
|
600
610
|
const auth = this.#authenticate(req, res);
|
|
601
611
|
if (!auth) return;
|
|
602
|
-
if (
|
|
612
|
+
if (this.#isApp(auth.grant.clientId)) {
|
|
613
|
+
if (this.#idp.githubApp.permissions.emails === void 0) return this.#notAccessible(res, auth.grant);
|
|
614
|
+
} else if (!this.#hasScope(auth.grant, ["user:email", "user"])) return this.#notFound(res, auth.grant);
|
|
603
615
|
this.#json(res, auth.grant, 200, auth.account.emails);
|
|
604
616
|
}
|
|
605
617
|
/**
|
|
@@ -611,18 +623,20 @@ var FakeGitHub = class {
|
|
|
611
623
|
#membership(req, res, org) {
|
|
612
624
|
const auth = this.#authenticate(req, res);
|
|
613
625
|
if (!auth) return;
|
|
614
|
-
if (
|
|
626
|
+
if (this.#isApp(auth.grant.clientId)) {
|
|
627
|
+
if (this.#idp.githubApp.permissions.members === void 0 || !this.#idp.githubApp.installedOn(org)) return this.#notAccessible(res, auth.grant);
|
|
628
|
+
} else if (!this.#hasScope(auth.grant, [
|
|
615
629
|
"read:org",
|
|
616
630
|
"write:org",
|
|
617
631
|
"admin:org",
|
|
618
632
|
"user"
|
|
619
633
|
])) return this.#json(res, auth.grant, 403, {
|
|
620
634
|
message: "You need at least read:org scope or user scope to read organization memberships.",
|
|
621
|
-
documentation_url: DOCS
|
|
635
|
+
documentation_url: DOCS$1
|
|
622
636
|
});
|
|
623
637
|
const login = auth.account.orgs.find((o) => o.toLowerCase() === org.toLowerCase());
|
|
624
638
|
if (!login) return this.#notFound(res, auth.grant);
|
|
625
|
-
const api = `${this.#idp.origin}${API}`;
|
|
639
|
+
const api = `${this.#idp.origin}${API$1}`;
|
|
626
640
|
this.#json(res, auth.grant, 200, {
|
|
627
641
|
url: `${api}/orgs/${login}/memberships/${auth.account.login}`,
|
|
628
642
|
state: "active",
|
|
@@ -646,7 +660,7 @@ var FakeGitHub = class {
|
|
|
646
660
|
if (token === void 0) {
|
|
647
661
|
sendJson(res, 401, {
|
|
648
662
|
message: "Requires authentication",
|
|
649
|
-
documentation_url: DOCS
|
|
663
|
+
documentation_url: DOCS$1
|
|
650
664
|
});
|
|
651
665
|
return;
|
|
652
666
|
}
|
|
@@ -654,7 +668,7 @@ var FakeGitHub = class {
|
|
|
654
668
|
if (!grant) {
|
|
655
669
|
sendJson(res, 401, {
|
|
656
670
|
message: "Bad credentials",
|
|
657
|
-
documentation_url: DOCS
|
|
671
|
+
documentation_url: DOCS$1
|
|
658
672
|
});
|
|
659
673
|
return;
|
|
660
674
|
}
|
|
@@ -663,6 +677,16 @@ var FakeGitHub = class {
|
|
|
663
677
|
account: this.#idp.gitHubUserFor(normalizeEmail(grant.email))
|
|
664
678
|
};
|
|
665
679
|
}
|
|
680
|
+
/** The deployment's GitHub App's OAuth client, whose tokens are user tokens of the app's. */
|
|
681
|
+
#isApp(clientId) {
|
|
682
|
+
return clientId === this.#idp.githubApp.clientId;
|
|
683
|
+
}
|
|
684
|
+
#notAccessible(res, grant) {
|
|
685
|
+
this.#json(res, grant, 403, {
|
|
686
|
+
message: "Resource not accessible by integration",
|
|
687
|
+
documentation_url: DOCS$1
|
|
688
|
+
});
|
|
689
|
+
}
|
|
666
690
|
#hasScope(grant, accepted) {
|
|
667
691
|
return grant.scope.some((s) => accepted.includes(s));
|
|
668
692
|
}
|
|
@@ -673,10 +697,326 @@ var FakeGitHub = class {
|
|
|
673
697
|
#notFound(res, grant) {
|
|
674
698
|
this.#json(res, grant, 404, {
|
|
675
699
|
message: "Not Found",
|
|
700
|
+
documentation_url: DOCS$1
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
};
|
|
704
|
+
//#endregion
|
|
705
|
+
//#region src/idp/github-app.ts
|
|
706
|
+
const API = "/github/api";
|
|
707
|
+
const DOCS = "https://docs.github.com/rest";
|
|
708
|
+
/** acme's repositories, private ones among them, and a user's; one installation suspended. */
|
|
709
|
+
function fakeInstallations() {
|
|
710
|
+
return [
|
|
711
|
+
{
|
|
712
|
+
id: 1,
|
|
713
|
+
account: {
|
|
714
|
+
login: "acme",
|
|
715
|
+
id: 9919,
|
|
716
|
+
type: "Organization"
|
|
717
|
+
},
|
|
718
|
+
suspended: false,
|
|
719
|
+
repositories: [
|
|
720
|
+
{
|
|
721
|
+
id: 41532,
|
|
722
|
+
name: "api",
|
|
723
|
+
private: false
|
|
724
|
+
},
|
|
725
|
+
{
|
|
726
|
+
id: 41533,
|
|
727
|
+
name: "web",
|
|
728
|
+
private: false
|
|
729
|
+
},
|
|
730
|
+
{
|
|
731
|
+
id: 41540,
|
|
732
|
+
name: "infra",
|
|
733
|
+
private: true
|
|
734
|
+
},
|
|
735
|
+
{
|
|
736
|
+
id: 41541,
|
|
737
|
+
name: "payments",
|
|
738
|
+
private: true
|
|
739
|
+
},
|
|
740
|
+
{
|
|
741
|
+
id: 41550,
|
|
742
|
+
name: "deploy-actions",
|
|
743
|
+
private: true
|
|
744
|
+
}
|
|
745
|
+
]
|
|
746
|
+
},
|
|
747
|
+
{
|
|
748
|
+
id: 2,
|
|
749
|
+
account: {
|
|
750
|
+
login: "octo-dev",
|
|
751
|
+
id: 583231,
|
|
752
|
+
type: "User"
|
|
753
|
+
},
|
|
754
|
+
suspended: false,
|
|
755
|
+
repositories: [{
|
|
756
|
+
id: 70001,
|
|
757
|
+
name: "dotfiles",
|
|
758
|
+
private: true
|
|
759
|
+
}]
|
|
760
|
+
},
|
|
761
|
+
{
|
|
762
|
+
id: 3,
|
|
763
|
+
account: {
|
|
764
|
+
login: "old-co",
|
|
765
|
+
id: 77001,
|
|
766
|
+
type: "Organization"
|
|
767
|
+
},
|
|
768
|
+
suspended: true,
|
|
769
|
+
repositories: [{
|
|
770
|
+
id: 80001,
|
|
771
|
+
name: "legacy",
|
|
772
|
+
private: true
|
|
773
|
+
}]
|
|
774
|
+
}
|
|
775
|
+
];
|
|
776
|
+
}
|
|
777
|
+
/**
|
|
778
|
+
* The deployment's GitHub App, as GitHub's REST API answers it, under
|
|
779
|
+
* `/github/api` beside the fake GitHub's sign-in: the app's JWT, checked
|
|
780
|
+
* against its public key as GitHub does, buys installation tokens, which
|
|
781
|
+
* list each installation's repositories. What `@coffre/core`'s `GitHubApp`
|
|
782
|
+
* runs against, through its real code paths.
|
|
783
|
+
*
|
|
784
|
+
* Its private key is GitHub's download, PKCS #1: `privateKey`, for the
|
|
785
|
+
* deployment's configuration, made at `start()` unless given.
|
|
786
|
+
*/
|
|
787
|
+
var FakeGitHubApp = class {
|
|
788
|
+
#origin;
|
|
789
|
+
appId = "1001";
|
|
790
|
+
name = "Coffre (dev)";
|
|
791
|
+
slug = "coffre-dev";
|
|
792
|
+
/** Its OAuth client, for signing people in: a GitHub App's client ID, and a local fixture for its secret. */
|
|
793
|
+
clientId = "Iv23li-coffre-dev";
|
|
794
|
+
clientSecret = "coffre-dev-app-secret";
|
|
795
|
+
/**
|
|
796
|
+
* What it may do, as its settings page grants it. Its user tokens carry
|
|
797
|
+
* these, not OAuth scopes: their email addresses with `emails`, and
|
|
798
|
+
* organization memberships with `members`, where it is installed.
|
|
799
|
+
*/
|
|
800
|
+
permissions = {
|
|
801
|
+
metadata: "read",
|
|
802
|
+
emails: "read",
|
|
803
|
+
members: "read"
|
|
804
|
+
};
|
|
805
|
+
/** The installations, by account; a test edits them as it likes. */
|
|
806
|
+
installations = fakeInstallations();
|
|
807
|
+
/** How long an installation token lives: an hour, as GitHub's. */
|
|
808
|
+
tokenTtlMs = 36e5;
|
|
809
|
+
/** Installation tokens issued so far. */
|
|
810
|
+
tokensIssued = 0;
|
|
811
|
+
/** Every request answered 503 when set: GitHub down. */
|
|
812
|
+
down = false;
|
|
813
|
+
#privateKey;
|
|
814
|
+
#publicKey = null;
|
|
815
|
+
#tokens = /* @__PURE__ */ new Map();
|
|
816
|
+
constructor(origin, privateKey) {
|
|
817
|
+
this.#origin = origin;
|
|
818
|
+
this.#privateKey = privateKey;
|
|
819
|
+
}
|
|
820
|
+
/** The app's private key, PKCS #1 PEM, as GitHub downloads it. */
|
|
821
|
+
get privateKey() {
|
|
822
|
+
if (this.#privateKey === void 0) throw new Error("the fake GitHub App is not started");
|
|
823
|
+
return this.#privateKey;
|
|
824
|
+
}
|
|
825
|
+
/** What a deployment's `githubApp(…)` names to reach this one, for repositories and sign-in alike. */
|
|
826
|
+
get options() {
|
|
827
|
+
return {
|
|
828
|
+
appId: this.appId,
|
|
829
|
+
privateKey: this.privateKey,
|
|
830
|
+
clientId: this.clientId,
|
|
831
|
+
clientSecret: this.clientSecret,
|
|
832
|
+
webUrl: `${this.#origin()}/github`,
|
|
833
|
+
apiUrl: `${this.#origin()}${API}`
|
|
834
|
+
};
|
|
835
|
+
}
|
|
836
|
+
/** Whether it is installed on this account, and not suspended: where its user tokens reach. */
|
|
837
|
+
installedOn(login) {
|
|
838
|
+
return this.installations.some((each) => each.account.login.toLowerCase() === login.toLowerCase() && !each.suspended);
|
|
839
|
+
}
|
|
840
|
+
async start() {
|
|
841
|
+
if (this.#privateKey === void 0) {
|
|
842
|
+
const { privateKey } = await promisify(generateKeyPair)("rsa", { modulusLength: 2048 });
|
|
843
|
+
this.#privateKey = privateKey.export({
|
|
844
|
+
type: "pkcs1",
|
|
845
|
+
format: "pem"
|
|
846
|
+
});
|
|
847
|
+
}
|
|
848
|
+
this.#publicKey = createPublicKey(this.#privateKey);
|
|
849
|
+
}
|
|
850
|
+
/** Every token issued dies now: as when the app is reinstalled. */
|
|
851
|
+
revokeTokens() {
|
|
852
|
+
this.#tokens.clear();
|
|
853
|
+
}
|
|
854
|
+
routes() {
|
|
855
|
+
const repo = "([A-Za-z0-9-]+)/([A-Za-z0-9._-]+)";
|
|
856
|
+
return [
|
|
857
|
+
{
|
|
858
|
+
method: "GET",
|
|
859
|
+
path: `${API}/app`,
|
|
860
|
+
handler: async (req, res) => this.#asApp(req, res, () => this.#app())
|
|
861
|
+
},
|
|
862
|
+
{
|
|
863
|
+
method: "GET",
|
|
864
|
+
path: `${API}/app/installations`,
|
|
865
|
+
handler: async (req, res, url) => this.#asApp(req, res, () => [200, this.#installations(url)])
|
|
866
|
+
},
|
|
867
|
+
{
|
|
868
|
+
method: "POST",
|
|
869
|
+
path: new RegExp(`^${API}/app/installations/[0-9]+/access_tokens$`),
|
|
870
|
+
handler: async (req, res, url) => this.#asApp(req, res, () => this.#issue(Number(url.pathname.split("/").at(-2))))
|
|
871
|
+
},
|
|
872
|
+
{
|
|
873
|
+
method: "GET",
|
|
874
|
+
path: new RegExp(`^${API}/repos/${repo}/installation$`),
|
|
875
|
+
handler: async (req, res, url) => this.#asApp(req, res, () => this.#installationOf(url.pathname.split("/").slice(-3, -1).join("/")))
|
|
876
|
+
},
|
|
877
|
+
{
|
|
878
|
+
method: "GET",
|
|
879
|
+
path: `${API}/installation/repositories`,
|
|
880
|
+
handler: async (req, res, url) => this.#asInstallation(req, res, (installation) => [200, this.#page(installation, url)])
|
|
881
|
+
},
|
|
882
|
+
{
|
|
883
|
+
method: "GET",
|
|
884
|
+
path: new RegExp(`^${API}/repos/${repo}$`),
|
|
885
|
+
handler: async (req, res, url) => this.#asInstallation(req, res, (installation) => {
|
|
886
|
+
const name = url.pathname.split("/").slice(-2).join("/");
|
|
887
|
+
const found = installation.repositories.find((each) => `${installation.account.login}/${each.name}`.toLowerCase() === name.toLowerCase());
|
|
888
|
+
return found === void 0 ? notFound() : [200, this.#repository(installation, found)];
|
|
889
|
+
})
|
|
890
|
+
}
|
|
891
|
+
];
|
|
892
|
+
}
|
|
893
|
+
#app() {
|
|
894
|
+
return [200, {
|
|
895
|
+
id: Number(this.appId),
|
|
896
|
+
slug: this.slug,
|
|
897
|
+
name: this.name,
|
|
898
|
+
client_id: this.clientId,
|
|
899
|
+
html_url: `${this.#origin()}/github/apps/${this.slug}`,
|
|
900
|
+
permissions: this.permissions
|
|
901
|
+
}];
|
|
902
|
+
}
|
|
903
|
+
/** https://docs.github.com/en/rest/apps/apps#list-installations-for-the-authenticated-app */
|
|
904
|
+
#installations(url) {
|
|
905
|
+
const perPage = Math.min(Number(url.searchParams.get("per_page") ?? 30), 100);
|
|
906
|
+
return this.installations.slice(0, perPage).map((installation) => ({
|
|
907
|
+
id: installation.id,
|
|
908
|
+
account: {
|
|
909
|
+
login: installation.account.login,
|
|
910
|
+
id: installation.account.id,
|
|
911
|
+
type: installation.account.type
|
|
912
|
+
},
|
|
913
|
+
repository_selection: "selected",
|
|
914
|
+
app_id: Number(this.appId),
|
|
915
|
+
suspended_at: installation.suspended ? "2026-01-01T00:00:00Z" : null
|
|
916
|
+
}));
|
|
917
|
+
}
|
|
918
|
+
/** https://docs.github.com/en/rest/apps/apps#create-an-installation-access-token-for-an-app */
|
|
919
|
+
#issue(id) {
|
|
920
|
+
const installation = this.installations.find((each) => each.id === id);
|
|
921
|
+
if (installation === void 0) return notFound();
|
|
922
|
+
if (installation.suspended) return [403, {
|
|
923
|
+
message: "This installation has been suspended",
|
|
676
924
|
documentation_url: DOCS
|
|
925
|
+
}];
|
|
926
|
+
const token = `ghs_${randomBytes(18).toString("hex")}`;
|
|
927
|
+
const expiresAt = Date.now() + this.tokenTtlMs;
|
|
928
|
+
this.#tokens.set(token, {
|
|
929
|
+
installation: id,
|
|
930
|
+
expiresAt
|
|
677
931
|
});
|
|
932
|
+
this.tokensIssued++;
|
|
933
|
+
return [201, {
|
|
934
|
+
token,
|
|
935
|
+
expires_at: new Date(expiresAt).toISOString().replace(/\.\d{3}Z$/, "Z"),
|
|
936
|
+
permissions: this.permissions,
|
|
937
|
+
repository_selection: "selected"
|
|
938
|
+
}];
|
|
939
|
+
}
|
|
940
|
+
/** https://docs.github.com/en/rest/apps/apps#get-a-repository-installation-for-the-authenticated-app */
|
|
941
|
+
#installationOf(name) {
|
|
942
|
+
const installation = this.installations.find((each) => each.repositories.some((repository) => `${each.account.login}/${repository.name}`.toLowerCase() === name.toLowerCase()));
|
|
943
|
+
if (installation === void 0) return notFound();
|
|
944
|
+
return [200, {
|
|
945
|
+
id: installation.id,
|
|
946
|
+
account: {
|
|
947
|
+
login: installation.account.login,
|
|
948
|
+
id: installation.account.id
|
|
949
|
+
},
|
|
950
|
+
suspended_at: installation.suspended ? "2026-01-01T00:00:00Z" : null
|
|
951
|
+
}];
|
|
952
|
+
}
|
|
953
|
+
/** https://docs.github.com/en/rest/apps/installations#list-repositories-accessible-to-the-app-installation */
|
|
954
|
+
#page(installation, url) {
|
|
955
|
+
const perPage = Math.min(Number(url.searchParams.get("per_page") ?? 30), 100);
|
|
956
|
+
const page = Math.max(Number(url.searchParams.get("page") ?? 1), 1);
|
|
957
|
+
return {
|
|
958
|
+
total_count: installation.repositories.length,
|
|
959
|
+
repository_selection: "selected",
|
|
960
|
+
repositories: installation.repositories.slice((page - 1) * perPage, page * perPage).map((each) => this.#repository(installation, each))
|
|
961
|
+
};
|
|
962
|
+
}
|
|
963
|
+
#repository(installation, repository) {
|
|
964
|
+
const { login, id, type } = installation.account;
|
|
965
|
+
return {
|
|
966
|
+
id: repository.id,
|
|
967
|
+
node_id: `R_dev${repository.id}`,
|
|
968
|
+
name: repository.name,
|
|
969
|
+
full_name: `${login}/${repository.name}`,
|
|
970
|
+
private: repository.private,
|
|
971
|
+
owner: {
|
|
972
|
+
login,
|
|
973
|
+
id,
|
|
974
|
+
type
|
|
975
|
+
},
|
|
976
|
+
html_url: `https://github.com/${login}/${repository.name}`
|
|
977
|
+
};
|
|
978
|
+
}
|
|
979
|
+
/** As the app, by its JWT: RS256 under its key, issued by its ID, for ten minutes at most. */
|
|
980
|
+
async #asApp(req, res, answer) {
|
|
981
|
+
if (this.down) return send(res, [503, { message: "Service Unavailable" }]);
|
|
982
|
+
const jwt = authorizationToken(req, ["bearer"]);
|
|
983
|
+
try {
|
|
984
|
+
if (jwt === void 0 || this.#publicKey === null) throw new Error("no JWT");
|
|
985
|
+
const { payload } = await jwtVerify(jwt, this.#publicKey, {
|
|
986
|
+
algorithms: ["RS256"],
|
|
987
|
+
issuer: this.appId,
|
|
988
|
+
clockTolerance: 60
|
|
989
|
+
});
|
|
990
|
+
if (typeof payload.iat !== "number" || typeof payload.exp !== "number" || payload.exp - payload.iat > 600) throw new Error("too long");
|
|
991
|
+
} catch {
|
|
992
|
+
return send(res, [401, {
|
|
993
|
+
message: "A JSON web token could not be decoded",
|
|
994
|
+
documentation_url: DOCS
|
|
995
|
+
}]);
|
|
996
|
+
}
|
|
997
|
+
send(res, answer());
|
|
998
|
+
}
|
|
999
|
+
/** As an installation, by a token it issued that lives still. */
|
|
1000
|
+
async #asInstallation(req, res, answer) {
|
|
1001
|
+
if (this.down) return send(res, [503, { message: "Service Unavailable" }]);
|
|
1002
|
+
const token = this.#tokens.get(authorizationToken(req, ["bearer", "token"]) ?? "");
|
|
1003
|
+
const installation = this.installations.find((each) => each.id === token?.installation);
|
|
1004
|
+
if (token === void 0 || token.expiresAt <= Date.now() || installation === void 0 || installation.suspended) return send(res, [401, {
|
|
1005
|
+
message: "Bad credentials",
|
|
1006
|
+
documentation_url: DOCS
|
|
1007
|
+
}]);
|
|
1008
|
+
send(res, answer(installation));
|
|
678
1009
|
}
|
|
679
1010
|
};
|
|
1011
|
+
function notFound() {
|
|
1012
|
+
return [404, {
|
|
1013
|
+
message: "Not Found",
|
|
1014
|
+
documentation_url: DOCS
|
|
1015
|
+
}];
|
|
1016
|
+
}
|
|
1017
|
+
function send(res, [status, body]) {
|
|
1018
|
+
sendJson(res, status, body, { "x-github-api-version-selected": "2022-11-28" });
|
|
1019
|
+
}
|
|
680
1020
|
//#endregion
|
|
681
1021
|
//#region src/idp/oidc.ts
|
|
682
1022
|
const ACCESS_TOKEN_TTL = 3600;
|
|
@@ -931,7 +1271,7 @@ var WorkloadIssuer = class {
|
|
|
931
1271
|
}
|
|
932
1272
|
async start() {
|
|
933
1273
|
this.#keys = await Promise.all(["RS256", "ES256"].map(async (alg) => {
|
|
934
|
-
const { privateKey, publicKey } = await generateKeyPair(alg, { extractable: true });
|
|
1274
|
+
const { privateKey, publicKey } = await generateKeyPair$1(alg, { extractable: true });
|
|
935
1275
|
const kid = `dev-workloads-${alg.toLowerCase()}`;
|
|
936
1276
|
return {
|
|
937
1277
|
alg,
|
|
@@ -1043,7 +1383,7 @@ const DEFAULT_CLIENT = Object.freeze({
|
|
|
1043
1383
|
* For Cloudflare Access, it generates a keypair, serves a JWKS at the same
|
|
1044
1384
|
* `cdn-cgi` path Access uses, and mints Access-shaped tokens. It is also an
|
|
1045
1385
|
* OpenID Connect provider (under `/oauth`) signing with the same key, and an
|
|
1046
|
-
* imitation of GitHub's OAuth apps and REST API (under `/github`). The
|
|
1386
|
+
* imitation of GitHub's OAuth apps, a GitHub App and REST API (under `/github`). The
|
|
1047
1387
|
* point is that the code under test runs its real remote code paths against
|
|
1048
1388
|
* real HTTP endpoints -- local mode is a different implementation of the same
|
|
1049
1389
|
* interface, never a branch that skips verification.
|
|
@@ -1066,18 +1406,28 @@ var DevIdp = class {
|
|
|
1066
1406
|
autoApprove;
|
|
1067
1407
|
/** A CI platform's ID tokens, under `/workloads`, for trust bindings. */
|
|
1068
1408
|
workloads = new WorkloadIssuer(() => this.origin);
|
|
1409
|
+
/** A deployment's GitHub App, under `/github/api` beside the fake GitHub. */
|
|
1410
|
+
githubApp;
|
|
1069
1411
|
constructor(options = {}) {
|
|
1070
1412
|
this.autoApprove = options.autoApprove ?? false;
|
|
1413
|
+
this.githubApp = new FakeGitHubApp(() => this.origin, options.githubAppKey);
|
|
1071
1414
|
this.#clients.set(DEFAULT_CLIENT.clientId, {
|
|
1072
1415
|
...DEFAULT_CLIENT,
|
|
1073
1416
|
redirectUris: [],
|
|
1074
1417
|
anyLoopbackRedirect: true
|
|
1075
1418
|
});
|
|
1076
1419
|
for (const client of options.clients ?? []) this.registerClient(client);
|
|
1420
|
+
this.#clients.set(this.githubApp.clientId, {
|
|
1421
|
+
clientId: this.githubApp.clientId,
|
|
1422
|
+
clientSecret: this.githubApp.clientSecret,
|
|
1423
|
+
redirectUris: [],
|
|
1424
|
+
anyLoopbackRedirect: true
|
|
1425
|
+
});
|
|
1077
1426
|
const authz = new AuthorizationServer(this);
|
|
1078
1427
|
this.#routes = [
|
|
1079
1428
|
...new OidcProvider(this, authz).routes(),
|
|
1080
1429
|
...new FakeGitHub(this, authz).routes(),
|
|
1430
|
+
...this.githubApp.routes(),
|
|
1081
1431
|
...this.workloads.routes()
|
|
1082
1432
|
];
|
|
1083
1433
|
}
|
|
@@ -1094,7 +1444,7 @@ var DevIdp = class {
|
|
|
1094
1444
|
return `${this.origin}/cdn-cgi/access/certs`;
|
|
1095
1445
|
}
|
|
1096
1446
|
async start() {
|
|
1097
|
-
const { privateKey, publicKey } = await generateKeyPair("RS256", { extractable: true });
|
|
1447
|
+
const { privateKey, publicKey } = await generateKeyPair$1("RS256", { extractable: true });
|
|
1098
1448
|
this.privateKey = privateKey;
|
|
1099
1449
|
this.publicJwk = {
|
|
1100
1450
|
...await exportJWK(publicKey),
|
|
@@ -1103,6 +1453,7 @@ var DevIdp = class {
|
|
|
1103
1453
|
use: "sig"
|
|
1104
1454
|
};
|
|
1105
1455
|
await this.workloads.start();
|
|
1456
|
+
await this.githubApp.start();
|
|
1106
1457
|
this.#server = createServer((req, res) => {
|
|
1107
1458
|
if (req.url?.startsWith("/dev/mint")) {
|
|
1108
1459
|
const url = new URL(req.url, this.origin);
|
|
@@ -1263,4 +1614,4 @@ var DevIdp = class {
|
|
|
1263
1614
|
}
|
|
1264
1615
|
};
|
|
1265
1616
|
//#endregion
|
|
1266
|
-
export {
|
|
1617
|
+
export { FakeGitHubApp as a, defaultGitHubAccount as c, WorkloadIssuer as i, DevIdp as n, fakeInstallations as o, GITHUB_RUN as r, PERSONAS as s, DEFAULT_CLIENT as t };
|
package/dist/idp.d.ts
CHANGED
|
@@ -8,6 +8,80 @@ interface Route {
|
|
|
8
8
|
handler: Handler;
|
|
9
9
|
}
|
|
10
10
|
//#endregion
|
|
11
|
+
//#region src/idp/github-app.d.ts
|
|
12
|
+
type FakeRepository = {
|
|
13
|
+
id: number;
|
|
14
|
+
name: string;
|
|
15
|
+
private: boolean;
|
|
16
|
+
};
|
|
17
|
+
type FakeInstallation = {
|
|
18
|
+
id: number;
|
|
19
|
+
account: {
|
|
20
|
+
login: string;
|
|
21
|
+
id: number;
|
|
22
|
+
type: 'Organization' | 'User';
|
|
23
|
+
};
|
|
24
|
+
suspended: boolean;
|
|
25
|
+
repositories: FakeRepository[];
|
|
26
|
+
};
|
|
27
|
+
/** acme's repositories, private ones among them, and a user's; one installation suspended. */
|
|
28
|
+
export declare function fakeInstallations(): FakeInstallation[];
|
|
29
|
+
/**
|
|
30
|
+
* The deployment's GitHub App, as GitHub's REST API answers it, under
|
|
31
|
+
* `/github/api` beside the fake GitHub's sign-in: the app's JWT, checked
|
|
32
|
+
* against its public key as GitHub does, buys installation tokens, which
|
|
33
|
+
* list each installation's repositories. What `@coffre/core`'s `GitHubApp`
|
|
34
|
+
* runs against, through its real code paths.
|
|
35
|
+
*
|
|
36
|
+
* Its private key is GitHub's download, PKCS #1: `privateKey`, for the
|
|
37
|
+
* deployment's configuration, made at `start()` unless given.
|
|
38
|
+
*/
|
|
39
|
+
export declare class FakeGitHubApp {
|
|
40
|
+
#private;
|
|
41
|
+
readonly appId = "1001";
|
|
42
|
+
readonly name = "Coffre (dev)";
|
|
43
|
+
readonly slug = "coffre-dev";
|
|
44
|
+
/** Its OAuth client, for signing people in: a GitHub App's client ID, and a local fixture for its secret. */
|
|
45
|
+
readonly clientId = "Iv23li-coffre-dev";
|
|
46
|
+
readonly clientSecret = "coffre-dev-app-secret";
|
|
47
|
+
/**
|
|
48
|
+
* What it may do, as its settings page grants it. Its user tokens carry
|
|
49
|
+
* these, not OAuth scopes: their email addresses with `emails`, and
|
|
50
|
+
* organization memberships with `members`, where it is installed.
|
|
51
|
+
*/
|
|
52
|
+
permissions: {
|
|
53
|
+
metadata: 'read';
|
|
54
|
+
emails?: 'read';
|
|
55
|
+
members?: 'read';
|
|
56
|
+
};
|
|
57
|
+
/** The installations, by account; a test edits them as it likes. */
|
|
58
|
+
installations: FakeInstallation[];
|
|
59
|
+
/** How long an installation token lives: an hour, as GitHub's. */
|
|
60
|
+
tokenTtlMs: number;
|
|
61
|
+
/** Installation tokens issued so far. */
|
|
62
|
+
tokensIssued: number;
|
|
63
|
+
/** Every request answered 503 when set: GitHub down. */
|
|
64
|
+
down: boolean;
|
|
65
|
+
constructor(origin: () => string, privateKey?: string);
|
|
66
|
+
/** The app's private key, PKCS #1 PEM, as GitHub downloads it. */
|
|
67
|
+
get privateKey(): string;
|
|
68
|
+
/** What a deployment's `githubApp(…)` names to reach this one, for repositories and sign-in alike. */
|
|
69
|
+
get options(): {
|
|
70
|
+
appId: string;
|
|
71
|
+
privateKey: string;
|
|
72
|
+
clientId: string;
|
|
73
|
+
clientSecret: string;
|
|
74
|
+
webUrl: string;
|
|
75
|
+
apiUrl: string;
|
|
76
|
+
};
|
|
77
|
+
/** Whether it is installed on this account, and not suspended: where its user tokens reach. */
|
|
78
|
+
installedOn(login: string): boolean;
|
|
79
|
+
start(): Promise<void>;
|
|
80
|
+
/** Every token issued dies now: as when the app is reinstalled. */
|
|
81
|
+
revokeTokens(): void;
|
|
82
|
+
routes(): Route[];
|
|
83
|
+
}
|
|
84
|
+
//#endregion
|
|
11
85
|
//#region src/idp/workloads.d.ts
|
|
12
86
|
/**
|
|
13
87
|
* A CI platform's ID tokens, as GitHub Actions signs them for a run: an
|
|
@@ -114,6 +188,8 @@ interface DevIdpOptions {
|
|
|
114
188
|
clients?: readonly DevIdpClient[];
|
|
115
189
|
/** Skip the persona page when the request carries a login hint. For tests. */
|
|
116
190
|
autoApprove?: boolean;
|
|
191
|
+
/** The fake GitHub App's private key, PKCS #1 PEM: made at start unless given. */
|
|
192
|
+
githubAppKey?: string;
|
|
117
193
|
}
|
|
118
194
|
/** Built in, so local dev needs no registration step. */
|
|
119
195
|
export declare const DEFAULT_CLIENT: Readonly<{
|
|
@@ -126,7 +202,7 @@ export declare const DEFAULT_CLIENT: Readonly<{
|
|
|
126
202
|
* For Cloudflare Access, it generates a keypair, serves a JWKS at the same
|
|
127
203
|
* `cdn-cgi` path Access uses, and mints Access-shaped tokens. It is also an
|
|
128
204
|
* OpenID Connect provider (under `/oauth`) signing with the same key, and an
|
|
129
|
-
* imitation of GitHub's OAuth apps and REST API (under `/github`). The
|
|
205
|
+
* imitation of GitHub's OAuth apps, a GitHub App and REST API (under `/github`). The
|
|
130
206
|
* point is that the code under test runs its real remote code paths against
|
|
131
207
|
* real HTTP endpoints -- local mode is a different implementation of the same
|
|
132
208
|
* interface, never a branch that skips verification.
|
|
@@ -144,6 +220,8 @@ export declare class DevIdp {
|
|
|
144
220
|
autoApprove: boolean;
|
|
145
221
|
/** A CI platform's ID tokens, under `/workloads`, for trust bindings. */
|
|
146
222
|
readonly workloads: WorkloadIssuer;
|
|
223
|
+
/** A deployment's GitHub App, under `/github/api` beside the fake GitHub. */
|
|
224
|
+
readonly githubApp: FakeGitHubApp;
|
|
147
225
|
constructor(options?: DevIdpOptions);
|
|
148
226
|
get origin(): string;
|
|
149
227
|
/** Matches the Cloudflare Access team-domain issuer shape. */
|
|
@@ -206,4 +284,4 @@ export declare class DevIdp {
|
|
|
206
284
|
}): Promise<string>;
|
|
207
285
|
}
|
|
208
286
|
//#endregion
|
|
209
|
-
export type { DevIdpClient, DevIdpOptions, GitHubAccount, GitHubAccountPatch, Persona, RegisteredClient };
|
|
287
|
+
export type { DevIdpClient, DevIdpOptions, FakeInstallation, FakeRepository, GitHubAccount, GitHubAccountPatch, Persona, RegisteredClient };
|
package/dist/idp.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as
|
|
2
|
-
export { DEFAULT_CLIENT, DevIdp, GITHUB_RUN, PERSONAS, WorkloadIssuer };
|
|
1
|
+
import { a as FakeGitHubApp, i as WorkloadIssuer, n as DevIdp, o as fakeInstallations, r as GITHUB_RUN, s as PERSONAS, t as DEFAULT_CLIENT } from "./idp-B8u4UPA4.js";
|
|
2
|
+
export { DEFAULT_CLIENT, DevIdp, FakeGitHubApp, GITHUB_RUN, PERSONAS, WorkloadIssuer, fakeInstallations };
|