@entifix/jwt 0.1.0-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/LICENSE +21 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/token-service/jose-token-service.d.ts +58 -0
- package/dist/token-service/jose-token-service.d.ts.map +1 -0
- package/dist/token-service/jose-token-service.js +80 -0
- package/dist/token-service/jose-token-service.js.map +1 -0
- package/dist/token-service/jwks.d.ts +17 -0
- package/dist/token-service/jwks.d.ts.map +1 -0
- package/dist/token-service/jwks.js +33 -0
- package/dist/token-service/jwks.js.map +1 -0
- package/dist/token-service/unverified-claims.d.ts +18 -0
- package/dist/token-service/unverified-claims.d.ts.map +1 -0
- package/dist/token-service/unverified-claims.js +38 -0
- package/dist/token-service/unverified-claims.js.map +1 -0
- package/package.json +68 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 r10c Technologies
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,uCAAuC,CAAC;AACtD,cAAc,yBAAyB,CAAC;AACxC,cAAc,sCAAsC,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export * from './token-service/jose-token-service.js';\nexport * from './token-service/jwks.js';\nexport * from './token-service/unverified-claims.js';\n"],"names":[],"mappings":"AAAA,cAAc,wCAAwC;AACtD,cAAc,0BAA0B;AACxC,cAAc,uCAAuC"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { TokenClaims, TokenService } from '@entifix/business';
|
|
2
|
+
/** The one algorithm this service signs and accepts. See {@link verifyAccessToken}. */
|
|
3
|
+
export declare const TOKEN_ALGORITHM = "RS256";
|
|
4
|
+
/**
|
|
5
|
+
* Settings for signing and verifying access tokens.
|
|
6
|
+
*
|
|
7
|
+
* Asymmetric on purpose. The verification half travels to every service — and,
|
|
8
|
+
* once buyers can sign in, to the public storefront — so it must be material
|
|
9
|
+
* that cannot mint a token. A shared secret would have put the *signing* key in
|
|
10
|
+
* the least privileged, highest traffic app in the fleet, where a compromise
|
|
11
|
+
* mints `super-admin`.
|
|
12
|
+
*/
|
|
13
|
+
export interface JoseTokenServiceOptions {
|
|
14
|
+
/** Public key, SPKI PEM. Always required — every holder at least verifies. */
|
|
15
|
+
readonly publicKeyPem: string;
|
|
16
|
+
/**
|
|
17
|
+
* Private key, PKCS#8 PEM. Only the service that mints tokens holds one; an
|
|
18
|
+
* instance without it verifies and refuses to sign.
|
|
19
|
+
*/
|
|
20
|
+
readonly privateKeyPem?: string;
|
|
21
|
+
/**
|
|
22
|
+
* The `kid` header stamped on every signed token, and the name of the key a
|
|
23
|
+
* JWKS publishes it under. Rotation is two keys sharing a verifier for a
|
|
24
|
+
* while, which is only expressible if tokens say which key signed them.
|
|
25
|
+
*/
|
|
26
|
+
readonly keyId: string;
|
|
27
|
+
/** `iss` claim set on sign and required on verify. */
|
|
28
|
+
readonly issuer: string;
|
|
29
|
+
/** `aud` claim set on sign and required on verify. */
|
|
30
|
+
readonly audience: string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Sign {@link TokenClaims} into a compact JWT that expires after `ttlSeconds`.
|
|
34
|
+
* Exported on its own (not only behind the Effect service) so the same signing
|
|
35
|
+
* path is testable and reusable outside an Effect runtime.
|
|
36
|
+
*/
|
|
37
|
+
export declare const signAccessToken: (claims: TokenClaims, options: JoseTokenServiceOptions, ttlSeconds: number) => Promise<string>;
|
|
38
|
+
/**
|
|
39
|
+
* Verify a token's signature, issuer, audience and expiry, returning its
|
|
40
|
+
* claims. A **pure Promise** so it runs anywhere jose does — notably a Next
|
|
41
|
+
* middleware / edge runtime where pulling in the Effect runtime is unwanted.
|
|
42
|
+
* Rejects on any invalid token.
|
|
43
|
+
*
|
|
44
|
+
* `algorithms` is pinned to a single value and that is this file's security
|
|
45
|
+
* boundary: left unset, jose honours the algorithm named in the token's own
|
|
46
|
+
* header, so a caller could present an `HS256` token and have the public key
|
|
47
|
+
* treated as a shared secret. One accepted algorithm makes that unexpressible.
|
|
48
|
+
*/
|
|
49
|
+
export declare const verifyAccessToken: (token: string, options: JoseTokenServiceOptions) => Promise<TokenClaims>;
|
|
50
|
+
/**
|
|
51
|
+
* A `jose`-backed {@link TokenService}. The perimeter provides it with both keys
|
|
52
|
+
* to mint access tokens from a resolved session; every downstream service
|
|
53
|
+
* provides it with the public key alone to verify them. Signing failures are
|
|
54
|
+
* infrastructure errors; a verification failure is the caller presenting a bad
|
|
55
|
+
* token — an {@link EntifixBuildError}, which the perimeter collapses to `401`.
|
|
56
|
+
*/
|
|
57
|
+
export declare const makeJoseTokenService: (options: JoseTokenServiceOptions) => TokenService;
|
|
58
|
+
//# sourceMappingURL=jose-token-service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jose-token-service.d.ts","sourceRoot":"","sources":["../../src/token-service/jose-token-service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAWnE,uFAAuF;AACvF,eAAO,MAAM,eAAe,UAAU,CAAC;AAEvC;;;;;;;;GAQG;AACH,MAAM,WAAW,uBAAuB;IACtC,8EAA8E;IAC9E,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B;;;OAGG;IACH,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;IAChC;;;;OAIG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,sDAAsD;IACtD,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,sDAAsD;IACtD,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC3B;AAkDD;;;;GAIG;AACH,eAAO,MAAM,eAAe,GAC1B,QAAQ,WAAW,EACnB,SAAS,uBAAuB,EAChC,YAAY,MAAM,KACjB,OAAO,CAAC,MAAM,CAOmB,CAAC;AAErC;;;;;;;;;;GAUG;AACH,eAAO,MAAM,iBAAiB,GAC5B,OAAO,MAAM,EACb,SAAS,uBAAuB,KAC/B,OAAO,CAAC,WAAW,CAOrB,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,GAC/B,SAAS,uBAAuB,KAC/B,YAkBD,CAAC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { _ as _extends } from "@swc/helpers/_/_extends";
|
|
2
|
+
import { EntifixBuildError, EntifixConnError } from '@entifix/core';
|
|
3
|
+
import { Effect } from 'effect';
|
|
4
|
+
import { importPKCS8, importSPKI, jwtVerify, SignJWT } from 'jose';
|
|
5
|
+
/** The one algorithm this service signs and accepts. See {@link verifyAccessToken}. */ export const TOKEN_ALGORITHM = 'RS256';
|
|
6
|
+
/**
|
|
7
|
+
* Import a PEM once per options object rather than once per token.
|
|
8
|
+
*
|
|
9
|
+
* `importPKCS8`/`importSPKI` are async, but the factory below has to stay
|
|
10
|
+
* synchronously constructible — every composition root builds it inside
|
|
11
|
+
* `Layer.succeed`. Caching the *promise* satisfies both: construction returns
|
|
12
|
+
* immediately, and the parse happens once no matter how many tokens follow.
|
|
13
|
+
*/ const privateKeys = new WeakMap();
|
|
14
|
+
const publicKeys = new WeakMap();
|
|
15
|
+
const cachedKey = (cache, options, load)=>{
|
|
16
|
+
const cached = cache.get(options);
|
|
17
|
+
if (cached !== undefined) {
|
|
18
|
+
return cached;
|
|
19
|
+
}
|
|
20
|
+
const loading = load();
|
|
21
|
+
cache.set(options, loading);
|
|
22
|
+
return loading;
|
|
23
|
+
};
|
|
24
|
+
/** The private key an options object signs with, or a build error if it has none. */ const signingKey = (options)=>{
|
|
25
|
+
const { privateKeyPem } = options;
|
|
26
|
+
if (privateKeyPem === undefined) {
|
|
27
|
+
return Promise.reject(new EntifixBuildError('This token service holds no private key and cannot sign — it was configured to verify only'));
|
|
28
|
+
}
|
|
29
|
+
return cachedKey(privateKeys, options, ()=>importPKCS8(privateKeyPem, TOKEN_ALGORITHM));
|
|
30
|
+
};
|
|
31
|
+
/** The public key an options object verifies with. Cached apart from the private one. */ const verificationKey = (options)=>cachedKey(publicKeys, options, ()=>importSPKI(options.publicKeyPem, TOKEN_ALGORITHM));
|
|
32
|
+
/**
|
|
33
|
+
* Sign {@link TokenClaims} into a compact JWT that expires after `ttlSeconds`.
|
|
34
|
+
* Exported on its own (not only behind the Effect service) so the same signing
|
|
35
|
+
* path is testable and reusable outside an Effect runtime.
|
|
36
|
+
*/ export const signAccessToken = async (claims, options, ttlSeconds)=>new SignJWT(_extends({}, claims)).setProtectedHeader({
|
|
37
|
+
alg: TOKEN_ALGORITHM,
|
|
38
|
+
kid: options.keyId
|
|
39
|
+
}).setIssuedAt().setIssuer(options.issuer).setAudience(options.audience).setExpirationTime(`${ttlSeconds}s`).sign(await signingKey(options));
|
|
40
|
+
/**
|
|
41
|
+
* Verify a token's signature, issuer, audience and expiry, returning its
|
|
42
|
+
* claims. A **pure Promise** so it runs anywhere jose does — notably a Next
|
|
43
|
+
* middleware / edge runtime where pulling in the Effect runtime is unwanted.
|
|
44
|
+
* Rejects on any invalid token.
|
|
45
|
+
*
|
|
46
|
+
* `algorithms` is pinned to a single value and that is this file's security
|
|
47
|
+
* boundary: left unset, jose honours the algorithm named in the token's own
|
|
48
|
+
* header, so a caller could present an `HS256` token and have the public key
|
|
49
|
+
* treated as a shared secret. One accepted algorithm makes that unexpressible.
|
|
50
|
+
*/ export const verifyAccessToken = async (token, options)=>{
|
|
51
|
+
const { payload } = await jwtVerify(token, await verificationKey(options), {
|
|
52
|
+
algorithms: [
|
|
53
|
+
TOKEN_ALGORITHM
|
|
54
|
+
],
|
|
55
|
+
issuer: options.issuer,
|
|
56
|
+
audience: options.audience
|
|
57
|
+
});
|
|
58
|
+
return payload;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* A `jose`-backed {@link TokenService}. The perimeter provides it with both keys
|
|
62
|
+
* to mint access tokens from a resolved session; every downstream service
|
|
63
|
+
* provides it with the public key alone to verify them. Signing failures are
|
|
64
|
+
* infrastructure errors; a verification failure is the caller presenting a bad
|
|
65
|
+
* token — an {@link EntifixBuildError}, which the perimeter collapses to `401`.
|
|
66
|
+
*/ export const makeJoseTokenService = (options)=>({
|
|
67
|
+
sign: (claims, ttlSeconds)=>Effect.tryPromise({
|
|
68
|
+
try: ()=>signAccessToken(claims, options, ttlSeconds),
|
|
69
|
+
// A missing private key is a misconfiguration, not a connectivity
|
|
70
|
+
// problem, and it must not be reported as one: it surfaces as the build
|
|
71
|
+
// error it already is rather than being wrapped into a retryable shape.
|
|
72
|
+
catch: (error)=>error instanceof EntifixBuildError ? error : new EntifixConnError('Access token signing failed', error)
|
|
73
|
+
}),
|
|
74
|
+
verify: (token)=>Effect.tryPromise({
|
|
75
|
+
try: ()=>verifyAccessToken(token, options),
|
|
76
|
+
catch: (error)=>new EntifixBuildError('Access token verification failed', error)
|
|
77
|
+
})
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
//# sourceMappingURL=jose-token-service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/token-service/jose-token-service.ts"],"sourcesContent":["import type { TokenClaims, TokenService } from '@entifix/business';\nimport { EntifixBuildError, EntifixConnError } from '@entifix/core';\nimport { Effect } from 'effect';\nimport {\n type CryptoKey,\n importPKCS8,\n importSPKI,\n jwtVerify,\n SignJWT,\n} from 'jose';\n\n/** The one algorithm this service signs and accepts. See {@link verifyAccessToken}. */\nexport const TOKEN_ALGORITHM = 'RS256';\n\n/**\n * Settings for signing and verifying access tokens.\n *\n * Asymmetric on purpose. The verification half travels to every service — and,\n * once buyers can sign in, to the public storefront — so it must be material\n * that cannot mint a token. A shared secret would have put the *signing* key in\n * the least privileged, highest traffic app in the fleet, where a compromise\n * mints `super-admin`.\n */\nexport interface JoseTokenServiceOptions {\n /** Public key, SPKI PEM. Always required — every holder at least verifies. */\n readonly publicKeyPem: string;\n /**\n * Private key, PKCS#8 PEM. Only the service that mints tokens holds one; an\n * instance without it verifies and refuses to sign.\n */\n readonly privateKeyPem?: string;\n /**\n * The `kid` header stamped on every signed token, and the name of the key a\n * JWKS publishes it under. Rotation is two keys sharing a verifier for a\n * while, which is only expressible if tokens say which key signed them.\n */\n readonly keyId: string;\n /** `iss` claim set on sign and required on verify. */\n readonly issuer: string;\n /** `aud` claim set on sign and required on verify. */\n readonly audience: string;\n}\n\n/**\n * Import a PEM once per options object rather than once per token.\n *\n * `importPKCS8`/`importSPKI` are async, but the factory below has to stay\n * synchronously constructible — every composition root builds it inside\n * `Layer.succeed`. Caching the *promise* satisfies both: construction returns\n * immediately, and the parse happens once no matter how many tokens follow.\n */\nconst privateKeys = new WeakMap<JoseTokenServiceOptions, Promise<CryptoKey>>();\nconst publicKeys = new WeakMap<JoseTokenServiceOptions, Promise<CryptoKey>>();\n\nconst cachedKey = (\n cache: WeakMap<JoseTokenServiceOptions, Promise<CryptoKey>>,\n options: JoseTokenServiceOptions,\n load: () => Promise<CryptoKey>,\n): Promise<CryptoKey> => {\n const cached = cache.get(options);\n if (cached !== undefined) {\n return cached;\n }\n const loading = load();\n cache.set(options, loading);\n return loading;\n};\n\n/** The private key an options object signs with, or a build error if it has none. */\nconst signingKey = (options: JoseTokenServiceOptions): Promise<CryptoKey> => {\n const { privateKeyPem } = options;\n if (privateKeyPem === undefined) {\n return Promise.reject(\n new EntifixBuildError(\n 'This token service holds no private key and cannot sign — it was configured to verify only',\n ),\n );\n }\n return cachedKey(privateKeys, options, () =>\n importPKCS8(privateKeyPem, TOKEN_ALGORITHM),\n );\n};\n\n/** The public key an options object verifies with. Cached apart from the private one. */\nconst verificationKey = (\n options: JoseTokenServiceOptions,\n): Promise<CryptoKey> =>\n cachedKey(publicKeys, options, () =>\n importSPKI(options.publicKeyPem, TOKEN_ALGORITHM),\n );\n\n/**\n * Sign {@link TokenClaims} into a compact JWT that expires after `ttlSeconds`.\n * Exported on its own (not only behind the Effect service) so the same signing\n * path is testable and reusable outside an Effect runtime.\n */\nexport const signAccessToken = async (\n claims: TokenClaims,\n options: JoseTokenServiceOptions,\n ttlSeconds: number,\n): Promise<string> =>\n new SignJWT({ ...claims })\n .setProtectedHeader({ alg: TOKEN_ALGORITHM, kid: options.keyId })\n .setIssuedAt()\n .setIssuer(options.issuer)\n .setAudience(options.audience)\n .setExpirationTime(`${ttlSeconds}s`)\n .sign(await signingKey(options));\n\n/**\n * Verify a token's signature, issuer, audience and expiry, returning its\n * claims. A **pure Promise** so it runs anywhere jose does — notably a Next\n * middleware / edge runtime where pulling in the Effect runtime is unwanted.\n * Rejects on any invalid token.\n *\n * `algorithms` is pinned to a single value and that is this file's security\n * boundary: left unset, jose honours the algorithm named in the token's own\n * header, so a caller could present an `HS256` token and have the public key\n * treated as a shared secret. One accepted algorithm makes that unexpressible.\n */\nexport const verifyAccessToken = async (\n token: string,\n options: JoseTokenServiceOptions,\n): Promise<TokenClaims> => {\n const { payload } = await jwtVerify(token, await verificationKey(options), {\n algorithms: [TOKEN_ALGORITHM],\n issuer: options.issuer,\n audience: options.audience,\n });\n return payload as unknown as TokenClaims;\n};\n\n/**\n * A `jose`-backed {@link TokenService}. The perimeter provides it with both keys\n * to mint access tokens from a resolved session; every downstream service\n * provides it with the public key alone to verify them. Signing failures are\n * infrastructure errors; a verification failure is the caller presenting a bad\n * token — an {@link EntifixBuildError}, which the perimeter collapses to `401`.\n */\nexport const makeJoseTokenService = (\n options: JoseTokenServiceOptions,\n): TokenService => ({\n sign: (claims, ttlSeconds) =>\n Effect.tryPromise({\n try: () => signAccessToken(claims, options, ttlSeconds),\n // A missing private key is a misconfiguration, not a connectivity\n // problem, and it must not be reported as one: it surfaces as the build\n // error it already is rather than being wrapped into a retryable shape.\n catch: error =>\n error instanceof EntifixBuildError\n ? error\n : new EntifixConnError('Access token signing failed', error),\n }),\n verify: token =>\n Effect.tryPromise({\n try: () => verifyAccessToken(token, options),\n catch: error =>\n new EntifixBuildError('Access token verification failed', error),\n }),\n});\n"],"names":["EntifixBuildError","EntifixConnError","Effect","importPKCS8","importSPKI","jwtVerify","SignJWT","TOKEN_ALGORITHM","privateKeys","WeakMap","publicKeys","cachedKey","cache","options","load","cached","get","undefined","loading","set","signingKey","privateKeyPem","Promise","reject","verificationKey","publicKeyPem","signAccessToken","claims","ttlSeconds","setProtectedHeader","alg","kid","keyId","setIssuedAt","setIssuer","issuer","setAudience","audience","setExpirationTime","sign","verifyAccessToken","token","payload","algorithms","makeJoseTokenService","tryPromise","try","catch","error","verify"],"mappings":";AACA,SAASA,iBAAiB,EAAEC,gBAAgB,QAAQ,gBAAgB;AACpE,SAASC,MAAM,QAAQ,SAAS;AAChC,SAEEC,WAAW,EACXC,UAAU,EACVC,SAAS,EACTC,OAAO,QACF,OAAO;AAEd,qFAAqF,GACrF,OAAO,MAAMC,kBAAkB,QAAQ;AA+BvC;;;;;;;CAOC,GACD,MAAMC,cAAc,IAAIC;AACxB,MAAMC,aAAa,IAAID;AAEvB,MAAME,YAAY,CAChBC,OACAC,SACAC;IAEA,MAAMC,SAASH,MAAMI,GAAG,CAACH;IACzB,IAAIE,WAAWE,WAAW;QACxB,OAAOF;IACT;IACA,MAAMG,UAAUJ;IAChBF,MAAMO,GAAG,CAACN,SAASK;IACnB,OAAOA;AACT;AAEA,mFAAmF,GACnF,MAAME,aAAa,CAACP;IAClB,MAAM,EAAEQ,aAAa,EAAE,GAAGR;IAC1B,IAAIQ,kBAAkBJ,WAAW;QAC/B,OAAOK,QAAQC,MAAM,CACnB,IAAIvB,kBACF;IAGN;IACA,OAAOW,UAAUH,aAAaK,SAAS,IACrCV,YAAYkB,eAAed;AAE/B;AAEA,uFAAuF,GACvF,MAAMiB,kBAAkB,CACtBX,UAEAF,UAAUD,YAAYG,SAAS,IAC7BT,WAAWS,QAAQY,YAAY,EAAElB;AAGrC;;;;CAIC,GACD,OAAO,MAAMmB,kBAAkB,OAC7BC,QACAd,SACAe,aAEA,IAAItB,QAAQ,aAAKqB,SACdE,kBAAkB,CAAC;QAAEC,KAAKvB;QAAiBwB,KAAKlB,QAAQmB,KAAK;IAAC,GAC9DC,WAAW,GACXC,SAAS,CAACrB,QAAQsB,MAAM,EACxBC,WAAW,CAACvB,QAAQwB,QAAQ,EAC5BC,iBAAiB,CAAC,GAAGV,WAAW,CAAC,CAAC,EAClCW,IAAI,CAAC,MAAMnB,WAAWP,UAAU;AAErC;;;;;;;;;;CAUC,GACD,OAAO,MAAM2B,oBAAoB,OAC/BC,OACA5B;IAEA,MAAM,EAAE6B,OAAO,EAAE,GAAG,MAAMrC,UAAUoC,OAAO,MAAMjB,gBAAgBX,UAAU;QACzE8B,YAAY;YAACpC;SAAgB;QAC7B4B,QAAQtB,QAAQsB,MAAM;QACtBE,UAAUxB,QAAQwB,QAAQ;IAC5B;IACA,OAAOK;AACT,EAAE;AAEF;;;;;;CAMC,GACD,OAAO,MAAME,uBAAuB,CAClC/B,UACkB,CAAA;QAClB0B,MAAM,CAACZ,QAAQC,aACb1B,OAAO2C,UAAU,CAAC;gBAChBC,KAAK,IAAMpB,gBAAgBC,QAAQd,SAASe;gBAC5C,kEAAkE;gBAClE,wEAAwE;gBACxE,wEAAwE;gBACxEmB,OAAOC,CAAAA,QACLA,iBAAiBhD,oBACbgD,QACA,IAAI/C,iBAAiB,+BAA+B+C;YAC5D;QACFC,QAAQR,CAAAA,QACNvC,OAAO2C,UAAU,CAAC;gBAChBC,KAAK,IAAMN,kBAAkBC,OAAO5B;gBACpCkC,OAAOC,CAAAA,QACL,IAAIhD,kBAAkB,oCAAoCgD;YAC9D;IACJ,CAAA,EAAG"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type JSONWebKeySet } from 'jose';
|
|
2
|
+
/**
|
|
3
|
+
* The service's public signing key as a JWK Set, ready to serve at
|
|
4
|
+
* `/.well-known/jwks.json`.
|
|
5
|
+
*
|
|
6
|
+
* Services in this fleet do **not** need it: each resolves `jwt.publicKey` from
|
|
7
|
+
* config-service at boot and verifies locally, so no verification depends on
|
|
8
|
+
* the minting service being reachable. The endpoint exists for the consumers
|
|
9
|
+
* that cannot do that — a browser or edge runtime holding no fleet
|
|
10
|
+
* configuration — and because publishing a key set is the interoperable shape
|
|
11
|
+
* every OIDC client already knows how to consume.
|
|
12
|
+
*
|
|
13
|
+
* Serving it unauthenticated is the point: a public key is public, and
|
|
14
|
+
* withholding it protects nothing while breaking every standard client.
|
|
15
|
+
*/
|
|
16
|
+
export declare const publicJwks: (publicKeyPem: string, keyId: string) => Promise<JSONWebKeySet>;
|
|
17
|
+
//# sourceMappingURL=jwks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jwks.d.ts","sourceRoot":"","sources":["../../src/token-service/jwks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyB,KAAK,aAAa,EAAE,MAAM,MAAM,CAAC;AAIjE;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,UAAU,GACrB,cAAc,MAAM,EACpB,OAAO,MAAM,KACZ,OAAO,CAAC,aAAa,CAevB,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { _ as _extends } from "@swc/helpers/_/_extends";
|
|
2
|
+
import { exportJWK, importSPKI } from 'jose';
|
|
3
|
+
import { TOKEN_ALGORITHM } from './jose-token-service.js';
|
|
4
|
+
/**
|
|
5
|
+
* The service's public signing key as a JWK Set, ready to serve at
|
|
6
|
+
* `/.well-known/jwks.json`.
|
|
7
|
+
*
|
|
8
|
+
* Services in this fleet do **not** need it: each resolves `jwt.publicKey` from
|
|
9
|
+
* config-service at boot and verifies locally, so no verification depends on
|
|
10
|
+
* the minting service being reachable. The endpoint exists for the consumers
|
|
11
|
+
* that cannot do that — a browser or edge runtime holding no fleet
|
|
12
|
+
* configuration — and because publishing a key set is the interoperable shape
|
|
13
|
+
* every OIDC client already knows how to consume.
|
|
14
|
+
*
|
|
15
|
+
* Serving it unauthenticated is the point: a public key is public, and
|
|
16
|
+
* withholding it protects nothing while breaking every standard client.
|
|
17
|
+
*/ export const publicJwks = async (publicKeyPem, keyId)=>{
|
|
18
|
+
const key = await importSPKI(publicKeyPem, TOKEN_ALGORITHM);
|
|
19
|
+
return {
|
|
20
|
+
keys: [
|
|
21
|
+
_extends({}, await exportJWK(key), {
|
|
22
|
+
kid: keyId,
|
|
23
|
+
// `use` and `alg` are advisory, and they are what stop a client from
|
|
24
|
+
// trying this key for encryption or for an algorithm it was never
|
|
25
|
+
// issued for.
|
|
26
|
+
use: 'sig',
|
|
27
|
+
alg: TOKEN_ALGORITHM
|
|
28
|
+
})
|
|
29
|
+
]
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
//# sourceMappingURL=jwks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/token-service/jwks.ts"],"sourcesContent":["import { exportJWK, importSPKI, type JSONWebKeySet } from 'jose';\n\nimport { TOKEN_ALGORITHM } from './jose-token-service.js';\n\n/**\n * The service's public signing key as a JWK Set, ready to serve at\n * `/.well-known/jwks.json`.\n *\n * Services in this fleet do **not** need it: each resolves `jwt.publicKey` from\n * config-service at boot and verifies locally, so no verification depends on\n * the minting service being reachable. The endpoint exists for the consumers\n * that cannot do that — a browser or edge runtime holding no fleet\n * configuration — and because publishing a key set is the interoperable shape\n * every OIDC client already knows how to consume.\n *\n * Serving it unauthenticated is the point: a public key is public, and\n * withholding it protects nothing while breaking every standard client.\n */\nexport const publicJwks = async (\n publicKeyPem: string,\n keyId: string,\n): Promise<JSONWebKeySet> => {\n const key = await importSPKI(publicKeyPem, TOKEN_ALGORITHM);\n return {\n keys: [\n {\n ...(await exportJWK(key)),\n kid: keyId,\n // `use` and `alg` are advisory, and they are what stop a client from\n // trying this key for encryption or for an algorithm it was never\n // issued for.\n use: 'sig',\n alg: TOKEN_ALGORITHM,\n },\n ],\n };\n};\n"],"names":["exportJWK","importSPKI","TOKEN_ALGORITHM","publicJwks","publicKeyPem","keyId","key","keys","kid","use","alg"],"mappings":";AAAA,SAASA,SAAS,EAAEC,UAAU,QAA4B,OAAO;AAEjE,SAASC,eAAe,QAAQ,0BAA0B;AAE1D;;;;;;;;;;;;;CAaC,GACD,OAAO,MAAMC,aAAa,OACxBC,cACAC;IAEA,MAAMC,MAAM,MAAML,WAAWG,cAAcF;IAC3C,OAAO;QACLK,MAAM;YACJ,aACM,MAAMP,UAAUM;gBACpBE,KAAKH;gBACL,qEAAqE;gBACrE,kEAAkE;gBAClE,cAAc;gBACdI,KAAK;gBACLC,KAAKR;;SAER;IACH;AACF,EAAE"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { TokenClaims } from '@entifix/business';
|
|
2
|
+
/**
|
|
3
|
+
* Read a token's claims **without checking its signature**.
|
|
4
|
+
*
|
|
5
|
+
* ⚠️ This proves nothing. A caller can forge any claims it likes. Use it only
|
|
6
|
+
* for presentation — deciding which nav entries to render, say — where being
|
|
7
|
+
* wrong costs a menu item and every request behind that item is still refused
|
|
8
|
+
* by the service's `requirePermission`. Never branch on it for access.
|
|
9
|
+
*
|
|
10
|
+
* It exists so a Next server component can shape a menu without either calling
|
|
11
|
+
* a service on every render or copying `jwt.secret` out of config-service into
|
|
12
|
+
* the app runtime. Anything that must be *true* goes to
|
|
13
|
+
* {@link verifyAccessToken} or to the owning service.
|
|
14
|
+
*
|
|
15
|
+
* Returns `undefined` for anything that is not a well-formed JWT payload.
|
|
16
|
+
*/
|
|
17
|
+
export declare const unverifiedClaims: (token: string) => TokenClaims | undefined;
|
|
18
|
+
//# sourceMappingURL=unverified-claims.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unverified-claims.d.ts","sourceRoot":"","sources":["../../src/token-service/unverified-claims.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAiBrD;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,gBAAgB,GAAI,OAAO,MAAM,KAAG,WAAW,GAAG,SAa9D,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base64url → JSON. `atob` + `TextDecoder` rather than `Buffer` so the same
|
|
3
|
+
* function runs in Node, the browser and the edge runtime unchanged.
|
|
4
|
+
*/ const decodeSegment = (segment)=>{
|
|
5
|
+
const base64 = segment.replace(/-/g, '+').replace(/_/g, '/');
|
|
6
|
+
const padded = base64.padEnd(base64.length + (4 - base64.length % 4) % 4, '=');
|
|
7
|
+
const binary = atob(padded);
|
|
8
|
+
const bytes = Uint8Array.from(binary, (character)=>character.charCodeAt(0));
|
|
9
|
+
return JSON.parse(new TextDecoder().decode(bytes));
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Read a token's claims **without checking its signature**.
|
|
13
|
+
*
|
|
14
|
+
* ⚠️ This proves nothing. A caller can forge any claims it likes. Use it only
|
|
15
|
+
* for presentation — deciding which nav entries to render, say — where being
|
|
16
|
+
* wrong costs a menu item and every request behind that item is still refused
|
|
17
|
+
* by the service's `requirePermission`. Never branch on it for access.
|
|
18
|
+
*
|
|
19
|
+
* It exists so a Next server component can shape a menu without either calling
|
|
20
|
+
* a service on every render or copying `jwt.secret` out of config-service into
|
|
21
|
+
* the app runtime. Anything that must be *true* goes to
|
|
22
|
+
* {@link verifyAccessToken} or to the owning service.
|
|
23
|
+
*
|
|
24
|
+
* Returns `undefined` for anything that is not a well-formed JWT payload.
|
|
25
|
+
*/ export const unverifiedClaims = (token)=>{
|
|
26
|
+
const [, payloadSegment, signature] = token.split('.');
|
|
27
|
+
if (payloadSegment === undefined || signature === undefined) {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
const payload = decodeSegment(payloadSegment);
|
|
32
|
+
return typeof payload === 'object' && payload !== null ? payload : undefined;
|
|
33
|
+
} catch (unused) {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
//# sourceMappingURL=unverified-claims.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/token-service/unverified-claims.ts"],"sourcesContent":["import type { TokenClaims } from '@entifix/business';\n\n/**\n * Base64url → JSON. `atob` + `TextDecoder` rather than `Buffer` so the same\n * function runs in Node, the browser and the edge runtime unchanged.\n */\nconst decodeSegment = (segment: string): unknown => {\n const base64 = segment.replace(/-/g, '+').replace(/_/g, '/');\n const padded = base64.padEnd(\n base64.length + ((4 - (base64.length % 4)) % 4),\n '=',\n );\n const binary = atob(padded);\n const bytes = Uint8Array.from(binary, character => character.charCodeAt(0));\n return JSON.parse(new TextDecoder().decode(bytes));\n};\n\n/**\n * Read a token's claims **without checking its signature**.\n *\n * ⚠️ This proves nothing. A caller can forge any claims it likes. Use it only\n * for presentation — deciding which nav entries to render, say — where being\n * wrong costs a menu item and every request behind that item is still refused\n * by the service's `requirePermission`. Never branch on it for access.\n *\n * It exists so a Next server component can shape a menu without either calling\n * a service on every render or copying `jwt.secret` out of config-service into\n * the app runtime. Anything that must be *true* goes to\n * {@link verifyAccessToken} or to the owning service.\n *\n * Returns `undefined` for anything that is not a well-formed JWT payload.\n */\nexport const unverifiedClaims = (token: string): TokenClaims | undefined => {\n const [, payloadSegment, signature] = token.split('.');\n if (payloadSegment === undefined || signature === undefined) {\n return undefined;\n }\n try {\n const payload = decodeSegment(payloadSegment);\n return typeof payload === 'object' && payload !== null\n ? (payload as TokenClaims)\n : undefined;\n } catch {\n return undefined;\n }\n};\n"],"names":["decodeSegment","segment","base64","replace","padded","padEnd","length","binary","atob","bytes","Uint8Array","from","character","charCodeAt","JSON","parse","TextDecoder","decode","unverifiedClaims","token","payloadSegment","signature","split","undefined","payload"],"mappings":"AAEA;;;CAGC,GACD,MAAMA,gBAAgB,CAACC;IACrB,MAAMC,SAASD,QAAQE,OAAO,CAAC,MAAM,KAAKA,OAAO,CAAC,MAAM;IACxD,MAAMC,SAASF,OAAOG,MAAM,CAC1BH,OAAOI,MAAM,GAAI,AAAC,CAAA,IAAKJ,OAAOI,MAAM,GAAG,CAAC,IAAK,GAC7C;IAEF,MAAMC,SAASC,KAAKJ;IACpB,MAAMK,QAAQC,WAAWC,IAAI,CAACJ,QAAQK,CAAAA,YAAaA,UAAUC,UAAU,CAAC;IACxE,OAAOC,KAAKC,KAAK,CAAC,IAAIC,cAAcC,MAAM,CAACR;AAC7C;AAEA;;;;;;;;;;;;;;CAcC,GACD,OAAO,MAAMS,mBAAmB,CAACC;IAC/B,MAAM,GAAGC,gBAAgBC,UAAU,GAAGF,MAAMG,KAAK,CAAC;IAClD,IAAIF,mBAAmBG,aAAaF,cAAcE,WAAW;QAC3D,OAAOA;IACT;IACA,IAAI;QACF,MAAMC,UAAUxB,cAAcoB;QAC9B,OAAO,OAAOI,YAAY,YAAYA,YAAY,OAC7CA,UACDD;IACN,EAAE,eAAM;QACN,OAAOA;IACT;AACF,EAAE"}
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@entifix/jwt",
|
|
3
|
+
"version": "0.1.0-0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/r10c-technologies/entifix.git",
|
|
8
|
+
"directory": "packages/ts/jwt-client"
|
|
9
|
+
},
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"module": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
"./package.json": "./package.json",
|
|
19
|
+
".": {
|
|
20
|
+
"@entifix/source": "./src/index.ts",
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"import": "./dist/index.js",
|
|
23
|
+
"default": "./dist/index.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist",
|
|
28
|
+
"!**/*.tsbuildinfo",
|
|
29
|
+
"LICENSE"
|
|
30
|
+
],
|
|
31
|
+
"nx": {
|
|
32
|
+
"tags": [
|
|
33
|
+
"layer:entifix",
|
|
34
|
+
"scope:shared",
|
|
35
|
+
"entifix:client",
|
|
36
|
+
"tier:2"
|
|
37
|
+
],
|
|
38
|
+
"sourceRoot": "packages/ts/jwt-client/src",
|
|
39
|
+
"targets": {
|
|
40
|
+
"build": {
|
|
41
|
+
"executor": "@nx/js:swc",
|
|
42
|
+
"outputs": [
|
|
43
|
+
"{options.outputPath}"
|
|
44
|
+
],
|
|
45
|
+
"options": {
|
|
46
|
+
"outputPath": "packages/ts/jwt-client/dist",
|
|
47
|
+
"main": "packages/ts/jwt-client/src/index.ts",
|
|
48
|
+
"tsConfig": "packages/ts/jwt-client/tsconfig.lib.json",
|
|
49
|
+
"skipTypeCheck": true,
|
|
50
|
+
"stripLeadingPaths": true
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"dependencies": {
|
|
56
|
+
"@swc/helpers": "0.5.23",
|
|
57
|
+
"jose": "6.2.8",
|
|
58
|
+
"@entifix/business": "0.1.0-0",
|
|
59
|
+
"@entifix/core": "0.1.0-0"
|
|
60
|
+
},
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"effect": "3.22.1",
|
|
63
|
+
"@entifix/testing-unit": "0.1.0-0"
|
|
64
|
+
},
|
|
65
|
+
"peerDependencies": {
|
|
66
|
+
"effect": ">=3.22 <4"
|
|
67
|
+
}
|
|
68
|
+
}
|