@north-light/identity 0.3.748 → 0.3.750
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.d.ts +8 -0
- package/dist/index.js +18 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -131,3 +131,11 @@ export declare function verifyIdToken(options: {
|
|
|
131
131
|
issuer: string;
|
|
132
132
|
audience: string;
|
|
133
133
|
}): Promise<IdTokenClaims>;
|
|
134
|
+
/** A parsed principal: `user:<id>`, `app:<id>`, or `app:<id>/<segment>(/<segment>)*`. `namespace` is `user:<id>` or `app:<id>`; `path` is the segments under an app (empty for a namespace root). */
|
|
135
|
+
export interface Principal {
|
|
136
|
+
kind: 'user' | 'app';
|
|
137
|
+
namespace: string;
|
|
138
|
+
path: string[];
|
|
139
|
+
}
|
|
140
|
+
/** The one principal parser: every site that reads a principal string denies a `null`. Segments may contain `:` but not `/`; a bare id, an unknown prefix, an empty segment or a path under `user:` is rejected. */
|
|
141
|
+
export declare function parsePrincipal(value: string): Principal | null;
|
package/dist/index.js
CHANGED
|
@@ -368,3 +368,21 @@ export async function verifyIdToken(options) {
|
|
|
368
368
|
throw new Error('Invalid ID token claims');
|
|
369
369
|
return payload;
|
|
370
370
|
}
|
|
371
|
+
const PRINCIPAL_ID = /^[A-Za-z0-9_-]{1,64}$/;
|
|
372
|
+
const PRINCIPAL_SEGMENT = /^[^/\s]+$/;
|
|
373
|
+
/** The one principal parser: every site that reads a principal string denies a `null`. Segments may contain `:` but not `/`; a bare id, an unknown prefix, an empty segment or a path under `user:` is rejected. */
|
|
374
|
+
export function parsePrincipal(value) {
|
|
375
|
+
const m = /^(user|app):([^/]*)((?:\/.*)?)$/.exec(value);
|
|
376
|
+
if (m === null || !PRINCIPAL_ID.test(m[2]))
|
|
377
|
+
return null;
|
|
378
|
+
const kind = m[1];
|
|
379
|
+
const rest = m[3];
|
|
380
|
+
if (rest === '')
|
|
381
|
+
return { kind, namespace: `${kind}:${m[2]}`, path: [] };
|
|
382
|
+
if (kind === 'user')
|
|
383
|
+
return null;
|
|
384
|
+
const path = rest.slice(1).split('/');
|
|
385
|
+
if (!path.every((s) => PRINCIPAL_SEGMENT.test(s)))
|
|
386
|
+
return null;
|
|
387
|
+
return { kind, namespace: `app:${m[2]}`, path };
|
|
388
|
+
}
|