@calimero-network/mero-js 19.3.0 → 19.4.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/index.browser.mjs +2 -2
- package/dist/index.browser.mjs.map +4 -4
- package/dist/index.cjs +101 -0
- package/dist/index.cjs.map +4 -4
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +101 -0
- package/dist/index.mjs.map +4 -4
- package/dist/login/index.d.ts +2 -0
- package/dist/login/index.js +1 -0
- package/dist/login/session.d.ts +108 -0
- package/dist/login/session.js +148 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -24,6 +24,8 @@ export type { SignMemberJoinInput } from './namespace-op/index.js';
|
|
|
24
24
|
export type { WarrantInput } from './warrant/index.js';
|
|
25
25
|
export { signLoginStatement } from './login/index.js';
|
|
26
26
|
export type { Audience, LoginStatementInput } from './login/index.js';
|
|
27
|
+
export { login, generateSessionKey } from './login/index.js';
|
|
28
|
+
export type { LoginConfig, DelegatedSession, SessionKeyPair, } from './login/index.js';
|
|
27
29
|
export * from './capabilities.js';
|
|
28
30
|
export { DEFAULT_LOCAL_NODE_PORTS, localNodeUrl, nodeEndpoint, probeNodeHealth, discoverLocalNodes, } from './nodeDiscovery.js';
|
|
29
31
|
export type { DiscoverLocalNodesOptions } from './nodeDiscovery.js';
|
package/dist/index.js
CHANGED
|
@@ -28,6 +28,7 @@ export { signDeviceCert, mintDeviceId, deviceCertPayload, accountForRoot, } from
|
|
|
28
28
|
export { signMemberJoinOp, encodeSignedInvitation, SIGNED_NAMESPACE_OP_SCHEMA_VERSION, } from './namespace-op/index.js';
|
|
29
29
|
// Login-statement signing — the device's half of a password-free session
|
|
30
30
|
export { signLoginStatement } from './login/index.js';
|
|
31
|
+
export { login, generateSessionKey } from './login/index.js';
|
|
31
32
|
// Member capability bitmask constants & helpers
|
|
32
33
|
export * from './capabilities.js';
|
|
33
34
|
// Utilities
|
package/dist/index.mjs
CHANGED
|
@@ -3674,6 +3674,105 @@ async function signLoginStatement(input) {
|
|
|
3674
3674
|
);
|
|
3675
3675
|
}
|
|
3676
3676
|
|
|
3677
|
+
// src/login/session.ts
|
|
3678
|
+
var DEFAULT_TTL_SECONDS2 = 300;
|
|
3679
|
+
async function generateSessionKey() {
|
|
3680
|
+
const secret = hex(crypto.getRandomValues(new Uint8Array(32)));
|
|
3681
|
+
return { publicKey: hex(await derivePublicKey(secret)), secret };
|
|
3682
|
+
}
|
|
3683
|
+
function audienceLabel(audience) {
|
|
3684
|
+
switch (audience.kind) {
|
|
3685
|
+
case "webOrigin":
|
|
3686
|
+
return audience.origin;
|
|
3687
|
+
case "codeSigningId":
|
|
3688
|
+
return audience.id;
|
|
3689
|
+
case "cli":
|
|
3690
|
+
return "cli";
|
|
3691
|
+
}
|
|
3692
|
+
}
|
|
3693
|
+
async function login(config) {
|
|
3694
|
+
const baseUrl = config.nodeUrl.replace(/\/+$/, "");
|
|
3695
|
+
const doFetch = config.fetch ?? globalThis.fetch;
|
|
3696
|
+
const timeoutMs = config.timeoutMs ?? 1e4;
|
|
3697
|
+
const request = async (path, init) => {
|
|
3698
|
+
const controller = new AbortController();
|
|
3699
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
3700
|
+
let response;
|
|
3701
|
+
try {
|
|
3702
|
+
response = await doFetch(`${baseUrl}${path}`, {
|
|
3703
|
+
...init,
|
|
3704
|
+
signal: controller.signal
|
|
3705
|
+
});
|
|
3706
|
+
} catch (err) {
|
|
3707
|
+
throw new HTTPError(
|
|
3708
|
+
0,
|
|
3709
|
+
err instanceof Error ? err.name : "NetworkError",
|
|
3710
|
+
`${baseUrl}${path}`,
|
|
3711
|
+
new Headers(),
|
|
3712
|
+
err instanceof Error ? err.message : String(err)
|
|
3713
|
+
);
|
|
3714
|
+
} finally {
|
|
3715
|
+
clearTimeout(timer);
|
|
3716
|
+
}
|
|
3717
|
+
const text = await response.text().catch(() => "");
|
|
3718
|
+
if (!response.ok) {
|
|
3719
|
+
throw new HTTPError(
|
|
3720
|
+
response.status,
|
|
3721
|
+
response.statusText,
|
|
3722
|
+
`${baseUrl}${path}`,
|
|
3723
|
+
response.headers,
|
|
3724
|
+
text
|
|
3725
|
+
);
|
|
3726
|
+
}
|
|
3727
|
+
return text ? JSON.parse(text) : {};
|
|
3728
|
+
};
|
|
3729
|
+
const challengeBody = await request("/auth/challenge", {
|
|
3730
|
+
method: "GET",
|
|
3731
|
+
headers: { Accept: "application/json" }
|
|
3732
|
+
});
|
|
3733
|
+
const challenge = challengeBody.challenge ?? challengeBody.data?.challenge;
|
|
3734
|
+
if (!challenge) {
|
|
3735
|
+
throw new Error("the node issued no challenge");
|
|
3736
|
+
}
|
|
3737
|
+
const session = await generateSessionKey();
|
|
3738
|
+
const issuedAt = Math.floor(Date.now() / 1e3);
|
|
3739
|
+
const statement = await signLoginStatement({
|
|
3740
|
+
node: config.node,
|
|
3741
|
+
audience: config.audience,
|
|
3742
|
+
challenge,
|
|
3743
|
+
sessionKey: session.publicKey,
|
|
3744
|
+
issuedAt,
|
|
3745
|
+
expiresAt: issuedAt + (config.ttlSeconds ?? DEFAULT_TTL_SECONDS2),
|
|
3746
|
+
deviceSecret: config.deviceSecret
|
|
3747
|
+
});
|
|
3748
|
+
const tokenBody = await request("/auth/token", {
|
|
3749
|
+
method: "POST",
|
|
3750
|
+
headers: { "Content-Type": "application/json", Accept: "application/json" },
|
|
3751
|
+
body: JSON.stringify({
|
|
3752
|
+
auth_method: "account_proof",
|
|
3753
|
+
public_key: session.publicKey,
|
|
3754
|
+
client_name: config.clientName ?? audienceLabel(config.audience),
|
|
3755
|
+
timestamp: issuedAt,
|
|
3756
|
+
provider_data: {
|
|
3757
|
+
challenge,
|
|
3758
|
+
login_statement: statement,
|
|
3759
|
+
account_proof: config.accountProof
|
|
3760
|
+
}
|
|
3761
|
+
})
|
|
3762
|
+
});
|
|
3763
|
+
const accessToken = tokenBody.access_token ?? tokenBody.data?.access_token;
|
|
3764
|
+
const refreshToken = tokenBody.refresh_token ?? tokenBody.data?.refresh_token;
|
|
3765
|
+
if (!accessToken) {
|
|
3766
|
+
throw new Error("the node minted no session");
|
|
3767
|
+
}
|
|
3768
|
+
return {
|
|
3769
|
+
accessToken,
|
|
3770
|
+
refreshToken: refreshToken ?? "",
|
|
3771
|
+
sessionKey: session.publicKey,
|
|
3772
|
+
sessionSecret: session.secret
|
|
3773
|
+
};
|
|
3774
|
+
}
|
|
3775
|
+
|
|
3677
3776
|
// src/capabilities.ts
|
|
3678
3777
|
var CAPABILITIES = {
|
|
3679
3778
|
CAN_CREATE_CONTEXT: 1 << 0,
|
|
@@ -3806,10 +3905,12 @@ export {
|
|
|
3806
3905
|
deviceCertPayload,
|
|
3807
3906
|
discoverLocalNodes,
|
|
3808
3907
|
encodeSignedInvitation,
|
|
3908
|
+
generateSessionKey,
|
|
3809
3909
|
hasCap,
|
|
3810
3910
|
intentHash,
|
|
3811
3911
|
jsonCodec,
|
|
3812
3912
|
localNodeUrl,
|
|
3913
|
+
login,
|
|
3813
3914
|
mintDeviceId,
|
|
3814
3915
|
nodeEndpoint,
|
|
3815
3916
|
parseAuthCallback,
|