@jskit-ai/users-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 +1 -7
- package/package.json +42 -19
- package/patterns/user-administration-server/PATTERN.md +16 -1
- package/patterns/user-administration-server/example/packages/users/package.json +3 -2
- package/patterns/user-administration-server/example/packages/users/src/server/types.js +5 -0
- package/patterns/user-administration-server/example/packages/users-workspace/package.json +4 -3
- package/patterns/user-administration-server/example/packages/users-workspace/src/server/UsersWorkspaceFeature.js +2 -9
- package/patterns/user-administration-server/example/packages/users-workspace/src/server/types.js +5 -0
- package/src/server/UsersFeature.js +26 -78
- package/src/server/UsersIdentityProvider.js +11 -22
- package/src/server/accountNotifications/accountNotificationsActions.js +28 -41
- package/src/server/accountNotifications/{bootAccountNotificationsRoutes.js → accountNotificationsRoutes.js} +1 -4
- package/src/server/accountNotifications/accountNotificationsService.js +9 -10
- package/src/server/accountPreferences/accountPreferencesActions.js +28 -41
- package/src/server/accountPreferences/{bootAccountPreferencesRoutes.js → accountPreferencesRoutes.js} +1 -4
- package/src/server/accountPreferences/accountPreferencesService.js +10 -10
- package/src/server/accountProfile/accountProfileActions.js +87 -127
- package/src/server/accountProfile/{bootAccountProfileRoutes.js → accountProfileRoutes.js} +12 -18
- package/src/server/accountProfile/accountProfileService.js +19 -35
- package/src/server/accountProfile/avatarService.js +15 -39
- package/src/server/accountProfile/avatarStorageService.js +2 -1
- package/src/server/accountSecurity/accountSecurityActions.js +100 -148
- package/src/server/accountSecurity/{bootAccountSecurityRoutes.js → accountSecurityRoutes.js} +9 -8
- package/src/server/accountSecurity/accountSecurityService.js +15 -44
- package/src/server/common/formatters/accountAvatarFormatter.js +4 -4
- package/src/server/common/formatters/accountSecurityStatusFormatter.js +2 -3
- package/src/server/common/formatters/accountSettingsResponseFormatter.js +11 -2
- package/src/server/common/repositories/userProfilesRepository.js +31 -55
- package/src/server/common/repositories/userSettingsRepository.js +16 -21
- package/src/server/common/services/accountContextService.js +9 -7
- package/src/server/common/services/authProfileSyncService.js +15 -42
- package/src/server/common/support/accountSettingsJsonApiTransport.js +10 -3
- package/src/server/common/support/realtimeServiceEvents.js +3 -2
- package/src/server/common/support/resolveActionUser.js +9 -2
- package/src/server/types.js +29 -0
- package/src/server/usersBootstrapContributor.js +60 -84
- package/src/server/usersExtensions.js +1 -0
- package/src/server/usersIdentity.js +28 -0
- package/src/server/usersRuntime.js +61 -0
- package/src/shared/operationMessages.js +6 -15
- package/src/shared/resources/userProfileResource.js +3 -31
- package/src/shared/resources/userProfileValues.js +32 -0
- package/src/shared/resources/userSettingsResource.js +2 -4
- package/test/accountProfileService.test.js +1 -0
- package/test/accountSecurityService.test.js +2 -4
- package/test/exportsContract.test.js +2 -0
- package/test/featureRuntime.test.js +11 -8
- package/test/i18nCodes.test.js +3 -10
- package/test/usersBootstrapContributor.test.js +0 -1
- package/test/usersPackageScaffoldContract.test.js +3 -5
- package/test/usersRouteRequestInputValidator.test.js +4 -4
- package/src/server/common/repositories/repositoryUtils.js +0 -44
- package/src/server/common/support/deepFreeze.js +0 -1
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
isDuplicateEntryError,
|
|
4
|
-
retryDeadlocks
|
|
5
|
-
} from "./repositoryUtils.js";
|
|
1
|
+
import { isLostDuplicateInsertRace, resolveTransaction, retryDeadlocks } from "@jskit-ai/database-runtime/shared";
|
|
2
|
+
import { normalizeRecordId } from "@jskit-ai/kernel/shared/support/normalize";
|
|
6
3
|
import {
|
|
7
4
|
createJsonRestContext,
|
|
8
5
|
extractJsonRestCollectionRows
|
|
@@ -36,7 +33,7 @@ function pickPatchFields(source = {}) {
|
|
|
36
33
|
return patch;
|
|
37
34
|
}
|
|
38
35
|
|
|
39
|
-
function
|
|
36
|
+
function buildDefaultUserSettingsCreatePayload(userId) {
|
|
40
37
|
return {
|
|
41
38
|
id: userId,
|
|
42
39
|
theme: DEFAULT_USER_SETTINGS.theme,
|
|
@@ -54,10 +51,8 @@ function createDefaultUserSettingsCreatePayload(userId) {
|
|
|
54
51
|
};
|
|
55
52
|
}
|
|
56
53
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
throw new TypeError("userSettingsRepository requires json-rest-api userSettings resource.");
|
|
60
|
-
}
|
|
54
|
+
/** @param {{ api: import("@jskit-ai/json-rest-api-core/server/types").RuntimeJsonRestApi }} dependencies */
|
|
55
|
+
function createRepository({ api }) {
|
|
61
56
|
const withTransaction = (work) => retryDeadlocks(() => api.transaction(work));
|
|
62
57
|
|
|
63
58
|
async function queryFirst(filters = {}, options = {}) {
|
|
@@ -67,10 +62,10 @@ function createRepository({ api } = {}) {
|
|
|
67
62
|
queryParams: {
|
|
68
63
|
filters
|
|
69
64
|
},
|
|
70
|
-
transaction: options
|
|
65
|
+
transaction: resolveTransaction(options),
|
|
71
66
|
format: "plain"
|
|
72
67
|
},
|
|
73
|
-
createJsonRestContext(options
|
|
68
|
+
createJsonRestContext(options.context)
|
|
74
69
|
)
|
|
75
70
|
);
|
|
76
71
|
|
|
@@ -102,15 +97,15 @@ function createRepository({ api } = {}) {
|
|
|
102
97
|
try {
|
|
103
98
|
await api.resources.userSettings.post(
|
|
104
99
|
{
|
|
105
|
-
data:
|
|
100
|
+
data: buildDefaultUserSettingsCreatePayload(normalizedUserId),
|
|
106
101
|
format: "plain",
|
|
107
102
|
returning: "full",
|
|
108
|
-
transaction: options
|
|
103
|
+
transaction: resolveTransaction(options)
|
|
109
104
|
},
|
|
110
|
-
createJsonRestContext(options
|
|
105
|
+
createJsonRestContext(options.context)
|
|
111
106
|
);
|
|
112
107
|
} catch (error) {
|
|
113
|
-
if (
|
|
108
|
+
if (!isLostDuplicateInsertRace(error, options)) {
|
|
114
109
|
throw error;
|
|
115
110
|
}
|
|
116
111
|
}
|
|
@@ -118,7 +113,7 @@ function createRepository({ api } = {}) {
|
|
|
118
113
|
return findByUserId(normalizedUserId, options);
|
|
119
114
|
}
|
|
120
115
|
|
|
121
|
-
async function patchUserSettings(userId, patch
|
|
116
|
+
async function patchUserSettings(userId, patch, options = {}) {
|
|
122
117
|
const normalizedUserId = normalizeRecordId(userId, { fallback: null });
|
|
123
118
|
if (!normalizedUserId) {
|
|
124
119
|
throw new TypeError("userSettingsRepository.patchUserSettings requires a valid user id.");
|
|
@@ -141,19 +136,19 @@ function createRepository({ api } = {}) {
|
|
|
141
136
|
},
|
|
142
137
|
format: "plain",
|
|
143
138
|
returning: "full",
|
|
144
|
-
transaction: options
|
|
139
|
+
transaction: resolveTransaction(options)
|
|
145
140
|
},
|
|
146
|
-
createJsonRestContext(options
|
|
141
|
+
createJsonRestContext(options.context)
|
|
147
142
|
);
|
|
148
143
|
|
|
149
144
|
return findByUserId(normalizedUserId, options);
|
|
150
145
|
}
|
|
151
146
|
|
|
152
|
-
async function updatePreferences(userId, patch
|
|
147
|
+
async function updatePreferences(userId, patch, options = {}) {
|
|
153
148
|
return patchUserSettings(userId, patch, options);
|
|
154
149
|
}
|
|
155
150
|
|
|
156
|
-
async function updateNotifications(userId, patch
|
|
151
|
+
async function updateNotifications(userId, patch, options = {}) {
|
|
157
152
|
return patchUserSettings(userId, patch, options);
|
|
158
153
|
}
|
|
159
154
|
|
|
@@ -2,6 +2,10 @@ import { normalizeRecordId } from "@jskit-ai/kernel/shared/support/normalize";
|
|
|
2
2
|
import { isAuthOperationSupported } from "@jskit-ai/auth-core/shared/authCapabilities";
|
|
3
3
|
import { normalizeIdentity } from "../support/identity.js";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* @param {import("../../types.js").UserProfilesRepository} userProfilesRepository
|
|
7
|
+
* @param {object} user
|
|
8
|
+
*/
|
|
5
9
|
async function resolveUserProfile(userProfilesRepository, user) {
|
|
6
10
|
const identity = normalizeIdentity(user);
|
|
7
11
|
if (identity) {
|
|
@@ -22,16 +26,14 @@ async function resolveUserProfile(userProfilesRepository, user) {
|
|
|
22
26
|
return null;
|
|
23
27
|
}
|
|
24
28
|
|
|
29
|
+
/**
|
|
30
|
+
* @param {import("@jskit-ai/auth-core/server/types").AuthService} authService
|
|
31
|
+
* @param {object} request
|
|
32
|
+
*/
|
|
25
33
|
async function resolveSecurityStatus(authService, request) {
|
|
26
|
-
if (!authService || typeof authService.getSecurityStatus !== "function") {
|
|
27
|
-
return {};
|
|
28
|
-
}
|
|
29
34
|
// A provider that keeps passwords elsewhere, such as an accounts hub, has no
|
|
30
35
|
// security status; its settings pages still load.
|
|
31
|
-
if (
|
|
32
|
-
typeof authService.getCapabilities === "function" &&
|
|
33
|
-
!isAuthOperationSupported(authService.getCapabilities(), "getSecurityStatus")
|
|
34
|
-
) {
|
|
36
|
+
if (!isAuthOperationSupported(authService.getCapabilities(), "getSecurityStatus")) {
|
|
35
37
|
return {};
|
|
36
38
|
}
|
|
37
39
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { normalizeLowerText, normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
|
|
1
|
+
import { joinOrOpenTransaction } from "@jskit-ai/database-runtime/shared";
|
|
2
|
+
import { normalizeLowerText, normalizeRecordId, normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
|
|
3
3
|
import { normalizeIdentity } from "../support/identity.js";
|
|
4
4
|
|
|
5
5
|
function buildNormalizedIdentityKey(identityLike) {
|
|
@@ -54,38 +54,15 @@ function requireSynchronizedProfile(profile) {
|
|
|
54
54
|
throw new Error("Profile synchronization failed.");
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function createService({
|
|
68
|
-
userProfilesRepository,
|
|
69
|
-
resolveLifecycleContributors = () => [],
|
|
70
|
-
userSettingsRepository = null
|
|
71
|
-
} = {}) {
|
|
72
|
-
if (!userProfilesRepository || typeof userProfilesRepository.findByIdentity !== "function") {
|
|
73
|
-
throw new Error("authProfileSyncService requires userProfilesRepository.findByIdentity().");
|
|
74
|
-
}
|
|
75
|
-
if (typeof userProfilesRepository.upsert !== "function") {
|
|
76
|
-
throw new Error("authProfileSyncService requires userProfilesRepository.upsert().");
|
|
77
|
-
}
|
|
78
|
-
if (typeof userProfilesRepository.withTransaction !== "function") {
|
|
79
|
-
throw new Error("authProfileSyncService requires userProfilesRepository.withTransaction().");
|
|
80
|
-
}
|
|
81
|
-
if (!userSettingsRepository || typeof userSettingsRepository.ensureForUserId !== "function") {
|
|
82
|
-
throw new Error("authProfileSyncService requires userSettingsRepository.ensureForUserId().");
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (typeof resolveLifecycleContributors !== "function") {
|
|
86
|
-
throw new Error("authProfileSyncService requires resolveLifecycleContributors().");
|
|
87
|
-
}
|
|
88
|
-
|
|
57
|
+
/**
|
|
58
|
+
* The profile projector: keeps a users row in step with the identity that signs in.
|
|
59
|
+
* @param {object} dependencies
|
|
60
|
+
* @param {import("../../types.js").UserProfilesRepository} dependencies.userProfilesRepository
|
|
61
|
+
* @param {import("../../types.js").UserSettingsRepository} dependencies.userSettingsRepository
|
|
62
|
+
* @param {import("../../types.js").ResolveProfileSyncLifecycleContributors} dependencies.resolveLifecycleContributors
|
|
63
|
+
* Answers the registered contributors that run after each synchronization.
|
|
64
|
+
*/
|
|
65
|
+
function createService({ userProfilesRepository, resolveLifecycleContributors, userSettingsRepository }) {
|
|
89
66
|
async function findByIdentity(identityLike, options = {}) {
|
|
90
67
|
const normalized = buildNormalizedIdentityKey(identityLike);
|
|
91
68
|
return userProfilesRepository.findByIdentity(
|
|
@@ -113,10 +90,10 @@ function createService({
|
|
|
113
90
|
|
|
114
91
|
async function syncIdentityProfile(profileLike, options = {}) {
|
|
115
92
|
const normalized = buildNormalizedIdentityProfile(profileLike);
|
|
116
|
-
const lifecycleContributors =
|
|
93
|
+
const lifecycleContributors = resolveLifecycleContributors();
|
|
117
94
|
|
|
118
|
-
const runSync = async (trx
|
|
119
|
-
const operationOptions =
|
|
95
|
+
const runSync = async (trx) => {
|
|
96
|
+
const operationOptions = { ...options, trx };
|
|
120
97
|
const existing = await findByIdentity(normalized, operationOptions);
|
|
121
98
|
let created = false;
|
|
122
99
|
if (!profileNeedsUpdate(existing, normalized)) {
|
|
@@ -147,11 +124,7 @@ function createService({
|
|
|
147
124
|
return synchronizedProfile;
|
|
148
125
|
};
|
|
149
126
|
|
|
150
|
-
|
|
151
|
-
return runSync(options.trx);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
return userProfilesRepository.withTransaction((trx) => runSync(trx));
|
|
127
|
+
return joinOrOpenTransaction(options, userProfilesRepository.withTransaction, runSync);
|
|
155
128
|
}
|
|
156
129
|
|
|
157
130
|
return Object.freeze({
|
|
@@ -1,6 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { resolveActorId } from "@jskit-ai/kernel/server/actions/actionContributorHelpers";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The account settings record's JSON:API id: the signed-in user's id.
|
|
5
|
+
* @param {unknown} _record
|
|
6
|
+
* @param {{ request: object }} context The route transport's context.
|
|
7
|
+
*/
|
|
8
|
+
function resolveAccountSettingsResourceId(_record, context) {
|
|
9
|
+
const userId = resolveActorId(null, context.request);
|
|
10
|
+
if (!userId) {
|
|
4
11
|
throw new Error("JSON:API account settings response requires request.user.id.");
|
|
5
12
|
}
|
|
6
13
|
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { createEntityChangedActionEvent } from "@jskit-ai/kernel/server/actions";
|
|
2
|
+
import { resolveActorId } from "@jskit-ai/kernel/server/actions/actionContributorHelpers";
|
|
2
3
|
|
|
3
|
-
function resolveActorScopedEntityId({ context }
|
|
4
|
-
return context
|
|
4
|
+
function resolveActorScopedEntityId({ context }) {
|
|
5
|
+
return resolveActorId(context);
|
|
5
6
|
}
|
|
6
7
|
|
|
7
8
|
const ACCOUNT_SETTINGS_AND_BOOTSTRAP_EVENTS = Object.freeze([
|
|
@@ -1,9 +1,16 @@
|
|
|
1
|
+
import { resolveActor } from "@jskit-ai/kernel/server/actions/actionContributorHelpers";
|
|
1
2
|
import { normalizeObject } from "@jskit-ai/kernel/shared/support/normalize";
|
|
2
3
|
|
|
4
|
+
/**
|
|
5
|
+
* The user an account action runs for: the input's user, when an internal
|
|
6
|
+
* caller names one, else the signed-in user (kernel's resolveActor: the
|
|
7
|
+
* context's actor, else the request's user).
|
|
8
|
+
* @param {object} context
|
|
9
|
+
* @param {unknown} [input]
|
|
10
|
+
*/
|
|
3
11
|
function resolveActionUser(context, input) {
|
|
4
12
|
const payload = normalizeObject(input);
|
|
5
|
-
|
|
6
|
-
return payload.user || request?.user || context?.actor || null;
|
|
13
|
+
return payload.user || resolveActor(context);
|
|
7
14
|
}
|
|
8
15
|
|
|
9
16
|
export { resolveActionUser };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// The types of the users capabilities, for features that require them.
|
|
2
|
+
|
|
3
|
+
/** @typedef {ReturnType<typeof import("./usersRuntime.js").createUsersRuntime>} UsersCore */
|
|
4
|
+
/** @typedef {ReturnType<typeof import("./usersExtensions.js").createUsersExtensions>} UsersExtensions */
|
|
5
|
+
/** @typedef {ReturnType<typeof import("./usersIdentity.js").createUsersIdentity>} UsersIdentity */
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The users' profile repository, as the account services receive it.
|
|
9
|
+
* @typedef {ReturnType<typeof import("./common/repositories/userProfilesRepository.js").createRepository>} UserProfilesRepository
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* The users' settings repository, as the account services receive it.
|
|
13
|
+
* @typedef {ReturnType<typeof import("./common/repositories/userSettingsRepository.js").createRepository>} UserSettingsRepository
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Answers the registered profile synchronization lifecycle contributors, in
|
|
18
|
+
* order: `profileSyncLifecycleContributors` of `users.extensions`.
|
|
19
|
+
* @typedef {UsersExtensions["profileSyncLifecycleContributors"]} ResolveProfileSyncLifecycleContributors
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/** @typedef {ReturnType<typeof import("./accountNotifications/accountNotificationsService.js").createService>} AccountNotificationsService The notification settings service: `services.accountNotifications` of `users.core`. */
|
|
23
|
+
/** @typedef {ReturnType<typeof import("./accountPreferences/accountPreferencesService.js").createService>} AccountPreferencesService The preferences service: `services.accountPreferences` of `users.core`. */
|
|
24
|
+
/** @typedef {ReturnType<typeof import("./accountProfile/accountProfileService.js").createService>} AccountProfileService The profile service: `services.accountProfile` of `users.core`. */
|
|
25
|
+
/** @typedef {ReturnType<typeof import("./accountProfile/avatarService.js").createService>} AvatarService The avatar service: `services.avatar` of `users.core`. */
|
|
26
|
+
/** @typedef {ReturnType<typeof import("./accountProfile/avatarStorageService.js").createService>} AvatarStorageService The avatar files in `runtime.storage`, as the avatar service receives them. */
|
|
27
|
+
/** @typedef {ReturnType<typeof import("./accountSecurity/accountSecurityService.js").createService>} AccountSecurityService The account security service: `services.accountSecurity` of `users.core`. */
|
|
28
|
+
|
|
29
|
+
export {};
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { AppError } from "@jskit-ai/kernel/server/runtime";
|
|
2
|
-
import {
|
|
3
|
-
import { normalizeLowerText, normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
|
|
4
|
-
import { normalizeObject } from "@jskit-ai/kernel/shared/support/normalize";
|
|
2
|
+
import { normalizeLowerText, normalizeObject, normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
|
|
5
3
|
import { accountAvatarFormatter } from "./common/formatters/accountAvatarFormatter.js";
|
|
4
|
+
import { formatUserSettingsSection } from "./common/formatters/accountSettingsResponseFormatter.js";
|
|
6
5
|
import { USER_SETTINGS_BOOTSTRAP_KEYS } from "../shared/resources/userSettingsResource.js";
|
|
7
6
|
|
|
7
|
+
/** @param {import("@jskit-ai/auth-core/server/types").AuthService} authService */
|
|
8
8
|
function getOAuthProviderCatalogPayload(authService) {
|
|
9
|
-
|
|
9
|
+
// readable-check: allow method-probe - optional operation: getOAuthProviderCatalog is an optional auth.service operation
|
|
10
|
+
if (typeof authService.getOAuthProviderCatalog !== "function") {
|
|
10
11
|
return {
|
|
11
12
|
oauthProviders: [],
|
|
12
13
|
oauthDefaultProvider: null
|
|
@@ -30,14 +31,14 @@ function getOAuthProviderCatalogPayload(authService) {
|
|
|
30
31
|
};
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
function
|
|
34
|
+
function buildAnonymousBootstrapPayload(surfaceAccess) {
|
|
34
35
|
return {
|
|
35
36
|
session: {
|
|
36
37
|
authenticated: false
|
|
37
38
|
},
|
|
38
39
|
profile: null,
|
|
39
40
|
app: {},
|
|
40
|
-
surfaceAccess
|
|
41
|
+
surfaceAccess,
|
|
41
42
|
userSettings: null,
|
|
42
43
|
requestMeta: {
|
|
43
44
|
hasRequest: false
|
|
@@ -45,92 +46,67 @@ function createAnonymousBootstrapPayload({ surfaceAccess = {} }) {
|
|
|
45
46
|
};
|
|
46
47
|
}
|
|
47
48
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
requireServiceMethod(userSettingsRepository, "ensureForUserId", contributorId, {
|
|
70
|
-
serviceLabel: "internal.repository.user-settings"
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
return Object.freeze({
|
|
74
|
-
contributorId,
|
|
75
|
-
order: 100,
|
|
76
|
-
async contribute({ request = null, reply = null, payload: existingPayload = {} } = {}) {
|
|
77
|
-
const authResult = await request.executeAction({
|
|
78
|
-
actionId: "auth.session.read"
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
if (authResult?.clearSession === true && typeof authService?.clearSessionCookies === "function") {
|
|
82
|
-
authService.clearSessionCookies(reply);
|
|
83
|
-
}
|
|
84
|
-
if (authResult?.session && typeof authService?.writeSessionCookies === "function") {
|
|
85
|
-
authService.writeSessionCookies(reply, authResult.session);
|
|
86
|
-
}
|
|
87
|
-
if (authResult?.transientFailure === true) {
|
|
88
|
-
throw new AppError(503, "Authentication service temporarily unavailable. Please retry.", {
|
|
89
|
-
code: "users.bootstrap.authUnavailable"
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const normalizedUser = authResult?.authenticated === true ? authResult?.actor || null : null;
|
|
94
|
-
const inheritedSurfaceAccess = normalizeObject(existingPayload?.surfaceAccess);
|
|
95
|
-
let payload = createAnonymousBootstrapPayload({
|
|
96
|
-
surfaceAccess: inheritedSurfaceAccess
|
|
49
|
+
/**
|
|
50
|
+
* @param {object} dependencies
|
|
51
|
+
* @param {import("./types.js").UserProfilesRepository} dependencies.userProfilesRepository
|
|
52
|
+
* @param {import("./types.js").UserSettingsRepository} dependencies.userSettingsRepository
|
|
53
|
+
* @param {import("@jskit-ai/auth-core/server/types").AuthService} dependencies.authService
|
|
54
|
+
*/
|
|
55
|
+
function createUsersBootstrapContributor({ userProfilesRepository, userSettingsRepository, authService }) {
|
|
56
|
+
async function contribute({ request, reply, payload: existingPayload }) {
|
|
57
|
+
const authResult = await request.executeAction({
|
|
58
|
+
actionId: "auth.session.read"
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
if (authResult?.clearSession === true) {
|
|
62
|
+
authService.clearSessionCookies(reply);
|
|
63
|
+
}
|
|
64
|
+
if (authResult?.session) {
|
|
65
|
+
authService.writeSessionCookies(reply, authResult.session);
|
|
66
|
+
}
|
|
67
|
+
if (authResult?.transientFailure === true) {
|
|
68
|
+
throw new AppError(503, "Authentication service temporarily unavailable. Please retry.", {
|
|
69
|
+
code: "users.bootstrap.authUnavailable"
|
|
97
70
|
});
|
|
71
|
+
}
|
|
98
72
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
payload = {
|
|
104
|
-
session: {
|
|
105
|
-
authenticated: true,
|
|
106
|
-
userId: latestProfile.id
|
|
107
|
-
},
|
|
108
|
-
profile: {
|
|
109
|
-
displayName: latestProfile.displayName,
|
|
110
|
-
email: latestProfile.email,
|
|
111
|
-
avatar: accountAvatarFormatter(latestProfile, userSettings)
|
|
112
|
-
},
|
|
113
|
-
app: {},
|
|
114
|
-
surfaceAccess: inheritedSurfaceAccess,
|
|
115
|
-
userSettings: mapUserSettingsBootstrap(userSettings),
|
|
116
|
-
requestMeta: {
|
|
117
|
-
hasRequest: Boolean(request)
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
}
|
|
73
|
+
const normalizedUser = authResult?.authenticated === true ? authResult.actor || null : null;
|
|
74
|
+
const inheritedSurfaceAccess = normalizeObject(existingPayload.surfaceAccess);
|
|
75
|
+
let payload = buildAnonymousBootstrapPayload(inheritedSurfaceAccess);
|
|
121
76
|
|
|
122
|
-
|
|
123
|
-
const
|
|
77
|
+
if (normalizedUser) {
|
|
78
|
+
const latestProfile = (await userProfilesRepository.findById(normalizedUser.id)) || normalizedUser;
|
|
79
|
+
const userSettings = await userSettingsRepository.ensureForUserId(latestProfile.id);
|
|
124
80
|
|
|
125
|
-
|
|
126
|
-
...payload,
|
|
81
|
+
payload = {
|
|
127
82
|
session: {
|
|
128
|
-
|
|
129
|
-
|
|
83
|
+
authenticated: true,
|
|
84
|
+
userId: latestProfile.id
|
|
85
|
+
},
|
|
86
|
+
profile: {
|
|
87
|
+
displayName: latestProfile.displayName,
|
|
88
|
+
email: latestProfile.email,
|
|
89
|
+
avatar: accountAvatarFormatter(latestProfile, userSettings)
|
|
90
|
+
},
|
|
91
|
+
app: {},
|
|
92
|
+
surfaceAccess: inheritedSurfaceAccess,
|
|
93
|
+
userSettings: formatUserSettingsSection(USER_SETTINGS_BOOTSTRAP_KEYS, userSettings),
|
|
94
|
+
requestMeta: {
|
|
95
|
+
hasRequest: true
|
|
130
96
|
}
|
|
131
97
|
};
|
|
132
98
|
}
|
|
133
|
-
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
...payload,
|
|
102
|
+
session: {
|
|
103
|
+
...payload.session,
|
|
104
|
+
...getOAuthProviderCatalogPayload(authService)
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return Object.freeze({ contribute });
|
|
134
110
|
}
|
|
135
111
|
|
|
136
112
|
export { createUsersBootstrapContributor };
|
|
@@ -6,6 +6,7 @@ function normalizeContributor(value) {
|
|
|
6
6
|
if (!contributorId) {
|
|
7
7
|
throw new TypeError("Profile synchronization lifecycle contributor requires contributorId.");
|
|
8
8
|
}
|
|
9
|
+
// readable-check: allow method-probe - input validation: the contributor an application registers
|
|
9
10
|
if (typeof value.afterIdentityProfileSynced !== "function") {
|
|
10
11
|
throw new TypeError(`Profile synchronization lifecycle contributor "${contributorId}" requires afterIdentityProfileSynced().`);
|
|
11
12
|
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { createRepository as createUserProfilesRepository } from "./common/repositories/userProfilesRepository.js";
|
|
2
|
+
import { createRepository as createUserSettingsRepository } from "./common/repositories/userSettingsRepository.js";
|
|
3
|
+
import { createService as createAuthProfileSyncService } from "./common/services/authProfileSyncService.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The users' profile and settings records, and the projector that keeps a
|
|
7
|
+
* profile in step with the identity that signs in.
|
|
8
|
+
* @param {object} dependencies
|
|
9
|
+
* @param {import("@jskit-ai/json-rest-api-core/server/types").RuntimeJsonRestApi} dependencies.jsonRestApi
|
|
10
|
+
* @param {import("./types.js").UsersExtensions} dependencies.extensions
|
|
11
|
+
*/
|
|
12
|
+
function createUsersIdentity({ jsonRestApi, extensions }) {
|
|
13
|
+
const repositories = Object.freeze({
|
|
14
|
+
userProfiles: createUserProfilesRepository({ api: jsonRestApi }),
|
|
15
|
+
userSettings: createUserSettingsRepository({ api: jsonRestApi })
|
|
16
|
+
});
|
|
17
|
+
const profileProjector = createAuthProfileSyncService({
|
|
18
|
+
userProfilesRepository: repositories.userProfiles,
|
|
19
|
+
userSettingsRepository: repositories.userSettings,
|
|
20
|
+
resolveLifecycleContributors: extensions.profileSyncLifecycleContributors
|
|
21
|
+
});
|
|
22
|
+
return Object.freeze({
|
|
23
|
+
repositories,
|
|
24
|
+
profileProjector
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export { createUsersIdentity };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { USERS_SHARED_API } from "../shared/index.js";
|
|
2
|
+
import { createService as createAccountNotificationsService } from "./accountNotifications/accountNotificationsService.js";
|
|
3
|
+
import { createService as createAccountPreferencesService } from "./accountPreferences/accountPreferencesService.js";
|
|
4
|
+
import { createService as createAccountProfileService } from "./accountProfile/accountProfileService.js";
|
|
5
|
+
import { createService as createAvatarService } from "./accountProfile/avatarService.js";
|
|
6
|
+
import { createService as createAvatarStorageService } from "./accountProfile/avatarStorageService.js";
|
|
7
|
+
import { createService as createAccountSecurityService } from "./accountSecurity/accountSecurityService.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The `users.core` capability: the users' repositories and the account
|
|
11
|
+
* services built on them.
|
|
12
|
+
* @param {object} dependencies
|
|
13
|
+
* @param {import("@jskit-ai/auth-core/server/types").AuthService} dependencies.authService
|
|
14
|
+
* @param {import("./types.js").UsersIdentity} dependencies.identity
|
|
15
|
+
* @param {import("@jskit-ai/storage-runtime/server/types").RuntimeStorage} dependencies.storage
|
|
16
|
+
*/
|
|
17
|
+
function createUsersRuntime({ authService, identity, storage }) {
|
|
18
|
+
const userProfilesRepository = identity.repositories.userProfiles;
|
|
19
|
+
const userSettingsRepository = identity.repositories.userSettings;
|
|
20
|
+
const avatarStorageService = createAvatarStorageService({ storage });
|
|
21
|
+
const avatarService = createAvatarService({ userProfilesRepository, avatarStorageService });
|
|
22
|
+
const accountProfileService = createAccountProfileService({
|
|
23
|
+
authService,
|
|
24
|
+
avatarService,
|
|
25
|
+
userProfilesRepository,
|
|
26
|
+
userSettingsRepository
|
|
27
|
+
});
|
|
28
|
+
const accountPreferencesService = createAccountPreferencesService({
|
|
29
|
+
authService,
|
|
30
|
+
userProfilesRepository,
|
|
31
|
+
userSettingsRepository
|
|
32
|
+
});
|
|
33
|
+
const accountNotificationsService = createAccountNotificationsService({
|
|
34
|
+
authService,
|
|
35
|
+
userProfilesRepository,
|
|
36
|
+
userSettingsRepository
|
|
37
|
+
});
|
|
38
|
+
const accountSecurityService = createAccountSecurityService({
|
|
39
|
+
authService,
|
|
40
|
+
userProfilesRepository,
|
|
41
|
+
userSettingsRepository
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
return Object.freeze({
|
|
45
|
+
repositories: Object.freeze({
|
|
46
|
+
userProfiles: userProfilesRepository,
|
|
47
|
+
userSettings: userSettingsRepository
|
|
48
|
+
}),
|
|
49
|
+
services: Object.freeze({
|
|
50
|
+
accountNotifications: accountNotificationsService,
|
|
51
|
+
accountPreferences: accountPreferencesService,
|
|
52
|
+
accountProfile: accountProfileService,
|
|
53
|
+
accountSecurity: accountSecurityService,
|
|
54
|
+
avatar: avatarService,
|
|
55
|
+
profileProjector: identity.profileProjector
|
|
56
|
+
}),
|
|
57
|
+
shared: USERS_SHARED_API
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export { createUsersRuntime };
|
|
@@ -1,16 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const apiValidation = String(apiValidationMessage || validation || "Validation failed.");
|
|
1
|
+
/** The messages a resource's operations show when their input fails validation. */
|
|
2
|
+
const OPERATION_MESSAGES = Object.freeze({
|
|
3
|
+
validation: "Validation failed.",
|
|
4
|
+
apiValidation: "Validation failed."
|
|
5
|
+
});
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
validation,
|
|
10
|
-
apiValidation
|
|
11
|
-
});
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export {
|
|
15
|
-
createOperationMessages
|
|
16
|
-
};
|
|
7
|
+
export { OPERATION_MESSAGES };
|
|
@@ -1,36 +1,10 @@
|
|
|
1
1
|
import { createSchema } from "json-rest-schema";
|
|
2
2
|
import { normalizeLowerText, normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
|
|
3
3
|
import { defineCrudResource } from "@jskit-ai/resource-crud-core/shared/crudResource";
|
|
4
|
-
import {
|
|
4
|
+
import { OPERATION_MESSAGES } from "../operationMessages.js";
|
|
5
5
|
import { userProfileOutputSchema } from "./accountSettingsSchemas.js";
|
|
6
6
|
import { userSettingsOutputSchema } from "./userSettingsResource.js";
|
|
7
|
-
|
|
8
|
-
const USERNAME_MAX_LENGTH = 120;
|
|
9
|
-
|
|
10
|
-
function normalizeUsername(value) {
|
|
11
|
-
const normalized = normalizeLowerText(value)
|
|
12
|
-
.replace(/[^a-z0-9]+/g, "-")
|
|
13
|
-
.replace(/^-+|-+$/g, "")
|
|
14
|
-
.slice(0, USERNAME_MAX_LENGTH);
|
|
15
|
-
|
|
16
|
-
return normalized || "";
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function normalizeNullableString(value) {
|
|
20
|
-
if (value === null || value === undefined) {
|
|
21
|
-
return null;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return normalizeText(value);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function normalizeNullableVersion(value) {
|
|
28
|
-
if (value === null || value === undefined || value === "") {
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return String(value);
|
|
33
|
-
}
|
|
7
|
+
import { USERNAME_MAX_LENGTH, normalizeNullableString, normalizeNullableVersion, normalizeUsername } from "./userProfileValues.js";
|
|
34
8
|
|
|
35
9
|
const userProfileBodySchema = createSchema({
|
|
36
10
|
displayName: {
|
|
@@ -75,8 +49,6 @@ const avatarUploadBodySchema = createSchema({
|
|
|
75
49
|
}
|
|
76
50
|
});
|
|
77
51
|
|
|
78
|
-
const USER_PROFILE_OPERATION_MESSAGES = createOperationMessages();
|
|
79
|
-
|
|
80
52
|
const userProfileResource = defineCrudResource({
|
|
81
53
|
namespace: "userProfile",
|
|
82
54
|
tableName: "users",
|
|
@@ -160,7 +132,7 @@ const userProfileResource = defineCrudResource({
|
|
|
160
132
|
}
|
|
161
133
|
}
|
|
162
134
|
},
|
|
163
|
-
messages:
|
|
135
|
+
messages: OPERATION_MESSAGES,
|
|
164
136
|
crudOperations: ["view", "list", "create", "replace", "patch"],
|
|
165
137
|
crud: {
|
|
166
138
|
output: userProfileOutputSchema,
|