@friggframework/core 2.0.0--canary.678.58db4b2.0 → 2.0.0--canary.680.b3ca7f1.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/CHANGELOG.md +11 -0
- package/CLAUDE.md +0 -2
- package/README.md +0 -57
- package/application/commands/integration-commands.js +18 -8
- package/application/index.js +6 -2
- package/database/encryption/README.md +2 -0
- package/database/encryption/encryption-schema-registry.js +35 -12
- package/database/encryption/field-encryption-service.js +30 -3
- package/database/encryption/test-support/fake-ciphertext.js +15 -0
- package/handlers/app-definition-loader.js +16 -13
- package/handlers/backend-utils.js +31 -25
- package/handlers/routers/health.js +1 -4
- package/handlers/workers/integration-defined-workers.js +5 -2
- package/index.js +4 -2
- package/integrations/index.js +0 -4
- package/integrations/integration-router.js +16 -29
- package/integrations/utils/map-integration-dto.js +1 -15
- package/management-api/openapi/components.js +7 -1
- package/management-api/openapi/v2-operations.js +4 -0
- package/management-api/v2/dependencies.js +23 -13
- package/modules/entity-type-registry.js +311 -0
- package/modules/index.js +6 -0
- package/modules/use-cases/entity-types.js +36 -12
- package/package.json +5 -6
- package/types/index.d.ts +1 -1
- package/types/modules/index.d.ts +58 -0
- package/user/use-cases/get-user-from-adopter-jwt.js +122 -109
- package/user/use-cases/get-user-from-x-frigg-headers.js +51 -29
- package/user/user.js +0 -11
- package/types/user/index.d.ts +0 -67
- package/user/adopter-jwt-config.js +0 -177
- package/user/adopter-jwt-errors.js +0 -65
- package/user/adopter-token-verifier.js +0 -122
- package/user/app-user-helpers.js +0 -73
- package/user/tests/doubles/adopter-jwt-fixtures.js +0 -130
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
const { getLogger } = require('../logs');
|
|
2
|
-
|
|
3
|
-
const log = getLogger('frigg.user.adopter_jwt');
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Normalises and validates `user.jwtConfig` from the app definition (ADR-056 §2).
|
|
7
|
-
* Called once at startup by the app definition loader and once per verifier,
|
|
8
|
-
* never per request.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
const ASYMMETRIC_ALGORITHMS = ['ES256', 'ES384', 'RS256', 'RS384', 'RS512'];
|
|
12
|
-
const HMAC_ALGORITHMS = ['HS256', 'HS384', 'HS512'];
|
|
13
|
-
const SUPPORTED_ALGORITHMS = [...ASYMMETRIC_ALGORITHMS, ...HMAC_ALGORITHMS];
|
|
14
|
-
|
|
15
|
-
const DEFAULTS = Object.freeze({
|
|
16
|
-
jwksAlgorithms: Object.freeze(['ES256', 'RS256']),
|
|
17
|
-
secretAlgorithms: Object.freeze(['HS256']),
|
|
18
|
-
clockToleranceSec: 30,
|
|
19
|
-
userIdClaim: 'sub',
|
|
20
|
-
orgIdClaim: 'org_id',
|
|
21
|
-
roleClaim: 'org_role',
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
const isNonEmptyString = (value) =>
|
|
25
|
-
typeof value === 'string' && value.trim() !== '';
|
|
26
|
-
|
|
27
|
-
const hasAudience = (audience) =>
|
|
28
|
-
isNonEmptyString(audience) ||
|
|
29
|
-
(Array.isArray(audience) &&
|
|
30
|
-
audience.length > 0 &&
|
|
31
|
-
audience.every(isNonEmptyString));
|
|
32
|
-
|
|
33
|
-
const LOOPBACK_IPV4 = /^127(\.\d{1,3}){3}$/;
|
|
34
|
-
|
|
35
|
-
function isLoopbackHost(hostname) {
|
|
36
|
-
return (
|
|
37
|
-
hostname === 'localhost' ||
|
|
38
|
-
hostname === '[::1]' ||
|
|
39
|
-
LOOPBACK_IPV4.test(hostname)
|
|
40
|
-
);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* The JWKS is the trust root: fetched over https, or over http only to the
|
|
45
|
-
* machine itself (a local identity provider in development).
|
|
46
|
-
*/
|
|
47
|
-
function assertJwksUri(jwksUri) {
|
|
48
|
-
let url;
|
|
49
|
-
try {
|
|
50
|
-
url = new URL(jwksUri);
|
|
51
|
-
} catch {
|
|
52
|
-
throw new Error('user.jwtConfig.jwksUri is not a valid URL');
|
|
53
|
-
}
|
|
54
|
-
if (url.protocol === 'https:') return;
|
|
55
|
-
if (url.protocol === 'http:' && isLoopbackHost(url.hostname)) return;
|
|
56
|
-
throw new Error(
|
|
57
|
-
'user.jwtConfig.jwksUri must use https (http is allowed only for localhost, 127.0.0.0/8 and ::1)'
|
|
58
|
-
);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* @param {Object} [jwtConfig] `userConfig.jwtConfig`
|
|
63
|
-
* @returns {{jwksUri?: string, secret?: string, issuer?: string,
|
|
64
|
-
* audience?: string|string[], algorithms: string[], clockToleranceSec: number,
|
|
65
|
-
* userIdClaim: string, orgIdClaim: string, roleClaim: string}}
|
|
66
|
-
* @throws {Error} When the config cannot verify any token safely.
|
|
67
|
-
*/
|
|
68
|
-
function resolveAdopterJwtConfig(jwtConfig) {
|
|
69
|
-
const config = jwtConfig && typeof jwtConfig === 'object' ? jwtConfig : {};
|
|
70
|
-
const hasJwks = isNonEmptyString(config.jwksUri);
|
|
71
|
-
const hasSecret = isNonEmptyString(config.secret);
|
|
72
|
-
|
|
73
|
-
if (!hasJwks && !hasSecret) {
|
|
74
|
-
throw new Error(
|
|
75
|
-
'user.authModes.adopterJwt is enabled but user.jwtConfig has neither jwksUri nor secret'
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
if (hasJwks && hasSecret) {
|
|
79
|
-
throw new Error(
|
|
80
|
-
'user.jwtConfig sets both jwksUri and secret; set one, so a token cannot choose which key family verifies it'
|
|
81
|
-
);
|
|
82
|
-
}
|
|
83
|
-
if (hasJwks) {
|
|
84
|
-
assertJwksUri(config.jwksUri);
|
|
85
|
-
// Without an audience, a token minted by the same issuer for another
|
|
86
|
-
// service verifies here. HMAC mode shares its secret with one issuer,
|
|
87
|
-
// so it stays lenient.
|
|
88
|
-
if (!hasAudience(config.audience)) {
|
|
89
|
-
throw new Error(
|
|
90
|
-
'user.jwtConfig.audience is required when jwksUri is used'
|
|
91
|
-
);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
if (config.algorithm !== undefined && config.algorithms !== undefined) {
|
|
95
|
-
throw new Error(
|
|
96
|
-
'user.jwtConfig sets both algorithm and algorithms; use algorithms (algorithm is a deprecated alias)'
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const configured =
|
|
101
|
-
config.algorithms ??
|
|
102
|
-
(config.algorithm ? [config.algorithm] : undefined);
|
|
103
|
-
const algorithms =
|
|
104
|
-
configured ??
|
|
105
|
-
(hasSecret ? DEFAULTS.secretAlgorithms : DEFAULTS.jwksAlgorithms);
|
|
106
|
-
|
|
107
|
-
if (
|
|
108
|
-
!Array.isArray(algorithms) ||
|
|
109
|
-
algorithms.length === 0 ||
|
|
110
|
-
algorithms.some((alg) => !SUPPORTED_ALGORITHMS.includes(alg))
|
|
111
|
-
) {
|
|
112
|
-
throw new Error(
|
|
113
|
-
`user.jwtConfig.algorithms must be a non-empty list drawn from ${SUPPORTED_ALGORITHMS.join(
|
|
114
|
-
', '
|
|
115
|
-
)}`
|
|
116
|
-
);
|
|
117
|
-
}
|
|
118
|
-
// A key family only verifies its own algorithms; mixing them is how
|
|
119
|
-
// algorithm-confusion attacks are built.
|
|
120
|
-
if (hasSecret && algorithms.some((alg) => !HMAC_ALGORITHMS.includes(alg))) {
|
|
121
|
-
throw new Error(
|
|
122
|
-
'user.jwtConfig.algorithms may only list HS* algorithms when secret is used'
|
|
123
|
-
);
|
|
124
|
-
}
|
|
125
|
-
if (hasJwks && algorithms.some((alg) => HMAC_ALGORITHMS.includes(alg))) {
|
|
126
|
-
throw new Error(
|
|
127
|
-
'user.jwtConfig.algorithms may not list HS* algorithms when jwksUri is used'
|
|
128
|
-
);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const clockToleranceSec =
|
|
132
|
-
config.clockToleranceSec ?? DEFAULTS.clockToleranceSec;
|
|
133
|
-
if (!Number.isInteger(clockToleranceSec) || clockToleranceSec < 0) {
|
|
134
|
-
throw new Error(
|
|
135
|
-
'user.jwtConfig.clockToleranceSec must be an integer >= 0'
|
|
136
|
-
);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
return {
|
|
140
|
-
jwksUri: hasJwks ? config.jwksUri : undefined,
|
|
141
|
-
secret: hasSecret ? config.secret : undefined,
|
|
142
|
-
issuer: config.issuer || undefined,
|
|
143
|
-
audience: config.audience || undefined,
|
|
144
|
-
algorithms,
|
|
145
|
-
clockToleranceSec,
|
|
146
|
-
userIdClaim: config.userIdClaim || DEFAULTS.userIdClaim,
|
|
147
|
-
orgIdClaim: config.orgIdClaim || DEFAULTS.orgIdClaim,
|
|
148
|
-
roleClaim: config.roleClaim || DEFAULTS.roleClaim,
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Startup check: throws when `authModes.adopterJwt` is on and `jwtConfig`
|
|
154
|
-
* is unusable, and warns when a jwksUri has no issuer. A no-op when the mode
|
|
155
|
-
* is off, so apps that never enabled it are not affected by a stray `jwtConfig`.
|
|
156
|
-
*
|
|
157
|
-
* @param {Object|null} [userConfig] `appDefinition.user`
|
|
158
|
-
* @throws {Error}
|
|
159
|
-
*/
|
|
160
|
-
function assertAdopterJwtConfig(userConfig) {
|
|
161
|
-
if (userConfig?.authModes?.adopterJwt !== true) return;
|
|
162
|
-
const config = resolveAdopterJwtConfig(userConfig.jwtConfig);
|
|
163
|
-
if (config.jwksUri && !config.issuer) {
|
|
164
|
-
log.warn(
|
|
165
|
-
'Adopter JWT issuer is not set; any issuer on this JWKS is accepted',
|
|
166
|
-
{
|
|
167
|
-
eventName: `${log.name}.issuer_unset`,
|
|
168
|
-
}
|
|
169
|
-
);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
module.exports = {
|
|
174
|
-
resolveAdopterJwtConfig,
|
|
175
|
-
assertAdopterJwtConfig,
|
|
176
|
-
SUPPORTED_ALGORITHMS,
|
|
177
|
-
};
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
/* eslint-disable camelcase -- the reason codes are snake_case by spec */
|
|
2
|
-
const Boom = require('@hapi/boom');
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Every failure adopter-token authentication can produce, keyed by the stable
|
|
6
|
-
* reason code clients see in `error.data.reason`. Messages are fixed text:
|
|
7
|
-
* the token and its claims never reach a message, `data` or a log record.
|
|
8
|
-
*/
|
|
9
|
-
const ADOPTER_JWT_ERRORS = Object.freeze({
|
|
10
|
-
token_expired: Object.freeze({ status: 401, message: 'Token has expired' }),
|
|
11
|
-
token_not_yet_valid: Object.freeze({
|
|
12
|
-
status: 401,
|
|
13
|
-
message: 'Token is not yet valid',
|
|
14
|
-
}),
|
|
15
|
-
token_invalid_signature: Object.freeze({
|
|
16
|
-
status: 401,
|
|
17
|
-
message: 'Token signature could not be verified',
|
|
18
|
-
}),
|
|
19
|
-
token_wrong_issuer: Object.freeze({
|
|
20
|
-
status: 401,
|
|
21
|
-
message: 'Token issuer is not accepted',
|
|
22
|
-
}),
|
|
23
|
-
token_wrong_audience: Object.freeze({
|
|
24
|
-
status: 401,
|
|
25
|
-
message: 'Token audience is not accepted',
|
|
26
|
-
}),
|
|
27
|
-
token_missing_user_claim: Object.freeze({
|
|
28
|
-
status: 401,
|
|
29
|
-
message: 'Token does not identify a user',
|
|
30
|
-
}),
|
|
31
|
-
token_malformed: Object.freeze({
|
|
32
|
-
status: 401,
|
|
33
|
-
message: 'Token is malformed',
|
|
34
|
-
}),
|
|
35
|
-
// Our dependency failing, not the caller's token: not a 401, so a client
|
|
36
|
-
// does not discard a good session during a JWKS outage.
|
|
37
|
-
jwks_unavailable: Object.freeze({
|
|
38
|
-
status: 503,
|
|
39
|
-
message: 'Token verification keys are unavailable',
|
|
40
|
-
}),
|
|
41
|
-
multi_org_unsupported: Object.freeze({
|
|
42
|
-
status: 403,
|
|
43
|
-
message:
|
|
44
|
-
'This user already belongs to a different organization. ' +
|
|
45
|
-
'Membership in more than one organization is not supported yet (ADR-056 §3.6).',
|
|
46
|
-
}),
|
|
47
|
-
jwt_config_invalid: Object.freeze({
|
|
48
|
-
status: 500,
|
|
49
|
-
message: 'Adopter JWT authentication is misconfigured',
|
|
50
|
-
}),
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* @param {keyof typeof ADOPTER_JWT_ERRORS} code
|
|
55
|
-
* @returns {Boom.Boom} with `data.reason` set to `code`
|
|
56
|
-
*/
|
|
57
|
-
function fail(code) {
|
|
58
|
-
const { status, message } = ADOPTER_JWT_ERRORS[code];
|
|
59
|
-
return new Boom.Boom(message, {
|
|
60
|
-
statusCode: status,
|
|
61
|
-
data: { reason: code },
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
module.exports = { ADOPTER_JWT_ERRORS, fail };
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
const jose = require('jose');
|
|
2
|
-
const { getLogger } = require('../logs');
|
|
3
|
-
const { resolveAdopterJwtConfig } = require('./adopter-jwt-config');
|
|
4
|
-
const { fail } = require('./adopter-jwt-errors');
|
|
5
|
-
|
|
6
|
-
const log = getLogger('frigg.user.adopter_jwt');
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* The only failures that are the caller's token. Anything else thrown while
|
|
10
|
-
* verifying (a refused connection, a DNS failure, a non-200 from the JWKS, a
|
|
11
|
-
* generic JOSE error) is our key source failing, not a bad token.
|
|
12
|
-
*/
|
|
13
|
-
function tokenFailureCode(error) {
|
|
14
|
-
const { errors } = jose;
|
|
15
|
-
if (error instanceof errors.JWTExpired) return 'token_expired';
|
|
16
|
-
if (error instanceof errors.JWTClaimValidationFailed) {
|
|
17
|
-
if (error.claim === 'iss') return 'token_wrong_issuer';
|
|
18
|
-
if (error.claim === 'aud') return 'token_wrong_audience';
|
|
19
|
-
if (error.claim === 'nbf') return 'token_not_yet_valid';
|
|
20
|
-
return 'token_malformed';
|
|
21
|
-
}
|
|
22
|
-
if (
|
|
23
|
-
error instanceof errors.JWSSignatureVerificationFailed ||
|
|
24
|
-
error instanceof errors.JOSEAlgNotAllowed ||
|
|
25
|
-
error instanceof errors.JWKSNoMatchingKey ||
|
|
26
|
-
error instanceof errors.JWKSMultipleMatchingKeys
|
|
27
|
-
) {
|
|
28
|
-
return 'token_invalid_signature';
|
|
29
|
-
}
|
|
30
|
-
if (
|
|
31
|
-
error instanceof errors.JWSInvalid ||
|
|
32
|
-
error instanceof errors.JWTInvalid
|
|
33
|
-
) {
|
|
34
|
-
return 'token_malformed';
|
|
35
|
-
}
|
|
36
|
-
return null;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Verifies adopter-issued tokens (ADR-056 §2). Owns config resolution, the key
|
|
41
|
-
* source and the mapping from jose failures to typed Boom errors.
|
|
42
|
-
*
|
|
43
|
-
* Build one per app definition: the config is resolved once here, and the
|
|
44
|
-
* remote JWKS is an instance field, so jose's key cache lives as long as the
|
|
45
|
-
* verifier does. A bad config does not throw here (apps with adopterJwt off
|
|
46
|
-
* build a verifier too); `verify` answers 500 and logs the real reason.
|
|
47
|
-
*
|
|
48
|
-
* @param {Object} params
|
|
49
|
-
* @param {Object} [params.jwtConfig] `userConfig.jwtConfig`
|
|
50
|
-
* @param {Function|Object|Uint8Array} [params.keySource] Replaces the key
|
|
51
|
-
* derived from the config: a jose key or a getKey function such as
|
|
52
|
-
* `jose.createLocalJWKSet(jwks)`. Tests and custom key stores use it.
|
|
53
|
-
* @returns {{verify: (token: string) => Promise<Object>,
|
|
54
|
-
* claimNames: {userIdClaim: string, orgIdClaim: string, roleClaim: string}}}
|
|
55
|
-
*/
|
|
56
|
-
function createAdopterTokenVerifier({ jwtConfig, keySource } = {}) {
|
|
57
|
-
let config = null;
|
|
58
|
-
let configError = null;
|
|
59
|
-
try {
|
|
60
|
-
config = resolveAdopterJwtConfig(jwtConfig);
|
|
61
|
-
} catch (error) {
|
|
62
|
-
configError = error;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
let key = null;
|
|
66
|
-
if (config) {
|
|
67
|
-
key =
|
|
68
|
-
keySource ??
|
|
69
|
-
(config.jwksUri
|
|
70
|
-
? jose.createRemoteJWKSet(new URL(config.jwksUri))
|
|
71
|
-
: new TextEncoder().encode(config.secret));
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const requireConfig = () => {
|
|
75
|
-
if (config) return config;
|
|
76
|
-
log.error('Adopter JWT config is invalid', {
|
|
77
|
-
eventName: `${log.name}.config_invalid`,
|
|
78
|
-
validationMessage: configError.message,
|
|
79
|
-
});
|
|
80
|
-
throw fail('jwt_config_invalid');
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
return {
|
|
84
|
-
get claimNames() {
|
|
85
|
-
const { userIdClaim, orgIdClaim, roleClaim } = requireConfig();
|
|
86
|
-
return { userIdClaim, orgIdClaim, roleClaim };
|
|
87
|
-
},
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* @param {string} token
|
|
91
|
-
* @returns {Promise<Object>} the verified claims
|
|
92
|
-
* @throws {Boom} 401 with `data.reason` for a token the verifier refuses
|
|
93
|
-
* @throws {Boom} 503 `jwks_unavailable` for any other verification failure
|
|
94
|
-
* @throws {Boom} 500 `jwt_config_invalid` when the config is unusable
|
|
95
|
-
*/
|
|
96
|
-
async verify(token) {
|
|
97
|
-
const resolved = requireConfig();
|
|
98
|
-
try {
|
|
99
|
-
const { payload } = await jose.jwtVerify(token, key, {
|
|
100
|
-
algorithms: resolved.algorithms,
|
|
101
|
-
issuer: resolved.issuer,
|
|
102
|
-
audience: resolved.audience,
|
|
103
|
-
clockTolerance: resolved.clockToleranceSec,
|
|
104
|
-
// A token that never expires is not browser-safe.
|
|
105
|
-
requiredClaims: ['exp'],
|
|
106
|
-
});
|
|
107
|
-
return payload;
|
|
108
|
-
} catch (error) {
|
|
109
|
-
const code = tokenFailureCode(error) ?? 'jwks_unavailable';
|
|
110
|
-
if (code !== 'jwks_unavailable') {
|
|
111
|
-
log.debug('Adopter token rejected', {
|
|
112
|
-
eventName: `${log.name}.rejected`,
|
|
113
|
-
reason: code,
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
throw fail(code);
|
|
117
|
-
}
|
|
118
|
-
},
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
module.exports = { createAdopterTokenVerifier };
|
package/user/app-user-helpers.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
const { User } = require('./user');
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Find-or-create steps shared by the paths that identify a user by an app user
|
|
5
|
-
* id and/or app organization id (x-frigg headers, adopter token). Each path
|
|
6
|
-
* keeps its own policy between "find" and "create".
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
const wantsIndividual = (userConfig) =>
|
|
10
|
-
userConfig.individualUserRequired !== false;
|
|
11
|
-
|
|
12
|
-
const wantsOrganization = (userConfig, appOrgId) =>
|
|
13
|
-
Boolean(appOrgId) && Boolean(userConfig.organizationUserRequired);
|
|
14
|
-
|
|
15
|
-
async function findAppUsers(
|
|
16
|
-
userRepository,
|
|
17
|
-
userConfig,
|
|
18
|
-
{ appUserId, appOrgId }
|
|
19
|
-
) {
|
|
20
|
-
const individual =
|
|
21
|
-
appUserId && wantsIndividual(userConfig)
|
|
22
|
-
? await userRepository.findIndividualUserByAppUserId(appUserId)
|
|
23
|
-
: null;
|
|
24
|
-
const organization = wantsOrganization(userConfig, appOrgId)
|
|
25
|
-
? await userRepository.findOrganizationUserByAppOrgId(appOrgId)
|
|
26
|
-
: null;
|
|
27
|
-
return { individual, organization };
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Creates whichever of the two users is wanted and missing. Does not link
|
|
32
|
-
* them: linking is policy.
|
|
33
|
-
*/
|
|
34
|
-
async function createMissingAppUsers(
|
|
35
|
-
userRepository,
|
|
36
|
-
userConfig,
|
|
37
|
-
{ appUserId, appOrgId, individual, organization }
|
|
38
|
-
) {
|
|
39
|
-
let createdOrganization = false;
|
|
40
|
-
if (!individual && appUserId && wantsIndividual(userConfig)) {
|
|
41
|
-
individual = await userRepository.createIndividualUser({
|
|
42
|
-
appUserId,
|
|
43
|
-
username: `app-user-${appUserId}`,
|
|
44
|
-
email: `${appUserId}@app.local`,
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
if (!organization && wantsOrganization(userConfig, appOrgId)) {
|
|
48
|
-
organization = await userRepository.createOrganizationUser({
|
|
49
|
-
appOrgId,
|
|
50
|
-
});
|
|
51
|
-
createdOrganization = true;
|
|
52
|
-
}
|
|
53
|
-
return { individual, organization, createdOrganization };
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function buildUser(userConfig, individual, organization) {
|
|
57
|
-
return new User(
|
|
58
|
-
individual,
|
|
59
|
-
organization,
|
|
60
|
-
userConfig.usePassword,
|
|
61
|
-
userConfig.primary,
|
|
62
|
-
userConfig.individualUserRequired,
|
|
63
|
-
userConfig.organizationUserRequired
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
module.exports = {
|
|
68
|
-
wantsIndividual,
|
|
69
|
-
wantsOrganization,
|
|
70
|
-
findAppUsers,
|
|
71
|
-
createMissingAppUsers,
|
|
72
|
-
buildUser,
|
|
73
|
-
};
|
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
/* eslint-disable camelcase -- JWT claim names (org_id, org_role) and reason codes are snake_case by spec */
|
|
2
|
-
const jose = require('jose');
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Key material and token signing for adopter-JWT tests. Real keys and real
|
|
6
|
-
* signatures: nothing here fakes the verifier.
|
|
7
|
-
*/
|
|
8
|
-
async function createKeys() {
|
|
9
|
-
const es = await jose.generateKeyPair('ES256');
|
|
10
|
-
const rs = await jose.generateKeyPair('RS256');
|
|
11
|
-
const attacker = await jose.generateKeyPair('ES256');
|
|
12
|
-
const publicJwks = {
|
|
13
|
-
keys: [
|
|
14
|
-
{
|
|
15
|
-
...(await jose.exportJWK(es.publicKey)),
|
|
16
|
-
kid: 'es-1',
|
|
17
|
-
alg: 'ES256',
|
|
18
|
-
use: 'sig',
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
...(await jose.exportJWK(rs.publicKey)),
|
|
22
|
-
kid: 'rs-1',
|
|
23
|
-
alg: 'RS256',
|
|
24
|
-
use: 'sig',
|
|
25
|
-
},
|
|
26
|
-
],
|
|
27
|
-
};
|
|
28
|
-
return {
|
|
29
|
-
es,
|
|
30
|
-
rs,
|
|
31
|
-
attacker,
|
|
32
|
-
publicJwks,
|
|
33
|
-
localJwks: jose.createLocalJWKSet(publicJwks),
|
|
34
|
-
hmacSecret: 'a-shared-secret-that-is-long-enough-for-hs256',
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* @param {Object} claims
|
|
40
|
-
* @param {Object} opts
|
|
41
|
-
* @param {Object|Uint8Array} opts.key - signing key
|
|
42
|
-
* @param {string} [opts.alg='ES256']
|
|
43
|
-
* @param {string} [opts.kid]
|
|
44
|
-
* @param {string|null} [opts.exp='5m'] - null omits exp
|
|
45
|
-
*/
|
|
46
|
-
async function signToken(
|
|
47
|
-
claims,
|
|
48
|
-
{ key, alg = 'ES256', kid, exp = '5m', iss, aud, nbf } = {}
|
|
49
|
-
) {
|
|
50
|
-
const jwt = new jose.SignJWT(claims)
|
|
51
|
-
.setProtectedHeader(kid ? { alg, kid } : { alg })
|
|
52
|
-
.setIssuedAt();
|
|
53
|
-
if (exp !== null) jwt.setExpirationTime(exp);
|
|
54
|
-
if (iss) jwt.setIssuer(iss);
|
|
55
|
-
if (aud) jwt.setAudience(aud);
|
|
56
|
-
if (nbf) jwt.setNotBefore(nbf);
|
|
57
|
-
return jwt.sign(key);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const b64url = (value) =>
|
|
61
|
-
Buffer.from(JSON.stringify(value)).toString('base64url');
|
|
62
|
-
|
|
63
|
-
/** An unsigned token: `alg: none`, empty signature. */
|
|
64
|
-
function unsignedToken(claims) {
|
|
65
|
-
return `${b64url({ alg: 'none', typ: 'JWT' })}.${b64url({
|
|
66
|
-
...claims,
|
|
67
|
-
exp: Math.floor(Date.now() / 1000) + 300,
|
|
68
|
-
})}.`;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Stateful fake of the user repository, built from jest.fn so every call is
|
|
73
|
-
* observable. Individuals carry `organizationId`, as the real repositories
|
|
74
|
-
* return it.
|
|
75
|
-
*/
|
|
76
|
-
function createUserRepositoryDouble({
|
|
77
|
-
individuals = [],
|
|
78
|
-
organizations = [],
|
|
79
|
-
} = {}) {
|
|
80
|
-
const individualUsers = new Map(
|
|
81
|
-
individuals.map((u) => [u.appUserId, { ...u }])
|
|
82
|
-
);
|
|
83
|
-
const organizationUsers = new Map(
|
|
84
|
-
organizations.map((o) => [o.appOrgId, { ...o }])
|
|
85
|
-
);
|
|
86
|
-
let seq = 0;
|
|
87
|
-
|
|
88
|
-
const repo = {
|
|
89
|
-
findIndividualUserByAppUserId: jest.fn(
|
|
90
|
-
async (appUserId) => individualUsers.get(appUserId) ?? null
|
|
91
|
-
),
|
|
92
|
-
findOrganizationUserByAppOrgId: jest.fn(
|
|
93
|
-
async (appOrgId) => organizationUsers.get(appOrgId) ?? null
|
|
94
|
-
),
|
|
95
|
-
createIndividualUser: jest.fn(async (params) => {
|
|
96
|
-
const created = {
|
|
97
|
-
id: `ind-${++seq}`,
|
|
98
|
-
organizationId: undefined,
|
|
99
|
-
...params,
|
|
100
|
-
};
|
|
101
|
-
individualUsers.set(created.appUserId, created);
|
|
102
|
-
return created;
|
|
103
|
-
}),
|
|
104
|
-
createOrganizationUser: jest.fn(async (params) => {
|
|
105
|
-
const created = { id: `org-${++seq}`, ...params };
|
|
106
|
-
organizationUsers.set(created.appOrgId, created);
|
|
107
|
-
return created;
|
|
108
|
-
}),
|
|
109
|
-
linkIndividualToOrganization: jest.fn(
|
|
110
|
-
async (individualId, organizationId) => {
|
|
111
|
-
for (const u of individualUsers.values()) {
|
|
112
|
-
if (u.id === individualId) {
|
|
113
|
-
u.organizationId = organizationId;
|
|
114
|
-
return { ...u };
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
throw new Error('individual not found');
|
|
118
|
-
}
|
|
119
|
-
),
|
|
120
|
-
};
|
|
121
|
-
repo.state = { individualUsers, organizationUsers };
|
|
122
|
-
return repo;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
module.exports = {
|
|
126
|
-
createKeys,
|
|
127
|
-
signToken,
|
|
128
|
-
unsignedToken,
|
|
129
|
-
createUserRepositoryDouble,
|
|
130
|
-
};
|