acebase-server 1.16.2 → 1.17.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/build-vars.js +2 -1
- package/dist/cjs/auth.js +88 -88
- package/dist/cjs/cluster.js +56 -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 +415 -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 +53 -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 +54 -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 +50 -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 +1 -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 +19 -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 +4 -4
- package/src/routes/data-set.yaml +5 -5
- package/src/routes/data-update.yaml +4 -4
|
@@ -1,266 +1,266 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.addRoute = void 0;
|
|
13
|
-
const acebase_core_1 = require("acebase-core");
|
|
14
|
-
const password_1 = require("../shared/password");
|
|
15
|
-
const tokens_1 = require("../shared/tokens");
|
|
16
|
-
const simple_fetch_1 = require("../shared/simple-fetch");
|
|
17
|
-
const socketSignInSuccess = `<html><script>window.close()</script><body>Signed in succesfully. You can <a href="javascript:window.close()">close</a> this page</body></html>`;
|
|
18
|
-
const socketSignInFailed = `<html><script>window.close()</script><body>Failed to sign in. You can <a href="javascript:window.close()">close</a> this page</body></html>`;
|
|
19
|
-
const addRoute = (env) => {
|
|
20
|
-
env.
|
|
21
|
-
// This is where the user is redirected to by the provider after signin or error
|
|
22
|
-
var _a, _b, _c;
|
|
23
|
-
const LOG_ACTION = 'oauth2.signin';
|
|
24
|
-
const LOG_DETAILS = { ip: req.ip, provider: null };
|
|
25
|
-
try {
|
|
26
|
-
const state = (0, tokens_1.parseSignedPublicToken)(req.query.state, env.tokenSalt);
|
|
27
|
-
if (req.query.error) {
|
|
28
|
-
if (state.flow === 'socket') {
|
|
29
|
-
const client = env.clients.get(state.client_id);
|
|
30
|
-
client.socket.emit('oauth2-signin', { error: req.query.error, reason: req.query.error_reason, description: req.query.error_description, provider: state.provider });
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
const callbackUrl = `${state.callbackUrl}?provider=${state.provider}&error=${req.query.error}&reason=${req.query.error_reason}&description=${req.query.error_description}`;
|
|
34
|
-
res.redirect(callbackUrl);
|
|
35
|
-
}
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
// Got authorization code
|
|
39
|
-
const authCode = req.query.code;
|
|
40
|
-
const provider = env.authProviders[state.provider];
|
|
41
|
-
LOG_DETAILS.provider = state.provider;
|
|
42
|
-
// Get access & refresh tokens
|
|
43
|
-
const tokens = yield provider.getAccessToken({ type: 'auth', auth_code: authCode, redirect_url: `${req.protocol}://${req.headers.host}/oauth2/${env.db.name}/signin` });
|
|
44
|
-
// let user_details;
|
|
45
|
-
// TODO: Have we got an id_token?
|
|
46
|
-
// if (tokens.id_token) {
|
|
47
|
-
// // decode, extract user information
|
|
48
|
-
// }
|
|
49
|
-
// else {
|
|
50
|
-
const user_details = yield provider.getUserInfo(tokens.access_token);
|
|
51
|
-
// }
|
|
52
|
-
if (user_details.picture && user_details.picture.length > 0) {
|
|
53
|
-
// Download it, convert to base64
|
|
54
|
-
const best = user_details.picture.sort((a, b) => a.width * a.height > b.width * b.height ? -1 : 1)[0];
|
|
55
|
-
try {
|
|
56
|
-
const response = yield (0, simple_fetch_1.fetch)(best.url);
|
|
57
|
-
const contentType = response.headers.get('Content-Type');
|
|
58
|
-
if (contentType === 'image/png') { //state.provider === 'google' &&
|
|
59
|
-
// Don't accept image/png, because it's probably a placeholder image. Google does this by creating a png with people's initials
|
|
60
|
-
user_details.picture = [];
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
const image = yield response.arrayBuffer();
|
|
64
|
-
const buff = Buffer.from(image);
|
|
65
|
-
best.url = `data:${contentType};base64,${buff.toString('base64')}`;
|
|
66
|
-
user_details.picture = [best]; // Only keep the best one
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
catch (err) {
|
|
70
|
-
env.debug.warn(`Could not fetch profile picture from "${best.url}": `, err);
|
|
71
|
-
user_details.picture = null;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
const getProviderSettings = () => {
|
|
75
|
-
// Returns an object with all info (except picture) the provider has about the user
|
|
76
|
-
const settings = {
|
|
77
|
-
[`${state.provider}_id`]: user_details.id,
|
|
78
|
-
[`${state.provider}_email`]: user_details.email,
|
|
79
|
-
[`${state.provider}_email_verified`]: user_details.email_verified,
|
|
80
|
-
[`${state.provider}_name`]: user_details.name,
|
|
81
|
-
[`${state.provider}_display_name`]: user_details.display_name,
|
|
82
|
-
};
|
|
83
|
-
Object.keys(user_details.other).forEach(key => {
|
|
84
|
-
settings[`${state.provider}_${key}`] = user_details.other[key];
|
|
85
|
-
});
|
|
86
|
-
return settings;
|
|
87
|
-
};
|
|
88
|
-
const providerUsername = `${state.provider}:${user_details.id}`;
|
|
89
|
-
// Check if this user exists in the database
|
|
90
|
-
let snaps;
|
|
91
|
-
let addToExistingAccount = true;
|
|
92
|
-
if (typeof state.uid === 'string') {
|
|
93
|
-
// Use the signed in uid to link this account to. This allows multiple auth provider
|
|
94
|
-
// accounts (with different email addresses) to be linked to the account the user
|
|
95
|
-
// is signed into, and also allows multiple AceBase users to link to the same provider
|
|
96
|
-
// accounts, eg if a client app allows users to link their own account to a shared
|
|
97
|
-
// family Spotify / Dropbox account.
|
|
98
|
-
const snap = yield env.authRef.child(state.uid).get();
|
|
99
|
-
if (!snap.exists()) {
|
|
100
|
-
// This is wrong!
|
|
101
|
-
throw new Error(`Invalid uid`);
|
|
102
|
-
}
|
|
103
|
-
snaps = [snap];
|
|
104
|
-
const user = snap.val();
|
|
105
|
-
addToExistingAccount = user.email === user_details.email;
|
|
106
|
-
}
|
|
107
|
-
else {
|
|
108
|
-
const query = env.authRef.query();
|
|
109
|
-
if (user_details.email) {
|
|
110
|
-
query.filter('email', '==', user_details.email);
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
// User did not allow reading e-mail address, or provider does not have one (eg whatsapp?)
|
|
114
|
-
// Switch to using a generated username such as "facebook-3292389234" instead
|
|
115
|
-
query.filter('username', '==', providerUsername);
|
|
116
|
-
}
|
|
117
|
-
snaps = yield query.get();
|
|
118
|
-
}
|
|
119
|
-
if (snaps.length === 0 && user_details.email) {
|
|
120
|
-
// Try again with providerUsername, use might previously have denied access to email,
|
|
121
|
-
// and now has granted access. In that case, we'll already have an account with the
|
|
122
|
-
// generated providerUsername
|
|
123
|
-
snaps = yield env.authRef.query().filter('username', '==', providerUsername).get();
|
|
124
|
-
}
|
|
125
|
-
let user;
|
|
126
|
-
if (snaps.length === 1) {
|
|
127
|
-
const uid = snaps[0].key;
|
|
128
|
-
user = snaps[0].val();
|
|
129
|
-
user.uid = uid;
|
|
130
|
-
if (addToExistingAccount) {
|
|
131
|
-
// Update user details
|
|
132
|
-
user.email_verified = user.email_verified || user_details.email_verified;
|
|
133
|
-
user.email = user.email || user_details.email;
|
|
134
|
-
if (((_a = user_details.picture) === null || _a === void 0 ? void 0 : _a.length) > 0) {
|
|
135
|
-
user.picture = user_details.picture[0];
|
|
136
|
-
}
|
|
137
|
-
yield env.authRef.child(uid).update({
|
|
138
|
-
email: user.email || null,
|
|
139
|
-
email_verified: user.email_verified,
|
|
140
|
-
last_signin: new Date(),
|
|
141
|
-
last_signin_ip: req.ip,
|
|
142
|
-
picture: user.picture,
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
// Add provider details
|
|
146
|
-
yield env.authRef.child(uid).child('settings').update(getProviderSettings());
|
|
147
|
-
// Log success
|
|
148
|
-
env.log.event(LOG_ACTION, Object.assign(Object.assign({}, LOG_DETAILS), { uid }));
|
|
149
|
-
// Cache the user
|
|
150
|
-
env.authCache.set(user.uid, user);
|
|
151
|
-
// Request signin e-mail to be sent
|
|
152
|
-
const request = {
|
|
153
|
-
type: 'user_signin',
|
|
154
|
-
user: {
|
|
155
|
-
uid: user.uid,
|
|
156
|
-
username: user.username,
|
|
157
|
-
email: user.email,
|
|
158
|
-
displayName: user.display_name,
|
|
159
|
-
settings: user.settings,
|
|
160
|
-
},
|
|
161
|
-
date: user.created,
|
|
162
|
-
ip: req.ip,
|
|
163
|
-
activationCode: user.email_verified ? null : (0, tokens_1.createSignedPublicToken)({ uid: user.uid }, env.tokenSalt),
|
|
164
|
-
emailVerified: user.email_verified,
|
|
165
|
-
provider: state.provider,
|
|
166
|
-
};
|
|
167
|
-
(_b = env.config.email) === null || _b === void 0 ? void 0 : _b.send(request).catch(err => {
|
|
168
|
-
env.log.error(LOG_ACTION + '.email', 'unexpected', Object.assign(Object.assign({}, LOG_DETAILS), { uid, request }), err);
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
else if (snaps.length === 0) {
|
|
172
|
-
// User does not exist, create
|
|
173
|
-
if (!env.config.auth.allowUserSignup) {
|
|
174
|
-
env.log.error(LOG_ACTION, 'user_signup_disabled', Object.assign(Object.assign({}, LOG_DETAILS), { email: user_details.email }));
|
|
175
|
-
res.statusCode = 403; // Forbidden
|
|
176
|
-
return res.send({ code: 'admin_only', message: 'Only admin is allowed to create users' });
|
|
177
|
-
}
|
|
178
|
-
// Create user with Generated password
|
|
179
|
-
const pwd = (0, password_1.createPasswordHash)((0, password_1.generatePassword)());
|
|
180
|
-
user = {
|
|
181
|
-
uid: null,
|
|
182
|
-
username: typeof user_details.email === 'undefined' ? providerUsername : null,
|
|
183
|
-
email: user_details.email || null,
|
|
184
|
-
email_verified: user_details.email_verified,
|
|
185
|
-
display_name: user_details.display_name,
|
|
186
|
-
password: pwd.hash,
|
|
187
|
-
password_salt: pwd.salt,
|
|
188
|
-
created: new Date(),
|
|
189
|
-
created_ip: req.ip,
|
|
190
|
-
access_token: acebase_core_1.ID.generate(),
|
|
191
|
-
access_token_created: new Date(),
|
|
192
|
-
last_signin: new Date(),
|
|
193
|
-
last_signin_ip: req.ip,
|
|
194
|
-
picture: user_details.picture && user_details.picture[0],
|
|
195
|
-
settings: getProviderSettings(),
|
|
196
|
-
};
|
|
197
|
-
const userRef = yield env.authRef.push(user);
|
|
198
|
-
const uid = userRef.key;
|
|
199
|
-
user.uid = uid;
|
|
200
|
-
// Log success
|
|
201
|
-
env.log.event(LOG_ACTION, Object.assign(Object.assign({}, LOG_DETAILS), { uid }));
|
|
202
|
-
// Cache the user
|
|
203
|
-
env.authCache.set(user.uid, user);
|
|
204
|
-
// Request welcome e-mail to be sent
|
|
205
|
-
const request = {
|
|
206
|
-
type: 'user_signup',
|
|
207
|
-
user: {
|
|
208
|
-
uid: user.uid,
|
|
209
|
-
username: user.username,
|
|
210
|
-
email: user.email,
|
|
211
|
-
displayName: user.display_name,
|
|
212
|
-
settings: user.settings,
|
|
213
|
-
},
|
|
214
|
-
date: user.created,
|
|
215
|
-
ip: user.created_ip,
|
|
216
|
-
activationCode: user.email_verified ? null : (0, tokens_1.createSignedPublicToken)({ uid: user.uid }, env.tokenSalt),
|
|
217
|
-
emailVerified: user.email_verified,
|
|
218
|
-
provider: state.provider,
|
|
219
|
-
};
|
|
220
|
-
(_c = env.config.email) === null || _c === void 0 ? void 0 : _c.send(request).catch(err => {
|
|
221
|
-
env.log.error(LOG_ACTION + '.email', 'unexpected', Object.assign(Object.assign({}, LOG_DETAILS), { uid, request }), err);
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
else {
|
|
225
|
-
// More than 1?!!
|
|
226
|
-
if (state.flow === 'socket') {
|
|
227
|
-
const client = env.clients.get(state.client_id);
|
|
228
|
-
client.socket.emit('oauth2-signin', { action: 'error', error: 'account_duplicates' });
|
|
229
|
-
return res.send(socketSignInFailed);
|
|
230
|
-
}
|
|
231
|
-
else {
|
|
232
|
-
const callbackUrl = `${state.callbackUrl}?provider=${state.provider}&error=account_duplicates`;
|
|
233
|
-
return res.redirect(callbackUrl);
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
const result = {
|
|
237
|
-
provider: {
|
|
238
|
-
name: state.provider,
|
|
239
|
-
access_token: tokens.access_token,
|
|
240
|
-
refresh_token: tokens.refresh_token,
|
|
241
|
-
expires_in: tokens.expires_in,
|
|
242
|
-
},
|
|
243
|
-
access_token: (0, tokens_1.createPublicAccessToken)(user.uid, req.ip, user.access_token, env.tokenSalt),
|
|
244
|
-
// Disabled sending user details because:
|
|
245
|
-
// 1) they might be too big to send as redirect header,
|
|
246
|
-
// 2) client should verify the sent access token and get user details from the server
|
|
247
|
-
// user: getPublicAccountDetails(user)
|
|
248
|
-
};
|
|
249
|
-
if (state.flow === 'socket') {
|
|
250
|
-
const client = env.clients.get(state.client_id);
|
|
251
|
-
client.socket.emit('oauth2-signin', { action: 'success', result });
|
|
252
|
-
res.send(socketSignInSuccess);
|
|
253
|
-
}
|
|
254
|
-
else {
|
|
255
|
-
const base64Result = Buffer.from(JSON.stringify(result)).toString('base64');
|
|
256
|
-
const callbackUrl = `${state.callbackUrl}?result=${base64Result}`;
|
|
257
|
-
res.redirect(callbackUrl);
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
catch (err) {
|
|
261
|
-
res.status(500).send(err.message);
|
|
262
|
-
}
|
|
263
|
-
}));
|
|
264
|
-
};
|
|
265
|
-
exports.addRoute = addRoute;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.addRoute = void 0;
|
|
13
|
+
const acebase_core_1 = require("acebase-core");
|
|
14
|
+
const password_1 = require("../shared/password");
|
|
15
|
+
const tokens_1 = require("../shared/tokens");
|
|
16
|
+
const simple_fetch_1 = require("../shared/simple-fetch");
|
|
17
|
+
const socketSignInSuccess = `<html><script>window.close()</script><body>Signed in succesfully. You can <a href="javascript:window.close()">close</a> this page</body></html>`;
|
|
18
|
+
const socketSignInFailed = `<html><script>window.close()</script><body>Failed to sign in. You can <a href="javascript:window.close()">close</a> this page</body></html>`;
|
|
19
|
+
const addRoute = (env) => {
|
|
20
|
+
env.router.get(`/oauth2/${env.db.name}/signin`, (req, res) => __awaiter(void 0, void 0, void 0, function* () {
|
|
21
|
+
// This is where the user is redirected to by the provider after signin or error
|
|
22
|
+
var _a, _b, _c;
|
|
23
|
+
const LOG_ACTION = 'oauth2.signin';
|
|
24
|
+
const LOG_DETAILS = { ip: req.ip, provider: null };
|
|
25
|
+
try {
|
|
26
|
+
const state = (0, tokens_1.parseSignedPublicToken)(req.query.state, env.tokenSalt);
|
|
27
|
+
if (req.query.error) {
|
|
28
|
+
if (state.flow === 'socket') {
|
|
29
|
+
const client = env.clients.get(state.client_id);
|
|
30
|
+
client.socket.emit('oauth2-signin', { error: req.query.error, reason: req.query.error_reason, description: req.query.error_description, provider: state.provider });
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
const callbackUrl = `${state.callbackUrl}?provider=${state.provider}&error=${req.query.error}&reason=${req.query.error_reason}&description=${req.query.error_description}`;
|
|
34
|
+
res.redirect(callbackUrl);
|
|
35
|
+
}
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
// Got authorization code
|
|
39
|
+
const authCode = req.query.code;
|
|
40
|
+
const provider = env.authProviders[state.provider];
|
|
41
|
+
LOG_DETAILS.provider = state.provider;
|
|
42
|
+
// Get access & refresh tokens
|
|
43
|
+
const tokens = yield provider.getAccessToken({ type: 'auth', auth_code: authCode, redirect_url: `${req.protocol}://${req.headers.host}/oauth2/${env.db.name}/signin` });
|
|
44
|
+
// let user_details;
|
|
45
|
+
// TODO: Have we got an id_token?
|
|
46
|
+
// if (tokens.id_token) {
|
|
47
|
+
// // decode, extract user information
|
|
48
|
+
// }
|
|
49
|
+
// else {
|
|
50
|
+
const user_details = yield provider.getUserInfo(tokens.access_token);
|
|
51
|
+
// }
|
|
52
|
+
if (user_details.picture && user_details.picture.length > 0) {
|
|
53
|
+
// Download it, convert to base64
|
|
54
|
+
const best = user_details.picture.sort((a, b) => a.width * a.height > b.width * b.height ? -1 : 1)[0];
|
|
55
|
+
try {
|
|
56
|
+
const response = yield (0, simple_fetch_1.fetch)(best.url);
|
|
57
|
+
const contentType = response.headers.get('Content-Type');
|
|
58
|
+
if (contentType === 'image/png') { //state.provider === 'google' &&
|
|
59
|
+
// Don't accept image/png, because it's probably a placeholder image. Google does this by creating a png with people's initials
|
|
60
|
+
user_details.picture = [];
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
const image = yield response.arrayBuffer();
|
|
64
|
+
const buff = Buffer.from(image);
|
|
65
|
+
best.url = `data:${contentType};base64,${buff.toString('base64')}`;
|
|
66
|
+
user_details.picture = [best]; // Only keep the best one
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
env.debug.warn(`Could not fetch profile picture from "${best.url}": `, err);
|
|
71
|
+
user_details.picture = null;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const getProviderSettings = () => {
|
|
75
|
+
// Returns an object with all info (except picture) the provider has about the user
|
|
76
|
+
const settings = {
|
|
77
|
+
[`${state.provider}_id`]: user_details.id,
|
|
78
|
+
[`${state.provider}_email`]: user_details.email,
|
|
79
|
+
[`${state.provider}_email_verified`]: user_details.email_verified,
|
|
80
|
+
[`${state.provider}_name`]: user_details.name,
|
|
81
|
+
[`${state.provider}_display_name`]: user_details.display_name,
|
|
82
|
+
};
|
|
83
|
+
Object.keys(user_details.other).forEach(key => {
|
|
84
|
+
settings[`${state.provider}_${key}`] = user_details.other[key];
|
|
85
|
+
});
|
|
86
|
+
return settings;
|
|
87
|
+
};
|
|
88
|
+
const providerUsername = `${state.provider}:${user_details.id}`;
|
|
89
|
+
// Check if this user exists in the database
|
|
90
|
+
let snaps;
|
|
91
|
+
let addToExistingAccount = true;
|
|
92
|
+
if (typeof state.uid === 'string') {
|
|
93
|
+
// Use the signed in uid to link this account to. This allows multiple auth provider
|
|
94
|
+
// accounts (with different email addresses) to be linked to the account the user
|
|
95
|
+
// is signed into, and also allows multiple AceBase users to link to the same provider
|
|
96
|
+
// accounts, eg if a client app allows users to link their own account to a shared
|
|
97
|
+
// family Spotify / Dropbox account.
|
|
98
|
+
const snap = yield env.authRef.child(state.uid).get();
|
|
99
|
+
if (!snap.exists()) {
|
|
100
|
+
// This is wrong!
|
|
101
|
+
throw new Error(`Invalid uid`);
|
|
102
|
+
}
|
|
103
|
+
snaps = [snap];
|
|
104
|
+
const user = snap.val();
|
|
105
|
+
addToExistingAccount = user.email === user_details.email;
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
const query = env.authRef.query();
|
|
109
|
+
if (user_details.email) {
|
|
110
|
+
query.filter('email', '==', user_details.email);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
// User did not allow reading e-mail address, or provider does not have one (eg whatsapp?)
|
|
114
|
+
// Switch to using a generated username such as "facebook-3292389234" instead
|
|
115
|
+
query.filter('username', '==', providerUsername);
|
|
116
|
+
}
|
|
117
|
+
snaps = yield query.get();
|
|
118
|
+
}
|
|
119
|
+
if (snaps.length === 0 && user_details.email) {
|
|
120
|
+
// Try again with providerUsername, use might previously have denied access to email,
|
|
121
|
+
// and now has granted access. In that case, we'll already have an account with the
|
|
122
|
+
// generated providerUsername
|
|
123
|
+
snaps = yield env.authRef.query().filter('username', '==', providerUsername).get();
|
|
124
|
+
}
|
|
125
|
+
let user;
|
|
126
|
+
if (snaps.length === 1) {
|
|
127
|
+
const uid = snaps[0].key;
|
|
128
|
+
user = snaps[0].val();
|
|
129
|
+
user.uid = uid;
|
|
130
|
+
if (addToExistingAccount) {
|
|
131
|
+
// Update user details
|
|
132
|
+
user.email_verified = user.email_verified || user_details.email_verified;
|
|
133
|
+
user.email = user.email || user_details.email;
|
|
134
|
+
if (((_a = user_details.picture) === null || _a === void 0 ? void 0 : _a.length) > 0) {
|
|
135
|
+
user.picture = user_details.picture[0];
|
|
136
|
+
}
|
|
137
|
+
yield env.authRef.child(uid).update({
|
|
138
|
+
email: user.email || null,
|
|
139
|
+
email_verified: user.email_verified,
|
|
140
|
+
last_signin: new Date(),
|
|
141
|
+
last_signin_ip: req.ip,
|
|
142
|
+
picture: user.picture,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
// Add provider details
|
|
146
|
+
yield env.authRef.child(uid).child('settings').update(getProviderSettings());
|
|
147
|
+
// Log success
|
|
148
|
+
env.log.event(LOG_ACTION, Object.assign(Object.assign({}, LOG_DETAILS), { uid }));
|
|
149
|
+
// Cache the user
|
|
150
|
+
env.authCache.set(user.uid, user);
|
|
151
|
+
// Request signin e-mail to be sent
|
|
152
|
+
const request = {
|
|
153
|
+
type: 'user_signin',
|
|
154
|
+
user: {
|
|
155
|
+
uid: user.uid,
|
|
156
|
+
username: user.username,
|
|
157
|
+
email: user.email,
|
|
158
|
+
displayName: user.display_name,
|
|
159
|
+
settings: user.settings,
|
|
160
|
+
},
|
|
161
|
+
date: user.created,
|
|
162
|
+
ip: req.ip,
|
|
163
|
+
activationCode: user.email_verified ? null : (0, tokens_1.createSignedPublicToken)({ uid: user.uid }, env.tokenSalt),
|
|
164
|
+
emailVerified: user.email_verified,
|
|
165
|
+
provider: state.provider,
|
|
166
|
+
};
|
|
167
|
+
(_b = env.config.email) === null || _b === void 0 ? void 0 : _b.send(request).catch(err => {
|
|
168
|
+
env.log.error(LOG_ACTION + '.email', 'unexpected', Object.assign(Object.assign({}, LOG_DETAILS), { uid, request }), err);
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
else if (snaps.length === 0) {
|
|
172
|
+
// User does not exist, create
|
|
173
|
+
if (!env.config.auth.allowUserSignup) {
|
|
174
|
+
env.log.error(LOG_ACTION, 'user_signup_disabled', Object.assign(Object.assign({}, LOG_DETAILS), { email: user_details.email }));
|
|
175
|
+
res.statusCode = 403; // Forbidden
|
|
176
|
+
return res.send({ code: 'admin_only', message: 'Only admin is allowed to create users' });
|
|
177
|
+
}
|
|
178
|
+
// Create user with Generated password
|
|
179
|
+
const pwd = (0, password_1.createPasswordHash)((0, password_1.generatePassword)());
|
|
180
|
+
user = {
|
|
181
|
+
uid: null,
|
|
182
|
+
username: typeof user_details.email === 'undefined' ? providerUsername : null,
|
|
183
|
+
email: user_details.email || null,
|
|
184
|
+
email_verified: user_details.email_verified,
|
|
185
|
+
display_name: user_details.display_name,
|
|
186
|
+
password: pwd.hash,
|
|
187
|
+
password_salt: pwd.salt,
|
|
188
|
+
created: new Date(),
|
|
189
|
+
created_ip: req.ip,
|
|
190
|
+
access_token: acebase_core_1.ID.generate(),
|
|
191
|
+
access_token_created: new Date(),
|
|
192
|
+
last_signin: new Date(),
|
|
193
|
+
last_signin_ip: req.ip,
|
|
194
|
+
picture: user_details.picture && user_details.picture[0],
|
|
195
|
+
settings: getProviderSettings(),
|
|
196
|
+
};
|
|
197
|
+
const userRef = yield env.authRef.push(user);
|
|
198
|
+
const uid = userRef.key;
|
|
199
|
+
user.uid = uid;
|
|
200
|
+
// Log success
|
|
201
|
+
env.log.event(LOG_ACTION, Object.assign(Object.assign({}, LOG_DETAILS), { uid }));
|
|
202
|
+
// Cache the user
|
|
203
|
+
env.authCache.set(user.uid, user);
|
|
204
|
+
// Request welcome e-mail to be sent
|
|
205
|
+
const request = {
|
|
206
|
+
type: 'user_signup',
|
|
207
|
+
user: {
|
|
208
|
+
uid: user.uid,
|
|
209
|
+
username: user.username,
|
|
210
|
+
email: user.email,
|
|
211
|
+
displayName: user.display_name,
|
|
212
|
+
settings: user.settings,
|
|
213
|
+
},
|
|
214
|
+
date: user.created,
|
|
215
|
+
ip: user.created_ip,
|
|
216
|
+
activationCode: user.email_verified ? null : (0, tokens_1.createSignedPublicToken)({ uid: user.uid }, env.tokenSalt),
|
|
217
|
+
emailVerified: user.email_verified,
|
|
218
|
+
provider: state.provider,
|
|
219
|
+
};
|
|
220
|
+
(_c = env.config.email) === null || _c === void 0 ? void 0 : _c.send(request).catch(err => {
|
|
221
|
+
env.log.error(LOG_ACTION + '.email', 'unexpected', Object.assign(Object.assign({}, LOG_DETAILS), { uid, request }), err);
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
// More than 1?!!
|
|
226
|
+
if (state.flow === 'socket') {
|
|
227
|
+
const client = env.clients.get(state.client_id);
|
|
228
|
+
client.socket.emit('oauth2-signin', { action: 'error', error: 'account_duplicates' });
|
|
229
|
+
return res.send(socketSignInFailed);
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
const callbackUrl = `${state.callbackUrl}?provider=${state.provider}&error=account_duplicates`;
|
|
233
|
+
return res.redirect(callbackUrl);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
const result = {
|
|
237
|
+
provider: {
|
|
238
|
+
name: state.provider,
|
|
239
|
+
access_token: tokens.access_token,
|
|
240
|
+
refresh_token: tokens.refresh_token,
|
|
241
|
+
expires_in: tokens.expires_in,
|
|
242
|
+
},
|
|
243
|
+
access_token: (0, tokens_1.createPublicAccessToken)(user.uid, req.ip, user.access_token, env.tokenSalt),
|
|
244
|
+
// Disabled sending user details because:
|
|
245
|
+
// 1) they might be too big to send as redirect header,
|
|
246
|
+
// 2) client should verify the sent access token and get user details from the server
|
|
247
|
+
// user: getPublicAccountDetails(user)
|
|
248
|
+
};
|
|
249
|
+
if (state.flow === 'socket') {
|
|
250
|
+
const client = env.clients.get(state.client_id);
|
|
251
|
+
client.socket.emit('oauth2-signin', { action: 'success', result });
|
|
252
|
+
res.send(socketSignInSuccess);
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
const base64Result = Buffer.from(JSON.stringify(result)).toString('base64');
|
|
256
|
+
const callbackUrl = `${state.callbackUrl}?result=${base64Result}`;
|
|
257
|
+
res.redirect(callbackUrl);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
catch (err) {
|
|
261
|
+
res.status(500).send(err.message);
|
|
262
|
+
}
|
|
263
|
+
}));
|
|
264
|
+
};
|
|
265
|
+
exports.addRoute = addRoute;
|
|
266
266
|
//# sourceMappingURL=oauth2-signin.js.map
|
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.addRoutes = void 0;
|
|
4
|
-
const rootpath_1 = require("../shared/rootpath");
|
|
5
|
-
const path_1 = require("path");
|
|
6
|
-
const addRoutes = (env) => {
|
|
7
|
-
const webManagerDir = `webmanager`;
|
|
8
|
-
// Add redirect from root to webmanager
|
|
9
|
-
env.
|
|
10
|
-
res.redirect(`/${env.rootPath ? `${env.rootPath}/` : ''}${webManagerDir}/`);
|
|
11
|
-
});
|
|
12
|
-
// Serve static files from webmanager directory
|
|
13
|
-
env.
|
|
14
|
-
const filePath = req.path.slice(webManagerDir.length + 2);
|
|
15
|
-
const assetsPath = (0, path_1.join)(rootpath_1.packageRootPath, '/webmanager');
|
|
16
|
-
if (filePath.length === 0) {
|
|
17
|
-
// Send default file
|
|
18
|
-
res.sendFile((0, path_1.join)(assetsPath, '/index.html'));
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
21
|
-
res.sendFile((0, path_1.join)(assetsPath, '/', filePath));
|
|
22
|
-
}
|
|
23
|
-
});
|
|
24
|
-
};
|
|
25
|
-
exports.addRoutes = addRoutes;
|
|
26
|
-
exports.default = exports.addRoutes;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.addRoutes = void 0;
|
|
4
|
+
const rootpath_1 = require("../shared/rootpath");
|
|
5
|
+
const path_1 = require("path");
|
|
6
|
+
const addRoutes = (env) => {
|
|
7
|
+
const webManagerDir = `webmanager`;
|
|
8
|
+
// Add redirect from root to webmanager
|
|
9
|
+
env.router.get('/', (req, res) => {
|
|
10
|
+
res.redirect(`/${env.rootPath ? `${env.rootPath}/` : ''}${webManagerDir}/`);
|
|
11
|
+
});
|
|
12
|
+
// Serve static files from webmanager directory
|
|
13
|
+
env.router.get(`/${webManagerDir}/*`, (req, res) => {
|
|
14
|
+
const filePath = req.path.slice(webManagerDir.length + 2);
|
|
15
|
+
const assetsPath = (0, path_1.join)(rootpath_1.packageRootPath, '/webmanager');
|
|
16
|
+
if (filePath.length === 0) {
|
|
17
|
+
// Send default file
|
|
18
|
+
res.sendFile((0, path_1.join)(assetsPath, '/index.html'));
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
res.sendFile((0, path_1.join)(assetsPath, '/', filePath));
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
exports.addRoutes = addRoutes;
|
|
26
|
+
exports.default = exports.addRoutes;
|
|
27
27
|
//# sourceMappingURL=webmanager.js.map
|