acebase-server 1.11.0 → 1.13.1
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/.vscode/launch.json +24 -0
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/logger.js +74 -0
- package/dist/cjs/middleware/admin-only.js +29 -0
- package/dist/cjs/middleware/user.js +4 -4
- package/dist/cjs/routes/auth-change-password.js +7 -4
- package/dist/cjs/routes/auth-delete.js +9 -7
- package/dist/cjs/routes/auth-forgot-password.js +9 -6
- package/dist/cjs/routes/auth-reset-password.js +5 -3
- package/dist/cjs/routes/auth-signout.js +6 -4
- package/dist/cjs/routes/auth-signup.js +17 -15
- package/dist/cjs/routes/auth-update.js +9 -6
- package/dist/cjs/routes/auth-verify-email.js +6 -3
- package/dist/cjs/routes/data-index-create.js +3 -5
- package/dist/cjs/routes/data-index-delete.js +2 -4
- package/dist/cjs/routes/data-index-list.js +2 -4
- package/dist/cjs/routes/data-reflect.js +2 -4
- package/dist/cjs/routes/data-schema-get.js +2 -4
- package/dist/cjs/routes/data-schema-set.js +2 -4
- package/dist/cjs/routes/data-schema-test.js +2 -4
- package/dist/cjs/routes/data-schemas-list.js +2 -4
- package/dist/cjs/routes/data-set.js +8 -7
- package/dist/cjs/routes/data-transaction.js +15 -8
- package/dist/cjs/routes/data-update.js +8 -6
- package/dist/cjs/routes/meta-logs.js +50 -0
- package/dist/cjs/routes/meta.js +3 -0
- package/dist/cjs/routes/oauth2-signin.js +9 -6
- package/dist/cjs/server.js +3 -1
- package/dist/cjs/shared/rootpath.js +2 -0
- package/dist/cjs/shared/signin.js +8 -3
- package/dist/cjs/start.js +3 -0
- package/dist/cjs/websocket/index.js +42 -23
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/logger.js +52 -0
- package/dist/esm/middleware/admin-only.js +25 -0
- package/dist/esm/middleware/user.js +4 -4
- package/dist/esm/routes/auth-change-password.js +6 -3
- package/dist/esm/routes/auth-delete.js +7 -5
- package/dist/esm/routes/auth-forgot-password.js +8 -6
- package/dist/esm/routes/auth-reset-password.js +4 -2
- package/dist/esm/routes/auth-signout.js +4 -2
- package/dist/esm/routes/auth-signup.js +12 -10
- package/dist/esm/routes/auth-update.js +8 -6
- package/dist/esm/routes/auth-verify-email.js +6 -2
- package/dist/esm/routes/data-index-create.js +4 -6
- package/dist/esm/routes/data-index-delete.js +3 -5
- package/dist/esm/routes/data-index-list.js +3 -5
- package/dist/esm/routes/data-reflect.js +1 -4
- package/dist/esm/routes/data-schema-get.js +3 -5
- package/dist/esm/routes/data-schema-set.js +3 -5
- package/dist/esm/routes/data-schema-test.js +3 -5
- package/dist/esm/routes/data-schemas-list.js +3 -5
- package/dist/esm/routes/data-set.js +6 -5
- package/dist/esm/routes/data-transaction.js +12 -5
- package/dist/esm/routes/data-update.js +6 -4
- package/dist/esm/routes/meta-logs.js +36 -0
- package/dist/esm/routes/meta.js +3 -0
- package/dist/esm/routes/oauth2-signin.js +8 -5
- package/dist/esm/server.js +3 -1
- package/dist/esm/shared/rootpath.js +2 -0
- package/dist/esm/shared/signin.js +7 -3
- package/dist/esm/start.js +3 -0
- package/dist/esm/websocket/index.js +37 -22
- package/dist/types/logger.d.ts +35 -0
- package/dist/types/middleware/admin-only.d.ts +19 -0
- package/dist/types/routes/meta-logs.d.ts +20 -0
- package/dist/types/rules.d.ts +2 -2
- package/dist/types/shared/env.d.ts +3 -1
- package/dist/types/shared/signin.d.ts +1 -1
- package/dist/types/websocket/index.d.ts +4 -0
- package/package.json +5 -5
- package/webmanager/acebase-client-browser.min.js +16 -16
- package/create-package-files +0 -12
- package/dist/cjs/auth.js.map +0 -1
- package/dist/cjs/cluster.js.map +0 -1
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/middleware/404.js.map +0 -1
- package/dist/cjs/middleware/cache.js.map +0 -1
- package/dist/cjs/middleware/connection.js.map +0 -1
- package/dist/cjs/middleware/context.js.map +0 -1
- package/dist/cjs/middleware/cors.js.map +0 -1
- package/dist/cjs/middleware/swagger.js.map +0 -1
- package/dist/cjs/middleware/user.js.map +0 -1
- package/dist/cjs/oauth-providers/dropbox.js.map +0 -1
- package/dist/cjs/oauth-providers/facebook.js.map +0 -1
- package/dist/cjs/oauth-providers/google.js.map +0 -1
- package/dist/cjs/oauth-providers/index.js.map +0 -1
- package/dist/cjs/oauth-providers/instagram.js.map +0 -1
- package/dist/cjs/oauth-providers/oauth-provider.js.map +0 -1
- package/dist/cjs/oauth-providers/spotify.js.map +0 -1
- package/dist/cjs/routes/auth-change-password.js.map +0 -1
- package/dist/cjs/routes/auth-delete.js.map +0 -1
- package/dist/cjs/routes/auth-forgot-password.js.map +0 -1
- package/dist/cjs/routes/auth-reset-password.js.map +0 -1
- package/dist/cjs/routes/auth-signin.js.map +0 -1
- package/dist/cjs/routes/auth-signout.js.map +0 -1
- package/dist/cjs/routes/auth-signup.js.map +0 -1
- package/dist/cjs/routes/auth-state.js.map +0 -1
- package/dist/cjs/routes/auth-update.js.map +0 -1
- package/dist/cjs/routes/auth-verify-email.js.map +0 -1
- package/dist/cjs/routes/auth.js.map +0 -1
- package/dist/cjs/routes/data-exists.js.map +0 -1
- package/dist/cjs/routes/data-export.js.map +0 -1
- package/dist/cjs/routes/data-get.js.map +0 -1
- package/dist/cjs/routes/data-import.js.map +0 -1
- package/dist/cjs/routes/data-index-create.js.map +0 -1
- package/dist/cjs/routes/data-index-delete.js.map +0 -1
- package/dist/cjs/routes/data-index-list.js.map +0 -1
- package/dist/cjs/routes/data-query.js.map +0 -1
- package/dist/cjs/routes/data-reflect.js.map +0 -1
- package/dist/cjs/routes/data-schema-get.js.map +0 -1
- package/dist/cjs/routes/data-schema-set.js.map +0 -1
- package/dist/cjs/routes/data-schema-test.js.map +0 -1
- package/dist/cjs/routes/data-schemas-list.js.map +0 -1
- package/dist/cjs/routes/data-set.js.map +0 -1
- package/dist/cjs/routes/data-sync-changes.js.map +0 -1
- package/dist/cjs/routes/data-sync-mutations.js.map +0 -1
- package/dist/cjs/routes/data-transaction.js.map +0 -1
- package/dist/cjs/routes/data-update.js.map +0 -1
- package/dist/cjs/routes/data.js.map +0 -1
- package/dist/cjs/routes/docs.js.map +0 -1
- package/dist/cjs/routes/meta-info.js.map +0 -1
- package/dist/cjs/routes/meta-ping.js.map +0 -1
- package/dist/cjs/routes/meta-stats.js.map +0 -1
- package/dist/cjs/routes/meta.js.map +0 -1
- package/dist/cjs/routes/oauth2-init.js.map +0 -1
- package/dist/cjs/routes/oauth2-refresh.js.map +0 -1
- package/dist/cjs/routes/oauth2-signin.js.map +0 -1
- package/dist/cjs/routes/webmanager.js.map +0 -1
- package/dist/cjs/rules.js.map +0 -1
- package/dist/cjs/schema/user.js.map +0 -1
- package/dist/cjs/server.js.map +0 -1
- package/dist/cjs/settings/email.js.map +0 -1
- package/dist/cjs/settings/index.js.map +0 -1
- package/dist/cjs/shared/clients.js.map +0 -1
- package/dist/cjs/shared/email.js.map +0 -1
- package/dist/cjs/shared/env.js.map +0 -1
- package/dist/cjs/shared/error.js.map +0 -1
- package/dist/cjs/shared/http.js.map +0 -1
- package/dist/cjs/shared/password.js.map +0 -1
- package/dist/cjs/shared/rootpath.js.map +0 -1
- package/dist/cjs/shared/signin.js.map +0 -1
- package/dist/cjs/shared/simple-fetch.js.map +0 -1
- package/dist/cjs/shared/tokens.js.map +0 -1
- package/dist/cjs/shared/validate.js.map +0 -1
- package/dist/cjs/start.js.map +0 -1
- package/dist/cjs/test.js.map +0 -1
- package/dist/cjs/websocket/index.js.map +0 -1
- package/dist/cjs/websocket/manager.js.map +0 -1
- package/dist/cjs/websocket/socket.io.js.map +0 -1
- package/dist/esm/auth.js.map +0 -1
- package/dist/esm/cluster.js.map +0 -1
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/middleware/404.js.map +0 -1
- package/dist/esm/middleware/cache.js.map +0 -1
- package/dist/esm/middleware/connection.js.map +0 -1
- package/dist/esm/middleware/context.js.map +0 -1
- package/dist/esm/middleware/cors.js.map +0 -1
- package/dist/esm/middleware/swagger.js.map +0 -1
- package/dist/esm/middleware/user.js.map +0 -1
- package/dist/esm/oauth-providers/dropbox.js.map +0 -1
- package/dist/esm/oauth-providers/facebook.js.map +0 -1
- package/dist/esm/oauth-providers/google.js.map +0 -1
- package/dist/esm/oauth-providers/index.js.map +0 -1
- package/dist/esm/oauth-providers/instagram.js.map +0 -1
- package/dist/esm/oauth-providers/oauth-provider.js.map +0 -1
- package/dist/esm/oauth-providers/spotify.js.map +0 -1
- package/dist/esm/routes/auth-change-password.js.map +0 -1
- package/dist/esm/routes/auth-delete.js.map +0 -1
- package/dist/esm/routes/auth-forgot-password.js.map +0 -1
- package/dist/esm/routes/auth-reset-password.js.map +0 -1
- package/dist/esm/routes/auth-signin.js.map +0 -1
- package/dist/esm/routes/auth-signout.js.map +0 -1
- package/dist/esm/routes/auth-signup.js.map +0 -1
- package/dist/esm/routes/auth-state.js.map +0 -1
- package/dist/esm/routes/auth-update.js.map +0 -1
- package/dist/esm/routes/auth-verify-email.js.map +0 -1
- package/dist/esm/routes/auth.js.map +0 -1
- package/dist/esm/routes/data-exists.js.map +0 -1
- package/dist/esm/routes/data-export.js.map +0 -1
- package/dist/esm/routes/data-get.js.map +0 -1
- package/dist/esm/routes/data-import.js.map +0 -1
- package/dist/esm/routes/data-index-create.js.map +0 -1
- package/dist/esm/routes/data-index-delete.js.map +0 -1
- package/dist/esm/routes/data-index-list.js.map +0 -1
- package/dist/esm/routes/data-query.js.map +0 -1
- package/dist/esm/routes/data-reflect.js.map +0 -1
- package/dist/esm/routes/data-schema-get.js.map +0 -1
- package/dist/esm/routes/data-schema-set.js.map +0 -1
- package/dist/esm/routes/data-schema-test.js.map +0 -1
- package/dist/esm/routes/data-schemas-list.js.map +0 -1
- package/dist/esm/routes/data-set.js.map +0 -1
- package/dist/esm/routes/data-sync-changes.js.map +0 -1
- package/dist/esm/routes/data-sync-mutations.js.map +0 -1
- package/dist/esm/routes/data-transaction.js.map +0 -1
- package/dist/esm/routes/data-update.js.map +0 -1
- package/dist/esm/routes/data.js.map +0 -1
- package/dist/esm/routes/docs.js.map +0 -1
- package/dist/esm/routes/meta-info.js.map +0 -1
- package/dist/esm/routes/meta-ping.js.map +0 -1
- package/dist/esm/routes/meta-stats.js.map +0 -1
- package/dist/esm/routes/meta.js.map +0 -1
- package/dist/esm/routes/oauth2-init.js.map +0 -1
- package/dist/esm/routes/oauth2-refresh.js.map +0 -1
- package/dist/esm/routes/oauth2-signin.js.map +0 -1
- package/dist/esm/routes/webmanager.js.map +0 -1
- package/dist/esm/rules.js.map +0 -1
- package/dist/esm/schema/user.js.map +0 -1
- package/dist/esm/server.js.map +0 -1
- package/dist/esm/settings/email.js.map +0 -1
- package/dist/esm/settings/index.js.map +0 -1
- package/dist/esm/shared/clients.js.map +0 -1
- package/dist/esm/shared/email.js.map +0 -1
- package/dist/esm/shared/env.js.map +0 -1
- package/dist/esm/shared/error.js.map +0 -1
- package/dist/esm/shared/http.js.map +0 -1
- package/dist/esm/shared/password.js.map +0 -1
- package/dist/esm/shared/rootpath.js.map +0 -1
- package/dist/esm/shared/signin.js.map +0 -1
- package/dist/esm/shared/simple-fetch.js.map +0 -1
- package/dist/esm/shared/tokens.js.map +0 -1
- package/dist/esm/shared/validate.js.map +0 -1
- package/dist/esm/start.js.map +0 -1
- package/dist/esm/test.js.map +0 -1
- package/dist/esm/websocket/index.js.map +0 -1
- package/dist/esm/websocket/manager.js.map +0 -1
- package/dist/esm/websocket/socket.io.js.map +0 -1
- package/src/auth.ts +0 -87
- package/src/cluster.ts +0 -62
- package/src/index.ts +0 -4
- package/src/middleware/404.ts +0 -13
- package/src/middleware/cache.ts +0 -15
- package/src/middleware/connection.ts +0 -48
- package/src/middleware/context.ts +0 -23
- package/src/middleware/cors.ts +0 -49
- package/src/middleware/swagger.ts +0 -19
- package/src/middleware/user.ts +0 -51
- package/src/oauth-providers/dropbox.ts +0 -144
- package/src/oauth-providers/facebook.ts +0 -103
- package/src/oauth-providers/google.ts +0 -147
- package/src/oauth-providers/index.ts +0 -21
- package/src/oauth-providers/instagram.ts +0 -98
- package/src/oauth-providers/oauth-provider.ts +0 -65
- package/src/oauth-providers/spotify.ts +0 -127
- package/src/placeholder.d.ts +0 -12
- package/src/routes/auth-change-password.ts +0 -89
- package/src/routes/auth-change-password.yaml +0 -74
- package/src/routes/auth-delete.ts +0 -47
- package/src/routes/auth-delete.yaml +0 -78
- package/src/routes/auth-forgot-password.ts +0 -73
- package/src/routes/auth-forgot-password.yaml +0 -50
- package/src/routes/auth-reset-password.ts +0 -93
- package/src/routes/auth-reset-password.yaml +0 -54
- package/src/routes/auth-signin.ts +0 -55
- package/src/routes/auth-signin.yaml +0 -130
- package/src/routes/auth-signout.ts +0 -61
- package/src/routes/auth-signout.yaml +0 -46
- package/src/routes/auth-signup.ts +0 -161
- package/src/routes/auth-signup.yaml +0 -136
- package/src/routes/auth-state.ts +0 -20
- package/src/routes/auth-state.yaml +0 -29
- package/src/routes/auth-update.ts +0 -147
- package/src/routes/auth-update.yaml +0 -152
- package/src/routes/auth-verify-email.ts +0 -67
- package/src/routes/auth-verify-email.yaml +0 -53
- package/src/routes/auth.ts +0 -67
- package/src/routes/data-exists.ts +0 -31
- package/src/routes/data-exists.yaml +0 -44
- package/src/routes/data-export.ts +0 -51
- package/src/routes/data-export.yaml +0 -64
- package/src/routes/data-get.ts +0 -68
- package/src/routes/data-get.yaml +0 -54
- package/src/routes/data-import.ts +0 -56
- package/src/routes/data-import.yaml +0 -75
- package/src/routes/data-index-create.ts +0 -85
- package/src/routes/data-index-create.yaml +0 -105
- package/src/routes/data-index-delete.ts +0 -39
- package/src/routes/data-index-delete.yaml +0 -51
- package/src/routes/data-index-list.ts +0 -42
- package/src/routes/data-index-list.yaml +0 -80
- package/src/routes/data-query.ts +0 -97
- package/src/routes/data-query.yaml +0 -166
- package/src/routes/data-reflect.ts +0 -94
- package/src/routes/data-reflect.yaml +0 -124
- package/src/routes/data-schema-get.ts +0 -38
- package/src/routes/data-schema-get.yaml +0 -53
- package/src/routes/data-schema-set.ts +0 -38
- package/src/routes/data-schema-set.yaml +0 -60
- package/src/routes/data-schema-test.ts +0 -53
- package/src/routes/data-schema-test.yaml +0 -66
- package/src/routes/data-schemas-list.ts +0 -33
- package/src/routes/data-schemas-list.yaml +0 -41
- package/src/routes/data-set.ts +0 -80
- package/src/routes/data-set.yaml +0 -77
- package/src/routes/data-sync-changes.ts +0 -78
- package/src/routes/data-sync-changes.yaml +0 -67
- package/src/routes/data-sync-mutations.ts +0 -78
- package/src/routes/data-sync-mutations.yaml +0 -67
- package/src/routes/data-transaction-finish.yaml +0 -82
- package/src/routes/data-transaction-start.yaml +0 -58
- package/src/routes/data-transaction.ts +0 -159
- package/src/routes/data-update.ts +0 -81
- package/src/routes/data-update.yaml +0 -77
- package/src/routes/data.ts +0 -79
- package/src/routes/docs.ts +0 -249
- package/src/routes/meta-info.ts +0 -80
- package/src/routes/meta-info.yaml +0 -41
- package/src/routes/meta-ping.ts +0 -15
- package/src/routes/meta-ping.yaml +0 -23
- package/src/routes/meta-stats.ts +0 -31
- package/src/routes/meta-stats.yaml +0 -41
- package/src/routes/meta.ts +0 -18
- package/src/routes/oauth2-init.ts +0 -44
- package/src/routes/oauth2-init.yaml +0 -63
- package/src/routes/oauth2-refresh.ts +0 -39
- package/src/routes/oauth2-refresh.yaml +0 -65
- package/src/routes/oauth2-signin.ts +0 -282
- package/src/routes/oauth2-signin.yaml +0 -66
- package/src/routes/webmanager.ts +0 -29
- package/src/rules.ts +0 -196
- package/src/schema/user.ts +0 -99
- package/src/server.ts +0 -418
- package/src/settings/email.ts +0 -18
- package/src/settings/index.ts +0 -241
- package/src/shared/clients.ts +0 -31
- package/src/shared/email.ts +0 -33
- package/src/shared/env.ts +0 -38
- package/src/shared/error.ts +0 -37
- package/src/shared/http.ts +0 -26
- package/src/shared/password.ts +0 -41
- package/src/shared/rootpath.ts +0 -15
- package/src/shared/signin.ts +0 -143
- package/src/shared/simple-fetch.ts +0 -65
- package/src/shared/tokens.ts +0 -77
- package/src/shared/validate.ts +0 -53
- package/src/start.ts +0 -45
- package/src/test.ts +0 -10
- package/src/websocket/index.ts +0 -320
- package/src/websocket/manager.ts +0 -46
- package/src/websocket/socket.io.ts +0 -90
- package/tsconfig-cjs.json +0 -10
- package/tsconfig.json +0 -21
package/src/settings/index.ts
DELETED
|
@@ -1,241 +0,0 @@
|
|
|
1
|
-
import { AceBaseStorageSettings } from 'acebase';
|
|
2
|
-
import { readFileSync } from 'fs';
|
|
3
|
-
import { AceBaseServerEmailSettings } from './email';
|
|
4
|
-
|
|
5
|
-
export type AceBaseServerHttpsSettings = {
|
|
6
|
-
enabled?: boolean;
|
|
7
|
-
keyPath?: string;
|
|
8
|
-
certPath?: string
|
|
9
|
-
pfxPath?: string;
|
|
10
|
-
passphrase?: string
|
|
11
|
-
} & (
|
|
12
|
-
{ keyPath: string; certPath: string } |
|
|
13
|
-
{ pfxPath: string; passphrase: string } |
|
|
14
|
-
{}
|
|
15
|
-
)
|
|
16
|
-
|
|
17
|
-
export class AceBaseServerHttpsConfig {
|
|
18
|
-
enabled: boolean = true;
|
|
19
|
-
key?: Buffer;
|
|
20
|
-
cert?: Buffer;
|
|
21
|
-
pfx?: Buffer;
|
|
22
|
-
passphrase?: string;
|
|
23
|
-
|
|
24
|
-
constructor(settings: AceBaseServerHttpsSettings) {
|
|
25
|
-
this.enabled = typeof settings === "object" && settings.enabled !== false;
|
|
26
|
-
if (!this.enabled) { return; }
|
|
27
|
-
if (settings.keyPath) {
|
|
28
|
-
this.key = readFileSync(settings.keyPath);
|
|
29
|
-
this.cert = readFileSync(settings.certPath);
|
|
30
|
-
}
|
|
31
|
-
else if (settings.pfxPath) {
|
|
32
|
-
this.pfx = readFileSync(settings.pfxPath);
|
|
33
|
-
this.passphrase = settings.passphrase;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export type AuthAccessDefault = 'deny'|'allow'|'auth';
|
|
39
|
-
export const AUTH_ACCESS_DEFAULT: { [key: string]: AuthAccessDefault } = {
|
|
40
|
-
DENY_ALL: 'deny',
|
|
41
|
-
ALLOW_ALL: 'allow',
|
|
42
|
-
ALLOW_AUTHENTICATED: 'auth'
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
export class AceBaseServerAuthenticationSettings {
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Whether to enable authorization. Without authorization the entire db can be read and written to by anyone (not recommended 🤷🏼♂️)
|
|
49
|
-
*/
|
|
50
|
-
readonly enabled: boolean = true;
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Whether new users creation is allowed for anyone, or just the admin
|
|
54
|
-
*/
|
|
55
|
-
readonly allowUserSignup: boolean = false;
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* How many new users can sign up per hour per IP address. not implemented yet
|
|
59
|
-
*/
|
|
60
|
-
readonly newUserRateLimit: number = 0;
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* How many minutes before access tokens expire. 0 for no expiration. (not implemented yet)
|
|
64
|
-
*/
|
|
65
|
-
readonly tokensExpire: number = 0;
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* When the server runs for the first time, what defaults to use to generate the rules.json file with. Options are: 'auth' (only authenticated access to db, default), 'deny' (deny access to anyone except admin user), 'allow' (allow access to anyone)
|
|
69
|
-
*/
|
|
70
|
-
readonly defaultAccessRule: AuthAccessDefault = AUTH_ACCESS_DEFAULT.ALLOW_AUTHENTICATED;
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* When the server runs for the first time, what password to use for the admin user. If not supplied, a generated password will be used and shown ONCE in the console output.
|
|
74
|
-
*/
|
|
75
|
-
readonly defaultAdminPassword?: string;
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Whether to use a separate database for auth and logging. 'v2' will store data in auth.db, which is NOT TESTED YET!
|
|
79
|
-
*/
|
|
80
|
-
readonly separateDb: boolean|'v2' = false;
|
|
81
|
-
|
|
82
|
-
constructor(settings: Partial<AceBaseServerAuthenticationSettings>) {
|
|
83
|
-
if (typeof settings !== "object") { settings = {}; }
|
|
84
|
-
if (typeof settings.enabled === 'boolean') { this.enabled = settings.enabled; }
|
|
85
|
-
if (typeof settings.allowUserSignup === 'boolean') { this.allowUserSignup = settings.allowUserSignup; }
|
|
86
|
-
if (typeof settings.newUserRateLimit === 'number') { this.newUserRateLimit = settings.newUserRateLimit; }
|
|
87
|
-
if (typeof settings.tokensExpire === 'number') { this.tokensExpire = settings.tokensExpire; }
|
|
88
|
-
if (typeof settings.defaultAccessRule === 'string') { this.defaultAccessRule = settings.defaultAccessRule; }
|
|
89
|
-
if (typeof settings.defaultAdminPassword === 'string') { this.defaultAdminPassword = settings.defaultAdminPassword; }
|
|
90
|
-
if (typeof (settings as any).seperateDb === 'boolean') { this.separateDb = (settings as any).seperateDb; } // Handle previous _wrong_ spelling
|
|
91
|
-
if (typeof settings.separateDb === 'boolean') { this.separateDb = settings.separateDb; }
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* TODO: Use AceBaseTransactionLogSettings from 'acebase/src/storage-acebase.js'
|
|
97
|
-
*/
|
|
98
|
-
export class AceBaseServerTransactionSettings {
|
|
99
|
-
/**
|
|
100
|
-
* Whether to enable transaction logging
|
|
101
|
-
*/
|
|
102
|
-
log: boolean = false;
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Max age in days to keep transactions in the log file
|
|
106
|
-
*/
|
|
107
|
-
maxAge: number = 30;
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Whether database write operations should not wait until transaction has been logged
|
|
111
|
-
*/
|
|
112
|
-
noWait: boolean = false;
|
|
113
|
-
|
|
114
|
-
constructor(settings: Partial<AceBaseServerTransactionSettings>) {
|
|
115
|
-
if (typeof settings !== 'object') { return; }
|
|
116
|
-
if (typeof settings.log === 'boolean') { this.log = settings.log; }
|
|
117
|
-
if (typeof settings.maxAge === 'number') { this.maxAge = settings.maxAge; }
|
|
118
|
-
if (typeof settings.noWait === 'boolean') { this.noWait = settings.noWait; }
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export interface IPCClientSettings {
|
|
123
|
-
/**
|
|
124
|
-
* IPC Server host to connect to. Default is `"localhost"`
|
|
125
|
-
*/
|
|
126
|
-
host?: string;
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* IPC Server port number
|
|
130
|
-
*/
|
|
131
|
-
port: number;
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Whether to use a secure connection to the server. Strongly recommended if `host` is not `"localhost"`. Default is `false`
|
|
135
|
-
*/
|
|
136
|
-
ssl?: boolean;
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Token used in the IPC Server configuration (optional). The server will refuse connections using the wrong token.
|
|
140
|
-
*/
|
|
141
|
-
token?: string;
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Determines the role of this IPC client. Only 1 process can be assigned the 'master' role, all other processes must use the role 'worker'
|
|
145
|
-
*/
|
|
146
|
-
role: 'master'|'worker';
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
export type AceBaseServerSettings = Partial<{
|
|
150
|
-
/**
|
|
151
|
-
* Level of messages logged to console
|
|
152
|
-
*/
|
|
153
|
-
logLevel: 'verbose'|'log'|'warn'|'error';
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* ip or hostname to start the server on
|
|
157
|
-
*/
|
|
158
|
-
host: string;
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* port number the server will be listening
|
|
162
|
-
*/
|
|
163
|
-
port: number;
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* target directory path to store/open the database. Default is '.'
|
|
167
|
-
*/
|
|
168
|
-
path: string;
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* Whether to use secure sockets layer (ssl)
|
|
172
|
-
*/
|
|
173
|
-
https: AceBaseServerHttpsSettings;
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* settings that define if and how authentication is used
|
|
177
|
-
*/
|
|
178
|
-
authentication: Partial<AceBaseServerAuthenticationSettings>;
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* maximum size to allow for posted data, eg for updating nodes. Default is '10mb'
|
|
182
|
-
*/
|
|
183
|
-
maxPayloadSize: string;
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Value to use for Access-Control-Allow-Origin CORS header. Default is '*'
|
|
187
|
-
*/
|
|
188
|
-
allowOrigin: string;
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Email settings that enable AceBaseServer to send e-mails, eg for welcoming new users, to reset passwords, notify of new sign ins etc
|
|
192
|
-
*/
|
|
193
|
-
email: AceBaseServerEmailSettings;
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Transaction logging settings. Warning: BETA stage, do NOT use in production yet
|
|
197
|
-
*/
|
|
198
|
-
transactions: Partial<AceBaseServerTransactionSettings>;
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* IPC settings for pm2 or cloud-based clusters. BETA stage, see https://github.com/appy-one/acebase-ipc-server
|
|
202
|
-
*/
|
|
203
|
-
ipc: IPCClientSettings;
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Allows overriding of default storage settings used by the database. ALPHA stage
|
|
207
|
-
*/
|
|
208
|
-
storage: AceBaseStorageSettings;
|
|
209
|
-
}>
|
|
210
|
-
|
|
211
|
-
export class AceBaseServerConfig {
|
|
212
|
-
|
|
213
|
-
readonly logLevel: 'verbose'|'log'|'warn'|'error' = 'log';
|
|
214
|
-
readonly host: string = 'localhost';
|
|
215
|
-
readonly port: number = 3000;
|
|
216
|
-
readonly path: string = '.';
|
|
217
|
-
readonly maxPayloadSize: string = '10mb';
|
|
218
|
-
readonly allowOrigin: string = '*';
|
|
219
|
-
readonly https: AceBaseServerHttpsConfig;
|
|
220
|
-
readonly auth: AceBaseServerAuthenticationSettings;
|
|
221
|
-
readonly email: AceBaseServerEmailSettings;
|
|
222
|
-
readonly transactions: AceBaseServerTransactionSettings;
|
|
223
|
-
readonly ipc: IPCClientSettings;
|
|
224
|
-
readonly storage?: AceBaseStorageSettings;
|
|
225
|
-
|
|
226
|
-
constructor(settings: AceBaseServerSettings) {
|
|
227
|
-
if (typeof settings !== "object") { settings = {}; }
|
|
228
|
-
if (typeof settings.logLevel === 'string') { this.logLevel = settings.logLevel; }
|
|
229
|
-
if (typeof settings.host === 'string') { this.host = settings.host; }
|
|
230
|
-
if (typeof settings.port === 'number') { this.port = settings.port; }
|
|
231
|
-
if (typeof settings.path === 'string') { this.path = settings.path; }
|
|
232
|
-
this.https = new AceBaseServerHttpsConfig(settings.https);
|
|
233
|
-
this.auth = new AceBaseServerAuthenticationSettings(settings.authentication);
|
|
234
|
-
if (typeof settings.maxPayloadSize === 'string') { this.maxPayloadSize = settings.maxPayloadSize; }
|
|
235
|
-
if (typeof settings.allowOrigin === 'string') { this.allowOrigin = settings.allowOrigin; }
|
|
236
|
-
if (typeof settings.email === 'object') { this.email = settings.email; }
|
|
237
|
-
this.transactions = new AceBaseServerTransactionSettings(settings.transactions);
|
|
238
|
-
this.ipc = settings.ipc;
|
|
239
|
-
if (typeof settings.storage === 'object') { this.storage = settings.storage; }
|
|
240
|
-
}
|
|
241
|
-
}
|
package/src/shared/clients.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { EventSubscriptionCallback, IApiQuery, IApiQueryOptions } from "acebase-core/src/api";
|
|
2
|
-
import { DbUserAccountDetails } from "../schema/user";
|
|
3
|
-
import { HttpSocket } from "./http";
|
|
4
|
-
|
|
5
|
-
export class ConnectedClient {
|
|
6
|
-
readonly id: string;
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
*
|
|
10
|
-
* @param socket Socket object used by the framework
|
|
11
|
-
* @param id optional: use if the socket object does not have an `id` property.
|
|
12
|
-
*/
|
|
13
|
-
constructor (public socket: HttpSocket, id?: string) {
|
|
14
|
-
this.id = id ?? socket.id;
|
|
15
|
-
if (!this.id) { throw new Error('Socket has no id'); }
|
|
16
|
-
}
|
|
17
|
-
// get id() { return this.socket.id; };
|
|
18
|
-
readonly connectedDate: Date = new Date();
|
|
19
|
-
|
|
20
|
-
/** user details if this socket client is signed in */
|
|
21
|
-
user?: DbUserAccountDetails;
|
|
22
|
-
|
|
23
|
-
/** Active event subscriptions for this client */
|
|
24
|
-
subscriptions: { [path: string]: Array<{ path: string, event: string, callback: EventSubscriptionCallback }> } = {};
|
|
25
|
-
|
|
26
|
-
/** Active realtime query subscriptions for this client */
|
|
27
|
-
realtimeQueries: { [id: string]: { path: string; query: IApiQuery, options: IApiQueryOptions }} = {};
|
|
28
|
-
|
|
29
|
-
/** Currently running transactions */
|
|
30
|
-
transactions: { [id: string]: { id: string; started: number; path: string; context: any; finish?: (val?: any) => Promise<{ cursor?: string }>; timeout: NodeJS.Timeout } } = {};
|
|
31
|
-
}
|
package/src/shared/email.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
export interface AceBaseEmailRequest {
|
|
2
|
-
/** email request type */
|
|
3
|
-
type: string;
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
export interface AceBaseUserEmailRequest extends AceBaseEmailRequest {
|
|
7
|
-
user: { uid: string; email: string; username?: string; displayName?: string; settings?: any };
|
|
8
|
-
ip: string;
|
|
9
|
-
date: Date
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export interface AceBaseUserSignupEmailRequest extends AceBaseUserEmailRequest {
|
|
13
|
-
type: 'user_signup';
|
|
14
|
-
activationCode: string;
|
|
15
|
-
emailVerified: boolean;
|
|
16
|
-
provider: string;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export interface AceBaseUserSignInEmailRequest extends AceBaseUserEmailRequest {
|
|
20
|
-
type: 'user_signin';
|
|
21
|
-
activationCode: string;
|
|
22
|
-
emailVerified: boolean;
|
|
23
|
-
provider: string;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export interface AceBaseUserResetPasswordEmailRequest extends AceBaseUserEmailRequest {
|
|
27
|
-
type: 'user_reset_password';
|
|
28
|
-
resetCode: string;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface AceBaseUserResetPasswordSuccessEmailRequest extends AceBaseUserEmailRequest {
|
|
32
|
-
type: 'user_reset_password_success';
|
|
33
|
-
}
|
package/src/shared/env.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import type { HttpApp, Request } from './http';
|
|
2
|
-
import type { AceBase, DataReference } from 'acebase';
|
|
3
|
-
import type { AceBaseServerConfig } from '../settings';
|
|
4
|
-
import type { DbUserAccountDetails } from '../schema/user';
|
|
5
|
-
import type { ConnectedClient } from './clients';
|
|
6
|
-
import type { DebugLogger, SimpleCache } from 'acebase-core';
|
|
7
|
-
import type { OAuth2Provider } from '../oauth-providers/oauth-provider';
|
|
8
|
-
import type { Server as HttpServer } from 'http';
|
|
9
|
-
import type { Server as SecureHttpServer } from 'https';
|
|
10
|
-
import type { PathBasedRules } from '../rules';
|
|
11
|
-
import { Api } from 'acebase-core/src/api';
|
|
12
|
-
|
|
13
|
-
export interface RouteInitEnvironment {
|
|
14
|
-
server: HttpServer | SecureHttpServer;
|
|
15
|
-
app: HttpApp;
|
|
16
|
-
config: AceBaseServerConfig;
|
|
17
|
-
db: AceBase & { api: Api };
|
|
18
|
-
authDb: AceBase;
|
|
19
|
-
debug: DebugLogger;
|
|
20
|
-
securityRef: DataReference;
|
|
21
|
-
authRef: DataReference;
|
|
22
|
-
logRef: DataReference;
|
|
23
|
-
tokenSalt: string;
|
|
24
|
-
clients: Map<string, ConnectedClient>;
|
|
25
|
-
authCache: SimpleCache<string, DbUserAccountDetails>;
|
|
26
|
-
authProviders: { [providerName: string]: OAuth2Provider };
|
|
27
|
-
rules: PathBasedRules
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
export interface RouteRequestEnvironment {
|
|
31
|
-
/** If the request has an Authentication: bearer token, the user will be bound to the incoming request */
|
|
32
|
-
user?: DbUserAccountDetails;
|
|
33
|
-
|
|
34
|
-
/** If context is sent through AceBase-Context header, it will be bound to the incoming request */
|
|
35
|
-
context: { [key: string]: any };
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
export type RouteRequest<Params = any, ResBody = any, ReqBody = any, ReqQuery = any, Locals extends Record<string, any> = Record<string, any>> = Request<Params, ResBody, ReqBody, ReqQuery, Locals> & RouteRequestEnvironment;
|
package/src/shared/error.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { Response } from './http';
|
|
2
|
-
|
|
3
|
-
export const sendNotAuthenticatedError = (res: Response, code: string, message: string) => {
|
|
4
|
-
res.statusCode = 401; // Unauthorized (not unauthenticated)
|
|
5
|
-
res.statusMessage = 'Not Authenticated';
|
|
6
|
-
res.contentType('application/json').send({ code, message });
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
export const sendUnauthorizedError = (res: Response, code: string, message: string) => {
|
|
10
|
-
res.statusCode = 403; // Forbidden
|
|
11
|
-
res.statusMessage = 'Unauthorized';
|
|
12
|
-
res.contentType('application/json').send({ code, message });
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
interface ErrorLike {
|
|
16
|
-
code?: string;
|
|
17
|
-
message: string;
|
|
18
|
-
stack?: string;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export const sendError = (res: Response, err: ErrorLike) => {
|
|
22
|
-
res.contentType('application/json');
|
|
23
|
-
if (typeof err.code === 'string') {
|
|
24
|
-
sendBadRequestError(res, err as { code: string; message: string }); //res.status(400).send({ code: err.code, message: err.message }); // Bad Request
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
sendUnexpectedError(res, err as Error); // res.status(500).send({ code: 'unknown', message: 'server error', details: err.message }); // Internal server error
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export const sendBadRequestError = (res: Response, err: { code: string, message: string }) => {
|
|
32
|
-
res.status(400).contentType('application/json').send({ code: err.code, message: err.message }); // Bad Request
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export const sendUnexpectedError = (res: Response, err: Error) => {
|
|
36
|
-
res.status(500).contentType('application/json').send({ code: 'unexpected', message: 'server error', details: err.message }); // Internal server error
|
|
37
|
-
}
|
package/src/shared/http.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import type { Socket } from 'socket.io';
|
|
2
|
-
export type { Express, Request, Response } from 'express';
|
|
3
|
-
import * as express from 'express';
|
|
4
|
-
const createExpress = (express as any).default ?? express; // ESM and CJS compatible approach
|
|
5
|
-
|
|
6
|
-
export type HttpApp = express.Express;
|
|
7
|
-
export type HttpSocket = Socket;
|
|
8
|
-
export type HttpRequest = express.Request;
|
|
9
|
-
export type HttpResponse = express.Response;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Creates an app that handles http requests, adds json body parsing.
|
|
13
|
-
* @param settings
|
|
14
|
-
* @returns
|
|
15
|
-
*/
|
|
16
|
-
export const createApp = (settings: { trustProxy: boolean; maxPayloadSize: string }) => {
|
|
17
|
-
const app = createExpress();
|
|
18
|
-
|
|
19
|
-
// When behind a trusted proxy server, req.ip and req.hostname will be set the right way
|
|
20
|
-
app.set('trust proxy', settings.trustProxy);
|
|
21
|
-
|
|
22
|
-
// Parse json request bodies
|
|
23
|
-
app.use(express.json({ limit: settings.maxPayloadSize })); // , extended: true ?
|
|
24
|
-
|
|
25
|
-
return app as HttpApp;
|
|
26
|
-
}
|
package/src/shared/password.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import * as crypto from 'crypto';
|
|
2
|
-
|
|
3
|
-
export const generatePassword = () => {
|
|
4
|
-
return Array.prototype.reduce.call('abcedefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ012345789!@#$%&', (password, c, i, chars) => {
|
|
5
|
-
if (i > 15) { return password; }
|
|
6
|
-
return password + chars[Math.floor(Math.random() * chars.length)];
|
|
7
|
-
}, '');
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Generates a random salt and hashes the provided password with it. The resulting salt and hash should be stored in the database
|
|
12
|
-
* @param password password to hash
|
|
13
|
-
*/
|
|
14
|
-
export const createPasswordHash = (password: string) => {
|
|
15
|
-
let length = 16;
|
|
16
|
-
let salt = crypto.randomBytes(Math.ceil(length/2)).toString('hex').slice(0,length);
|
|
17
|
-
let hash = crypto.createHmac('sha512', salt).update(password).digest('hex');
|
|
18
|
-
return {
|
|
19
|
-
salt,
|
|
20
|
-
hash
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* For checking passwords: uses given password and salt to get the hash that can be compared to a stored version.
|
|
26
|
-
* @param password A password prodived by the user trying to sign in
|
|
27
|
-
* @param salt the salt used to generate the hash, must be stored with the hash in the database
|
|
28
|
-
*/
|
|
29
|
-
export const getPasswordHash = (password: string, salt: string) => {
|
|
30
|
-
return crypto.createHmac('sha512', salt).update(password).digest('hex');
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Backward compatibility with old saltless md5 passwords. Becomes obsolete once all passwords have been updated (probably already so)
|
|
35
|
-
*/
|
|
36
|
-
export const getOldPasswordHash = (password: string) => {
|
|
37
|
-
// Backward compatibility with old saltless md5 passwords.
|
|
38
|
-
// Becomes obsolete once all passwords have been updated
|
|
39
|
-
return crypto.createHash('md5').update(password).digest('hex');
|
|
40
|
-
};
|
|
41
|
-
|
package/src/shared/rootpath.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { resolve } from "path";
|
|
2
|
-
|
|
3
|
-
// This file is in ./src/shared/rootpath.ts
|
|
4
|
-
// When this code runs it is at ./dist/cjs/shared/rootpath.js or ./dist/esm/shared/rootpath.js
|
|
5
|
-
// So, the package root is at ../../..
|
|
6
|
-
let currentDir = __dirname;
|
|
7
|
-
|
|
8
|
-
if (process.platform === 'win32' && currentDir.startsWith('/')) {
|
|
9
|
-
// tsc-esm-fix does not handle win32 file urls correctly, the drive letter in import.meta.url is prefixed with a slash: file:///C:/dir/file.js
|
|
10
|
-
currentDir = currentDir.slice(1);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export const packageRootPath = resolve(currentDir, '../../..');
|
|
14
|
-
|
|
15
|
-
export default packageRootPath;
|
package/src/shared/signin.ts
DELETED
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import { ID } from "acebase-core";
|
|
2
|
-
import { DbUserAccountDetails } from "../schema/user";
|
|
3
|
-
import { RouteInitEnvironment, RouteRequest } from "./env";
|
|
4
|
-
import { createPasswordHash, getOldPasswordHash, getPasswordHash } from "./password";
|
|
5
|
-
import { decodePublicAccessToken } from "./tokens";
|
|
6
|
-
|
|
7
|
-
export type SignInCredentials =
|
|
8
|
-
{ method: 'token'; access_token: string } |
|
|
9
|
-
{ method: 'private_token'; access_token: string } |
|
|
10
|
-
{ method: 'email'; email: string; password: string } |
|
|
11
|
-
{ method: 'account', username: string, password: string };
|
|
12
|
-
|
|
13
|
-
export class SignInError extends Error {
|
|
14
|
-
constructor(public code: string, message: string, public details: Object = {}) {
|
|
15
|
-
super(message);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Signs in a user and logs the request. If successful, adds the user to authCache, binds the user to the http request and returns the user details.
|
|
21
|
-
* Throws a `SignInError` if sign in fails for a known reason.
|
|
22
|
-
* @param credentials credentials to sign in the user with
|
|
23
|
-
* @param env environment state
|
|
24
|
-
* @param req current http request
|
|
25
|
-
* @returns
|
|
26
|
-
*/
|
|
27
|
-
export const signIn = async (credentials: SignInCredentials, env: RouteInitEnvironment, req: RouteRequest) => {
|
|
28
|
-
try {
|
|
29
|
-
const query = env.authRef.query();
|
|
30
|
-
let tokenDetails: ReturnType<typeof decodePublicAccessToken>;
|
|
31
|
-
switch (credentials.method) {
|
|
32
|
-
case 'token': {
|
|
33
|
-
if (typeof credentials.access_token !== 'string') {
|
|
34
|
-
throw new SignInError('invalid_details', 'sign in request has invalid arguments');
|
|
35
|
-
}
|
|
36
|
-
try {
|
|
37
|
-
tokenDetails = decodePublicAccessToken(credentials.access_token, env.tokenSalt);
|
|
38
|
-
query.filter('access_token', '==', tokenDetails.access_token);
|
|
39
|
-
}
|
|
40
|
-
catch (err) {
|
|
41
|
-
throw new SignInError('invalid_token', err.message);
|
|
42
|
-
}
|
|
43
|
-
break;
|
|
44
|
-
}
|
|
45
|
-
case 'private_token': {
|
|
46
|
-
// Method used internally: uses the access token extracted from a public access token (see tokenDetails.access_token in above 'token' case)
|
|
47
|
-
if (typeof credentials.access_token !== 'string') {
|
|
48
|
-
throw new SignInError('invalid_details', 'sign in request has invalid arguments');
|
|
49
|
-
}
|
|
50
|
-
query.filter('access_token', '==', credentials.access_token);
|
|
51
|
-
break;
|
|
52
|
-
}
|
|
53
|
-
case 'email': {
|
|
54
|
-
if (typeof credentials.email !== 'string' || typeof credentials.password !== 'string') {
|
|
55
|
-
throw new SignInError('invalid_details', 'sign in request has invalid arguments');
|
|
56
|
-
}
|
|
57
|
-
query.filter('email', '==', credentials.email);
|
|
58
|
-
break;
|
|
59
|
-
}
|
|
60
|
-
case 'account': {
|
|
61
|
-
if (typeof credentials.username !== 'string' || typeof credentials.password !== 'string') {
|
|
62
|
-
throw new SignInError('invalid_details', 'sign in request has invalid arguments');
|
|
63
|
-
}
|
|
64
|
-
query.filter('username', '==', credentials.username);
|
|
65
|
-
break;
|
|
66
|
-
}
|
|
67
|
-
default: {
|
|
68
|
-
throw new SignInError('invalid_method', `Unsupported sign in method ${(credentials as any).method}`);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const snaps = await query.get();
|
|
73
|
-
if (snaps.length === 0) {
|
|
74
|
-
throw new SignInError('not_found', `account not found`);
|
|
75
|
-
}
|
|
76
|
-
else if (snaps.length > 1) {
|
|
77
|
-
throw new SignInError('duplicate', `${snaps.length} users found with the same ${credentials.method}. Contact your database administrator`, { count: snaps.length });
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const snap = snaps[0];
|
|
81
|
-
const user: DbUserAccountDetails = snap.val();
|
|
82
|
-
user.uid = snap.key as string;
|
|
83
|
-
|
|
84
|
-
if (user.is_disabled === true) {
|
|
85
|
-
throw new SignInError('account_disabled', 'Your account has been disabled. Contact your database administrator');
|
|
86
|
-
}
|
|
87
|
-
if (credentials.method === 'token' && tokenDetails.uid !== user.uid) {
|
|
88
|
-
throw new SignInError('token_mismatch', 'Sign in again');
|
|
89
|
-
}
|
|
90
|
-
if (credentials.method === 'account' || credentials.method === 'email') {
|
|
91
|
-
// Check password
|
|
92
|
-
let hash = user.password_salt ? getPasswordHash(credentials.password, user.password_salt) : getOldPasswordHash(credentials.password);
|
|
93
|
-
if (user.password !== hash) {
|
|
94
|
-
throw new SignInError('wrong_password', 'Incorrect password');
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// Keep track of properties to update, both in db and in our object
|
|
99
|
-
const updates: Partial<DbUserAccountDetails> = {
|
|
100
|
-
// Update prev / last sign in stats
|
|
101
|
-
prev_signin: user.last_signin,
|
|
102
|
-
prev_signin_ip: user.last_signin_ip,
|
|
103
|
-
last_signin: new Date(),
|
|
104
|
-
last_signin_ip: req.ip
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
if ('password' in credentials) {
|
|
108
|
-
if (!user.password_salt) {
|
|
109
|
-
// OLD md5 password hash, convert to new salted hash
|
|
110
|
-
let pwd = createPasswordHash(credentials.password);
|
|
111
|
-
updates.password = pwd.hash;
|
|
112
|
-
updates.password_salt = pwd.salt;
|
|
113
|
-
}
|
|
114
|
-
if (!user.access_token) {
|
|
115
|
-
// Generate access token
|
|
116
|
-
updates.access_token = ID.generate();
|
|
117
|
-
updates.access_token_created = new Date();
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// Update user object
|
|
122
|
-
Object.assign(user, updates);
|
|
123
|
-
|
|
124
|
-
// Update db
|
|
125
|
-
await snap.ref.update(updates);
|
|
126
|
-
|
|
127
|
-
// Log history item
|
|
128
|
-
env.logRef.push({ action: `signin`, type: credentials.method, [credentials.method]: credentials[credentials.method], ip: req.ip, date: new Date(), success: true });
|
|
129
|
-
|
|
130
|
-
// Add to cache
|
|
131
|
-
env.authCache.set(user.uid, user);
|
|
132
|
-
|
|
133
|
-
// Bind user to current request
|
|
134
|
-
req.user = user;
|
|
135
|
-
|
|
136
|
-
return user;
|
|
137
|
-
}
|
|
138
|
-
catch (err) {
|
|
139
|
-
// Log error
|
|
140
|
-
env.logRef.push({ action: 'signin', type: credentials.method, [credentials.method]: credentials[credentials.method], success: false, code: err.code || 'unexpected', message: err.code ? null : err.message, ip: req.ip, date: new Date(), ...err.details });
|
|
141
|
-
throw err;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { request } from 'https';
|
|
2
|
-
export interface SimpleFetchResponse {
|
|
3
|
-
readonly status: number;
|
|
4
|
-
readonly headers: { get(name: string): string }
|
|
5
|
-
text(): Promise<string>
|
|
6
|
-
json(): Promise<any>
|
|
7
|
-
arrayBuffer(): Promise<ArrayBuffer>
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* Very lightweight custom fetch implementation to avoid additional dependencies
|
|
11
|
-
* @param url
|
|
12
|
-
* @param options
|
|
13
|
-
*/
|
|
14
|
-
export function fetch(url: string, options?: { method?: 'GET'|'POST'|'PUT'|'DELETE', headers?: { [name: string]: string }, body?: string }): Promise<SimpleFetchResponse> {
|
|
15
|
-
return new Promise((resolve, reject) => {
|
|
16
|
-
const method = options?.method || 'GET';
|
|
17
|
-
const headers = options?.headers;
|
|
18
|
-
const req = request(url, { method, headers }, res => {
|
|
19
|
-
|
|
20
|
-
// res.setEncoding('binary'); // will result in strings!!!!
|
|
21
|
-
const ready = new Promise<Buffer>((resolve, reject) => {
|
|
22
|
-
let chunks = [];
|
|
23
|
-
res.on('data', data => {
|
|
24
|
-
chunks.push(data);
|
|
25
|
-
});
|
|
26
|
-
res.on('end', () => {
|
|
27
|
-
const buffer = Buffer.concat(chunks);
|
|
28
|
-
resolve(buffer);
|
|
29
|
-
});
|
|
30
|
-
res.on('error', (err) => {
|
|
31
|
-
reject(err);
|
|
32
|
-
})
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
const response: SimpleFetchResponse = {
|
|
36
|
-
get status() { return res.statusCode },
|
|
37
|
-
get headers() { return {
|
|
38
|
-
get(name: string) {
|
|
39
|
-
let val = res.headers[name.toLowerCase()];
|
|
40
|
-
if (val instanceof Array) { return val.join(', '); }
|
|
41
|
-
return val;
|
|
42
|
-
}
|
|
43
|
-
} },
|
|
44
|
-
async text() {
|
|
45
|
-
const buffer = await ready;
|
|
46
|
-
return buffer.toString('utf8');
|
|
47
|
-
},
|
|
48
|
-
json() {
|
|
49
|
-
// return Promise.resolve(JSON.parse(body));
|
|
50
|
-
return this.text().then(json => JSON.parse(json));
|
|
51
|
-
},
|
|
52
|
-
async arrayBuffer() {
|
|
53
|
-
const data = await ready;
|
|
54
|
-
const arrayBuffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
|
|
55
|
-
return arrayBuffer;
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
resolve(response);
|
|
60
|
-
});
|
|
61
|
-
req.on('error', reject);
|
|
62
|
-
options?.body && req.write(options.body, 'utf8');
|
|
63
|
-
req.end();
|
|
64
|
-
})
|
|
65
|
-
}
|