acebase-server 1.16.2 → 1.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +180 -10
- package/bin/acebase-server.js +8 -0
- package/build-vars.js +2 -1
- package/dist/cjs/auth.js +88 -88
- package/dist/cjs/cluster.js +6 -56
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.js +24 -24
- package/dist/cjs/logger.js +73 -73
- package/dist/cjs/middleware/404.js +14 -14
- package/dist/cjs/middleware/admin-only.js +28 -28
- package/dist/cjs/middleware/cache.js +14 -14
- package/dist/cjs/middleware/connection.js +49 -49
- package/dist/cjs/middleware/context.js +20 -20
- package/dist/cjs/middleware/cors.js +50 -50
- package/dist/cjs/middleware/swagger.js +17 -17
- package/dist/cjs/middleware/user.js +57 -57
- package/dist/cjs/oauth-providers/dropbox.js +109 -109
- package/dist/cjs/oauth-providers/facebook.js +85 -85
- package/dist/cjs/oauth-providers/google.js +119 -119
- package/dist/cjs/oauth-providers/index.js +26 -26
- package/dist/cjs/oauth-providers/instagram.js +80 -80
- package/dist/cjs/oauth-providers/oauth-provider.js +20 -20
- package/dist/cjs/oauth-providers/spotify.js +113 -113
- package/dist/cjs/routes/auth-change-password.js +87 -87
- package/dist/cjs/routes/auth-delete.js +52 -52
- package/dist/cjs/routes/auth-forgot-password.js +77 -77
- package/dist/cjs/routes/auth-reset-password.js +97 -97
- package/dist/cjs/routes/auth-signin.js +46 -46
- package/dist/cjs/routes/auth-signout.js +61 -61
- package/dist/cjs/routes/auth-signup.js +142 -142
- package/dist/cjs/routes/auth-state.js +25 -25
- package/dist/cjs/routes/auth-update.js +131 -131
- package/dist/cjs/routes/auth-verify-email.js +71 -71
- package/dist/cjs/routes/auth.js +57 -54
- package/dist/cjs/routes/data-exists.js +33 -33
- package/dist/cjs/routes/data-export.js +48 -48
- package/dist/cjs/routes/data-get.js +70 -66
- package/dist/cjs/routes/data-import.js +54 -54
- package/dist/cjs/routes/data-index-create.js +42 -42
- package/dist/cjs/routes/data-index-delete.js +33 -33
- package/dist/cjs/routes/data-index-list.js +31 -31
- package/dist/cjs/routes/data-query.js +80 -70
- package/dist/cjs/routes/data-reflect.js +97 -86
- package/dist/cjs/routes/data-schema-get.js +36 -36
- package/dist/cjs/routes/data-schema-set.js +30 -30
- package/dist/cjs/routes/data-schema-test.js +43 -43
- package/dist/cjs/routes/data-schemas-list.js +32 -32
- package/dist/cjs/routes/data-set.js +85 -75
- package/dist/cjs/routes/data-sync-changes.js +81 -67
- package/dist/cjs/routes/data-sync-mutations.js +81 -67
- package/dist/cjs/routes/data-transaction.js +159 -139
- package/dist/cjs/routes/data-update.js +86 -75
- package/dist/cjs/routes/data.js +62 -62
- package/dist/cjs/routes/docs.js +258 -251
- package/dist/cjs/routes/meta-info.js +63 -63
- package/dist/cjs/routes/meta-logs.js +49 -49
- package/dist/cjs/routes/meta-ping.js +11 -11
- package/dist/cjs/routes/meta-stats.js +27 -27
- package/dist/cjs/routes/meta.js +19 -19
- package/dist/cjs/routes/oauth2-init.js +54 -54
- package/dist/cjs/routes/oauth2-refresh.js +42 -42
- package/dist/cjs/routes/oauth2-signin.js +265 -265
- package/dist/cjs/routes/webmanager.js +26 -26
- package/dist/cjs/rules.js +380 -167
- package/dist/cjs/sandbox.js +28 -0
- package/dist/cjs/schema/user.js +24 -24
- package/dist/cjs/server.js +410 -407
- package/dist/cjs/settings/email.js +2 -2
- package/dist/cjs/settings/index.js +175 -172
- package/dist/cjs/shared/clients.js +26 -26
- package/dist/cjs/shared/email.js +2 -2
- package/dist/cjs/shared/env.js +2 -2
- package/dist/cjs/shared/error.js +33 -33
- package/dist/cjs/shared/http.js +28 -28
- package/dist/cjs/shared/password.js +45 -45
- package/dist/cjs/shared/rootpath.js +16 -16
- package/dist/cjs/shared/signin.js +143 -143
- package/dist/cjs/shared/simple-fetch.js +76 -76
- package/dist/cjs/shared/tokens.js +79 -79
- package/dist/cjs/shared/validate.js +61 -61
- package/dist/cjs/start.js +119 -53
- package/dist/cjs/test.js +10 -10
- package/dist/cjs/websocket/index.js +331 -319
- package/dist/cjs/websocket/manager.js +15 -15
- package/dist/cjs/websocket/socket.io.js +76 -76
- package/dist/esm/auth.js +75 -75
- package/dist/esm/cluster.js +6 -54
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +4 -4
- package/dist/esm/logger.js +51 -51
- package/dist/esm/middleware/404.js +10 -10
- package/dist/esm/middleware/admin-only.js +24 -24
- package/dist/esm/middleware/cache.js +10 -10
- package/dist/esm/middleware/connection.js +45 -45
- package/dist/esm/middleware/context.js +16 -16
- package/dist/esm/middleware/cors.js +44 -44
- package/dist/esm/middleware/swagger.js +13 -13
- package/dist/esm/middleware/user.js +44 -44
- package/dist/esm/oauth-providers/dropbox.js +90 -90
- package/dist/esm/oauth-providers/facebook.js +66 -66
- package/dist/esm/oauth-providers/google.js +96 -96
- package/dist/esm/oauth-providers/index.js +18 -18
- package/dist/esm/oauth-providers/instagram.js +61 -61
- package/dist/esm/oauth-providers/oauth-provider.js +16 -16
- package/dist/esm/oauth-providers/spotify.js +92 -92
- package/dist/esm/routes/auth-change-password.js +72 -72
- package/dist/esm/routes/auth-delete.js +37 -37
- package/dist/esm/routes/auth-forgot-password.js +62 -62
- package/dist/esm/routes/auth-reset-password.js +81 -81
- package/dist/esm/routes/auth-signin.js +33 -33
- package/dist/esm/routes/auth-signout.js +47 -47
- package/dist/esm/routes/auth-signup.js +127 -127
- package/dist/esm/routes/auth-state.js +12 -12
- package/dist/esm/routes/auth-update.js +116 -116
- package/dist/esm/routes/auth-verify-email.js +57 -57
- package/dist/esm/routes/auth.js +53 -50
- package/dist/esm/routes/data-exists.js +20 -20
- package/dist/esm/routes/data-export.js +35 -35
- package/dist/esm/routes/data-get.js +56 -52
- package/dist/esm/routes/data-import.js +41 -41
- package/dist/esm/routes/data-index-create.js +28 -28
- package/dist/esm/routes/data-index-delete.js +20 -20
- package/dist/esm/routes/data-index-list.js +18 -18
- package/dist/esm/routes/data-query.js +66 -56
- package/dist/esm/routes/data-reflect.js +83 -72
- package/dist/esm/routes/data-schema-get.js +23 -23
- package/dist/esm/routes/data-schema-set.js +17 -17
- package/dist/esm/routes/data-schema-test.js +29 -29
- package/dist/esm/routes/data-schemas-list.js +19 -19
- package/dist/esm/routes/data-set.js +70 -60
- package/dist/esm/routes/data-sync-changes.js +67 -53
- package/dist/esm/routes/data-sync-mutations.js +67 -53
- package/dist/esm/routes/data-transaction.js +142 -123
- package/dist/esm/routes/data-update.js +71 -60
- package/dist/esm/routes/data.js +58 -58
- package/dist/esm/routes/docs.js +252 -245
- package/dist/esm/routes/meta-info.js +59 -59
- package/dist/esm/routes/meta-logs.js +35 -35
- package/dist/esm/routes/meta-ping.js +7 -7
- package/dist/esm/routes/meta-stats.js +14 -14
- package/dist/esm/routes/meta.js +15 -15
- package/dist/esm/routes/oauth2-init.js +41 -41
- package/dist/esm/routes/oauth2-refresh.js +29 -29
- package/dist/esm/routes/oauth2-signin.js +251 -251
- package/dist/esm/routes/webmanager.js +22 -22
- package/dist/esm/rules.js +374 -163
- package/dist/esm/sandbox.js +12 -0
- package/dist/esm/schema/user.js +19 -19
- package/dist/esm/server.js +395 -387
- package/dist/esm/settings/email.js +1 -1
- package/dist/esm/settings/index.js +168 -165
- package/dist/esm/shared/clients.js +22 -22
- package/dist/esm/shared/email.js +1 -1
- package/dist/esm/shared/env.js +1 -1
- package/dist/esm/shared/error.js +25 -25
- package/dist/esm/shared/http.js +22 -22
- package/dist/esm/shared/password.js +38 -38
- package/dist/esm/shared/rootpath.js +13 -13
- package/dist/esm/shared/signin.js +128 -128
- package/dist/esm/shared/simple-fetch.js +59 -59
- package/dist/esm/shared/tokens.js +72 -72
- package/dist/esm/shared/validate.js +41 -41
- package/dist/esm/start.js +116 -50
- package/dist/esm/test.js +8 -8
- package/dist/esm/websocket/index.js +313 -302
- package/dist/esm/websocket/manager.js +11 -11
- package/dist/esm/websocket/socket.io.js +71 -71
- package/dist/types/auth.d.ts +3 -3
- package/dist/types/cluster.d.ts +6 -1
- package/dist/types/index.d.ts +4 -4
- package/dist/types/logger.d.ts +35 -35
- package/dist/types/middleware/404.d.ts +7 -7
- package/dist/types/middleware/admin-only.d.ts +19 -19
- package/dist/types/middleware/cache.d.ts +3 -3
- package/dist/types/middleware/connection.d.ts +7 -7
- package/dist/types/middleware/context.d.ts +3 -3
- package/dist/types/middleware/cors.d.ts +25 -25
- package/dist/types/middleware/swagger.d.ts +3 -3
- package/dist/types/middleware/user.d.ts +2 -2
- package/dist/types/oauth-providers/dropbox.d.ts +52 -52
- package/dist/types/oauth-providers/facebook.d.ts +41 -41
- package/dist/types/oauth-providers/google.d.ts +43 -43
- package/dist/types/oauth-providers/index.d.ts +10 -10
- package/dist/types/oauth-providers/instagram.d.ts +50 -50
- package/dist/types/oauth-providers/oauth-provider.d.ts +74 -74
- package/dist/types/oauth-providers/spotify.d.ts +53 -53
- package/dist/types/routes/auth-change-password.d.ts +20 -20
- package/dist/types/routes/auth-delete.d.ts +16 -16
- package/dist/types/routes/auth-forgot-password.d.ts +16 -16
- package/dist/types/routes/auth-reset-password.d.ts +23 -23
- package/dist/types/routes/auth-signin.d.ts +24 -24
- package/dist/types/routes/auth-signout.d.ts +14 -14
- package/dist/types/routes/auth-signup.d.ts +32 -32
- package/dist/types/routes/auth-state.d.ts +13 -13
- package/dist/types/routes/auth-update.d.ts +33 -33
- package/dist/types/routes/auth-verify-email.d.ts +21 -21
- package/dist/types/routes/auth.d.ts +6 -6
- package/dist/types/routes/data-exists.d.ts +9 -9
- package/dist/types/routes/data-export.d.ts +10 -10
- package/dist/types/routes/data-get.d.ts +14 -14
- package/dist/types/routes/data-import.d.ts +13 -13
- package/dist/types/routes/data-index-create.d.ts +51 -51
- package/dist/types/routes/data-index-delete.d.ts +17 -17
- package/dist/types/routes/data-index-list.d.ts +21 -21
- package/dist/types/routes/data-query.d.ts +49 -49
- package/dist/types/routes/data-reflect.d.ts +29 -29
- package/dist/types/routes/data-schema-get.d.ts +11 -11
- package/dist/types/routes/data-schema-set.d.ts +20 -19
- package/dist/types/routes/data-schema-test.d.ts +19 -19
- package/dist/types/routes/data-schemas-list.d.ts +11 -11
- package/dist/types/routes/data-set.d.ts +27 -27
- package/dist/types/routes/data-sync-changes.d.ts +25 -28
- package/dist/types/routes/data-sync-mutations.d.ts +25 -28
- package/dist/types/routes/data-transaction.d.ts +34 -34
- package/dist/types/routes/data-update.d.ts +27 -27
- package/dist/types/routes/data.d.ts +3 -3
- package/dist/types/routes/docs.d.ts +3 -3
- package/dist/types/routes/meta-info.d.ts +11 -11
- package/dist/types/routes/meta-logs.d.ts +20 -20
- package/dist/types/routes/meta-ping.d.ts +7 -7
- package/dist/types/routes/meta-stats.d.ts +13 -13
- package/dist/types/routes/meta.d.ts +3 -3
- package/dist/types/routes/oauth2-init.d.ts +13 -13
- package/dist/types/routes/oauth2-refresh.d.ts +17 -17
- package/dist/types/routes/oauth2-signin.d.ts +15 -15
- package/dist/types/routes/webmanager.d.ts +3 -3
- package/dist/types/rules.d.ts +73 -34
- package/dist/types/sandbox.d.ts +3 -0
- package/dist/types/schema/user.d.ts +85 -85
- package/dist/types/server.d.ts +117 -111
- package/dist/types/settings/email.d.ts +14 -14
- package/dist/types/settings/index.d.ts +195 -187
- package/dist/types/shared/clients.d.ts +46 -46
- package/dist/types/shared/email.d.ts +34 -34
- package/dist/types/shared/env.d.ts +47 -44
- package/dist/types/shared/error.d.ts +15 -15
- package/dist/types/shared/http.d.ts +22 -21
- package/dist/types/shared/password.d.ts +19 -19
- package/dist/types/shared/rootpath.d.ts +2 -2
- package/dist/types/shared/signin.d.ts +31 -31
- package/dist/types/shared/simple-fetch.d.ts +21 -21
- package/dist/types/shared/tokens.d.ts +24 -24
- package/dist/types/shared/validate.d.ts +22 -22
- package/dist/types/start.d.ts +1 -1
- package/dist/types/test.d.ts +1 -1
- package/dist/types/websocket/index.d.ts +6 -6
- package/dist/types/websocket/manager.d.ts +99 -99
- package/dist/types/websocket/socket.io.d.ts +10 -10
- package/package.json +7 -4
- package/src/routes/data-schema-set.yaml +4 -0
- package/src/routes/data-set.yaml +5 -5
- package/src/routes/data-update.yaml +4 -4
|
@@ -1,117 +1,117 @@
|
|
|
1
|
-
import { getPublicAccountDetails } from '../schema/user.js';
|
|
2
|
-
import { emailExistsError, invalidDisplayNameError, invalidEmailError, invalidPictureError, invalidSettingsError, invalidUsernameError, isValidDisplayName, isValidEmail, isValidNewEmailAddress, isValidNewUsername, isValidPicture, isValidSettings, isValidUsername, usernameExistsError } from '../shared/validate.js';
|
|
3
|
-
import { sendNotAuthenticatedError, sendUnauthorizedError, sendUnexpectedError } from '../shared/error.js';
|
|
4
|
-
export class UpdateError extends Error {
|
|
5
|
-
constructor(code, message) {
|
|
6
|
-
super(message);
|
|
7
|
-
this.code = code;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
export const addRoute = (env) => {
|
|
11
|
-
env.
|
|
12
|
-
const details = req.body;
|
|
13
|
-
const LOG_ACTION = 'auth.update';
|
|
14
|
-
const LOG_DETAILS = { ip: req.ip, uid: req.user?.uid ?? null, update_uid: details.uid ?? null };
|
|
15
|
-
if (!req.user) {
|
|
16
|
-
env.log.error(LOG_ACTION, 'unauthenticated_update', LOG_DETAILS);
|
|
17
|
-
return sendNotAuthenticatedError(res, 'unauthenticated_update', 'Sign in to change details');
|
|
18
|
-
}
|
|
19
|
-
const uid = details.uid || req.user.uid;
|
|
20
|
-
if (req.user.uid !== 'admin' && (uid !== req.user.uid || typeof details.is_disabled === 'boolean')) {
|
|
21
|
-
env.log.error(LOG_ACTION, 'unauthorized_update', LOG_DETAILS);
|
|
22
|
-
return sendUnauthorizedError(res, 'unauthorized_update', 'You are not authorized to perform this update. This attempt has been logged.');
|
|
23
|
-
}
|
|
24
|
-
if (typeof details.display_name === 'undefined' && typeof details.displayName === 'string') {
|
|
25
|
-
// Allow displayName to be sent also (which is used in signup endpoint)
|
|
26
|
-
details.display_name = details.displayName;
|
|
27
|
-
}
|
|
28
|
-
// Check if sent details are ok
|
|
29
|
-
let err;
|
|
30
|
-
if (details.email && !isValidEmail(details.email)) {
|
|
31
|
-
err = invalidEmailError;
|
|
32
|
-
}
|
|
33
|
-
else if (details.email && !await isValidNewEmailAddress(env.authRef, details.email)) {
|
|
34
|
-
err = emailExistsError;
|
|
35
|
-
}
|
|
36
|
-
else if (details.username && !isValidUsername(details.username)) {
|
|
37
|
-
err = invalidUsernameError;
|
|
38
|
-
}
|
|
39
|
-
else if (details.username && !await isValidNewUsername(env.authRef, details.username)) {
|
|
40
|
-
err = usernameExistsError;
|
|
41
|
-
}
|
|
42
|
-
else if (details.display_name && !isValidDisplayName(details.display_name)) {
|
|
43
|
-
err = invalidDisplayNameError;
|
|
44
|
-
}
|
|
45
|
-
else if (details.picture && !isValidPicture(details.picture)) {
|
|
46
|
-
err = invalidPictureError;
|
|
47
|
-
}
|
|
48
|
-
else if (!isValidSettings(details.settings)) {
|
|
49
|
-
err = invalidSettingsError;
|
|
50
|
-
}
|
|
51
|
-
if (err) {
|
|
52
|
-
// Log failure
|
|
53
|
-
env.log.error(LOG_ACTION, err.code, LOG_DETAILS);
|
|
54
|
-
res.status(422).send(err); // Unprocessable Entity
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
try {
|
|
58
|
-
let user;
|
|
59
|
-
await env.authRef.child(uid).transaction(snap => {
|
|
60
|
-
if (!snap.exists()) {
|
|
61
|
-
throw new UpdateError('user_not_found', `No user found with uid ${uid}`);
|
|
62
|
-
}
|
|
63
|
-
user = snap.val();
|
|
64
|
-
if (details.email && details.email !== user.email) {
|
|
65
|
-
user.email = details.email;
|
|
66
|
-
user.email_verified = false; // TODO: send verification email
|
|
67
|
-
}
|
|
68
|
-
if (details.username) {
|
|
69
|
-
user.username = details.username;
|
|
70
|
-
}
|
|
71
|
-
if (details.display_name) {
|
|
72
|
-
user.display_name = details.display_name;
|
|
73
|
-
}
|
|
74
|
-
if (details.picture) {
|
|
75
|
-
user.picture = details.picture;
|
|
76
|
-
}
|
|
77
|
-
if (details.settings) {
|
|
78
|
-
if (typeof user.settings !== 'object') {
|
|
79
|
-
user.settings = {};
|
|
80
|
-
}
|
|
81
|
-
Object.keys(details.settings).forEach(key => {
|
|
82
|
-
user.settings[key] = details.settings[key];
|
|
83
|
-
});
|
|
84
|
-
if (!isValidSettings(user.settings)) {
|
|
85
|
-
err = invalidSettingsError;
|
|
86
|
-
env.log.error(LOG_ACTION, 'too_many_settings', LOG_DETAILS);
|
|
87
|
-
res.statusCode = 422; // Unprocessable Entity
|
|
88
|
-
res.send(err);
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
if (typeof details.is_disabled === 'boolean') {
|
|
93
|
-
user.is_disabled = details.is_disabled;
|
|
94
|
-
}
|
|
95
|
-
return user; // Update db user
|
|
96
|
-
});
|
|
97
|
-
// Update cache
|
|
98
|
-
env.authCache.set(user.uid, user);
|
|
99
|
-
// Send merged results back
|
|
100
|
-
res.send({ user: getPublicAccountDetails(user) });
|
|
101
|
-
}
|
|
102
|
-
catch (err) {
|
|
103
|
-
// All known errors except user_not_found will have been sent already
|
|
104
|
-
if (err.code === 'user_not_found') {
|
|
105
|
-
env.log.error(LOG_ACTION, err.code, LOG_DETAILS);
|
|
106
|
-
res.statusCode = 404; // Not Found
|
|
107
|
-
res.send(err);
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
// Unexpected
|
|
111
|
-
env.log.error(LOG_ACTION, err.code ?? 'unexpected', LOG_DETAILS, err);
|
|
112
|
-
sendUnexpectedError(res, err);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
};
|
|
1
|
+
import { getPublicAccountDetails } from '../schema/user.js';
|
|
2
|
+
import { emailExistsError, invalidDisplayNameError, invalidEmailError, invalidPictureError, invalidSettingsError, invalidUsernameError, isValidDisplayName, isValidEmail, isValidNewEmailAddress, isValidNewUsername, isValidPicture, isValidSettings, isValidUsername, usernameExistsError } from '../shared/validate.js';
|
|
3
|
+
import { sendNotAuthenticatedError, sendUnauthorizedError, sendUnexpectedError } from '../shared/error.js';
|
|
4
|
+
export class UpdateError extends Error {
|
|
5
|
+
constructor(code, message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.code = code;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export const addRoute = (env) => {
|
|
11
|
+
env.router.post(`/auth/${env.db.name}/update`, async (req, res) => {
|
|
12
|
+
const details = req.body;
|
|
13
|
+
const LOG_ACTION = 'auth.update';
|
|
14
|
+
const LOG_DETAILS = { ip: req.ip, uid: req.user?.uid ?? null, update_uid: details.uid ?? null };
|
|
15
|
+
if (!req.user) {
|
|
16
|
+
env.log.error(LOG_ACTION, 'unauthenticated_update', LOG_DETAILS);
|
|
17
|
+
return sendNotAuthenticatedError(res, 'unauthenticated_update', 'Sign in to change details');
|
|
18
|
+
}
|
|
19
|
+
const uid = details.uid || req.user.uid;
|
|
20
|
+
if (req.user.uid !== 'admin' && (uid !== req.user.uid || typeof details.is_disabled === 'boolean')) {
|
|
21
|
+
env.log.error(LOG_ACTION, 'unauthorized_update', LOG_DETAILS);
|
|
22
|
+
return sendUnauthorizedError(res, 'unauthorized_update', 'You are not authorized to perform this update. This attempt has been logged.');
|
|
23
|
+
}
|
|
24
|
+
if (typeof details.display_name === 'undefined' && typeof details.displayName === 'string') {
|
|
25
|
+
// Allow displayName to be sent also (which is used in signup endpoint)
|
|
26
|
+
details.display_name = details.displayName;
|
|
27
|
+
}
|
|
28
|
+
// Check if sent details are ok
|
|
29
|
+
let err;
|
|
30
|
+
if (details.email && !isValidEmail(details.email)) {
|
|
31
|
+
err = invalidEmailError;
|
|
32
|
+
}
|
|
33
|
+
else if (details.email && !await isValidNewEmailAddress(env.authRef, details.email)) {
|
|
34
|
+
err = emailExistsError;
|
|
35
|
+
}
|
|
36
|
+
else if (details.username && !isValidUsername(details.username)) {
|
|
37
|
+
err = invalidUsernameError;
|
|
38
|
+
}
|
|
39
|
+
else if (details.username && !await isValidNewUsername(env.authRef, details.username)) {
|
|
40
|
+
err = usernameExistsError;
|
|
41
|
+
}
|
|
42
|
+
else if (details.display_name && !isValidDisplayName(details.display_name)) {
|
|
43
|
+
err = invalidDisplayNameError;
|
|
44
|
+
}
|
|
45
|
+
else if (details.picture && !isValidPicture(details.picture)) {
|
|
46
|
+
err = invalidPictureError;
|
|
47
|
+
}
|
|
48
|
+
else if (!isValidSettings(details.settings)) {
|
|
49
|
+
err = invalidSettingsError;
|
|
50
|
+
}
|
|
51
|
+
if (err) {
|
|
52
|
+
// Log failure
|
|
53
|
+
env.log.error(LOG_ACTION, err.code, LOG_DETAILS);
|
|
54
|
+
res.status(422).send(err); // Unprocessable Entity
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
let user;
|
|
59
|
+
await env.authRef.child(uid).transaction(snap => {
|
|
60
|
+
if (!snap.exists()) {
|
|
61
|
+
throw new UpdateError('user_not_found', `No user found with uid ${uid}`);
|
|
62
|
+
}
|
|
63
|
+
user = snap.val();
|
|
64
|
+
if (details.email && details.email !== user.email) {
|
|
65
|
+
user.email = details.email;
|
|
66
|
+
user.email_verified = false; // TODO: send verification email
|
|
67
|
+
}
|
|
68
|
+
if (details.username) {
|
|
69
|
+
user.username = details.username;
|
|
70
|
+
}
|
|
71
|
+
if (details.display_name) {
|
|
72
|
+
user.display_name = details.display_name;
|
|
73
|
+
}
|
|
74
|
+
if (details.picture) {
|
|
75
|
+
user.picture = details.picture;
|
|
76
|
+
}
|
|
77
|
+
if (details.settings) {
|
|
78
|
+
if (typeof user.settings !== 'object') {
|
|
79
|
+
user.settings = {};
|
|
80
|
+
}
|
|
81
|
+
Object.keys(details.settings).forEach(key => {
|
|
82
|
+
user.settings[key] = details.settings[key];
|
|
83
|
+
});
|
|
84
|
+
if (!isValidSettings(user.settings)) {
|
|
85
|
+
err = invalidSettingsError;
|
|
86
|
+
env.log.error(LOG_ACTION, 'too_many_settings', LOG_DETAILS);
|
|
87
|
+
res.statusCode = 422; // Unprocessable Entity
|
|
88
|
+
res.send(err);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (typeof details.is_disabled === 'boolean') {
|
|
93
|
+
user.is_disabled = details.is_disabled;
|
|
94
|
+
}
|
|
95
|
+
return user; // Update db user
|
|
96
|
+
});
|
|
97
|
+
// Update cache
|
|
98
|
+
env.authCache.set(user.uid, user);
|
|
99
|
+
// Send merged results back
|
|
100
|
+
res.send({ user: getPublicAccountDetails(user) });
|
|
101
|
+
}
|
|
102
|
+
catch (err) {
|
|
103
|
+
// All known errors except user_not_found will have been sent already
|
|
104
|
+
if (err.code === 'user_not_found') {
|
|
105
|
+
env.log.error(LOG_ACTION, err.code, LOG_DETAILS);
|
|
106
|
+
res.statusCode = 404; // Not Found
|
|
107
|
+
res.send(err);
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
// Unexpected
|
|
111
|
+
env.log.error(LOG_ACTION, err.code ?? 'unexpected', LOG_DETAILS, err);
|
|
112
|
+
sendUnexpectedError(res, err);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
117
|
//# sourceMappingURL=auth-update.js.map
|
|
@@ -1,58 +1,58 @@
|
|
|
1
|
-
import { parseSignedPublicToken } from '../shared/tokens.js';
|
|
2
|
-
import { sendBadRequestError, sendUnexpectedError } from '../shared/error.js';
|
|
3
|
-
export class VerifyEmailError extends Error {
|
|
4
|
-
constructor(code, message) {
|
|
5
|
-
super(message);
|
|
6
|
-
this.code = code;
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* Adds the verify_email route and returns the verification function that can be used to manually verify an email address
|
|
11
|
-
* @param env environment
|
|
12
|
-
* @returns returns the verification function
|
|
13
|
-
*/
|
|
14
|
-
export const addRoute = (env) => {
|
|
15
|
-
const LOG_ACTION = 'auth.verify_email';
|
|
16
|
-
const verifyEmailAddress = async (clientIp, code) => {
|
|
17
|
-
const LOG_DETAILS = { ip: clientIp, uid: null };
|
|
18
|
-
const verification = (() => {
|
|
19
|
-
try {
|
|
20
|
-
return parseSignedPublicToken(code, env.tokenSalt);
|
|
21
|
-
}
|
|
22
|
-
catch (err) {
|
|
23
|
-
throw new VerifyEmailError('invalid_code', err.message);
|
|
24
|
-
}
|
|
25
|
-
})();
|
|
26
|
-
LOG_DETAILS.uid = verification.uid;
|
|
27
|
-
const snap = await env.authRef.child(verification.uid).get();
|
|
28
|
-
if (!snap.exists()) {
|
|
29
|
-
env.log.error(LOG_ACTION, 'unknown_user', LOG_DETAILS);
|
|
30
|
-
throw new VerifyEmailError('unknown_user', 'Unknown user');
|
|
31
|
-
}
|
|
32
|
-
const user = snap.val();
|
|
33
|
-
user.uid = snap.key;
|
|
34
|
-
// No need to do further checks, code was signed by us so we can trust the contents
|
|
35
|
-
// Mark account as verified
|
|
36
|
-
await snap.ref.update({ email_verified: true });
|
|
37
|
-
env.log.event(LOG_ACTION, LOG_DETAILS);
|
|
38
|
-
};
|
|
39
|
-
env.
|
|
40
|
-
const details = req.body;
|
|
41
|
-
try {
|
|
42
|
-
await verifyEmailAddress(req.ip, details.code);
|
|
43
|
-
res.send('OK');
|
|
44
|
-
}
|
|
45
|
-
catch (err) {
|
|
46
|
-
if (err.code) {
|
|
47
|
-
sendBadRequestError(res, err);
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
env.log.error(LOG_ACTION, 'unexpected', { ip: req.ip, message: err.message, verificaion_code: details.code });
|
|
51
|
-
sendUnexpectedError(res, err);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
return verifyEmailAddress;
|
|
56
|
-
};
|
|
57
|
-
export default addRoute;
|
|
1
|
+
import { parseSignedPublicToken } from '../shared/tokens.js';
|
|
2
|
+
import { sendBadRequestError, sendUnexpectedError } from '../shared/error.js';
|
|
3
|
+
export class VerifyEmailError extends Error {
|
|
4
|
+
constructor(code, message) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.code = code;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Adds the verify_email route and returns the verification function that can be used to manually verify an email address
|
|
11
|
+
* @param env environment
|
|
12
|
+
* @returns returns the verification function
|
|
13
|
+
*/
|
|
14
|
+
export const addRoute = (env) => {
|
|
15
|
+
const LOG_ACTION = 'auth.verify_email';
|
|
16
|
+
const verifyEmailAddress = async (clientIp, code) => {
|
|
17
|
+
const LOG_DETAILS = { ip: clientIp, uid: null };
|
|
18
|
+
const verification = (() => {
|
|
19
|
+
try {
|
|
20
|
+
return parseSignedPublicToken(code, env.tokenSalt);
|
|
21
|
+
}
|
|
22
|
+
catch (err) {
|
|
23
|
+
throw new VerifyEmailError('invalid_code', err.message);
|
|
24
|
+
}
|
|
25
|
+
})();
|
|
26
|
+
LOG_DETAILS.uid = verification.uid;
|
|
27
|
+
const snap = await env.authRef.child(verification.uid).get();
|
|
28
|
+
if (!snap.exists()) {
|
|
29
|
+
env.log.error(LOG_ACTION, 'unknown_user', LOG_DETAILS);
|
|
30
|
+
throw new VerifyEmailError('unknown_user', 'Unknown user');
|
|
31
|
+
}
|
|
32
|
+
const user = snap.val();
|
|
33
|
+
user.uid = snap.key;
|
|
34
|
+
// No need to do further checks, code was signed by us so we can trust the contents
|
|
35
|
+
// Mark account as verified
|
|
36
|
+
await snap.ref.update({ email_verified: true });
|
|
37
|
+
env.log.event(LOG_ACTION, LOG_DETAILS);
|
|
38
|
+
};
|
|
39
|
+
env.router.post(`/auth/${env.db.name}/verify_email`, async (req, res) => {
|
|
40
|
+
const details = req.body;
|
|
41
|
+
try {
|
|
42
|
+
await verifyEmailAddress(req.ip, details.code);
|
|
43
|
+
res.send('OK');
|
|
44
|
+
}
|
|
45
|
+
catch (err) {
|
|
46
|
+
if (err.code) {
|
|
47
|
+
sendBadRequestError(res, err);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
env.log.error(LOG_ACTION, 'unexpected', { ip: req.ip, message: err.message, verificaion_code: details.code });
|
|
51
|
+
sendUnexpectedError(res, err);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
return verifyEmailAddress;
|
|
56
|
+
};
|
|
57
|
+
export default addRoute;
|
|
58
58
|
//# sourceMappingURL=auth-verify-email.js.map
|
package/dist/esm/routes/auth.js
CHANGED
|
@@ -1,51 +1,54 @@
|
|
|
1
|
-
import { addMiddleware as addBearerAuthMiddleware } from '../middleware/user.js';
|
|
2
|
-
import { addRoute as addStateRoute } from './auth-state.js';
|
|
3
|
-
import { addRoute as addSignInRoute } from './auth-signin.js';
|
|
4
|
-
import { addRoute as addSignOutRoute } from './auth-signout.js';
|
|
5
|
-
import { addRoute as addVerifyEmailRoute } from './auth-verify-email.js';
|
|
6
|
-
import { addRoute as
|
|
7
|
-
import { addRoute as
|
|
8
|
-
import { addRoute as
|
|
9
|
-
import { addRoute as
|
|
10
|
-
import { addRoute as
|
|
11
|
-
import { addRoute as
|
|
12
|
-
import { addRoute as
|
|
13
|
-
import { addRoute as
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
1
|
+
import { addMiddleware as addBearerAuthMiddleware } from '../middleware/user.js';
|
|
2
|
+
import { addRoute as addStateRoute } from './auth-state.js';
|
|
3
|
+
import { addRoute as addSignInRoute } from './auth-signin.js';
|
|
4
|
+
import { addRoute as addSignOutRoute } from './auth-signout.js';
|
|
5
|
+
import { addRoute as addVerifyEmailRoute } from './auth-verify-email.js';
|
|
6
|
+
import { addRoute as addForgotPasswordRoute } from './auth-forgot-password.js';
|
|
7
|
+
import { addRoute as addResetPasswordRoute } from './auth-reset-password.js';
|
|
8
|
+
import { addRoute as addChangePasswordRoute } from './auth-change-password.js';
|
|
9
|
+
import { addRoute as addSignUpRoute } from './auth-signup.js';
|
|
10
|
+
import { addRoute as addUpdateRoute } from './auth-update.js';
|
|
11
|
+
import { addRoute as addDeleteRoute } from './auth-delete.js';
|
|
12
|
+
import { addRoute as addOAuth2InitRoute } from './oauth2-init.js';
|
|
13
|
+
import { addRoute as addOAuth2SignInRoute } from './oauth2-signin.js';
|
|
14
|
+
import { addRoute as addOAuth2RefreshRoute } from './oauth2-refresh.js';
|
|
15
|
+
export const addAuthenticionRoutes = (env) => {
|
|
16
|
+
if (!env.config.auth.enabled) {
|
|
17
|
+
throw new Error('Authentication not enabled in the server settings');
|
|
18
|
+
}
|
|
19
|
+
// Bearer authentication middleware
|
|
20
|
+
addBearerAuthMiddleware(env);
|
|
21
|
+
// Auth state endpoint
|
|
22
|
+
addStateRoute(env);
|
|
23
|
+
// signin endpoint
|
|
24
|
+
addSignInRoute(env);
|
|
25
|
+
// signout endpoint
|
|
26
|
+
addSignOutRoute(env);
|
|
27
|
+
// verify email endpoint
|
|
28
|
+
const verifyEmailAddress = addVerifyEmailRoute(env);
|
|
29
|
+
// forgot password endpoint (issue password reset)
|
|
30
|
+
addForgotPasswordRoute(env);
|
|
31
|
+
// reset password endpoint (finish password reset)
|
|
32
|
+
const resetPassword = addResetPasswordRoute(env);
|
|
33
|
+
// change password endpoint
|
|
34
|
+
addChangePasswordRoute(env);
|
|
35
|
+
// signup endpoint
|
|
36
|
+
addSignUpRoute(env);
|
|
37
|
+
// update enpoint
|
|
38
|
+
addUpdateRoute(env);
|
|
39
|
+
// delete endpoint
|
|
40
|
+
addDeleteRoute(env);
|
|
41
|
+
// OAuth2 init endpoint
|
|
42
|
+
addOAuth2InitRoute(env);
|
|
43
|
+
// OAuth2 signin endpoint
|
|
44
|
+
addOAuth2SignInRoute(env);
|
|
45
|
+
// OAuth2 token refresh endpoint
|
|
46
|
+
addOAuth2RefreshRoute(env);
|
|
47
|
+
// Return auth functions that can be used directly through an AceBaseServer instance
|
|
48
|
+
return {
|
|
49
|
+
verifyEmailAddress,
|
|
50
|
+
resetPassword,
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
export default addAuthenticionRoutes;
|
|
51
54
|
//# sourceMappingURL=auth.js.map
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import { sendUnauthorizedError } from '../shared/error.js';
|
|
2
|
-
export const addRoute = (env) => {
|
|
3
|
-
env.
|
|
4
|
-
// Exists query
|
|
5
|
-
const path = req.path.slice(env.db.name.length + 9);
|
|
6
|
-
const access = env.rules.
|
|
7
|
-
if (!access.allow) {
|
|
8
|
-
return sendUnauthorizedError(res, access.code, access.message);
|
|
9
|
-
}
|
|
10
|
-
try {
|
|
11
|
-
const exists = await env.db.ref(path).exists();
|
|
12
|
-
res.send({ exists });
|
|
13
|
-
}
|
|
14
|
-
catch (err) {
|
|
15
|
-
res.statusCode = 500;
|
|
16
|
-
res.send(err);
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
};
|
|
20
|
-
export default addRoute;
|
|
1
|
+
import { sendUnauthorizedError } from '../shared/error.js';
|
|
2
|
+
export const addRoute = (env) => {
|
|
3
|
+
env.router.get(`/exists/${env.db.name}/*`, async (req, res) => {
|
|
4
|
+
// Exists query
|
|
5
|
+
const path = req.path.slice(env.db.name.length + 9);
|
|
6
|
+
const access = await env.rules.isOperationAllowed(req.user, path, 'exists', { context: req.context });
|
|
7
|
+
if (!access.allow) {
|
|
8
|
+
return sendUnauthorizedError(res, access.code, access.message);
|
|
9
|
+
}
|
|
10
|
+
try {
|
|
11
|
+
const exists = await env.db.ref(path).exists();
|
|
12
|
+
res.send({ exists });
|
|
13
|
+
}
|
|
14
|
+
catch (err) {
|
|
15
|
+
res.statusCode = 500;
|
|
16
|
+
res.send(err);
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
export default addRoute;
|
|
21
21
|
//# sourceMappingURL=data-exists.js.map
|
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
import { sendUnauthorizedError } from '../shared/error.js';
|
|
2
|
-
export const addRoute = (env) => {
|
|
3
|
-
env.
|
|
4
|
-
// Export API
|
|
5
|
-
const path = req.path.slice(env.db.name.length + 9);
|
|
6
|
-
const access = env.rules.
|
|
7
|
-
if (!access.allow) {
|
|
8
|
-
return sendUnauthorizedError(res, access.code, access.message);
|
|
9
|
-
}
|
|
10
|
-
const format = req.query.format || 'json';
|
|
11
|
-
const type_safe = req.query.type_safe !== '0';
|
|
12
|
-
const write = async (chunk) => {
|
|
13
|
-
const ok = res.write(chunk);
|
|
14
|
-
if (!ok) {
|
|
15
|
-
await new Promise(resolve => res.once('drain', resolve));
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
const ref = env.db.ref(path);
|
|
19
|
-
res.setHeader('Content-Disposition', `attachment; filename=${ref.key || 'export'}.json`); // Will be treated as a download in browser
|
|
20
|
-
try {
|
|
21
|
-
await ref.export(write, { format, type_safe });
|
|
22
|
-
}
|
|
23
|
-
catch (err) {
|
|
24
|
-
env.debug.error(`Error exporting data for path "/${path}": `, err);
|
|
25
|
-
if (!res.headersSent) {
|
|
26
|
-
res.statusCode = 500;
|
|
27
|
-
res.send(err);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
finally {
|
|
31
|
-
res.end();
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
};
|
|
35
|
-
export default addRoute;
|
|
1
|
+
import { sendUnauthorizedError } from '../shared/error.js';
|
|
2
|
+
export const addRoute = (env) => {
|
|
3
|
+
env.router.get(`/export/${env.db.name}/*`, async (req, res) => {
|
|
4
|
+
// Export API
|
|
5
|
+
const path = req.path.slice(env.db.name.length + 9);
|
|
6
|
+
const access = await env.rules.isOperationAllowed(req.user, path, 'export', { context: req.context });
|
|
7
|
+
if (!access.allow) {
|
|
8
|
+
return sendUnauthorizedError(res, access.code, access.message);
|
|
9
|
+
}
|
|
10
|
+
const format = req.query.format || 'json';
|
|
11
|
+
const type_safe = req.query.type_safe !== '0';
|
|
12
|
+
const write = async (chunk) => {
|
|
13
|
+
const ok = res.write(chunk);
|
|
14
|
+
if (!ok) {
|
|
15
|
+
await new Promise(resolve => res.once('drain', resolve));
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
const ref = env.db.ref(path);
|
|
19
|
+
res.setHeader('Content-Disposition', `attachment; filename=${ref.key || 'export'}.json`); // Will be treated as a download in browser
|
|
20
|
+
try {
|
|
21
|
+
await ref.export(write, { format, type_safe });
|
|
22
|
+
}
|
|
23
|
+
catch (err) {
|
|
24
|
+
env.debug.error(`Error exporting data for path "/${path}": `, err);
|
|
25
|
+
if (!res.headersSent) {
|
|
26
|
+
res.statusCode = 500;
|
|
27
|
+
res.send(err);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
finally {
|
|
31
|
+
res.end();
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
export default addRoute;
|
|
36
36
|
//# sourceMappingURL=data-export.js.map
|