@ontrails/permits 1.0.0-beta.12 → 1.0.0-beta.14
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/.turbo/turbo-lint.log +1 -1
- package/CHANGELOG.md +44 -5
- package/README.md +175 -0
- package/dist/auth-gate.d.ts +18 -0
- package/dist/auth-gate.d.ts.map +1 -0
- package/dist/auth-gate.js +56 -0
- package/dist/auth-gate.js.map +1 -0
- package/dist/auth-provision.d.ts +11 -0
- package/dist/auth-provision.d.ts.map +1 -0
- package/dist/auth-provision.js +22 -0
- package/dist/auth-provision.js.map +1 -0
- package/dist/connectors/connector.d.ts +26 -0
- package/dist/connectors/connector.d.ts.map +1 -0
- package/dist/connectors/connector.js +2 -0
- package/dist/connectors/connector.js.map +1 -0
- package/dist/connectors/jwt.d.ts +25 -0
- package/dist/connectors/jwt.d.ts.map +1 -0
- package/dist/connectors/jwt.js +148 -0
- package/dist/connectors/jwt.js.map +1 -0
- package/dist/extraction.d.ts +6 -6
- package/dist/extraction.d.ts.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/dist/permit.d.ts +2 -2
- package/dist/permit.js +2 -2
- package/dist/trails/auth-verify.d.ts +3 -3
- package/dist/trails/auth-verify.d.ts.map +1 -1
- package/dist/trails/auth-verify.js +31 -31
- package/dist/trails/auth-verify.js.map +1 -1
- package/package.json +3 -3
- package/src/__tests__/{auth-layer.test.ts → auth-gate.test.ts} +16 -16
- package/src/__tests__/{auth-service.test.ts → auth-provision.test.ts} +18 -18
- package/src/__tests__/auth-verify.test.ts +36 -35
- package/src/__tests__/{adapter.test.ts → connector.test.ts} +43 -43
- package/src/__tests__/permit.test.ts +11 -11
- package/src/__tests__/rules.test.ts +17 -17
- package/src/{auth-layer.ts → auth-gate.ts} +10 -10
- package/src/auth-provision.ts +26 -0
- package/src/{adapter.ts → connectors/connector.ts} +7 -7
- package/src/{adapters → connectors}/jwt.ts +10 -8
- package/src/extraction.ts +6 -6
- package/src/index.ts +8 -5
- package/src/permit.ts +2 -2
- package/src/trails/auth-verify.ts +35 -31
- package/tsconfig.tsbuildinfo +1 -1
- package/src/auth-service.ts +0 -25
|
@@ -2,10 +2,10 @@ import { describe, expect, test } from 'bun:test';
|
|
|
2
2
|
|
|
3
3
|
import { Result } from '@ontrails/core';
|
|
4
4
|
|
|
5
|
-
import type {
|
|
5
|
+
import type { AuthConnector, AuthError } from '../connectors/connector.js';
|
|
6
6
|
import type { PermitExtractionInput } from '../extraction.js';
|
|
7
7
|
import type { Permit } from '../permit.js';
|
|
8
|
-
import {
|
|
8
|
+
import { createJwtConnector } from '../connectors/jwt.js';
|
|
9
9
|
|
|
10
10
|
// ---------------------------------------------------------------------------
|
|
11
11
|
// Test helper: sign a JWT with HMAC-SHA256 using crypto.subtle
|
|
@@ -58,37 +58,37 @@ const testInput = (
|
|
|
58
58
|
overrides?: Partial<PermitExtractionInput>
|
|
59
59
|
): PermitExtractionInput => ({
|
|
60
60
|
requestId: 'test-req',
|
|
61
|
-
|
|
61
|
+
trailhead: 'http',
|
|
62
62
|
...overrides,
|
|
63
63
|
});
|
|
64
64
|
|
|
65
|
-
describe('
|
|
66
|
-
test('accepts a valid
|
|
67
|
-
const
|
|
68
|
-
// oxlint-disable-next-line require-await -- stub
|
|
65
|
+
describe('AuthConnector interface', () => {
|
|
66
|
+
test('accepts a valid connector implementation', async () => {
|
|
67
|
+
const connector: AuthConnector = {
|
|
68
|
+
// oxlint-disable-next-line require-await -- stub connector for type test
|
|
69
69
|
authenticate: async (_input: PermitExtractionInput) => Result.ok(null),
|
|
70
70
|
};
|
|
71
|
-
const result = await
|
|
71
|
+
const result = await connector.authenticate(testInput());
|
|
72
72
|
expect(result.isOk()).toBe(true);
|
|
73
73
|
});
|
|
74
74
|
});
|
|
75
75
|
|
|
76
76
|
/* oxlint-disable max-statements -- test suite with multiple concern groups */
|
|
77
|
-
describe('
|
|
78
|
-
test('returns an
|
|
79
|
-
const
|
|
80
|
-
expect(
|
|
81
|
-
expect(
|
|
77
|
+
describe('createJwtConnector', () => {
|
|
78
|
+
test('returns an AuthConnector', () => {
|
|
79
|
+
const connector = createJwtConnector({ secret: TEST_SECRET });
|
|
80
|
+
expect(connector).toBeDefined();
|
|
81
|
+
expect(connector.authenticate).toBeInstanceOf(Function);
|
|
82
82
|
});
|
|
83
83
|
|
|
84
84
|
test('verifies a valid HS256 token and returns a Permit', async () => {
|
|
85
|
-
const
|
|
85
|
+
const connector = createJwtConnector({ secret: TEST_SECRET });
|
|
86
86
|
const now = Math.floor(Date.now() / 1000);
|
|
87
87
|
const token = await signJwt(
|
|
88
88
|
{ exp: now + 3600, scope: 'read write', sub: 'user-123' },
|
|
89
89
|
TEST_SECRET
|
|
90
90
|
);
|
|
91
|
-
const result = await
|
|
91
|
+
const result = await connector.authenticate(
|
|
92
92
|
testInput({ bearerToken: token })
|
|
93
93
|
);
|
|
94
94
|
expect(result.isOk()).toBe(true);
|
|
@@ -99,13 +99,13 @@ describe('createJwtAdapter', () => {
|
|
|
99
99
|
});
|
|
100
100
|
|
|
101
101
|
test('rejects an expired token', async () => {
|
|
102
|
-
const
|
|
102
|
+
const connector = createJwtConnector({ secret: TEST_SECRET });
|
|
103
103
|
const past = Math.floor(Date.now() / 1000) - 3600;
|
|
104
104
|
const token = await signJwt(
|
|
105
105
|
{ exp: past, sub: 'user-expired' },
|
|
106
106
|
TEST_SECRET
|
|
107
107
|
);
|
|
108
|
-
const result = await
|
|
108
|
+
const result = await connector.authenticate(
|
|
109
109
|
testInput({ bearerToken: token })
|
|
110
110
|
);
|
|
111
111
|
expect(result.isErr()).toBe(true);
|
|
@@ -114,13 +114,13 @@ describe('createJwtAdapter', () => {
|
|
|
114
114
|
});
|
|
115
115
|
|
|
116
116
|
test('rejects an invalid signature', async () => {
|
|
117
|
-
const
|
|
117
|
+
const connector = createJwtConnector({ secret: TEST_SECRET });
|
|
118
118
|
const now = Math.floor(Date.now() / 1000);
|
|
119
119
|
const token = await signJwt(
|
|
120
120
|
{ exp: now + 3600, sub: 'user-bad-sig' },
|
|
121
121
|
'wrong-secret'
|
|
122
122
|
);
|
|
123
|
-
const result = await
|
|
123
|
+
const result = await connector.authenticate(
|
|
124
124
|
testInput({ bearerToken: token })
|
|
125
125
|
);
|
|
126
126
|
expect(result.isErr()).toBe(true);
|
|
@@ -129,13 +129,13 @@ describe('createJwtAdapter', () => {
|
|
|
129
129
|
});
|
|
130
130
|
|
|
131
131
|
test('rejects tokens with a missing subject claim', async () => {
|
|
132
|
-
const
|
|
132
|
+
const connector = createJwtConnector({ secret: TEST_SECRET });
|
|
133
133
|
const now = Math.floor(Date.now() / 1000);
|
|
134
134
|
const token = await signJwt(
|
|
135
135
|
{ exp: now + 3600, scope: 'read' },
|
|
136
136
|
TEST_SECRET
|
|
137
137
|
);
|
|
138
|
-
const result = await
|
|
138
|
+
const result = await connector.authenticate(
|
|
139
139
|
testInput({ bearerToken: token })
|
|
140
140
|
);
|
|
141
141
|
expect(result.isErr()).toBe(true);
|
|
@@ -145,7 +145,7 @@ describe('createJwtAdapter', () => {
|
|
|
145
145
|
});
|
|
146
146
|
|
|
147
147
|
test('extracts scopes from token claims', async () => {
|
|
148
|
-
const
|
|
148
|
+
const connector = createJwtConnector({
|
|
149
149
|
scopesClaim: 'permissions',
|
|
150
150
|
secret: TEST_SECRET,
|
|
151
151
|
});
|
|
@@ -158,7 +158,7 @@ describe('createJwtAdapter', () => {
|
|
|
158
158
|
},
|
|
159
159
|
TEST_SECRET
|
|
160
160
|
);
|
|
161
|
-
const result = await
|
|
161
|
+
const result = await connector.authenticate(
|
|
162
162
|
testInput({ bearerToken: token })
|
|
163
163
|
);
|
|
164
164
|
expect(result.isOk()).toBe(true);
|
|
@@ -167,13 +167,13 @@ describe('createJwtAdapter', () => {
|
|
|
167
167
|
});
|
|
168
168
|
|
|
169
169
|
test('extracts roles from token claims', async () => {
|
|
170
|
-
const
|
|
170
|
+
const connector = createJwtConnector({ secret: TEST_SECRET });
|
|
171
171
|
const now = Math.floor(Date.now() / 1000);
|
|
172
172
|
const token = await signJwt(
|
|
173
173
|
{ exp: now + 3600, roles: ['admin', 'editor'], sub: 'user-roles' },
|
|
174
174
|
TEST_SECRET
|
|
175
175
|
);
|
|
176
|
-
const result = await
|
|
176
|
+
const result = await connector.authenticate(
|
|
177
177
|
testInput({ bearerToken: token })
|
|
178
178
|
);
|
|
179
179
|
expect(result.isOk()).toBe(true);
|
|
@@ -182,14 +182,14 @@ describe('createJwtAdapter', () => {
|
|
|
182
182
|
});
|
|
183
183
|
|
|
184
184
|
test('returns null for missing credentials', async () => {
|
|
185
|
-
const
|
|
186
|
-
const result = await
|
|
185
|
+
const connector = createJwtConnector({ secret: TEST_SECRET });
|
|
186
|
+
const result = await connector.authenticate(testInput());
|
|
187
187
|
expect(result.isOk()).toBe(true);
|
|
188
188
|
expect(result.unwrap()).toBeNull();
|
|
189
189
|
});
|
|
190
190
|
|
|
191
191
|
test('checks issuer claim when configured', async () => {
|
|
192
|
-
const
|
|
192
|
+
const connector = createJwtConnector({
|
|
193
193
|
issuer: 'https://auth.example.com',
|
|
194
194
|
secret: TEST_SECRET,
|
|
195
195
|
});
|
|
@@ -198,7 +198,7 @@ describe('createJwtAdapter', () => {
|
|
|
198
198
|
{ exp: now + 3600, iss: 'https://evil.example.com', sub: 'user-iss' },
|
|
199
199
|
TEST_SECRET
|
|
200
200
|
);
|
|
201
|
-
const result = await
|
|
201
|
+
const result = await connector.authenticate(
|
|
202
202
|
testInput({ bearerToken: token })
|
|
203
203
|
);
|
|
204
204
|
expect(result.isErr()).toBe(true);
|
|
@@ -207,7 +207,7 @@ describe('createJwtAdapter', () => {
|
|
|
207
207
|
});
|
|
208
208
|
|
|
209
209
|
test('checks audience claim when configured', async () => {
|
|
210
|
-
const
|
|
210
|
+
const connector = createJwtConnector({
|
|
211
211
|
audience: 'my-api',
|
|
212
212
|
secret: TEST_SECRET,
|
|
213
213
|
});
|
|
@@ -216,7 +216,7 @@ describe('createJwtAdapter', () => {
|
|
|
216
216
|
{ aud: 'other-api', exp: now + 3600, sub: 'user-aud' },
|
|
217
217
|
TEST_SECRET
|
|
218
218
|
);
|
|
219
|
-
const result = await
|
|
219
|
+
const result = await connector.authenticate(
|
|
220
220
|
testInput({ bearerToken: token })
|
|
221
221
|
);
|
|
222
222
|
expect(result.isErr()).toBe(true);
|
|
@@ -225,7 +225,7 @@ describe('createJwtAdapter', () => {
|
|
|
225
225
|
});
|
|
226
226
|
|
|
227
227
|
test('accepts array-format audience containing configured value', async () => {
|
|
228
|
-
const
|
|
228
|
+
const connector = createJwtConnector({
|
|
229
229
|
audience: 'my-api',
|
|
230
230
|
secret: TEST_SECRET,
|
|
231
231
|
});
|
|
@@ -234,7 +234,7 @@ describe('createJwtAdapter', () => {
|
|
|
234
234
|
{ aud: ['my-api', 'account'], exp: now + 3600, sub: 'user-arr-aud' },
|
|
235
235
|
TEST_SECRET
|
|
236
236
|
);
|
|
237
|
-
const result = await
|
|
237
|
+
const result = await connector.authenticate(
|
|
238
238
|
testInput({ bearerToken: token })
|
|
239
239
|
);
|
|
240
240
|
expect(result.isOk()).toBe(true);
|
|
@@ -243,7 +243,7 @@ describe('createJwtAdapter', () => {
|
|
|
243
243
|
});
|
|
244
244
|
|
|
245
245
|
test('rejects array-format audience not containing configured value', async () => {
|
|
246
|
-
const
|
|
246
|
+
const connector = createJwtConnector({
|
|
247
247
|
audience: 'my-api',
|
|
248
248
|
secret: TEST_SECRET,
|
|
249
249
|
});
|
|
@@ -252,7 +252,7 @@ describe('createJwtAdapter', () => {
|
|
|
252
252
|
{ aud: ['other-api', 'account'], exp: now + 3600, sub: 'user-no-aud' },
|
|
253
253
|
TEST_SECRET
|
|
254
254
|
);
|
|
255
|
-
const result = await
|
|
255
|
+
const result = await connector.authenticate(
|
|
256
256
|
testInput({ bearerToken: token })
|
|
257
257
|
);
|
|
258
258
|
expect(result.isErr()).toBe(true);
|
|
@@ -261,13 +261,13 @@ describe('createJwtAdapter', () => {
|
|
|
261
261
|
});
|
|
262
262
|
|
|
263
263
|
test('extracts scopes from array-format claim', async () => {
|
|
264
|
-
const
|
|
264
|
+
const connector = createJwtConnector({ secret: TEST_SECRET });
|
|
265
265
|
const now = Math.floor(Date.now() / 1000);
|
|
266
266
|
const token = await signJwt(
|
|
267
267
|
{ exp: now + 3600, scope: ['read', 'write'], sub: 'user-arr-scope' },
|
|
268
268
|
TEST_SECRET
|
|
269
269
|
);
|
|
270
|
-
const result = await
|
|
270
|
+
const result = await connector.authenticate(
|
|
271
271
|
testInput({ bearerToken: token })
|
|
272
272
|
);
|
|
273
273
|
expect(result.isOk()).toBe(true);
|
|
@@ -276,7 +276,7 @@ describe('createJwtAdapter', () => {
|
|
|
276
276
|
});
|
|
277
277
|
|
|
278
278
|
test('filters empty strings from array-format scope claims', async () => {
|
|
279
|
-
const
|
|
279
|
+
const connector = createJwtConnector({ secret: TEST_SECRET });
|
|
280
280
|
const now = Math.floor(Date.now() / 1000);
|
|
281
281
|
const token = await signJwt(
|
|
282
282
|
{
|
|
@@ -286,7 +286,7 @@ describe('createJwtAdapter', () => {
|
|
|
286
286
|
},
|
|
287
287
|
TEST_SECRET
|
|
288
288
|
);
|
|
289
|
-
const result = await
|
|
289
|
+
const result = await connector.authenticate(
|
|
290
290
|
testInput({ bearerToken: token })
|
|
291
291
|
);
|
|
292
292
|
expect(result.isOk()).toBe(true);
|
|
@@ -295,7 +295,7 @@ describe('createJwtAdapter', () => {
|
|
|
295
295
|
});
|
|
296
296
|
|
|
297
297
|
test('returns error for malformed signature bytes', async () => {
|
|
298
|
-
const
|
|
298
|
+
const connector = createJwtConnector({ secret: TEST_SECRET });
|
|
299
299
|
const now = Math.floor(Date.now() / 1000);
|
|
300
300
|
const token = await signJwt(
|
|
301
301
|
{ exp: now + 3600, sub: 'user-bad' },
|
|
@@ -303,7 +303,7 @@ describe('createJwtAdapter', () => {
|
|
|
303
303
|
);
|
|
304
304
|
const parts = token.split('.');
|
|
305
305
|
const malformed = `${parts[0]}.${parts[1]}.!!!invalid-base64!!!`;
|
|
306
|
-
const result = await
|
|
306
|
+
const result = await connector.authenticate(
|
|
307
307
|
testInput({ bearerToken: malformed })
|
|
308
308
|
);
|
|
309
309
|
expect(result.isErr()).toBe(true);
|
|
@@ -312,7 +312,7 @@ describe('createJwtAdapter', () => {
|
|
|
312
312
|
});
|
|
313
313
|
|
|
314
314
|
test('accepts token with matching issuer and audience', async () => {
|
|
315
|
-
const
|
|
315
|
+
const connector = createJwtConnector({
|
|
316
316
|
audience: 'my-api',
|
|
317
317
|
issuer: 'https://auth.example.com',
|
|
318
318
|
secret: TEST_SECRET,
|
|
@@ -328,7 +328,7 @@ describe('createJwtAdapter', () => {
|
|
|
328
328
|
},
|
|
329
329
|
TEST_SECRET
|
|
330
330
|
);
|
|
331
|
-
const result = await
|
|
331
|
+
const result = await connector.authenticate(
|
|
332
332
|
testInput({ bearerToken: token })
|
|
333
333
|
);
|
|
334
334
|
expect(result.isOk()).toBe(true);
|
|
@@ -60,7 +60,7 @@ describe('getPermit()', () => {
|
|
|
60
60
|
expect(result).toBeUndefined();
|
|
61
61
|
});
|
|
62
62
|
|
|
63
|
-
test('preserves extended permit fields from the auth
|
|
63
|
+
test('preserves extended permit fields from the auth gate', () => {
|
|
64
64
|
const ctx = {
|
|
65
65
|
permit: {
|
|
66
66
|
id: 'usr_2',
|
|
@@ -77,42 +77,42 @@ describe('getPermit()', () => {
|
|
|
77
77
|
});
|
|
78
78
|
|
|
79
79
|
describe('PermitExtractionInput', () => {
|
|
80
|
-
test('accepts HTTP
|
|
80
|
+
test('accepts HTTP trailhead extraction', () => {
|
|
81
81
|
const input: PermitExtractionInput = {
|
|
82
82
|
bearerToken: 'eyJhbGciOiJSUzI1NiJ9.test',
|
|
83
83
|
headers: new Headers({
|
|
84
84
|
authorization: 'Bearer eyJhbGciOiJSUzI1NiJ9.test',
|
|
85
85
|
}),
|
|
86
86
|
requestId: 'req-http-1',
|
|
87
|
-
|
|
87
|
+
trailhead: 'http',
|
|
88
88
|
};
|
|
89
|
-
expect(input.
|
|
89
|
+
expect(input.trailhead).toBe('http');
|
|
90
90
|
expect(input.bearerToken).toBeDefined();
|
|
91
91
|
});
|
|
92
92
|
|
|
93
|
-
test('accepts MCP
|
|
93
|
+
test('accepts MCP trailhead extraction', () => {
|
|
94
94
|
const input: PermitExtractionInput = {
|
|
95
95
|
requestId: 'req-mcp-1',
|
|
96
96
|
sessionId: 'mcp-session-abc',
|
|
97
|
-
|
|
97
|
+
trailhead: 'mcp',
|
|
98
98
|
};
|
|
99
|
-
expect(input.
|
|
99
|
+
expect(input.trailhead).toBe('mcp');
|
|
100
100
|
expect(input.sessionId).toBe('mcp-session-abc');
|
|
101
101
|
});
|
|
102
102
|
|
|
103
|
-
test('accepts CLI
|
|
103
|
+
test('accepts CLI trailhead extraction', () => {
|
|
104
104
|
const input: PermitExtractionInput = {
|
|
105
105
|
bearerToken: 'cli-token-from-keyring',
|
|
106
106
|
requestId: 'req-cli-1',
|
|
107
|
-
|
|
107
|
+
trailhead: 'cli',
|
|
108
108
|
};
|
|
109
|
-
expect(input.
|
|
109
|
+
expect(input.trailhead).toBe('cli');
|
|
110
110
|
});
|
|
111
111
|
|
|
112
112
|
test('accepts minimal extraction with only required fields', () => {
|
|
113
113
|
const input: PermitExtractionInput = {
|
|
114
114
|
requestId: 'req-minimal',
|
|
115
|
-
|
|
115
|
+
trailhead: 'http',
|
|
116
116
|
};
|
|
117
117
|
expect(input.requestId).toBe('req-minimal');
|
|
118
118
|
expect(input.bearerToken).toBeUndefined();
|
|
@@ -24,9 +24,9 @@ const noopRun = () => Result.ok({});
|
|
|
24
24
|
describe('destroyWithoutPermit', () => {
|
|
25
25
|
test('error when destroy trail has no permit', () => {
|
|
26
26
|
const t = trail('user.delete', {
|
|
27
|
+
blaze: noopRun,
|
|
27
28
|
input: emptyInput,
|
|
28
29
|
intent: 'destroy',
|
|
29
|
-
run: noopRun,
|
|
30
30
|
});
|
|
31
31
|
const diagnostics = destroyWithoutPermit([t]);
|
|
32
32
|
expect(diagnostics).toHaveLength(1);
|
|
@@ -40,10 +40,10 @@ describe('destroyWithoutPermit', () => {
|
|
|
40
40
|
|
|
41
41
|
test('no diagnostic when destroy trail has a scoped permit', () => {
|
|
42
42
|
const t = trail('user.delete', {
|
|
43
|
+
blaze: noopRun,
|
|
43
44
|
input: emptyInput,
|
|
44
45
|
intent: 'destroy',
|
|
45
46
|
permit: { scopes: ['user:delete'] },
|
|
46
|
-
run: noopRun,
|
|
47
47
|
});
|
|
48
48
|
const diagnostics = destroyWithoutPermit([t]);
|
|
49
49
|
expect(diagnostics).toHaveLength(0);
|
|
@@ -51,10 +51,10 @@ describe('destroyWithoutPermit', () => {
|
|
|
51
51
|
|
|
52
52
|
test('error when destroy trail has permit: public', () => {
|
|
53
53
|
const t = trail('user.delete', {
|
|
54
|
+
blaze: noopRun,
|
|
54
55
|
input: emptyInput,
|
|
55
56
|
intent: 'destroy',
|
|
56
57
|
permit: 'public',
|
|
57
|
-
run: noopRun,
|
|
58
58
|
});
|
|
59
59
|
const diagnostics = destroyWithoutPermit([t]);
|
|
60
60
|
expect(diagnostics).toHaveLength(1);
|
|
@@ -73,8 +73,8 @@ describe('destroyWithoutPermit', () => {
|
|
|
73
73
|
describe('writeWithoutPermit', () => {
|
|
74
74
|
test('warning when write trail has no permit', () => {
|
|
75
75
|
const t = trail('user.create', {
|
|
76
|
+
blaze: noopRun,
|
|
76
77
|
input: emptyInput,
|
|
77
|
-
run: noopRun,
|
|
78
78
|
});
|
|
79
79
|
const diagnostics = writeWithoutPermit([t]);
|
|
80
80
|
expect(diagnostics).toHaveLength(1);
|
|
@@ -87,9 +87,9 @@ describe('writeWithoutPermit', () => {
|
|
|
87
87
|
|
|
88
88
|
test('no warning when write trail has permit: public', () => {
|
|
89
89
|
const t = trail('user.create', {
|
|
90
|
+
blaze: noopRun,
|
|
90
91
|
input: emptyInput,
|
|
91
92
|
permit: 'public',
|
|
92
|
-
run: noopRun,
|
|
93
93
|
});
|
|
94
94
|
const diagnostics = writeWithoutPermit([t]);
|
|
95
95
|
expect(diagnostics).toHaveLength(0);
|
|
@@ -97,8 +97,8 @@ describe('writeWithoutPermit', () => {
|
|
|
97
97
|
|
|
98
98
|
test('warning when trail has no intent (defaults to write)', () => {
|
|
99
99
|
const t = trail('user.update', {
|
|
100
|
+
blaze: noopRun,
|
|
100
101
|
input: emptyInput,
|
|
101
|
-
run: noopRun,
|
|
102
102
|
});
|
|
103
103
|
// Override intent to undefined to simulate a manually constructed trail
|
|
104
104
|
const noIntent = { ...t, intent: undefined } as unknown as ReturnType<
|
|
@@ -115,9 +115,9 @@ describe('writeWithoutPermit', () => {
|
|
|
115
115
|
|
|
116
116
|
test('no diagnostic for read trail without permit', () => {
|
|
117
117
|
const t = trail('user.list', {
|
|
118
|
+
blaze: noopRun,
|
|
118
119
|
input: emptyInput,
|
|
119
120
|
intent: 'read',
|
|
120
|
-
run: noopRun,
|
|
121
121
|
});
|
|
122
122
|
const diagnostics = writeWithoutPermit([t]);
|
|
123
123
|
expect(diagnostics).toHaveLength(0);
|
|
@@ -131,9 +131,9 @@ describe('writeWithoutPermit', () => {
|
|
|
131
131
|
describe('scopeNamingConsistency', () => {
|
|
132
132
|
test('scope user:write passes naming check', () => {
|
|
133
133
|
const t = trail('user.update', {
|
|
134
|
+
blaze: noopRun,
|
|
134
135
|
input: emptyInput,
|
|
135
136
|
permit: { scopes: ['user:write'] },
|
|
136
|
-
run: noopRun,
|
|
137
137
|
});
|
|
138
138
|
const diagnostics = scopeNamingConsistency([t]);
|
|
139
139
|
expect(diagnostics).toHaveLength(0);
|
|
@@ -141,9 +141,9 @@ describe('scopeNamingConsistency', () => {
|
|
|
141
141
|
|
|
142
142
|
test('warning for scope without colon', () => {
|
|
143
143
|
const t = trail('admin.panel', {
|
|
144
|
+
blaze: noopRun,
|
|
144
145
|
input: emptyInput,
|
|
145
146
|
permit: { scopes: ['admin'] },
|
|
146
|
-
run: noopRun,
|
|
147
147
|
});
|
|
148
148
|
const diagnostics = scopeNamingConsistency([t]);
|
|
149
149
|
expect(diagnostics).toHaveLength(1);
|
|
@@ -163,14 +163,14 @@ describe('scopeNamingConsistency', () => {
|
|
|
163
163
|
describe('orphanScopeDetection', () => {
|
|
164
164
|
test('warning for orphan scope (typo)', () => {
|
|
165
165
|
const t1 = trail('user.read', {
|
|
166
|
+
blaze: noopRun,
|
|
166
167
|
input: emptyInput,
|
|
167
168
|
permit: { scopes: ['user:read'] },
|
|
168
|
-
run: noopRun,
|
|
169
169
|
});
|
|
170
170
|
const t2 = trail('user.write', {
|
|
171
|
+
blaze: noopRun,
|
|
171
172
|
input: emptyInput,
|
|
172
173
|
permit: { scopes: ['user:wirte'] },
|
|
173
|
-
run: noopRun,
|
|
174
174
|
});
|
|
175
175
|
const diagnostics = orphanScopeDetection([t1, t2]);
|
|
176
176
|
// Both scopes are unique (appear in only 1 trail each)
|
|
@@ -181,14 +181,14 @@ describe('orphanScopeDetection', () => {
|
|
|
181
181
|
|
|
182
182
|
test('no warning for shared scopes', () => {
|
|
183
183
|
const t1 = trail('user.read', {
|
|
184
|
+
blaze: noopRun,
|
|
184
185
|
input: emptyInput,
|
|
185
186
|
permit: { scopes: ['user:read'] },
|
|
186
|
-
run: noopRun,
|
|
187
187
|
});
|
|
188
188
|
const t2 = trail('user.profile', {
|
|
189
|
+
blaze: noopRun,
|
|
189
190
|
input: emptyInput,
|
|
190
191
|
permit: { scopes: ['user:read'] },
|
|
191
|
-
run: noopRun,
|
|
192
192
|
});
|
|
193
193
|
const diagnostics = orphanScopeDetection([t1, t2]);
|
|
194
194
|
expect(diagnostics).toHaveLength(0);
|
|
@@ -203,23 +203,23 @@ describe('orphanScopeDetection', () => {
|
|
|
203
203
|
describe('validatePermits', () => {
|
|
204
204
|
test('runs all rules and aggregates diagnostics', () => {
|
|
205
205
|
const destroyNoPerm = trail('user.delete', {
|
|
206
|
+
blaze: noopRun,
|
|
206
207
|
input: emptyInput,
|
|
207
208
|
intent: 'destroy',
|
|
208
|
-
run: noopRun,
|
|
209
209
|
});
|
|
210
210
|
const writeNoPerm = trail('user.create', {
|
|
211
|
+
blaze: noopRun,
|
|
211
212
|
input: emptyInput,
|
|
212
|
-
run: noopRun,
|
|
213
213
|
});
|
|
214
214
|
const badScope = trail('admin.panel', {
|
|
215
|
+
blaze: noopRun,
|
|
215
216
|
input: emptyInput,
|
|
216
217
|
permit: { scopes: ['admin'] },
|
|
217
|
-
run: noopRun,
|
|
218
218
|
});
|
|
219
219
|
const orphanScope = trail('analytics.export', {
|
|
220
|
+
blaze: noopRun,
|
|
220
221
|
input: emptyInput,
|
|
221
222
|
permit: { scopes: ['analytics:exportt'] },
|
|
222
|
-
run: noopRun,
|
|
223
223
|
});
|
|
224
224
|
|
|
225
225
|
const diagnostics = validatePermits([
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Result } from '@ontrails/core';
|
|
2
|
-
import type {
|
|
2
|
+
import type { Gate } from '@ontrails/core';
|
|
3
3
|
|
|
4
4
|
import { PermitError } from './errors.js';
|
|
5
5
|
import { getPermit } from './permit.js';
|
|
@@ -25,25 +25,25 @@ const findMissing = (
|
|
|
25
25
|
): readonly string[] => required.filter((s) => !held.includes(s));
|
|
26
26
|
|
|
27
27
|
// ---------------------------------------------------------------------------
|
|
28
|
-
// Auth
|
|
28
|
+
// Auth gate
|
|
29
29
|
// ---------------------------------------------------------------------------
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
|
-
* A {@link
|
|
32
|
+
* A {@link Gate} that enforces permit scopes declared on trails.
|
|
33
33
|
*
|
|
34
|
-
* The
|
|
34
|
+
* The gate reads the trail's `permit` field (a `PermitRequirement`):
|
|
35
35
|
*
|
|
36
|
-
* - If `permit` is `'public'` or `undefined` the
|
|
37
|
-
* - If `permit` has `scopes`, the
|
|
36
|
+
* - If `permit` is `'public'` or `undefined` the gate passes through.
|
|
37
|
+
* - If `permit` has `scopes`, the gate checks that `ctx.permit` contains
|
|
38
38
|
* all required scopes. A superset is fine; missing scopes produce a
|
|
39
39
|
* `PermitError`.
|
|
40
40
|
*
|
|
41
|
-
* Because `ctx.
|
|
42
|
-
* this
|
|
43
|
-
* No special
|
|
41
|
+
* Because `ctx.cross()` re-enters `executeTrail` (which applies gates),
|
|
42
|
+
* this gate automatically re-checks on every invocation in a crossing chain.
|
|
43
|
+
* No special crossing-chain handling is needed — it is built into the
|
|
44
44
|
* architecture.
|
|
45
45
|
*/
|
|
46
|
-
export const
|
|
46
|
+
export const authGate: Gate = {
|
|
47
47
|
description: 'Enforces permit scopes declared on trails',
|
|
48
48
|
name: 'auth',
|
|
49
49
|
wrap: (_trail, impl) => {
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Result, provision } from '@ontrails/core';
|
|
2
|
+
|
|
3
|
+
import type { AuthConnector } from './connectors/connector.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Auth provision — manages the auth connector lifecycle.
|
|
7
|
+
*
|
|
8
|
+
* The v1 factory returns a no-op connector that always succeeds (null permit).
|
|
9
|
+
* Real connector configuration will come through `ProvisionSpec.config`
|
|
10
|
+
* (TRL-91). The mock factory provides a synthetic connector that always
|
|
11
|
+
* succeeds.
|
|
12
|
+
*/
|
|
13
|
+
export const authProvision = provision<AuthConnector>('auth', {
|
|
14
|
+
create: (_svc) =>
|
|
15
|
+
Result.ok({
|
|
16
|
+
// oxlint-disable-next-line require-await -- stub connector satisfies async interface
|
|
17
|
+
authenticate: async () => Result.ok(null),
|
|
18
|
+
} satisfies AuthConnector),
|
|
19
|
+
description: 'Authentication connector',
|
|
20
|
+
meta: { category: 'infrastructure' },
|
|
21
|
+
mock: () =>
|
|
22
|
+
({
|
|
23
|
+
// oxlint-disable-next-line require-await -- mock connector satisfies async interface
|
|
24
|
+
authenticate: async () => Result.ok(null),
|
|
25
|
+
}) satisfies AuthConnector,
|
|
26
|
+
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Result } from '@ontrails/core';
|
|
2
2
|
|
|
3
|
-
import type { PermitExtractionInput } from '
|
|
4
|
-
import type { Permit } from '
|
|
3
|
+
import type { PermitExtractionInput } from '../extraction.js';
|
|
4
|
+
import type { Permit } from '../permit.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* @deprecated Use {@link PermitExtractionInput} instead. Kept as an alias
|
|
@@ -9,7 +9,7 @@ import type { Permit } from './permit.js';
|
|
|
9
9
|
*/
|
|
10
10
|
export type AuthCredentials = PermitExtractionInput;
|
|
11
11
|
|
|
12
|
-
/** Errors from auth
|
|
12
|
+
/** Errors from auth connectors. */
|
|
13
13
|
export interface AuthError {
|
|
14
14
|
readonly code:
|
|
15
15
|
| 'expired_token'
|
|
@@ -20,15 +20,15 @@ export interface AuthError {
|
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
|
-
* Auth
|
|
23
|
+
* Auth connector port. Given extraction input, produce a permit or an error.
|
|
24
24
|
*
|
|
25
|
-
* The
|
|
25
|
+
* The connector receives the full {@link PermitExtractionInput} — trailhead,
|
|
26
26
|
* headers, requestId, and credential fields — so it can make richer
|
|
27
|
-
* decisions (e.g., rate-limit by
|
|
27
|
+
* decisions (e.g., rate-limit by trailhead or correlate via requestId).
|
|
28
28
|
*
|
|
29
29
|
* Deliberately narrow — no session management, no token refresh.
|
|
30
30
|
*/
|
|
31
|
-
export interface
|
|
31
|
+
export interface AuthConnector {
|
|
32
32
|
readonly authenticate: (
|
|
33
33
|
input: PermitExtractionInput
|
|
34
34
|
) => Promise<Result<Permit | null, AuthError>>;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { Result } from '@ontrails/core';
|
|
2
2
|
|
|
3
|
-
import type {
|
|
3
|
+
import type { AuthConnector, AuthError } from './connector.js';
|
|
4
4
|
import type { PermitExtractionInput } from '../extraction.js';
|
|
5
5
|
import type { Permit } from '../permit.js';
|
|
6
6
|
|
|
7
|
-
/** Configuration for the JWT auth
|
|
8
|
-
export interface
|
|
7
|
+
/** Configuration for the JWT auth connector. */
|
|
8
|
+
export interface JwtConnectorOptions {
|
|
9
9
|
/** HMAC secret for HS256 verification. */
|
|
10
10
|
readonly secret?: string;
|
|
11
11
|
/** JWKS endpoint for RS256/ES256 (not yet implemented). */
|
|
@@ -101,7 +101,7 @@ const verifyHmacSignature = (
|
|
|
101
101
|
/** Validate standard claims (exp, iss, aud). */
|
|
102
102
|
const validateClaims = (
|
|
103
103
|
payload: JwtPayload,
|
|
104
|
-
options:
|
|
104
|
+
options: JwtConnectorOptions
|
|
105
105
|
): AuthError | undefined => {
|
|
106
106
|
if (
|
|
107
107
|
payload.exp !== undefined &&
|
|
@@ -156,7 +156,7 @@ const extractRoles = (
|
|
|
156
156
|
/** Build a Permit from a validated JWT payload. */
|
|
157
157
|
const buildPermit = (
|
|
158
158
|
payload: JwtPayload,
|
|
159
|
-
options:
|
|
159
|
+
options: JwtConnectorOptions
|
|
160
160
|
): Result<Permit, AuthError> => {
|
|
161
161
|
if (!payload.sub) {
|
|
162
162
|
return authErr('invalid_token', 'Missing subject claim (sub)');
|
|
@@ -192,7 +192,7 @@ const decodeAndVerify = async (
|
|
|
192
192
|
/** Validate claims and build a permit from a verified payload. */
|
|
193
193
|
const payloadToPermit = (
|
|
194
194
|
payload: JwtPayload,
|
|
195
|
-
options:
|
|
195
|
+
options: JwtConnectorOptions
|
|
196
196
|
): Result<Permit, AuthError> => {
|
|
197
197
|
const claimError = validateClaims(payload, options);
|
|
198
198
|
if (claimError) {
|
|
@@ -206,13 +206,15 @@ const payloadToPermit = (
|
|
|
206
206
|
// ---------------------------------------------------------------------------
|
|
207
207
|
|
|
208
208
|
/**
|
|
209
|
-
* Create a JWT auth
|
|
209
|
+
* Create a JWT auth connector using Bun's native crypto.
|
|
210
210
|
*
|
|
211
211
|
* Verifies HS256-signed JWTs, extracts claims into a Permit, and checks
|
|
212
212
|
* issuer/audience when configured. Returns `Result.ok(null)` when no
|
|
213
213
|
* credentials are provided.
|
|
214
214
|
*/
|
|
215
|
-
export const
|
|
215
|
+
export const createJwtConnector = (
|
|
216
|
+
options: JwtConnectorOptions
|
|
217
|
+
): AuthConnector => {
|
|
216
218
|
const authenticate = async (
|
|
217
219
|
input: PermitExtractionInput
|
|
218
220
|
): Promise<Result<Permit | null, AuthError>> => {
|