@jskit-ai/auth-core 0.2.3 → 0.2.5
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/src/server/lib/plugin.js
CHANGED
|
@@ -2,6 +2,7 @@ import fastifyCookie from "@fastify/cookie";
|
|
|
2
2
|
import fastifyCsrfProtection from "@fastify/csrf-protection";
|
|
3
3
|
import fastifyRateLimit from "@fastify/rate-limit";
|
|
4
4
|
import { safeRequestUrl } from "@jskit-ai/kernel/server/runtime/requestUrl";
|
|
5
|
+
import { isProductionNodeEnv } from "../devAuth.js";
|
|
5
6
|
import { hostOnlyCookieName } from "../utils.js";
|
|
6
7
|
|
|
7
8
|
import { assertAuthPolicyDeps, normalizeActorResolution } from "./authPolicySupport.js";
|
|
@@ -35,7 +36,7 @@ function normalizeRateLimitPluginOptions(value) {
|
|
|
35
36
|
}
|
|
36
37
|
|
|
37
38
|
function resolveRouteConfig(request) {
|
|
38
|
-
if (!request
|
|
39
|
+
if (!request.routeOptions || typeof request.routeOptions !== "object") {
|
|
39
40
|
return {};
|
|
40
41
|
}
|
|
41
42
|
|
|
@@ -64,6 +65,7 @@ function resolveCsrfToken(request) {
|
|
|
64
65
|
}
|
|
65
66
|
|
|
66
67
|
function resolveOwnerValue(meta, request) {
|
|
68
|
+
// readable-check: allow method-probe - data: a route's auth policy config may name an owner resolver
|
|
67
69
|
if (typeof meta.ownerResolver === "function") {
|
|
68
70
|
return meta.ownerResolver({
|
|
69
71
|
req: request,
|
|
@@ -81,29 +83,20 @@ function resolveOwnerValue(meta, request) {
|
|
|
81
83
|
return null;
|
|
82
84
|
}
|
|
83
85
|
|
|
84
|
-
function notifyPolicyDenied(onPolicyDenied, payload) {
|
|
85
|
-
if (typeof onPolicyDenied !== "function") {
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
onPolicyDenied(payload);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
86
|
function authPolicyPlugin(deps = {}, options = {}) {
|
|
92
87
|
const { resolveActor, resolveContext, hasPermission, onPolicyDenied } = assertAuthPolicyDeps(deps);
|
|
93
88
|
const apiPrefix = normalizeApiPrefix(options.apiPrefix);
|
|
94
89
|
const unsafeMethods = normalizeUnsafeMethods(options.unsafeMethods);
|
|
95
90
|
const rateLimitPluginOptions = normalizeRateLimitPluginOptions(options.rateLimitPluginOptions);
|
|
96
|
-
const nodeEnv = String(options.nodeEnv || "").trim();
|
|
97
91
|
const csrfCookieOpts =
|
|
98
92
|
options.csrfCookieOpts && typeof options.csrfCookieOpts === "object" ? options.csrfCookieOpts : {};
|
|
99
|
-
const resolveCsrfTokenFn = typeof options.resolveCsrfToken === "function" ? options.resolveCsrfToken : resolveCsrfToken;
|
|
100
93
|
// createError(status, englishMessage, { code }) builds each refusal.
|
|
101
|
-
const
|
|
94
|
+
const { resolveCsrfToken: resolveCsrfTokenFn = resolveCsrfToken, createError = createAuthPolicyError } = options;
|
|
102
95
|
|
|
103
96
|
// CSRF uses tokens checked against a secret cookie, never SameSite alone. In
|
|
104
97
|
// production that cookie is host-only (__Host-), so a sibling host cannot plant
|
|
105
98
|
// a secret it knows and forge tokens.
|
|
106
|
-
const csrfCookieSecure = typeof csrfCookieOpts.secure === "boolean" ? csrfCookieOpts.secure : nodeEnv
|
|
99
|
+
const csrfCookieSecure = typeof csrfCookieOpts.secure === "boolean" ? csrfCookieOpts.secure : isProductionNodeEnv(options.nodeEnv);
|
|
107
100
|
const csrfCookieOptions = {
|
|
108
101
|
path: "/",
|
|
109
102
|
sameSite: "lax",
|
|
@@ -128,13 +121,13 @@ function authPolicyPlugin(deps = {}, options = {}) {
|
|
|
128
121
|
fastify.decorateRequest("permissions", null);
|
|
129
122
|
|
|
130
123
|
fastify.addHook("preHandler", async (request, reply) => {
|
|
131
|
-
const pathname = request
|
|
124
|
+
const pathname = request.raw.url || request.url || "/";
|
|
132
125
|
if (!String(pathname).startsWith(apiPrefix)) {
|
|
133
126
|
return;
|
|
134
127
|
}
|
|
135
128
|
|
|
136
129
|
const meta = resolveAuthPolicyMeta(resolveRouteConfig(request));
|
|
137
|
-
if (meta.csrfProtection && unsafeMethods.has(String(request
|
|
130
|
+
if (meta.csrfProtection && unsafeMethods.has(String(request.method || "").toUpperCase())) {
|
|
138
131
|
await enforceCsrfProtection(fastify, request, reply);
|
|
139
132
|
}
|
|
140
133
|
|
|
@@ -144,7 +137,7 @@ function authPolicyPlugin(deps = {}, options = {}) {
|
|
|
144
137
|
|
|
145
138
|
const actorResolution = normalizeActorResolution(await resolveActor(request, reply, meta));
|
|
146
139
|
if (actorResolution.transientFailure) {
|
|
147
|
-
|
|
140
|
+
onPolicyDenied({
|
|
148
141
|
reason: AUTH_POLICY_DENY_REASONS.AUTH_UPSTREAM_UNAVAILABLE,
|
|
149
142
|
statusCode: 503,
|
|
150
143
|
request,
|
|
@@ -158,7 +151,7 @@ function authPolicyPlugin(deps = {}, options = {}) {
|
|
|
158
151
|
}
|
|
159
152
|
|
|
160
153
|
if (!actorResolution.authenticated) {
|
|
161
|
-
|
|
154
|
+
onPolicyDenied({
|
|
162
155
|
reason: AUTH_POLICY_DENY_REASONS.UNAUTHENTICATED,
|
|
163
156
|
statusCode: 401,
|
|
164
157
|
request,
|
|
@@ -176,10 +169,10 @@ function authPolicyPlugin(deps = {}, options = {}) {
|
|
|
176
169
|
|
|
177
170
|
if (meta.authPolicy === AUTH_POLICIES.OWN) {
|
|
178
171
|
const ownerValue = await resolveOwnerValue(meta, request);
|
|
179
|
-
const userValue = request
|
|
172
|
+
const userValue = request.user ? request.user[meta.userField] : null;
|
|
180
173
|
|
|
181
174
|
if (ownerValue == null || userValue == null) {
|
|
182
|
-
|
|
175
|
+
onPolicyDenied({
|
|
183
176
|
reason: AUTH_POLICY_DENY_REASONS.OWNER_UNRESOLVED,
|
|
184
177
|
statusCode: 400,
|
|
185
178
|
request,
|
|
@@ -191,7 +184,7 @@ function authPolicyPlugin(deps = {}, options = {}) {
|
|
|
191
184
|
}
|
|
192
185
|
|
|
193
186
|
if (String(ownerValue) !== String(userValue)) {
|
|
194
|
-
|
|
187
|
+
onPolicyDenied({
|
|
195
188
|
reason: AUTH_POLICY_DENY_REASONS.FORBIDDEN_OWNER_MISMATCH,
|
|
196
189
|
statusCode: 403,
|
|
197
190
|
request,
|
|
@@ -202,7 +195,7 @@ function authPolicyPlugin(deps = {}, options = {}) {
|
|
|
202
195
|
throw createError(403, "Forbidden.", { code: "auth.policy.ownerMismatch" });
|
|
203
196
|
}
|
|
204
197
|
} else if (meta.authPolicy !== AUTH_POLICIES.REQUIRED) {
|
|
205
|
-
|
|
198
|
+
onPolicyDenied({
|
|
206
199
|
reason: AUTH_POLICY_DENY_REASONS.INVALID_AUTH_POLICY,
|
|
207
200
|
statusCode: 500,
|
|
208
201
|
request,
|
|
@@ -214,7 +207,7 @@ function authPolicyPlugin(deps = {}, options = {}) {
|
|
|
214
207
|
}
|
|
215
208
|
|
|
216
209
|
let context = null;
|
|
217
|
-
if (
|
|
210
|
+
if (meta.contextPolicy !== CONTEXT_POLICIES.NONE || meta.permission) {
|
|
218
211
|
context = await resolveContext({
|
|
219
212
|
request,
|
|
220
213
|
actor: actorResolution.actor,
|
|
@@ -238,7 +231,7 @@ function authPolicyPlugin(deps = {}, options = {}) {
|
|
|
238
231
|
})
|
|
239
232
|
);
|
|
240
233
|
if (!allowed) {
|
|
241
|
-
|
|
234
|
+
onPolicyDenied({
|
|
242
235
|
reason: AUTH_POLICY_DENY_REASONS.FORBIDDEN_PERMISSION,
|
|
243
236
|
statusCode: 403,
|
|
244
237
|
request,
|
|
@@ -32,6 +32,7 @@ function resolveAuthPolicyMeta(input = {}) {
|
|
|
32
32
|
permission: String(source.permission || "").trim(),
|
|
33
33
|
ownerParam: typeof source.ownerParam === "string" && source.ownerParam ? source.ownerParam : null,
|
|
34
34
|
userField: source.userField || "id",
|
|
35
|
+
// readable-check: allow method-probe - data: a route's auth policy config may name an owner resolver
|
|
35
36
|
ownerResolver: typeof source.ownerResolver === "function" ? source.ownerResolver : null,
|
|
36
37
|
csrfProtection: source.csrfProtection !== false
|
|
37
38
|
};
|
|
@@ -1,24 +1,25 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { resolveActorId } from "@jskit-ai/kernel/server/actions/actionContributorHelpers";
|
|
2
2
|
|
|
3
3
|
function createAuthRouteVisibilityResolver() {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
return {};
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const actor = context?.actor || request?.user || null;
|
|
12
|
-
const userId = normalizeOpaqueId(actor?.id);
|
|
13
|
-
if (userId == null) {
|
|
14
|
-
return {};
|
|
15
|
-
}
|
|
4
|
+
function resolve({ visibility, context, request }) {
|
|
5
|
+
if (visibility !== "user") {
|
|
6
|
+
return {};
|
|
7
|
+
}
|
|
16
8
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
};
|
|
9
|
+
const userId = resolveActorId(context, request);
|
|
10
|
+
if (userId == null) {
|
|
11
|
+
return {};
|
|
21
12
|
}
|
|
13
|
+
|
|
14
|
+
return {
|
|
15
|
+
userId,
|
|
16
|
+
requiresActorScope: true
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return Object.freeze({
|
|
21
|
+
resolverId: "auth.policy.visibility",
|
|
22
|
+
resolve
|
|
22
23
|
});
|
|
23
24
|
}
|
|
24
25
|
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `auth.profile.projector` default when no feature provides one: the
|
|
3
|
+
* application keeps no profiles of its own, so an auth provider's actors carry
|
|
4
|
+
* only the provider's user.
|
|
5
|
+
* @type {import("./types.js").AuthProfileProjector}
|
|
6
|
+
*/
|
|
7
|
+
const noAppProfiles = Object.freeze({
|
|
8
|
+
async syncIdentityProfile() {
|
|
9
|
+
return null;
|
|
10
|
+
},
|
|
11
|
+
async findByIdentity() {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
export { noAppProfiles };
|
|
@@ -1,23 +1,5 @@
|
|
|
1
1
|
import { defineProvider } from "@jskit-ai/kernel/shared/capabilities";
|
|
2
|
-
import
|
|
3
|
-
import * as authMethods from "../../shared/authMethods.js";
|
|
4
|
-
import * as oauthProviders from "../../shared/oauthProviders.js";
|
|
5
|
-
import * as oauthCallbackParams from "../../shared/oauthCallbackParams.js";
|
|
6
|
-
import * as membershipAccess from "../membershipAccess.js";
|
|
7
|
-
import * as inviteTokens from "../inviteTokens.js";
|
|
8
|
-
import * as utils from "../utils.js";
|
|
9
|
-
import * as validators from "../validators.js";
|
|
10
|
-
|
|
11
|
-
const access = Object.freeze({
|
|
12
|
-
authConstraints,
|
|
13
|
-
authMethods,
|
|
14
|
-
oauthProviders,
|
|
15
|
-
oauthCallbackParams,
|
|
16
|
-
membershipAccess,
|
|
17
|
-
inviteTokens,
|
|
18
|
-
utils,
|
|
19
|
-
validators
|
|
20
|
-
});
|
|
2
|
+
import { createAuthAccess } from "../authAccess.js";
|
|
21
3
|
|
|
22
4
|
const AuthAccessProvider = defineProvider({
|
|
23
5
|
id: "auth.access",
|
|
@@ -25,7 +7,7 @@ const AuthAccessProvider = defineProvider({
|
|
|
25
7
|
access: "auth.access"
|
|
26
8
|
},
|
|
27
9
|
setup() {
|
|
28
|
-
return { access };
|
|
10
|
+
return { access: createAuthAccess() };
|
|
29
11
|
}
|
|
30
12
|
});
|
|
31
13
|
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { defineFeature } from "@jskit-ai/kernel/server/features";
|
|
2
|
-
import {
|
|
2
|
+
import { createAuthActions } from "../actions/authActions.js";
|
|
3
|
+
|
|
4
|
+
// Without an auth provider installed there is no auth.service, so no auth actions.
|
|
5
|
+
const NO_AUTH_ACTIONS = Object.freeze([]);
|
|
3
6
|
|
|
4
7
|
const AuthFeature = defineFeature({
|
|
5
8
|
id: "auth.actions",
|
|
@@ -8,7 +11,7 @@ const AuthFeature = defineFeature({
|
|
|
8
11
|
authService: "auth.service"
|
|
9
12
|
},
|
|
10
13
|
actions({ authService }) {
|
|
11
|
-
return authService ?
|
|
14
|
+
return authService ? createAuthActions({ authService }) : NO_AUTH_ACTIONS;
|
|
12
15
|
}
|
|
13
16
|
});
|
|
14
17
|
|
|
@@ -1,45 +1,9 @@
|
|
|
1
1
|
import { defineProvider } from "@jskit-ai/kernel/shared/capabilities";
|
|
2
|
-
import {
|
|
2
|
+
import { createAuthPolicy, everyoneSignedOut } from "../authPolicy.js";
|
|
3
3
|
import { authPolicyPlugin } from "../lib/plugin.js";
|
|
4
4
|
import { createAuthActionContextContributor } from "../lib/actionContextContributor.js";
|
|
5
5
|
import { createAuthRouteVisibilityResolver } from "../lib/routeVisibilityResolver.js";
|
|
6
6
|
|
|
7
|
-
function parseList(value) {
|
|
8
|
-
return String(value || "").split(",").map((entry) => entry.trim()).filter(Boolean);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
function defaultHasPermission({ permission, permissions = [] } = {}) {
|
|
12
|
-
return !permission || (Array.isArray(permissions) && permissions.includes(permission));
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function createPolicy({ authService, extensions, env }) {
|
|
16
|
-
const unsafeMethods = parseList(env.AUTH_CSRF_UNSAFE_METHODS);
|
|
17
|
-
return Object.freeze({
|
|
18
|
-
async resolveActor(request) {
|
|
19
|
-
if (authService && typeof authService.authenticateRequest === "function") {
|
|
20
|
-
return authService.authenticateRequest(request);
|
|
21
|
-
}
|
|
22
|
-
return { authenticated: false, actor: null, transientFailure: false };
|
|
23
|
-
},
|
|
24
|
-
hasPermission: defaultHasPermission,
|
|
25
|
-
resolveContext(input = {}) {
|
|
26
|
-
return extensions.resolvePolicyContext(input);
|
|
27
|
-
},
|
|
28
|
-
options: Object.freeze({
|
|
29
|
-
nodeEnv: String(env.NODE_ENV || "development").trim() || "development",
|
|
30
|
-
apiPrefix: String(env.AUTH_API_PREFIX || "/api/").trim() || "/api/",
|
|
31
|
-
// Unset AUTH_CSRF_UNSAFE_METHODS keeps the plugin default (POST, PUT, PATCH, DELETE).
|
|
32
|
-
unsafeMethods: unsafeMethods.length > 0 ? Object.freeze(unsafeMethods) : undefined,
|
|
33
|
-
// Unset AUTH_CSRF_COOKIE_SECURE follows NODE_ENV (Secure in production).
|
|
34
|
-
csrfCookieOpts: Object.freeze(
|
|
35
|
-
String(env.AUTH_CSRF_COOKIE_SECURE ?? "").trim()
|
|
36
|
-
? { secure: parseBooleanFlag(env.AUTH_CSRF_COOKIE_SECURE, false) }
|
|
37
|
-
: {}
|
|
38
|
-
)
|
|
39
|
-
})
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
|
|
43
7
|
const AuthPolicyProvider = defineProvider({
|
|
44
8
|
id: "auth.policy",
|
|
45
9
|
requires: {
|
|
@@ -55,6 +19,14 @@ const AuthPolicyProvider = defineProvider({
|
|
|
55
19
|
provides: {
|
|
56
20
|
policy: "auth.policy"
|
|
57
21
|
},
|
|
22
|
+
/**
|
|
23
|
+
* @param {object} capabilities
|
|
24
|
+
* @param {import("@jskit-ai/kernel/server/types").ActionCatalogue} capabilities.actions
|
|
25
|
+
* @param {import("../types.js").AuthService | null} capabilities.authService The installed provider's `auth.service`, when there is one.
|
|
26
|
+
* @param {import("../types.js").AuthExtensions} capabilities.extensions
|
|
27
|
+
* @param {import("@jskit-ai/kernel/server/types").RuntimeHttp} capabilities.http
|
|
28
|
+
* @param {import("@jskit-ai/kernel/server/types").RuntimeEnv} capabilities.env
|
|
29
|
+
*/
|
|
58
30
|
setup({ actions, authService, extensions, http, env }) {
|
|
59
31
|
const actionContext = createAuthActionContextContributor();
|
|
60
32
|
actions.registerContextContributor({
|
|
@@ -66,7 +38,7 @@ const AuthPolicyProvider = defineProvider({
|
|
|
66
38
|
id: visibility.resolverId,
|
|
67
39
|
resolve: visibility.resolve
|
|
68
40
|
});
|
|
69
|
-
return { policy:
|
|
41
|
+
return { policy: createAuthPolicy({ authService: authService ?? everyoneSignedOut, extensions, env }) };
|
|
70
42
|
},
|
|
71
43
|
async boot({ fastify }, { outputs }) {
|
|
72
44
|
const policy = outputs.policy;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// The types of the auth capabilities, for features that require them.
|
|
2
|
+
|
|
3
|
+
/** @typedef {ReturnType<typeof import("./authAccess.js").createAuthAccess>} AuthAccess */
|
|
4
|
+
/** @typedef {ReturnType<typeof import("./authExtensions.js").createAuthExtensions>} AuthExtensions */
|
|
5
|
+
/** @typedef {ReturnType<typeof import("./authPolicy.js").createAuthPolicy>} AuthPolicy */
|
|
6
|
+
|
|
7
|
+
/** @typedef {ReturnType<typeof import("../shared/authCapabilities.js").normalizeAuthCapabilities>} AuthCapabilities */
|
|
8
|
+
/** @typedef {NonNullable<ReturnType<typeof import("./authActor.js").normalizeAuthActor>>} AuthActor */
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* What authenticateRequest() answers for a request.
|
|
12
|
+
* @typedef {object} AuthRequestResult
|
|
13
|
+
* @property {boolean} authenticated
|
|
14
|
+
* @property {AuthActor} [actor] The signed-in actor, when authenticated.
|
|
15
|
+
* @property {object | null} [session] The session to write with writeSessionCookies(), or null.
|
|
16
|
+
* @property {boolean} [clearSession] True when the request's session cookies should be cleared.
|
|
17
|
+
* @property {boolean} [transientFailure] True when authentication could not be decided at this time.
|
|
18
|
+
* @property {boolean} [refreshRequired] True when the session must be refreshed first, which a caller
|
|
19
|
+
* that asked for no refresh (`refreshSession: false`), such as a realtime socket, cannot do.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The `auth.profile.projector` capability: the application's profiles of the
|
|
24
|
+
* identities that sign in. An auth provider without one uses noAppProfiles
|
|
25
|
+
* (`@jskit-ai/auth-core/server/noAppProfiles`), which keeps none.
|
|
26
|
+
* @typedef {object} AuthProfileProjector
|
|
27
|
+
* @property {(profile: object, options?: object) => Promise<object | null>} syncIdentityProfile
|
|
28
|
+
* Creates or updates the application's profile of a signed-in identity and answers it,
|
|
29
|
+
* or null when the application keeps no profiles.
|
|
30
|
+
* @property {(identity: object, options?: object) => Promise<object | null>} findByIdentity
|
|
31
|
+
* Answers the existing profile without synchronizing it, or null when there is none.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The invitation context resolver an application registers on
|
|
36
|
+
* `auth.extensions`; an auth provider reads it once with
|
|
37
|
+
* resolveInvitationContextResolver(), which answers invitationAsGiven when none
|
|
38
|
+
* is registered.
|
|
39
|
+
* @typedef {object} AuthInvitationContextResolver
|
|
40
|
+
* @property {string} resolverId
|
|
41
|
+
* @property {(invitation: { token: string, source: string, email: string }) => Promise<object>} resolveInvitationContext
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* What a realtime server asks of the auth service, when it asks anything.
|
|
46
|
+
* @typedef {object} AuthRealtimePolicy
|
|
47
|
+
* @property {boolean} [requireAuthentication] Refuses sockets that are not signed in.
|
|
48
|
+
* @property {(input: { actor: AuthActor | null, event: { name: string, payload: any } }) => boolean | Promise<boolean>} [authorizeEvent]
|
|
49
|
+
* Answers true when the actor may receive the event.
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* What logout() answers: the provider signed the session out, and whether the
|
|
54
|
+
* caller should clear the request's session cookies.
|
|
55
|
+
* @typedef {object} AuthLogoutResult
|
|
56
|
+
* @property {boolean} ok
|
|
57
|
+
* @property {boolean} clearSession
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The `auth.service` capability: the installed auth provider's operations,
|
|
62
|
+
* after every auth service decorator. Every provider implements every
|
|
63
|
+
* operation without brackets. The ones named in AUTH_OPERATION_FEATURES
|
|
64
|
+
* (`@jskit-ai/auth-core/shared/authCapabilities`) depend on a feature of
|
|
65
|
+
* getCapabilities(): when the feature is off, the operation throws
|
|
66
|
+
* AUTH_OPERATION_UNSUPPORTED. An optional operation (in brackets) is one some
|
|
67
|
+
* provider lacks.
|
|
68
|
+
* @typedef {object} AuthService
|
|
69
|
+
* @property {() => AuthCapabilities} getCapabilities
|
|
70
|
+
* @property {(request: object, options?: { syncProfile?: boolean, refreshSession?: boolean }) => Promise<AuthRequestResult>} authenticateRequest
|
|
71
|
+
* @property {(request: object) => boolean} hasAccessTokenCookie
|
|
72
|
+
* @property {(request: object) => boolean} hasSessionCookie
|
|
73
|
+
* @property {(reply: object, session: object) => void} writeSessionCookies
|
|
74
|
+
* @property {(reply: object) => void} clearSessionCookies
|
|
75
|
+
* @property {(request: object) => Promise<AuthLogoutResult>} logout
|
|
76
|
+
* @property {() => boolean} isDevAuthBootstrapEnabled
|
|
77
|
+
* @property {(input: object) => Promise<object>} register
|
|
78
|
+
* @property {(input: object) => Promise<object>} resendRegisterConfirmation
|
|
79
|
+
* @property {(input: object) => Promise<object>} login
|
|
80
|
+
* @property {(input: object) => Promise<object>} requestOtpLogin
|
|
81
|
+
* @property {(input: object) => Promise<object>} verifyOtpLogin
|
|
82
|
+
* @property {(input: object, request: object | null) => Promise<object>} oauthStart
|
|
83
|
+
* @property {(input: object, request: object | null) => Promise<object>} oauthComplete
|
|
84
|
+
* @property {(input: object) => Promise<object>} requestPasswordReset
|
|
85
|
+
* @property {(input: object) => Promise<object>} completePasswordRecovery
|
|
86
|
+
* @property {(input: object, request: object | null) => Promise<object>} resetPassword
|
|
87
|
+
* @property {(request: object, input: object) => Promise<object>} changePassword
|
|
88
|
+
* @property {(request: object, input: object) => Promise<object>} updateDisplayName
|
|
89
|
+
* @property {(request: object) => Promise<object>} getSecurityStatus
|
|
90
|
+
* @property {(request: object, input: object) => Promise<object>} setPasswordSignInEnabled
|
|
91
|
+
* @property {(request: object, input: object) => Promise<object>} startProviderLink
|
|
92
|
+
* @property {(request: object, input: object) => Promise<object>} unlinkProvider
|
|
93
|
+
* @property {(request: object) => Promise<object>} signOutOtherSessions
|
|
94
|
+
* @property {(input: object, request: object | null) => Promise<object>} devLoginAs
|
|
95
|
+
* @property {(reply: object, result: object) => void} [writeOAuthStartCookies] Keeps the state of an OAuth sign-in that oauthStart() began.
|
|
96
|
+
* @property {(reply: object) => void} [clearOAuthStartCookies]
|
|
97
|
+
* @property {(request: object, options?: { everywhere?: boolean }) => Promise<any>} [logoutEverywhere]
|
|
98
|
+
* @property {() => object} [getSettingsProfileAuthInfo] What the account settings show about the provider's profile.
|
|
99
|
+
* @property {() => object} [getOAuthProviderCatalog] The OAuth providers the sign-in screen offers.
|
|
100
|
+
* @property {AuthRealtimePolicy} [realtime]
|
|
101
|
+
*/
|
|
102
|
+
|
|
103
|
+
export {};
|
|
@@ -2,8 +2,10 @@ import { AppError } from "@jskit-ai/kernel/server/runtime/errors";
|
|
|
2
2
|
|
|
3
3
|
const AUTH_OPERATION_UNSUPPORTED_CODE = "AUTH_OPERATION_UNSUPPORTED";
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
/**
|
|
6
|
+
* The code stays AUTH_OPERATION_UNSUPPORTED for callers that branch on it; the
|
|
7
|
+
* messageCode lets a client show the default sentence in the user's language.
|
|
8
|
+
*/
|
|
7
9
|
function createUnsupportedAuthOperationError(operation, message = "") {
|
|
8
10
|
const operationName = String(operation || "auth operation").trim() || "auth operation";
|
|
9
11
|
return new AppError(501, message || `Auth operation "${operationName}" is not supported by the active provider.`, {
|
package/src/server/utils.js
CHANGED
|
@@ -2,9 +2,11 @@ import { normalizeReturnToPath as normalizeSharedReturnToPath } from "@jskit-ai/
|
|
|
2
2
|
|
|
3
3
|
const HOST_ONLY_COOKIE_PREFIX = "__Host-";
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Secure (HTTPS) cookies carry the __Host- prefix: the browser then requires
|
|
7
|
+
* Secure, Path=/ and no Domain, so a sibling host under the same parent domain
|
|
8
|
+
* can neither set nor overwrite them (no cookie tossing or session fixation).
|
|
9
|
+
*/
|
|
8
10
|
export function hostOnlyCookieName(name, secure) {
|
|
9
11
|
const cookieName = String(name || "").trim();
|
|
10
12
|
return secure && !cookieName.startsWith(HOST_ONLY_COOKIE_PREFIX) ? `${HOST_ONLY_COOKIE_PREFIX}${cookieName}` : cookieName;
|
package/src/server/validators.js
CHANGED
|
@@ -110,10 +110,9 @@ function buildFieldErrors(entries = []) {
|
|
|
110
110
|
return { fieldErrors, fieldErrorCodes };
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
-
function buildEmailPasswordInput(payload
|
|
113
|
+
function buildEmailPasswordInput(payload, { passwordValidator }) {
|
|
114
114
|
const emailCheck = validateEmail(payload.email);
|
|
115
|
-
const
|
|
116
|
-
const passwordCheck = resolvePassword(payload.password);
|
|
115
|
+
const passwordCheck = passwordValidator(payload.password);
|
|
117
116
|
|
|
118
117
|
return {
|
|
119
118
|
email: emailCheck.email,
|
package/src/shared/authApi.js
CHANGED
|
@@ -1,56 +1,92 @@
|
|
|
1
1
|
import { AUTH_PATHS, buildAuthOauthStartPath } from "./authPaths.js";
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Sends one request and answers the response body, as the HTTP client's
|
|
5
|
+
* request does.
|
|
6
|
+
* @typedef {(path: string, options?: { method?: string, body?: unknown }) => Promise<any>} AuthApiRequest
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The auth API's calls, each one request to its auth path.
|
|
11
|
+
* @param {{ request: AuthApiRequest }} dependencies
|
|
12
|
+
*/
|
|
3
13
|
function createApi({ request }) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
return `${oauthStartPath}?${params.toString()}`;
|
|
34
|
-
},
|
|
35
|
-
oauthComplete(payload) {
|
|
36
|
-
return request(AUTH_PATHS.OAUTH_COMPLETE, { method: "POST", body: payload });
|
|
37
|
-
},
|
|
38
|
-
devLoginAs(payload) {
|
|
39
|
-
return request(AUTH_PATHS.DEV_LOGIN_AS, { method: "POST", body: payload });
|
|
40
|
-
},
|
|
41
|
-
requestPasswordReset(payload) {
|
|
42
|
-
return request(AUTH_PATHS.PASSWORD_FORGOT, { method: "POST", body: payload });
|
|
43
|
-
},
|
|
44
|
-
completePasswordRecovery(payload) {
|
|
45
|
-
return request(AUTH_PATHS.PASSWORD_RECOVERY, { method: "POST", body: payload });
|
|
46
|
-
},
|
|
47
|
-
resetPassword(payload) {
|
|
48
|
-
return request(AUTH_PATHS.PASSWORD_RESET, { method: "POST", body: payload });
|
|
49
|
-
},
|
|
50
|
-
logout() {
|
|
51
|
-
return request(AUTH_PATHS.LOGOUT, { method: "POST" });
|
|
14
|
+
function session() {
|
|
15
|
+
return request(AUTH_PATHS.SESSION);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function register(payload) {
|
|
19
|
+
return request(AUTH_PATHS.REGISTER, { method: "POST", body: payload });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function resendRegisterConfirmation(payload) {
|
|
23
|
+
return request(AUTH_PATHS.REGISTER_CONFIRMATION_RESEND, { method: "POST", body: payload });
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function login(payload) {
|
|
27
|
+
return request(AUTH_PATHS.LOGIN, { method: "POST", body: payload });
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function requestOtp(payload) {
|
|
31
|
+
return request(AUTH_PATHS.LOGIN_OTP_REQUEST, { method: "POST", body: payload });
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function verifyOtp(payload) {
|
|
35
|
+
return request(AUTH_PATHS.LOGIN_OTP_VERIFY, { method: "POST", body: payload });
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function oauthStartUrl(provider, options = {}) {
|
|
39
|
+
const oauthStartPath = buildAuthOauthStartPath(provider);
|
|
40
|
+
const returnTo = String(options.returnTo || "").trim();
|
|
41
|
+
if (!returnTo) {
|
|
42
|
+
return oauthStartPath;
|
|
52
43
|
}
|
|
53
|
-
|
|
44
|
+
|
|
45
|
+
const params = new URLSearchParams({
|
|
46
|
+
returnTo
|
|
47
|
+
});
|
|
48
|
+
return `${oauthStartPath}?${params.toString()}`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function oauthComplete(payload) {
|
|
52
|
+
return request(AUTH_PATHS.OAUTH_COMPLETE, { method: "POST", body: payload });
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function devLoginAs(payload) {
|
|
56
|
+
return request(AUTH_PATHS.DEV_LOGIN_AS, { method: "POST", body: payload });
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function requestPasswordReset(payload) {
|
|
60
|
+
return request(AUTH_PATHS.PASSWORD_FORGOT, { method: "POST", body: payload });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function completePasswordRecovery(payload) {
|
|
64
|
+
return request(AUTH_PATHS.PASSWORD_RECOVERY, { method: "POST", body: payload });
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function resetPassword(payload) {
|
|
68
|
+
return request(AUTH_PATHS.PASSWORD_RESET, { method: "POST", body: payload });
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function logout() {
|
|
72
|
+
return request(AUTH_PATHS.LOGOUT, { method: "POST" });
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return Object.freeze({
|
|
76
|
+
session,
|
|
77
|
+
register,
|
|
78
|
+
resendRegisterConfirmation,
|
|
79
|
+
login,
|
|
80
|
+
requestOtp,
|
|
81
|
+
verifyOtp,
|
|
82
|
+
oauthStartUrl,
|
|
83
|
+
oauthComplete,
|
|
84
|
+
devLoginAs,
|
|
85
|
+
requestPasswordReset,
|
|
86
|
+
completePasswordRecovery,
|
|
87
|
+
resetPassword,
|
|
88
|
+
logout
|
|
89
|
+
});
|
|
54
90
|
}
|
|
55
91
|
|
|
56
92
|
export { createApi };
|
|
@@ -2,7 +2,12 @@ import { normalizeOAuthProviderId } from "./oauthProviders.js";
|
|
|
2
2
|
|
|
3
3
|
const AUTH_PASSWORD_RECOVERY_DELIVERIES = Object.freeze(["email", "dev-log", "dev-response", "disabled"]);
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* The auth.service operations that depend on a feature of getCapabilities(),
|
|
7
|
+
* each with the feature's path. Every provider implements them; one whose
|
|
8
|
+
* feature is off throws AUTH_OPERATION_UNSUPPORTED.
|
|
9
|
+
*/
|
|
10
|
+
const AUTH_OPERATION_FEATURES = Object.freeze({
|
|
6
11
|
register: "password.register",
|
|
7
12
|
resendRegisterConfirmation: "emailConfirmation",
|
|
8
13
|
login: "password.login",
|
|
@@ -16,6 +21,7 @@ const AUTH_OPTIONAL_OPERATION_FEATURES = Object.freeze({
|
|
|
16
21
|
changePassword: "password.change",
|
|
17
22
|
updateDisplayName: "profileUpdate",
|
|
18
23
|
getSecurityStatus: "securityStatus",
|
|
24
|
+
// readable-check: allow setter-seam - not a slot: setPasswordSignInEnabled is an auth.service operation
|
|
19
25
|
setPasswordSignInEnabled: "password.methodToggle",
|
|
20
26
|
startProviderLink: "providerLinking.start",
|
|
21
27
|
unlinkProvider: "providerLinking.unlink",
|
|
@@ -167,7 +173,7 @@ function getCapabilityFeature(capabilities, path) {
|
|
|
167
173
|
}
|
|
168
174
|
|
|
169
175
|
function isAuthOperationSupported(capabilities, operationName) {
|
|
170
|
-
const path =
|
|
176
|
+
const path = AUTH_OPERATION_FEATURES[String(operationName || "").trim()];
|
|
171
177
|
if (!path) {
|
|
172
178
|
return true;
|
|
173
179
|
}
|
|
@@ -176,7 +182,7 @@ function isAuthOperationSupported(capabilities, operationName) {
|
|
|
176
182
|
|
|
177
183
|
export {
|
|
178
184
|
AUTH_PASSWORD_RECOVERY_DELIVERIES,
|
|
179
|
-
|
|
185
|
+
AUTH_OPERATION_FEATURES,
|
|
180
186
|
normalizeAuthProviderId,
|
|
181
187
|
normalizeAuthCapabilities,
|
|
182
188
|
getCapabilityFeature,
|