@adaptic/backend-legacy 0.0.1043 → 0.0.1045
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.
|
@@ -67,6 +67,14 @@ export declare const MAX_SERVICE_TOKEN_LIFETIME_SEC = 3600;
|
|
|
67
67
|
* a minter and this host and nothing more.
|
|
68
68
|
*/
|
|
69
69
|
export declare const MAX_SERVICE_TOKEN_CLOCK_SKEW_SEC = 60;
|
|
70
|
+
/**
|
|
71
|
+
* Which configured secret proved a credential's signature.
|
|
72
|
+
*
|
|
73
|
+
* `primary` is `BACKEND_SERVICE_JWT_SECRET`; `previous` is the optional
|
|
74
|
+
* `BACKEND_SERVICE_JWT_SECRET_PREVIOUS` that exists only for the length of a
|
|
75
|
+
* rotation.
|
|
76
|
+
*/
|
|
77
|
+
export type ServiceKeySlot = 'primary' | 'previous';
|
|
70
78
|
/**
|
|
71
79
|
* Attempt to establish a service principal from a bearer token.
|
|
72
80
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service-token.d.ts","sourceRoot":"","sources":["../../../src/auth/service-token.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAOH,OAAO,EAAa,KAAK,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAEpE;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,oBAAoB,CAAC;AAEtD;;;;;;GAMG;AACH,eAAO,MAAM,sBAAsB,oBAAoB,CAAC;AAExD,sEAAsE;AACtE,eAAO,MAAM,6BAA6B,KAAK,CAAC;AAEhD;;;;;;;;GAQG;AACH,eAAO,MAAM,8BAA8B,OAAO,CAAC;AAEnD;;;;;;;;GAQG;AACH,eAAO,MAAM,gCAAgC,KAAK,CAAC;
|
|
1
|
+
{"version":3,"file":"service-token.d.ts","sourceRoot":"","sources":["../../../src/auth/service-token.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;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AAM,MAAM,GAAG,OAAO,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;IACnE,MAAM,CAAC,+BAA+B,EAAE,EAAE,GAAG,EAAE,OAAO,EAAE,CAAC,CAAC;IAE1D,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,EAAE,CAAC;AACjC,CAAC"}
|
|
@@ -71,61 +71,74 @@ export const MAX_SERVICE_TOKEN_LIFETIME_SEC = 3600;
|
|
|
71
71
|
*/
|
|
72
72
|
export const MAX_SERVICE_TOKEN_CLOCK_SKEW_SEC = 60;
|
|
73
73
|
/**
|
|
74
|
-
*
|
|
75
|
-
* provisioned.
|
|
76
|
-
*
|
|
77
|
-
* Read per call rather than cached at import so an operator rotating the
|
|
78
|
-
* Railway variable takes effect on the next request rather than the next
|
|
79
|
-
* restart — the same discipline `SERVER_AUTH_TOKEN` already follows.
|
|
74
|
+
* Validate one raw secret value, or `null` when it cannot serve as one.
|
|
80
75
|
*
|
|
81
|
-
* @
|
|
76
|
+
* @param name - Env var name, for the operator-facing log line.
|
|
77
|
+
* @param raw - The raw env value.
|
|
78
|
+
* @returns The trimmed secret, or `null` when unset, blank, or too short.
|
|
82
79
|
*/
|
|
83
|
-
function
|
|
84
|
-
const raw = process.env.BACKEND_SERVICE_JWT_SECRET;
|
|
80
|
+
function readSecret(name, raw) {
|
|
85
81
|
if (typeof raw !== 'string')
|
|
86
82
|
return null;
|
|
87
83
|
const secret = raw.trim();
|
|
88
84
|
if (secret.length === 0)
|
|
89
85
|
return null;
|
|
90
86
|
if (secret.length < MINIMUM_SERVICE_SECRET_LENGTH) {
|
|
91
|
-
logger.error(
|
|
92
|
-
`${MINIMUM_SERVICE_SECRET_LENGTH} characters;
|
|
93
|
-
'Service callers will be rejected until a long enough secret is provisioned.', { secretLength: secret.length });
|
|
87
|
+
logger.error(`[auth] ${name} is set but shorter than the minimum ` +
|
|
88
|
+
`${MINIMUM_SERVICE_SECRET_LENGTH} characters; it will not be used to verify service tokens.`, { secretLength: secret.length });
|
|
94
89
|
return null;
|
|
95
90
|
}
|
|
96
91
|
return secret;
|
|
97
92
|
}
|
|
98
93
|
/**
|
|
99
|
-
*
|
|
94
|
+
* Resolve the service secrets to try, in order, or `null` when the path is not
|
|
95
|
+
* provisioned.
|
|
100
96
|
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
* subsequent problem with it (expiry, wrong audience, missing subject,
|
|
105
|
-
* over-long lifetime) throws, because at that point the caller's intent is
|
|
106
|
-
* unambiguous and falling through would report a misleading `bad_signature`
|
|
107
|
-
* for what is actually an expired or mis-scoped service token.
|
|
97
|
+
* Read per call rather than cached at import so an operator rotating the
|
|
98
|
+
* Railway variable takes effect on the next request rather than the next
|
|
99
|
+
* restart — the same discipline `SERVER_AUTH_TOKEN` already follows.
|
|
108
100
|
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
101
|
+
* WHY A SECOND SLOT. With one secret, rotation means every minter and this
|
|
102
|
+
* verifier must change at the same instant or service calls fail in between.
|
|
103
|
+
* `BACKEND_SERVICE_JWT_SECRET_PREVIOUS` lets the verifier accept the outgoing
|
|
104
|
+
* key while minters move to the new one, so the swap is: set PREVIOUS to the
|
|
105
|
+
* current value and the primary to the new one, move the minters, watch the
|
|
106
|
+
* `previous` log line drain to zero, then unset PREVIOUS.
|
|
107
|
+
*
|
|
108
|
+
* The previous slot never stands alone: with no valid primary the whole path
|
|
109
|
+
* stays disabled, so an operator cannot half-finish a rotation into a state
|
|
110
|
+
* where only the retiring key is honoured.
|
|
111
|
+
*
|
|
112
|
+
* @returns The slots to try, primary first, or `null` when unprovisioned.
|
|
112
113
|
*/
|
|
113
|
-
|
|
114
|
-
const
|
|
115
|
-
if (
|
|
114
|
+
function resolveServiceSecrets() {
|
|
115
|
+
const primary = readSecret('BACKEND_SERVICE_JWT_SECRET', process.env.BACKEND_SERVICE_JWT_SECRET);
|
|
116
|
+
if (primary === null) {
|
|
117
|
+
if (process.env.BACKEND_SERVICE_JWT_SECRET_PREVIOUS?.trim()) {
|
|
118
|
+
logger.error('[auth] BACKEND_SERVICE_JWT_SECRET_PREVIOUS is set without a valid ' +
|
|
119
|
+
'BACKEND_SERVICE_JWT_SECRET; the service-principal path is DISABLED. ' +
|
|
120
|
+
'The previous slot only extends a provisioned primary, it never replaces one.');
|
|
121
|
+
}
|
|
116
122
|
return null;
|
|
117
|
-
// A service secret set equal to the user-session secret would let any
|
|
118
|
-
// end-user JWT that carries our issuer/audience become a `server` principal,
|
|
119
|
-
// which bypasses every tenancy scope and role gate. Refuse rather than
|
|
120
|
-
// silently operate with the separation collapsed.
|
|
121
|
-
if (secret === jwtSecret) {
|
|
122
|
-
logger.error('[auth] BACKEND_SERVICE_JWT_SECRET is identical to JWT_SECRET. ' +
|
|
123
|
-
'These MUST be distinct secrets — sharing them collapses the ' +
|
|
124
|
-
'separation between end-user and service identity. Refusing to ' +
|
|
125
|
-
'verify service tokens until they differ.');
|
|
126
|
-
throw new AuthError('invalid_token', 'misconfigured');
|
|
127
123
|
}
|
|
128
|
-
|
|
124
|
+
const slots = [
|
|
125
|
+
{ slot: 'primary', secret: primary },
|
|
126
|
+
];
|
|
127
|
+
const previous = readSecret('BACKEND_SERVICE_JWT_SECRET_PREVIOUS', process.env.BACKEND_SERVICE_JWT_SECRET_PREVIOUS);
|
|
128
|
+
// Equal to the primary adds nothing; skip it so the log never claims a
|
|
129
|
+
// `previous` verification that was really the primary.
|
|
130
|
+
if (previous !== null && previous !== primary) {
|
|
131
|
+
slots.push({ slot: 'previous', secret: previous });
|
|
132
|
+
}
|
|
133
|
+
return slots;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Verify a token's signature and bound claims against one secret.
|
|
137
|
+
*
|
|
138
|
+
* @returns The payload, or `null` when the signature is not this secret's.
|
|
139
|
+
* @throws {AuthError} When the signature matched but the token is unacceptable.
|
|
140
|
+
*/
|
|
141
|
+
function verifyAgainst(token, secret) {
|
|
129
142
|
try {
|
|
130
143
|
// Algorithm pinned to HS256 for the same reason path 2 pins it: an
|
|
131
144
|
// unpinned verify accepts `alg: "none"` on some versions, which turns a
|
|
@@ -138,7 +151,7 @@ export function verifyServiceToken(token, nowMs = Date.now()) {
|
|
|
138
151
|
if (typeof verified === 'string') {
|
|
139
152
|
throw new AuthError('invalid_token', 'malformed');
|
|
140
153
|
}
|
|
141
|
-
|
|
154
|
+
return verified;
|
|
142
155
|
}
|
|
143
156
|
catch (error) {
|
|
144
157
|
if (error instanceof AuthError)
|
|
@@ -151,8 +164,9 @@ export function verifyServiceToken(token, nowMs = Date.now()) {
|
|
|
151
164
|
}
|
|
152
165
|
if (error instanceof JsonWebTokenError) {
|
|
153
166
|
const message = (error.message || '').toLowerCase();
|
|
154
|
-
// Signature mismatch means this is simply not
|
|
155
|
-
//
|
|
167
|
+
// Signature mismatch means this is simply not this secret's credential —
|
|
168
|
+
// the caller tries the next slot, then falls through to the app-JWT and
|
|
169
|
+
// Google paths.
|
|
156
170
|
if (message.includes('invalid signature') ||
|
|
157
171
|
message.includes('invalid algorithm')) {
|
|
158
172
|
return null;
|
|
@@ -169,6 +183,49 @@ export function verifyServiceToken(token, nowMs = Date.now()) {
|
|
|
169
183
|
}
|
|
170
184
|
return null;
|
|
171
185
|
}
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Attempt to establish a service principal from a bearer token.
|
|
189
|
+
*
|
|
190
|
+
* Returns `null` — meaning "not a service credential, keep looking" — only
|
|
191
|
+
* when the path is unprovisioned or the signature does not belong to the
|
|
192
|
+
* service secret. Once a token IS proven to carry that signature, every
|
|
193
|
+
* subsequent problem with it (expiry, wrong audience, missing subject,
|
|
194
|
+
* over-long lifetime) throws, because at that point the caller's intent is
|
|
195
|
+
* unambiguous and falling through would report a misleading `bad_signature`
|
|
196
|
+
* for what is actually an expired or mis-scoped service token.
|
|
197
|
+
*
|
|
198
|
+
* @param token - Raw bearer token; assumed to be three dot-separated segments.
|
|
199
|
+
* @returns The verified service principal, or `null` to fall through.
|
|
200
|
+
* @throws {AuthError} When the token is ours but unacceptable.
|
|
201
|
+
*/
|
|
202
|
+
export function verifyServiceToken(token, nowMs = Date.now()) {
|
|
203
|
+
const slots = resolveServiceSecrets();
|
|
204
|
+
if (slots === null)
|
|
205
|
+
return null;
|
|
206
|
+
// A service secret set equal to the user-session secret would let any
|
|
207
|
+
// end-user JWT that carries our issuer/audience become a `server` principal,
|
|
208
|
+
// which bypasses every tenancy scope and role gate. Refuse rather than
|
|
209
|
+
// silently operate with the separation collapsed. This holds for the
|
|
210
|
+
// previous slot too: a rotation must not reopen the hole.
|
|
211
|
+
if (slots.some(({ secret }) => secret === jwtSecret)) {
|
|
212
|
+
logger.error('[auth] BACKEND_SERVICE_JWT_SECRET (or its _PREVIOUS slot) is identical to JWT_SECRET. ' +
|
|
213
|
+
'These MUST be distinct secrets — sharing them collapses the ' +
|
|
214
|
+
'separation between end-user and service identity. Refusing to ' +
|
|
215
|
+
'verify service tokens until they differ.');
|
|
216
|
+
throw new AuthError('invalid_token', 'misconfigured');
|
|
217
|
+
}
|
|
218
|
+
let payload = null;
|
|
219
|
+
let keySlot = 'primary';
|
|
220
|
+
for (const { slot, secret } of slots) {
|
|
221
|
+
payload = verifyAgainst(token, secret);
|
|
222
|
+
if (payload !== null) {
|
|
223
|
+
keySlot = slot;
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
if (payload === null)
|
|
228
|
+
return null;
|
|
172
229
|
const sub = typeof payload.sub === 'string' ? payload.sub.trim() : '';
|
|
173
230
|
if (sub.length === 0) {
|
|
174
231
|
// An unattributable service credential defeats the audit trail this path
|
|
@@ -210,6 +267,12 @@ export function verifyServiceToken(token, nowMs = Date.now()) {
|
|
|
210
267
|
});
|
|
211
268
|
throw new AuthError('invalid_token', 'bad_audience');
|
|
212
269
|
}
|
|
270
|
+
// Which key verified is the rotation's progress signal: once `previous`
|
|
271
|
+
// stops appearing, every minter is on the new key and the slot can go.
|
|
272
|
+
// `previous` logs at info so it shows under any production LOG_LEVEL short
|
|
273
|
+
// of warn; `primary` is the steady state and stays at debug.
|
|
274
|
+
const keyLog = keySlot === 'previous' ? logger.info : logger.debug;
|
|
275
|
+
keyLog('[auth] service token verified', { sub, keySlot });
|
|
213
276
|
return { kind: 'server', sub };
|
|
214
277
|
}
|
|
215
278
|
//# sourceMappingURL=service-token.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adaptic/backend-legacy",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.1045",
|
|
4
4
|
"description": "Backend executable CRUD functions with dynamic variables construction, and type definitions for the Adaptic AI platform.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "index.d.ts",
|