@me2em/core 0.4.4-alpha.2 → 0.6.0-alpha.1
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 +293 -546
- package/dist/attestation.d.ts +151 -0
- package/dist/attestation.d.ts.map +1 -0
- package/dist/attestation.js +279 -0
- package/dist/attestation.js.map +1 -0
- package/dist/canonical-name.d.ts +3 -0
- package/dist/canonical-name.d.ts.map +1 -0
- package/dist/canonical-name.js +12 -0
- package/dist/canonical-name.js.map +1 -0
- package/dist/crypto/derivation-paths.d.ts +41 -0
- package/dist/crypto/derivation-paths.d.ts.map +1 -0
- package/dist/crypto/derivation-paths.js +50 -0
- package/dist/crypto/derivation-paths.js.map +1 -0
- package/dist/crypto/ed25519.d.ts +48 -8
- package/dist/crypto/ed25519.d.ts.map +1 -1
- package/dist/crypto/ed25519.js +63 -8
- package/dist/crypto/ed25519.js.map +1 -1
- package/dist/crypto/hkdf.d.ts.map +1 -1
- package/dist/crypto/hkdf.js +1 -0
- package/dist/crypto/hkdf.js.map +1 -1
- package/dist/crypto/index.d.ts +1 -0
- package/dist/crypto/index.d.ts.map +1 -1
- package/dist/crypto/index.js +2 -0
- package/dist/crypto/index.js.map +1 -1
- package/dist/crypto/init.d.ts.map +1 -1
- package/dist/crypto/init.js +1 -0
- package/dist/crypto/init.js.map +1 -1
- package/dist/handle.d.ts +147 -28
- package/dist/handle.d.ts.map +1 -1
- package/dist/handle.js +178 -33
- package/dist/handle.js.map +1 -1
- package/dist/identity.d.ts +99 -19
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +128 -32
- package/dist/identity.js.map +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/seed.d.ts +22 -35
- package/dist/seed.d.ts.map +1 -1
- package/dist/seed.js +26 -36
- package/dist/seed.js.map +1 -1
- package/dist/session.d.ts +207 -17
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +428 -11
- package/dist/session.js.map +1 -1
- package/dist/subhandle.d.ts +126 -0
- package/dist/subhandle.d.ts.map +1 -0
- package/dist/subhandle.js +158 -0
- package/dist/subhandle.js.map +1 -0
- package/dist/util.d.ts +9 -0
- package/dist/util.d.ts.map +1 -0
- package/dist/util.js +34 -0
- package/dist/util.js.map +1 -0
- package/package.json +3 -3
- package/src/attestation.ts +364 -0
- package/src/canonical-name.ts +12 -0
- package/src/crypto/derivation-paths.ts +56 -0
- package/src/crypto/ed25519.ts +82 -8
- package/src/crypto/hkdf.ts +2 -0
- package/src/crypto/index.ts +3 -1
- package/src/crypto/init.ts +1 -0
- package/src/handle.ts +226 -36
- package/src/identity.ts +164 -35
- package/src/index.ts +5 -1
- package/src/seed.ts +41 -54
- package/src/session.ts +637 -24
- package/src/subhandle.ts +230 -0
- package/src/util.ts +35 -0
package/src/subhandle.ts
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
// subhandle.ts
|
|
2
|
+
//
|
|
3
|
+
// SubHandle is a leaf node in the Me2em derivation tree.
|
|
4
|
+
// With MAX_DEPTH = 2, a SubHandle CANNOT derive children.
|
|
5
|
+
// It inherits from Handle for polymorphism in Session handling.
|
|
6
|
+
|
|
7
|
+
import { Handle, type HandleMetadata } from './handle.js';
|
|
8
|
+
import { normalizeName } from './canonical-name.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Metadata for a {@link SubHandle}, extending {@link HandleMetadata} with
|
|
12
|
+
* constraints that govern session creation.
|
|
13
|
+
*
|
|
14
|
+
* All constraint fields are optional. When present, they are enforced by
|
|
15
|
+
* {@link SubHandle.validateSessionOptions} at session creation time.
|
|
16
|
+
*
|
|
17
|
+
* @category Types
|
|
18
|
+
*/
|
|
19
|
+
export interface SubHandleMetadata extends HandleMetadata {
|
|
20
|
+
/**
|
|
21
|
+
* Restricts the set of audiences this SubHandle may create sessions for.
|
|
22
|
+
* `undefined` = unrestricted; `[]` = deny all.
|
|
23
|
+
*/
|
|
24
|
+
allowedAudiences?: string[];
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Restricts the set of scopes this SubHandle may request in sessions.
|
|
28
|
+
* `undefined` = unrestricted; `[]` = deny all.
|
|
29
|
+
*/
|
|
30
|
+
allowedScopes?: string[];
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Maximum allowed TTL (in seconds) for sessions created by this SubHandle.
|
|
34
|
+
* Attempts to create a session with a larger TTL will be rejected.
|
|
35
|
+
*/
|
|
36
|
+
maxSessionTtl?: number;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Unix timestamp (in seconds) at which this SubHandle itself expires.
|
|
40
|
+
* After this time, any attempt to create a session will fail.
|
|
41
|
+
* Useful for temporary access grants (e.g., contractor access for 2 weeks).
|
|
42
|
+
*/
|
|
43
|
+
expiresAt?: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Represents a context-isolated child Handle in the Me2em hierarchy.
|
|
48
|
+
*
|
|
49
|
+
* A SubHandle is always derived from a parent {@link Handle} and represents
|
|
50
|
+
* a leaf node in the derivation tree (MAX_DEPTH = 2). It inherits all
|
|
51
|
+
* cryptographic capabilities of Handle (signing, key derivation, etc.) but
|
|
52
|
+
* adds:
|
|
53
|
+
*
|
|
54
|
+
* - A **derivation path** (`[handleName, subName]`) carried in session tokens.
|
|
55
|
+
* - **Constraint enforcement** on audience, scopes, and TTL at session creation.
|
|
56
|
+
* - An optional **expiration timestamp** for temporary access grants.
|
|
57
|
+
*
|
|
58
|
+
* SubHandle is used for polymorphism in {@link Session}: both Handle and
|
|
59
|
+
* SubHandle can create and verify sessions, but SubHandle sessions carry
|
|
60
|
+
* the additional `hPath` field for hierarchical auditing.
|
|
61
|
+
*
|
|
62
|
+
* @category Core Primitives
|
|
63
|
+
*/
|
|
64
|
+
export class SubHandle extends Handle {
|
|
65
|
+
private readonly _path: string[];
|
|
66
|
+
private readonly _subMetadata: SubHandleMetadata;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Creates a new SubHandle instance.
|
|
70
|
+
*
|
|
71
|
+
* @internal Typically created via {@link Handle.deriveSubHandle} or
|
|
72
|
+
* {@link Identity.deriveSubHandle}. Direct construction is discouraged.
|
|
73
|
+
*
|
|
74
|
+
* @param privateKey - The 32-byte Ed25519 private key for this SubHandle.
|
|
75
|
+
* @param name - The SubHandle name.
|
|
76
|
+
* @param path - The derivation path, must have exactly 2 elements: `[handleName, subName]`.
|
|
77
|
+
* @param subMetadata - Optional constraints and metadata.
|
|
78
|
+
* @throws {Error} If the path does not have exactly 2 elements.
|
|
79
|
+
*/
|
|
80
|
+
constructor(
|
|
81
|
+
privateKey: Uint8Array,
|
|
82
|
+
name: string,
|
|
83
|
+
path: string[],
|
|
84
|
+
subMetadata?: SubHandleMetadata
|
|
85
|
+
) {
|
|
86
|
+
super(privateKey, name, subMetadata);
|
|
87
|
+
|
|
88
|
+
if (path.length !== 2) {
|
|
89
|
+
throw new Error(
|
|
90
|
+
`SubHandle path must have exactly 2 elements: [handleName, subName]. Got ${path.length}.`
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const normalizedName = normalizeName(name);
|
|
95
|
+
if (path[1] !== normalizedName) {
|
|
96
|
+
throw new Error(`path[1] must equal normalized name: path[1]=${JSON.stringify(path[1])}, name=${JSON.stringify(normalizedName)}`);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
this._path = path;
|
|
100
|
+
this._subMetadata = subMetadata ?? {};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Returns the full derivation path of this SubHandle.
|
|
105
|
+
*
|
|
106
|
+
* @returns A copy of the path array, e.g. `["station-001", "connector-1"]`.
|
|
107
|
+
*/
|
|
108
|
+
getPath(): string[] {
|
|
109
|
+
return [...this._path];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Returns the derivation path as a slash-separated string.
|
|
114
|
+
*
|
|
115
|
+
* @returns The path string, e.g. `"station-001/connector-1"`.
|
|
116
|
+
*/
|
|
117
|
+
getPathString(): string {
|
|
118
|
+
return this._path.join('/');
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Returns the depth of this SubHandle in the derivation hierarchy.
|
|
123
|
+
*
|
|
124
|
+
* With MAX_DEPTH = 2, this is always `2` (Identity = 0, Handle = 1, SubHandle = 2).
|
|
125
|
+
*
|
|
126
|
+
* @returns The depth, always `2`.
|
|
127
|
+
*/
|
|
128
|
+
getDepth(): number {
|
|
129
|
+
return 2;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Returns whether this SubHandle is a leaf node.
|
|
134
|
+
*
|
|
135
|
+
* With MAX_DEPTH = 2, a SubHandle is **always** a leaf and cannot derive children.
|
|
136
|
+
*
|
|
137
|
+
* @returns Always `true`.
|
|
138
|
+
*/
|
|
139
|
+
isLeaf(): boolean {
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* SubHandle is a leaf node (depth = 2) and cannot derive children.
|
|
145
|
+
* Overrides the inherited `Handle.deriveSubHandle` to throw an error.
|
|
146
|
+
*/
|
|
147
|
+
override async deriveSubHandle(): Promise<never> {
|
|
148
|
+
throw new Error('Cannot derive child: maximum depth (2) reached or this SubHandle is marked as leaf');
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Returns the SubHandle-specific metadata (constraints).
|
|
153
|
+
*
|
|
154
|
+
* @returns The SubHandleMetadata object.
|
|
155
|
+
*/
|
|
156
|
+
getSubMetadata(): SubHandleMetadata {
|
|
157
|
+
return {
|
|
158
|
+
...this._subMetadata,
|
|
159
|
+
allowedAudiences: this._subMetadata.allowedAudiences
|
|
160
|
+
? [...this._subMetadata.allowedAudiences]
|
|
161
|
+
: undefined,
|
|
162
|
+
allowedScopes: this._subMetadata.allowedScopes
|
|
163
|
+
? [...this._subMetadata.allowedScopes]
|
|
164
|
+
: undefined,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Validates session options against this SubHandle's constraints.
|
|
170
|
+
*
|
|
171
|
+
* Called internally by {@link Session.create} before signing. Throws if
|
|
172
|
+
* any constraint is violated:
|
|
173
|
+
* - `audience` not in `allowedAudiences`
|
|
174
|
+
* - any `scope` not in `allowedScopes`
|
|
175
|
+
* - `ttl` exceeds `maxSessionTtl`
|
|
176
|
+
* - current time is past `expiresAt`
|
|
177
|
+
*
|
|
178
|
+
* @param options - The session options to validate.
|
|
179
|
+
* @throws {Error} If any constraint is violated.
|
|
180
|
+
*/
|
|
181
|
+
validateSessionOptions(options: {
|
|
182
|
+
audience: string;
|
|
183
|
+
scopes: string[];
|
|
184
|
+
ttl: number;
|
|
185
|
+
}): void {
|
|
186
|
+
// Audience check: undefined = unrestricted, [] = deny all
|
|
187
|
+
if (this._subMetadata.allowedAudiences !== undefined) {
|
|
188
|
+
if (!this._subMetadata.allowedAudiences.includes(options.audience)) {
|
|
189
|
+
throw new Error(
|
|
190
|
+
`Audience "${options.audience}" not allowed for this SubHandle. ` +
|
|
191
|
+
`Allowed: ${this._subMetadata.allowedAudiences.length === 0
|
|
192
|
+
? '(none — empty list denies all)'
|
|
193
|
+
: this._subMetadata.allowedAudiences.join(', ')}`
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Scopes check: undefined = unrestricted, [] = deny all
|
|
199
|
+
if (this._subMetadata.allowedScopes !== undefined) {
|
|
200
|
+
const forbidden = options.scopes.filter(
|
|
201
|
+
s => !this._subMetadata.allowedScopes!.includes(s)
|
|
202
|
+
);
|
|
203
|
+
// An empty allow-list denies every requested scope.
|
|
204
|
+
if (forbidden.length > 0) {
|
|
205
|
+
throw new Error(
|
|
206
|
+
`Scopes not allowed for this SubHandle: ${forbidden.join(', ')}`
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// TTL check
|
|
212
|
+
if (
|
|
213
|
+
this._subMetadata.maxSessionTtl !== undefined &&
|
|
214
|
+
options.ttl > this._subMetadata.maxSessionTtl
|
|
215
|
+
) {
|
|
216
|
+
throw new Error(
|
|
217
|
+
`TTL ${options.ttl}s exceeds max allowed ` +
|
|
218
|
+
`${this._subMetadata.maxSessionTtl}s for this SubHandle`
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// SubHandle expiration check
|
|
223
|
+
if (this._subMetadata.expiresAt !== undefined) {
|
|
224
|
+
const now = Math.floor(Date.now() / 1000);
|
|
225
|
+
if (now >= this._subMetadata.expiresAt) {
|
|
226
|
+
throw new Error('SubHandle has expired');
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
package/src/util.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// util.ts
|
|
2
|
+
//
|
|
3
|
+
// Shared utility functions for the Me2em protocol.
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Encodes a Uint8Array to a URL-safe Base64 string (no padding).
|
|
7
|
+
*/
|
|
8
|
+
export function base64urlEncode(bytes: Uint8Array): string {
|
|
9
|
+
let binary = '';
|
|
10
|
+
for (const byte of bytes) {
|
|
11
|
+
binary += String.fromCharCode(byte);
|
|
12
|
+
}
|
|
13
|
+
return btoa(binary)
|
|
14
|
+
.replace(/\+/g, '-')
|
|
15
|
+
.replace(/\//g, '_')
|
|
16
|
+
.replace(/=+$/, '');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Decodes a URL-safe Base64 string to a Uint8Array.
|
|
21
|
+
*/
|
|
22
|
+
export function base64urlDecode(input: string): Uint8Array {
|
|
23
|
+
let base64 = input
|
|
24
|
+
.replace(/-/g, '+')
|
|
25
|
+
.replace(/_/g, '/');
|
|
26
|
+
while (base64.length % 4 !== 0) {
|
|
27
|
+
base64 += '=';
|
|
28
|
+
}
|
|
29
|
+
const binary = atob(base64);
|
|
30
|
+
const bytes = new Uint8Array(binary.length);
|
|
31
|
+
for (let i = 0; i < binary.length; i++) {
|
|
32
|
+
bytes[i] = binary.charCodeAt(i);
|
|
33
|
+
}
|
|
34
|
+
return bytes;
|
|
35
|
+
}
|