pico-engine 1.4.0 → 1.5.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/README.md +147 -0
- package/dist/auth/AuthService.js +537 -0
- package/dist/auth/index.js +8 -0
- package/dist/auth/webauthn.js +97 -0
- package/dist/cli.js +11 -0
- package/dist/index.js +75 -43
- package/dist/oauth/OAuthService.js +356 -0
- package/dist/oauth/acg.js +495 -0
- package/dist/oauth/channelEligibility.js +26 -0
- package/dist/oauth/errors.js +12 -0
- package/dist/oauth/index.js +21 -0
- package/dist/oauth/krlModule.js +27 -0
- package/dist/oauth/meshOAuth.js +43 -0
- package/dist/provisionRoot.js +80 -0
- package/dist/server.js +558 -15
- package/krl/io.picolabs.oauth.krl +48 -0
- package/krl/io.picolabs.wrangler.krl +4 -4
- package/package.json +14 -8
- package/public/pico-engine-ui.css +1 -1
- package/public/pico-engine-ui.js +21 -20
- package/public/pico-labs-logo.png +0 -0
- package/src/auth/AuthService.ts +755 -0
- package/src/auth/index.ts +17 -0
- package/src/auth/webauthn.ts +182 -0
- package/src/cli.ts +11 -0
- package/src/index.ts +115 -52
- package/src/oauth/OAuthService.ts +474 -0
- package/src/oauth/acg.ts +666 -0
- package/src/oauth/channelEligibility.ts +26 -0
- package/src/oauth/errors.ts +10 -0
- package/src/oauth/index.ts +14 -0
- package/src/oauth/krlModule.ts +31 -0
- package/src/oauth/meshOAuth.ts +50 -0
- package/src/provisionRoot.ts +97 -0
- package/src/server.ts +643 -24
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export {
|
|
2
|
+
OAuthService,
|
|
3
|
+
OAuthError,
|
|
4
|
+
OAuthServiceDeps,
|
|
5
|
+
OAuthChannelStatus,
|
|
6
|
+
OAuthChannelCredentials,
|
|
7
|
+
OAuthTokenResponse,
|
|
8
|
+
parseApproveBody,
|
|
9
|
+
parseAuthorizeQuery,
|
|
10
|
+
renderConsentHtml,
|
|
11
|
+
} from "./OAuthService";
|
|
12
|
+
export { OAUTH_WEBHOOK_TAG, isOAuthEligibleChannel, isSubscriptionChannel } from "./channelEligibility";
|
|
13
|
+
export { OAUTH_MESH_RULESET_RID, meshRequiresOAuth, rootHasOAuthMeshRuleset, rootPicoIdForChannelEci, isChannelUnderRoot } from "./meshOAuth";
|
|
14
|
+
export { default as initOAuthModule } from "./krlModule";
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { krl } from "krl-stdlib";
|
|
2
|
+
import { OAuthService } from "./OAuthService";
|
|
3
|
+
|
|
4
|
+
export default function initOAuthModule(oauth: OAuthService) {
|
|
5
|
+
const module: krl.Module = {
|
|
6
|
+
channelStatus: krl.Function(["eci"], function (eci: string) {
|
|
7
|
+
return oauth.channelStatusAsync(eci);
|
|
8
|
+
}),
|
|
9
|
+
|
|
10
|
+
meshRequiresOAuth: krl.Function(["eci"], function (eci: string) {
|
|
11
|
+
return oauth.meshRequiresOAuth(eci);
|
|
12
|
+
}),
|
|
13
|
+
|
|
14
|
+
createChannelSecret: krl.Function(["eci"], function (eci: string) {
|
|
15
|
+
oauth.assertChannelManagedBy(eci, this.rsCtx.pico().id);
|
|
16
|
+
return oauth.createChannelSecret(eci);
|
|
17
|
+
}),
|
|
18
|
+
|
|
19
|
+
revokeChannelSecret: krl.Function(["eci"], function (eci: string) {
|
|
20
|
+
oauth.assertChannelManagedBy(eci, this.rsCtx.pico().id);
|
|
21
|
+
return oauth.revokeChannelSecret(eci).then(() => ({ ok: true }));
|
|
22
|
+
}),
|
|
23
|
+
|
|
24
|
+
revokeTokens: krl.Function(["eci"], function (eci: string) {
|
|
25
|
+
oauth.assertChannelManagedBy(eci, this.rsCtx.pico().id);
|
|
26
|
+
return oauth.revokeTokens(eci).then(() => ({ ok: true }));
|
|
27
|
+
}),
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
return module;
|
|
31
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { PicoFramework } from "pico-framework";
|
|
2
|
+
|
|
3
|
+
/** Optional ruleset installed on a root pico to enable mesh-level OAuth (Layer 3b). */
|
|
4
|
+
export const OAUTH_MESH_RULESET_RID = "io.picolabs.oauth";
|
|
5
|
+
|
|
6
|
+
export function rootPicoIdForChannelEci(
|
|
7
|
+
pf: PicoFramework,
|
|
8
|
+
eci: string
|
|
9
|
+
): string | null {
|
|
10
|
+
try {
|
|
11
|
+
let pico = pf.lookupChannel(eci).pico;
|
|
12
|
+
while (pico.parent) {
|
|
13
|
+
pico = pf.getPico(pico.parent);
|
|
14
|
+
}
|
|
15
|
+
return pico.id;
|
|
16
|
+
} catch (_e) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** True when the channel's root pico has the mesh OAuth ruleset installed. */
|
|
22
|
+
export function meshRequiresOAuth(pf: PicoFramework, eci: string): boolean {
|
|
23
|
+
const rootId = rootPicoIdForChannelEci(pf, eci);
|
|
24
|
+
if (!rootId) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
return rootHasOAuthMeshRuleset(pf, rootId);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function rootHasOAuthMeshRuleset(
|
|
31
|
+
pf: PicoFramework,
|
|
32
|
+
rootPicoId: string
|
|
33
|
+
): boolean {
|
|
34
|
+
try {
|
|
35
|
+
const root = pf.getPico(rootPicoId);
|
|
36
|
+
return OAUTH_MESH_RULESET_RID in root.rulesets;
|
|
37
|
+
} catch (_e) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** True when the channel ECI belongs to a pico under rootPicoId (inclusive). */
|
|
43
|
+
export function isChannelUnderRoot(
|
|
44
|
+
pf: PicoFramework,
|
|
45
|
+
channelEci: string,
|
|
46
|
+
rootPicoId: string
|
|
47
|
+
): boolean {
|
|
48
|
+
const channelRootId = rootPicoIdForChannelEci(pf, channelEci);
|
|
49
|
+
return !!channelRootId && channelRootId === rootPicoId;
|
|
50
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import * as path from "path";
|
|
2
|
+
import { PicoEngineCore } from "pico-engine-core";
|
|
3
|
+
import { Pico, PicoFramework } from "pico-framework";
|
|
4
|
+
import { toFileUrl } from "./utils/toFileUrl";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Base rulesets installed onto every root pico. These make up the engine's
|
|
8
|
+
* "OS" layer (wrangler + friends) plus the built-in UI ruleset.
|
|
9
|
+
*/
|
|
10
|
+
const BASE_KRL_FILES = [
|
|
11
|
+
"io.picolabs.pico-engine-ui.krl",
|
|
12
|
+
"io.picolabs.wrangler.krl",
|
|
13
|
+
"io.picolabs.subscription.krl",
|
|
14
|
+
"io.picolabs.did-o.krl",
|
|
15
|
+
"io.picolabs.pds.krl",
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
const UI_CHANNEL_TAGS = ["engine", "ui"];
|
|
19
|
+
|
|
20
|
+
function uiChannelKey(tags: string[]): string {
|
|
21
|
+
return tags.slice(0).sort().join(",");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Find the ["engine","ui"] channel on a pico, if it exists.
|
|
26
|
+
*/
|
|
27
|
+
export function uiECIForRoot(
|
|
28
|
+
pf: PicoFramework,
|
|
29
|
+
rootPicoId: string
|
|
30
|
+
): string | null {
|
|
31
|
+
const pico = pf.rootPicos().find((p) => p.id === rootPicoId);
|
|
32
|
+
if (!pico) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
const chann = pico
|
|
36
|
+
.toReadOnly()
|
|
37
|
+
.channels.find((c) => uiChannelKey(UI_CHANNEL_TAGS) === uiChannelKey(c.tags));
|
|
38
|
+
return chann ? chann.id : null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Provision a root pico: install the base rulesets, ensure the ["engine","ui"]
|
|
43
|
+
* channel exists, fire `engine_ui setup`, and return the root + its UI ECI.
|
|
44
|
+
*
|
|
45
|
+
* Pass an existing `root` to (re)provision it — this is how the engine migrates
|
|
46
|
+
* a pre-existing single-root install. Omit it to mint a brand new root, which is
|
|
47
|
+
* what the passkey new-account registration flow does.
|
|
48
|
+
*/
|
|
49
|
+
export async function provisionRoot(
|
|
50
|
+
pf: PicoFramework,
|
|
51
|
+
core: PicoEngineCore,
|
|
52
|
+
opts: { root?: Pico; name?: string } = {}
|
|
53
|
+
): Promise<{ root: Pico; uiECI: string }> {
|
|
54
|
+
const root = opts.root || (await pf.createRootPico());
|
|
55
|
+
|
|
56
|
+
for (const file of BASE_KRL_FILES) {
|
|
57
|
+
const url = toFileUrl(path.resolve(__dirname, "..", "krl", file));
|
|
58
|
+
const { ruleset } = await core.rsRegistry.flush(url);
|
|
59
|
+
await root.install(ruleset, { url, config: {} });
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let uiChannel = root
|
|
63
|
+
.toReadOnly()
|
|
64
|
+
.channels.find((c) => uiChannelKey(UI_CHANNEL_TAGS) === uiChannelKey(c.tags));
|
|
65
|
+
if (!uiChannel) {
|
|
66
|
+
uiChannel = (
|
|
67
|
+
await root.newChannel({
|
|
68
|
+
tags: UI_CHANNEL_TAGS,
|
|
69
|
+
eventPolicy: {
|
|
70
|
+
allow: [{ domain: "engine_ui", name: "setup" }],
|
|
71
|
+
deny: [],
|
|
72
|
+
},
|
|
73
|
+
})
|
|
74
|
+
).toReadOnly();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
await pf.eventWait({
|
|
78
|
+
eci: uiChannel.id,
|
|
79
|
+
domain: "engine_ui",
|
|
80
|
+
name: "setup",
|
|
81
|
+
data: { attrs: {} },
|
|
82
|
+
time: 0,
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const name = (opts.name || "").trim();
|
|
86
|
+
if (name) {
|
|
87
|
+
await pf.eventWait({
|
|
88
|
+
eci: uiChannel.id,
|
|
89
|
+
domain: "engine_ui",
|
|
90
|
+
name: "box",
|
|
91
|
+
data: { attrs: { name } },
|
|
92
|
+
time: 0,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return { root, uiECI: uiChannel.id };
|
|
97
|
+
}
|