@jskit-ai/auth-core 0.2.2 → 0.2.4
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/locales/en.json +0 -1
- package/package.json +9 -3
- package/src/server/actions/authActions.js +167 -0
- package/src/server/authAccess.js +24 -0
- package/src/server/authExtensions.js +19 -25
- package/src/server/authPolicy.js +57 -0
- package/src/server/authPolicyContextResolverRegistry.js +5 -4
- package/src/server/authServiceDecoratorRegistry.js +8 -6
- package/src/server/deferredAuthService.js +1 -0
- package/src/server/devAuth.js +24 -7
- package/src/server/lib/actionContextContributor.js +23 -20
- package/src/server/lib/authPolicySupport.js +12 -2
- package/src/server/lib/errors.js +4 -2
- package/src/server/lib/plugin.js +15 -22
- package/src/server/lib/routeMeta.js +1 -0
- package/src/server/lib/routeVisibilityResolver.js +18 -17
- package/src/server/noAppProfiles.js +16 -0
- package/src/server/providers/AuthAccessProvider.js +2 -20
- package/src/server/providers/AuthFeature.js +5 -2
- package/src/server/providers/AuthPolicyProvider.js +10 -38
- package/src/server/types.js +103 -0
- package/src/server/unsupportedOperation.js +4 -2
- package/src/server/utils.js +5 -3
- package/src/server/validators.js +2 -3
- package/src/shared/authApi.js +85 -49
- package/src/shared/authCapabilities.js +9 -3
- package/src/shared/authMethods.js +5 -0
- package/src/shared/authSecurityStatus.js +1 -0
- package/src/shared/commands/authDevLoginAsCommand.js +1 -1
- package/src/shared/commands/authLoginOAuthCompleteCommand.js +1 -1
- package/src/shared/commands/authLoginOtpVerifyCommand.js +1 -1
- package/src/shared/commands/authPasswordRecoveryCompleteCommand.js +1 -1
- package/src/shared/index.js +1 -1
- package/src/shared/signOutFlow.js +16 -10
- package/test/authActions.test.js +11 -7
- package/test/devAuth.test.js +2 -1
- package/test/providerRuntime.test.js +9 -6
- package/src/server/actions/auth.contributor.js +0 -234
package/locales/en.json
CHANGED
|
@@ -16,7 +16,6 @@
|
|
|
16
16
|
},
|
|
17
17
|
"dev": {
|
|
18
18
|
"unavailable": "Not found.",
|
|
19
|
-
"secretMissing": "AUTH_DEV_BYPASS_SECRET is required when AUTH_DEV_BYPASS_ENABLED=true.",
|
|
20
19
|
"localhostOnly": "Dev auth bootstrap is only available from localhost.",
|
|
21
20
|
"exchangeUnauthorized": "Dev auth exchange is not authorized.",
|
|
22
21
|
"userRequired": "Provide a user id or email.",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/auth-core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "node --test --test-concurrency=1"
|
|
@@ -10,8 +10,9 @@
|
|
|
10
10
|
"./server/providers/AuthExtensionsProvider": "./src/server/providers/AuthExtensionsProvider.js",
|
|
11
11
|
"./server/providers/AuthFeature": "./src/server/providers/AuthFeature.js",
|
|
12
12
|
"./server/providers/AuthPolicyProvider": "./src/server/providers/AuthPolicyProvider.js",
|
|
13
|
-
"./server/actions/
|
|
13
|
+
"./server/actions/authActions": "./src/server/actions/authActions.js",
|
|
14
14
|
"./server/authExtensions": "./src/server/authExtensions.js",
|
|
15
|
+
"./server/noAppProfiles": "./src/server/noAppProfiles.js",
|
|
15
16
|
"./server/authPolicyContextResolverRegistry": "./src/server/authPolicyContextResolverRegistry.js",
|
|
16
17
|
"./server/authServiceDecoratorRegistry": "./src/server/authServiceDecoratorRegistry.js",
|
|
17
18
|
"./server/authActor": "./src/server/authActor.js",
|
|
@@ -27,6 +28,7 @@
|
|
|
27
28
|
"./server/lib/index": "./src/server/lib/index.js",
|
|
28
29
|
"./server/lib/plugin": "./src/server/lib/plugin.js",
|
|
29
30
|
"./server/lib/routeMeta": "./src/server/lib/routeMeta.js",
|
|
31
|
+
"./server/types": "./src/server/types.js",
|
|
30
32
|
"./client": "./src/client/index.js",
|
|
31
33
|
"./client/authApi": "./src/client/authApi.js",
|
|
32
34
|
"./client/signOutFlow": "./src/client/signOutFlow.js",
|
|
@@ -125,6 +127,10 @@
|
|
|
125
127
|
{
|
|
126
128
|
"subpath": "./shared",
|
|
127
129
|
"summary": "Exports shared auth client helpers (createApi and runAuthSignOutFlow), with structured subpaths at ./shared/authApi and ./shared/signOutFlow."
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
"subpath": "./server/types",
|
|
133
|
+
"summary": "Types of the auth.access, auth.extensions, auth.policy and auth.service capabilities (AuthService is the contract every auth provider implements), for features that require them."
|
|
128
134
|
}
|
|
129
135
|
],
|
|
130
136
|
"clientCapabilities": [
|
|
@@ -137,6 +143,6 @@
|
|
|
137
143
|
}
|
|
138
144
|
},
|
|
139
145
|
"peerDependencies": {
|
|
140
|
-
"@jskit-ai/kernel": "0.2.
|
|
146
|
+
"@jskit-ai/kernel": "0.2.4"
|
|
141
147
|
}
|
|
142
148
|
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { createSchema } from "json-rest-schema";
|
|
2
|
+
import {
|
|
3
|
+
emptyInputValidator,
|
|
4
|
+
resolveRequest
|
|
5
|
+
} from "@jskit-ai/kernel/server/actions/actionContributorHelpers";
|
|
6
|
+
import {
|
|
7
|
+
composeSchemaDefinitions
|
|
8
|
+
} from "@jskit-ai/kernel/shared/validators";
|
|
9
|
+
import { deepFreeze } from "@jskit-ai/kernel/shared/support/deepFreeze";
|
|
10
|
+
import {
|
|
11
|
+
authRegisterCommand,
|
|
12
|
+
authRegisterConfirmationResendCommand,
|
|
13
|
+
authLoginPasswordCommand,
|
|
14
|
+
authLoginOtpRequestCommand,
|
|
15
|
+
authLoginOtpVerifyCommand,
|
|
16
|
+
authLoginOAuthStartCommand,
|
|
17
|
+
authLoginOAuthCompleteCommand,
|
|
18
|
+
authDevLoginAsCommand,
|
|
19
|
+
authPasswordResetRequestCommand,
|
|
20
|
+
authPasswordRecoveryCompleteCommand,
|
|
21
|
+
authPasswordResetCommand
|
|
22
|
+
} from "../../shared/commands/index.js";
|
|
23
|
+
|
|
24
|
+
const authLoginOAuthStartInput = composeSchemaDefinitions([
|
|
25
|
+
authLoginOAuthStartCommand.operation.params,
|
|
26
|
+
authLoginOAuthStartCommand.operation.query
|
|
27
|
+
], {
|
|
28
|
+
mode: "create",
|
|
29
|
+
context: "authActions.authLoginOAuthStartInput"
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const authLogoutOutput = deepFreeze({
|
|
33
|
+
schema: createSchema({
|
|
34
|
+
ok: { type: "boolean", required: true },
|
|
35
|
+
clearSession: { type: "boolean", required: true }
|
|
36
|
+
}),
|
|
37
|
+
mode: "replace"
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The shared auth actions, each one call to the installed provider's auth.service.
|
|
42
|
+
* @param {{ authService: import("../types.js").AuthService }} dependencies
|
|
43
|
+
*/
|
|
44
|
+
function createAuthActions({ authService }) {
|
|
45
|
+
return Object.freeze([
|
|
46
|
+
Object.freeze({
|
|
47
|
+
id: "auth.register",
|
|
48
|
+
version: 1,
|
|
49
|
+
kind: "command",
|
|
50
|
+
channels: ["api", "internal"],
|
|
51
|
+
surfaces: ["*"],
|
|
52
|
+
input: authRegisterCommand.operation.body,
|
|
53
|
+
execute: (input) => authService.register(input)
|
|
54
|
+
}),
|
|
55
|
+
Object.freeze({
|
|
56
|
+
id: "auth.register.confirmation.resend",
|
|
57
|
+
version: 1,
|
|
58
|
+
kind: "command",
|
|
59
|
+
channels: ["api", "internal"],
|
|
60
|
+
surfaces: ["*"],
|
|
61
|
+
input: authRegisterConfirmationResendCommand.operation.body,
|
|
62
|
+
execute: (input) => authService.resendRegisterConfirmation(input)
|
|
63
|
+
}),
|
|
64
|
+
Object.freeze({
|
|
65
|
+
id: "auth.login.password",
|
|
66
|
+
version: 1,
|
|
67
|
+
kind: "command",
|
|
68
|
+
channels: ["api", "internal"],
|
|
69
|
+
surfaces: ["*"],
|
|
70
|
+
input: authLoginPasswordCommand.operation.body,
|
|
71
|
+
execute: (input) => authService.login(input)
|
|
72
|
+
}),
|
|
73
|
+
Object.freeze({
|
|
74
|
+
id: "auth.login.otp.request",
|
|
75
|
+
version: 1,
|
|
76
|
+
kind: "command",
|
|
77
|
+
channels: ["api", "internal"],
|
|
78
|
+
surfaces: ["*"],
|
|
79
|
+
input: authLoginOtpRequestCommand.operation.body,
|
|
80
|
+
execute: (input) => authService.requestOtpLogin(input)
|
|
81
|
+
}),
|
|
82
|
+
Object.freeze({
|
|
83
|
+
id: "auth.login.otp.verify",
|
|
84
|
+
version: 1,
|
|
85
|
+
kind: "command",
|
|
86
|
+
channels: ["api", "internal"],
|
|
87
|
+
surfaces: ["*"],
|
|
88
|
+
input: authLoginOtpVerifyCommand.operation.body,
|
|
89
|
+
execute: (input) => authService.verifyOtpLogin(input)
|
|
90
|
+
}),
|
|
91
|
+
Object.freeze({
|
|
92
|
+
id: "auth.login.oauth.start",
|
|
93
|
+
version: 1,
|
|
94
|
+
kind: "command",
|
|
95
|
+
channels: ["api", "internal"],
|
|
96
|
+
surfaces: ["*"],
|
|
97
|
+
input: authLoginOAuthStartInput,
|
|
98
|
+
execute: (input, context) => authService.oauthStart(input, resolveRequest(context))
|
|
99
|
+
}),
|
|
100
|
+
Object.freeze({
|
|
101
|
+
id: "auth.login.oauth.complete",
|
|
102
|
+
version: 1,
|
|
103
|
+
kind: "command",
|
|
104
|
+
channels: ["api", "internal"],
|
|
105
|
+
surfaces: ["*"],
|
|
106
|
+
input: authLoginOAuthCompleteCommand.operation.body,
|
|
107
|
+
execute: (input, context) => authService.oauthComplete(input, resolveRequest(context))
|
|
108
|
+
}),
|
|
109
|
+
Object.freeze({
|
|
110
|
+
id: "auth.dev.loginAs",
|
|
111
|
+
version: 1,
|
|
112
|
+
kind: "command",
|
|
113
|
+
channels: ["api", "internal"],
|
|
114
|
+
surfaces: ["*"],
|
|
115
|
+
input: authDevLoginAsCommand.operation.body,
|
|
116
|
+
execute: (input, context) => authService.devLoginAs(input, resolveRequest(context))
|
|
117
|
+
}),
|
|
118
|
+
Object.freeze({
|
|
119
|
+
id: "auth.password.reset.request",
|
|
120
|
+
version: 1,
|
|
121
|
+
kind: "command",
|
|
122
|
+
channels: ["api", "internal"],
|
|
123
|
+
surfaces: ["*"],
|
|
124
|
+
input: authPasswordResetRequestCommand.operation.body,
|
|
125
|
+
execute: (input) => authService.requestPasswordReset(input)
|
|
126
|
+
}),
|
|
127
|
+
Object.freeze({
|
|
128
|
+
id: "auth.password.recovery.complete",
|
|
129
|
+
version: 1,
|
|
130
|
+
kind: "command",
|
|
131
|
+
channels: ["api", "internal"],
|
|
132
|
+
surfaces: ["*"],
|
|
133
|
+
input: authPasswordRecoveryCompleteCommand.operation.body,
|
|
134
|
+
execute: (input) => authService.completePasswordRecovery(input)
|
|
135
|
+
}),
|
|
136
|
+
Object.freeze({
|
|
137
|
+
id: "auth.password.reset",
|
|
138
|
+
version: 1,
|
|
139
|
+
kind: "command",
|
|
140
|
+
channels: ["api", "internal"],
|
|
141
|
+
surfaces: ["*"],
|
|
142
|
+
input: authPasswordResetCommand.operation.body,
|
|
143
|
+
execute: (input, context) => authService.resetPassword(input, resolveRequest(context))
|
|
144
|
+
}),
|
|
145
|
+
Object.freeze({
|
|
146
|
+
id: "auth.logout",
|
|
147
|
+
version: 1,
|
|
148
|
+
kind: "command",
|
|
149
|
+
channels: ["api", "automation", "internal"],
|
|
150
|
+
surfaces: ["*"],
|
|
151
|
+
input: emptyInputValidator,
|
|
152
|
+
output: authLogoutOutput,
|
|
153
|
+
execute: (input, context) => authService.logout(resolveRequest(context))
|
|
154
|
+
}),
|
|
155
|
+
Object.freeze({
|
|
156
|
+
id: "auth.session.read",
|
|
157
|
+
version: 1,
|
|
158
|
+
kind: "query",
|
|
159
|
+
channels: ["api", "internal"],
|
|
160
|
+
surfaces: ["*"],
|
|
161
|
+
input: emptyInputValidator,
|
|
162
|
+
execute: (input, context) => authService.authenticateRequest(resolveRequest(context))
|
|
163
|
+
})
|
|
164
|
+
]);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export { createAuthActions };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as authConstraints from "../shared/authConstraints.js";
|
|
2
|
+
import * as authMethods from "../shared/authMethods.js";
|
|
3
|
+
import * as oauthProviders from "../shared/oauthProviders.js";
|
|
4
|
+
import * as oauthCallbackParams from "../shared/oauthCallbackParams.js";
|
|
5
|
+
import * as membershipAccess from "./membershipAccess.js";
|
|
6
|
+
import * as inviteTokens from "./inviteTokens.js";
|
|
7
|
+
import * as utils from "./utils.js";
|
|
8
|
+
import * as validators from "./validators.js";
|
|
9
|
+
|
|
10
|
+
/** The auth helpers other packages reach through the `auth.access` capability. */
|
|
11
|
+
function createAuthAccess() {
|
|
12
|
+
return Object.freeze({
|
|
13
|
+
authConstraints,
|
|
14
|
+
authMethods,
|
|
15
|
+
oauthProviders,
|
|
16
|
+
oauthCallbackParams,
|
|
17
|
+
membershipAccess,
|
|
18
|
+
inviteTokens,
|
|
19
|
+
utils,
|
|
20
|
+
validators
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { createAuthAccess };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { normalizePermissionList } from "@jskit-ai/kernel/shared/support/permissions";
|
|
2
2
|
|
|
3
3
|
function normalizeOrderedExtension(value, { idField, method, label } = {}) {
|
|
4
|
+
// readable-check: allow method-probe - input validation: an application registers the extension
|
|
4
5
|
if (!value || typeof value !== "object" || Array.isArray(value) || typeof value[method] !== "function") {
|
|
5
6
|
throw new TypeError(`${label} requires ${method}().`);
|
|
6
7
|
}
|
|
@@ -40,10 +41,21 @@ function mergePolicyContexts(contexts = []) {
|
|
|
40
41
|
return Object.keys(merged).length > 0 ? Object.freeze(merged) : null;
|
|
41
42
|
}
|
|
42
43
|
|
|
44
|
+
/**
|
|
45
|
+
* The invitation context resolver when no feature registers one: an invitation
|
|
46
|
+
* keeps the token and source it arrived with.
|
|
47
|
+
* @type {import("./types.js").AuthInvitationContextResolver}
|
|
48
|
+
*/
|
|
49
|
+
const invitationAsGiven = Object.freeze({
|
|
50
|
+
resolverId: "auth.invitation.as-given",
|
|
51
|
+
async resolveInvitationContext({ token, source }) {
|
|
52
|
+
return { token, source };
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
43
56
|
function createAuthExtensions() {
|
|
44
57
|
const serviceDecorators = [];
|
|
45
58
|
const policyContextResolvers = [];
|
|
46
|
-
const profileProjectors = [];
|
|
47
59
|
const invitationContextResolvers = [];
|
|
48
60
|
const ids = new Set();
|
|
49
61
|
let sealed = false;
|
|
@@ -74,14 +86,7 @@ function createAuthExtensions() {
|
|
|
74
86
|
});
|
|
75
87
|
}
|
|
76
88
|
|
|
77
|
-
|
|
78
|
-
return register(profileProjectors, extension, {
|
|
79
|
-
idField: "projectorId",
|
|
80
|
-
method: "syncIdentityProfile",
|
|
81
|
-
label: "Auth profile projector"
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
|
|
89
|
+
// readable-check: allow setter-seam - cycle: workspaces.integrations requires auth.service through workspaces.core and users.core
|
|
85
90
|
function registerInvitationContextResolver(extension) {
|
|
86
91
|
return register(invitationContextResolvers, extension, {
|
|
87
92
|
idField: "resolverId",
|
|
@@ -90,21 +95,13 @@ function createAuthExtensions() {
|
|
|
90
95
|
});
|
|
91
96
|
}
|
|
92
97
|
|
|
93
|
-
function
|
|
98
|
+
function resolveInvitationContextResolver() {
|
|
94
99
|
sealed = true;
|
|
95
|
-
const entries = ordered(
|
|
100
|
+
const entries = ordered(invitationContextResolvers);
|
|
96
101
|
if (entries.length > 1) {
|
|
97
|
-
throw new Error(
|
|
102
|
+
throw new Error(`Auth invitation context resolver is ambiguous: ${entries.map((entry) => entry.resolverId).join(", ")}.`);
|
|
98
103
|
}
|
|
99
|
-
return entries[0] ||
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function resolveProfileProjector() {
|
|
103
|
-
return resolveSingle(profileProjectors, "Auth profile projector");
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function resolveInvitationContextResolver() {
|
|
107
|
-
return resolveSingle(invitationContextResolvers, "Auth invitation context resolver");
|
|
104
|
+
return entries[0] || invitationAsGiven;
|
|
108
105
|
}
|
|
109
106
|
|
|
110
107
|
function decorateService(authService) {
|
|
@@ -133,7 +130,6 @@ function createAuthExtensions() {
|
|
|
133
130
|
sealed,
|
|
134
131
|
serviceDecoratorIds: Object.freeze(ordered(serviceDecorators).map((entry) => entry.decoratorId)),
|
|
135
132
|
policyContextResolverIds: Object.freeze(ordered(policyContextResolvers).map((entry) => entry.resolverId)),
|
|
136
|
-
profileProjectorIds: Object.freeze(ordered(profileProjectors).map((entry) => entry.projectorId)),
|
|
137
133
|
invitationContextResolverIds: Object.freeze(ordered(invitationContextResolvers).map((entry) => entry.resolverId))
|
|
138
134
|
});
|
|
139
135
|
}
|
|
@@ -141,15 +137,13 @@ function createAuthExtensions() {
|
|
|
141
137
|
const api = Object.freeze({
|
|
142
138
|
registerServiceDecorator,
|
|
143
139
|
registerPolicyContextResolver,
|
|
144
|
-
registerProfileProjector,
|
|
145
140
|
registerInvitationContextResolver,
|
|
146
141
|
decorateService,
|
|
147
142
|
resolvePolicyContext,
|
|
148
|
-
resolveProfileProjector,
|
|
149
143
|
resolveInvitationContextResolver,
|
|
150
144
|
diagnostics
|
|
151
145
|
});
|
|
152
146
|
return api;
|
|
153
147
|
}
|
|
154
148
|
|
|
155
|
-
export { createAuthExtensions, mergePolicyContexts };
|
|
149
|
+
export { createAuthExtensions, invitationAsGiven, mergePolicyContexts };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { parseBooleanFlag } from "./booleanFlag.js";
|
|
2
|
+
|
|
3
|
+
function parseList(value) {
|
|
4
|
+
return String(value || "").split(",").map((entry) => entry.trim()).filter(Boolean);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function defaultHasPermission({ permission, permissions }) {
|
|
8
|
+
return !permission || (Array.isArray(permissions) && permissions.includes(permission));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** The authentication an application without an `auth.service` has: every request is signed out. */
|
|
12
|
+
const everyoneSignedOut = Object.freeze({
|
|
13
|
+
async authenticateRequest() {
|
|
14
|
+
return { authenticated: false, actor: null, transientFailure: false };
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* How the auth policy plugin resolves a request's actor, permissions and policy context.
|
|
20
|
+
* @param {object} dependencies
|
|
21
|
+
* @param {Pick<import("./types.js").AuthService, "authenticateRequest">} dependencies.authService The installed
|
|
22
|
+
* provider's `auth.service`, or everyoneSignedOut.
|
|
23
|
+
* @param {import("./types.js").AuthExtensions} dependencies.extensions
|
|
24
|
+
* @param {import("@jskit-ai/kernel/server/types").RuntimeEnv} dependencies.env
|
|
25
|
+
*/
|
|
26
|
+
function createAuthPolicy({ authService, extensions, env }) {
|
|
27
|
+
const unsafeMethods = parseList(env.AUTH_CSRF_UNSAFE_METHODS);
|
|
28
|
+
|
|
29
|
+
function resolveActor(request) {
|
|
30
|
+
return authService.authenticateRequest(request);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function resolveContext(input) {
|
|
34
|
+
return extensions.resolvePolicyContext(input);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return Object.freeze({
|
|
38
|
+
resolveActor,
|
|
39
|
+
hasPermission: defaultHasPermission,
|
|
40
|
+
resolveContext,
|
|
41
|
+
options: Object.freeze({
|
|
42
|
+
// The plugin resolves both defaults: NODE_ENV not production, and the /api/ prefix.
|
|
43
|
+
nodeEnv: env.NODE_ENV,
|
|
44
|
+
apiPrefix: env.AUTH_API_PREFIX,
|
|
45
|
+
// Unset AUTH_CSRF_UNSAFE_METHODS keeps the plugin default (POST, PUT, PATCH, DELETE).
|
|
46
|
+
unsafeMethods: unsafeMethods.length > 0 ? Object.freeze(unsafeMethods) : undefined,
|
|
47
|
+
// Unset AUTH_CSRF_COOKIE_SECURE follows NODE_ENV (Secure in production).
|
|
48
|
+
csrfCookieOpts: Object.freeze(
|
|
49
|
+
String(env.AUTH_CSRF_COOKIE_SECURE ?? "").trim()
|
|
50
|
+
? { secure: parseBooleanFlag(env.AUTH_CSRF_COOKIE_SECURE, false) }
|
|
51
|
+
: {}
|
|
52
|
+
)
|
|
53
|
+
})
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export { createAuthPolicy, everyoneSignedOut };
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { mergePolicyContexts } from "./authExtensions.js";
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* @param {import("./types.js").AuthExtensions} extensions
|
|
5
|
+
* @param {object} resolver
|
|
6
|
+
*/
|
|
3
7
|
function registerAuthPolicyContextResolver(extensions, resolver) {
|
|
4
|
-
if (!extensions || typeof extensions.registerPolicyContextResolver !== "function") {
|
|
5
|
-
throw new TypeError("registerAuthPolicyContextResolver requires auth.extensions.");
|
|
6
|
-
}
|
|
7
8
|
return extensions.registerPolicyContextResolver(resolver);
|
|
8
9
|
}
|
|
9
10
|
|
|
11
|
+
/** @param {import("./types.js").AuthExtensions} extensions */
|
|
10
12
|
function resolveComposedAuthPolicyContextResolver(extensions) {
|
|
11
|
-
if (!extensions || typeof extensions.resolvePolicyContext !== "function") return null;
|
|
12
13
|
return (input = {}) => extensions.resolvePolicyContext(input);
|
|
13
14
|
}
|
|
14
15
|
|
|
@@ -1,14 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {import("./types.js").AuthExtensions} extensions
|
|
3
|
+
* @param {object} decorator
|
|
4
|
+
*/
|
|
1
5
|
function registerAuthServiceDecorator(extensions, decorator) {
|
|
2
|
-
if (!extensions || typeof extensions.registerServiceDecorator !== "function") {
|
|
3
|
-
throw new TypeError("registerAuthServiceDecorator requires auth.extensions.");
|
|
4
|
-
}
|
|
5
6
|
return extensions.registerServiceDecorator(decorator);
|
|
6
7
|
}
|
|
7
8
|
|
|
9
|
+
/**
|
|
10
|
+
* @param {import("./types.js").AuthExtensions} extensions
|
|
11
|
+
* @param {import("./types.js").AuthService} authService
|
|
12
|
+
*/
|
|
8
13
|
function applyAuthServiceDecorators(extensions, authService) {
|
|
9
|
-
if (!extensions || typeof extensions.decorateService !== "function") {
|
|
10
|
-
throw new TypeError("applyAuthServiceDecorators requires auth.extensions.");
|
|
11
|
-
}
|
|
12
14
|
return extensions.decorateService(authService);
|
|
13
15
|
}
|
|
14
16
|
|
package/src/server/devAuth.js
CHANGED
|
@@ -53,6 +53,15 @@ function isLocalDevAuthRequest(request) {
|
|
|
53
53
|
return isLoopbackIp(remoteAddress) && isLoopbackHostname(normalizeRequestHostname(request));
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
/**
|
|
57
|
+
* True when NODE_ENV names production, in any letter case. Every auth package
|
|
58
|
+
* decides "production" this one way.
|
|
59
|
+
* @param {unknown} nodeEnv
|
|
60
|
+
*/
|
|
61
|
+
function isProductionNodeEnv(nodeEnv) {
|
|
62
|
+
return String(nodeEnv || "").trim().toLowerCase() === "production";
|
|
63
|
+
}
|
|
64
|
+
|
|
56
65
|
function resolveDevAuthPolicy({
|
|
57
66
|
enabled = false,
|
|
58
67
|
nodeEnv = "development",
|
|
@@ -60,19 +69,27 @@ function resolveDevAuthPolicy({
|
|
|
60
69
|
} = {}) {
|
|
61
70
|
return Object.freeze({
|
|
62
71
|
enabled: parseBooleanFlag(enabled, false),
|
|
63
|
-
isProduction:
|
|
72
|
+
isProduction: isProductionNodeEnv(nodeEnv),
|
|
64
73
|
secret: String(secret || "").trim()
|
|
65
74
|
});
|
|
66
75
|
}
|
|
67
76
|
|
|
68
77
|
function resolveDevAuthPolicyFromEnv(env = {}) {
|
|
69
78
|
return resolveDevAuthPolicy({
|
|
70
|
-
enabled: env
|
|
71
|
-
nodeEnv: env
|
|
72
|
-
secret: env
|
|
79
|
+
enabled: env.AUTH_DEV_BYPASS_ENABLED,
|
|
80
|
+
nodeEnv: env.NODE_ENV,
|
|
81
|
+
secret: env.AUTH_DEV_BYPASS_SECRET
|
|
73
82
|
});
|
|
74
83
|
}
|
|
75
84
|
|
|
85
|
+
/**
|
|
86
|
+
* True when dev sign-in as another user is on: enabled, outside production.
|
|
87
|
+
* @param {ReturnType<typeof resolveDevAuthPolicy>} policy
|
|
88
|
+
*/
|
|
89
|
+
function isDevAuthBootstrapEnabled(policy) {
|
|
90
|
+
return policy.enabled && !policy.isProduction;
|
|
91
|
+
}
|
|
92
|
+
|
|
76
93
|
function assertDevAuthPolicy(policy = {}) {
|
|
77
94
|
if (!policy.enabled) {
|
|
78
95
|
return;
|
|
@@ -90,9 +107,7 @@ function ensureDevAuthRuntimeAvailable(policy = {}, request = null) {
|
|
|
90
107
|
throw new AppError(404, "Not found.", { code: "auth.dev.unavailable" });
|
|
91
108
|
}
|
|
92
109
|
if (!policy.secret) {
|
|
93
|
-
throw new
|
|
94
|
-
code: "auth.dev.secretMissing"
|
|
95
|
-
});
|
|
110
|
+
throw new Error("AUTH_DEV_BYPASS_SECRET is required when AUTH_DEV_BYPASS_ENABLED=true.");
|
|
96
111
|
}
|
|
97
112
|
if (!isLocalDevAuthRequest(request)) {
|
|
98
113
|
throw new AppError(403, "Dev auth bootstrap is only available from localhost.", { code: "auth.dev.localhostOnly" });
|
|
@@ -123,7 +138,9 @@ export {
|
|
|
123
138
|
DEV_AUTH_SECRET_HEADER,
|
|
124
139
|
ensureDevAuthExchangeAvailable,
|
|
125
140
|
ensureDevAuthRuntimeAvailable,
|
|
141
|
+
isDevAuthBootstrapEnabled,
|
|
126
142
|
isLocalDevAuthRequest,
|
|
143
|
+
isProductionNodeEnv,
|
|
127
144
|
resolveDevAuthPolicy,
|
|
128
145
|
resolveDevAuthPolicyFromEnv
|
|
129
146
|
};
|
|
@@ -1,31 +1,34 @@
|
|
|
1
|
+
import { resolveRequest } from "@jskit-ai/kernel/server/actions/actionContributorHelpers";
|
|
1
2
|
import { normalizePermissionList } from "@jskit-ai/kernel/shared/support/permissions";
|
|
2
3
|
|
|
3
4
|
function createAuthActionContextContributor() {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const contribution = {};
|
|
9
|
-
const permissions = normalizePermissionList(request?.permissions);
|
|
10
|
-
|
|
11
|
-
if (request?.user) {
|
|
12
|
-
contribution.actor = request.user;
|
|
13
|
-
}
|
|
5
|
+
function contribute({ context }) {
|
|
6
|
+
const request = resolveRequest(context);
|
|
7
|
+
const contribution = {};
|
|
8
|
+
const permissions = normalizePermissionList(request?.permissions);
|
|
14
9
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
10
|
+
if (request?.user) {
|
|
11
|
+
contribution.actor = request.user;
|
|
12
|
+
}
|
|
18
13
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
14
|
+
if (request?.workspace) {
|
|
15
|
+
contribution.workspace = request.workspace;
|
|
16
|
+
}
|
|
22
17
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
18
|
+
if (request?.membership) {
|
|
19
|
+
contribution.membership = request.membership;
|
|
20
|
+
}
|
|
26
21
|
|
|
27
|
-
|
|
22
|
+
if (permissions.length > 0) {
|
|
23
|
+
contribution.permissions = permissions;
|
|
28
24
|
}
|
|
25
|
+
|
|
26
|
+
return contribution;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return Object.freeze({
|
|
30
|
+
contributorId: "auth.policy.request-context",
|
|
31
|
+
contribute
|
|
29
32
|
});
|
|
30
33
|
}
|
|
31
34
|
|
|
@@ -7,6 +7,14 @@ function assertFunction(value, name) {
|
|
|
7
7
|
return value;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
/** The onPolicyDenied default when an application passes none: a denial is not reported. */
|
|
11
|
+
function ignorePolicyDenial() {}
|
|
12
|
+
|
|
13
|
+
/** The resolveContext default when an application passes none: a request has no policy context. */
|
|
14
|
+
function resolveNoPolicyContext() {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
|
|
10
18
|
function assertAuthPolicyDeps(deps = {}) {
|
|
11
19
|
const source = asObject(deps);
|
|
12
20
|
const resolveActor = assertFunction(source.resolveActor, "resolveActor");
|
|
@@ -14,9 +22,11 @@ function assertAuthPolicyDeps(deps = {}) {
|
|
|
14
22
|
|
|
15
23
|
return {
|
|
16
24
|
resolveActor,
|
|
17
|
-
|
|
25
|
+
// readable-check: allow method-probe - input validation: the callbacks an application passes authPolicyPlugin
|
|
26
|
+
resolveContext: typeof source.resolveContext === "function" ? source.resolveContext : resolveNoPolicyContext,
|
|
18
27
|
hasPermission,
|
|
19
|
-
|
|
28
|
+
// readable-check: allow method-probe - input validation: the callbacks an application passes authPolicyPlugin
|
|
29
|
+
onPolicyDenied: typeof source.onPolicyDenied === "function" ? source.onPolicyDenied : ignorePolicyDenial
|
|
20
30
|
};
|
|
21
31
|
}
|
|
22
32
|
|
package/src/server/lib/errors.js
CHANGED
|
@@ -7,8 +7,10 @@ const AUTH_POLICY_DENY_REASONS = Object.freeze({
|
|
|
7
7
|
FORBIDDEN_PERMISSION: "forbidden_permission"
|
|
8
8
|
});
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
/**
|
|
11
|
+
* code names the refusal (auth.session.required, auth.policy.permissionDenied...)
|
|
12
|
+
* so a client can show it in the user's language; message stays the English.
|
|
13
|
+
*/
|
|
12
14
|
function createAuthPolicyError(status, message, { code, params = {} } = {}) {
|
|
13
15
|
const normalizedStatus = Number(status) || 500;
|
|
14
16
|
const error = new Error(String(message || "Request failed."));
|