acebase-server 1.10.1 → 1.13.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/.vscode/launch.json +24 -0
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +16 -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/oauth-providers/dropbox.js +3 -2
- package/dist/cjs/oauth-providers/facebook.js +3 -2
- package/dist/cjs/oauth-providers/google.js +3 -2
- package/dist/cjs/oauth-providers/instagram.js +3 -2
- package/dist/cjs/oauth-providers/oauth-provider.js +23 -0
- package/dist/cjs/oauth-providers/spotify.js +3 -2
- 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 +11 -7
- package/dist/cjs/routes/data-transaction.js +30 -10
- package/dist/cjs/routes/data-update.js +11 -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 +15 -11
- package/dist/cjs/rules.js +5 -5
- package/dist/cjs/server.js +6 -4
- package/dist/cjs/settings/index.js +6 -7
- 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 +43 -24
- package/dist/cjs/websocket/socket.io.js +8 -11
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +2 -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/oauth-providers/dropbox.js +3 -2
- package/dist/esm/oauth-providers/facebook.js +3 -2
- package/dist/esm/oauth-providers/google.js +3 -2
- package/dist/esm/oauth-providers/instagram.js +3 -2
- package/dist/esm/oauth-providers/oauth-provider.js +21 -1
- package/dist/esm/oauth-providers/spotify.js +3 -2
- 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 +9 -5
- package/dist/esm/routes/data-transaction.js +28 -8
- package/dist/esm/routes/data-update.js +9 -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 +14 -10
- package/dist/esm/rules.js +5 -5
- package/dist/esm/server.js +6 -4
- package/dist/esm/settings/index.js +5 -6
- 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 +38 -23
- package/dist/esm/websocket/socket.io.js +9 -11
- package/dist/types/index.d.ts +2 -0
- package/dist/types/logger.d.ts +35 -0
- package/dist/types/middleware/admin-only.d.ts +19 -0
- package/dist/types/oauth-providers/dropbox.d.ts +3 -14
- package/dist/types/oauth-providers/facebook.d.ts +3 -7
- package/dist/types/oauth-providers/google.d.ts +3 -7
- package/dist/types/oauth-providers/index.d.ts +2 -10
- package/dist/types/oauth-providers/instagram.d.ts +3 -7
- package/dist/types/oauth-providers/oauth-provider.d.ts +25 -21
- package/dist/types/oauth-providers/spotify.d.ts +3 -7
- package/dist/types/routes/data-query.d.ts +1 -1
- package/dist/types/routes/meta-logs.d.ts +20 -0
- package/dist/types/rules.d.ts +4 -4
- package/dist/types/server.d.ts +9 -7
- package/dist/types/settings/email.d.ts +1 -1
- package/dist/types/settings/index.d.ts +5 -6
- package/dist/types/shared/clients.d.ts +3 -1
- package/dist/types/shared/env.d.ts +6 -4
- package/dist/types/shared/http.d.ts +2 -0
- package/dist/types/shared/signin.d.ts +1 -1
- package/dist/types/websocket/index.d.ts +4 -0
- package/dist/types/websocket/manager.d.ts +1 -1
- package/package.json +8 -8
- package/webmanager/acebase-client-browser.min.js +16 -0
- package/webmanager/index.html +2 -2
- package/webmanager/index.js +3 -6
- 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 -2
- 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 -143
- package/src/oauth-providers/facebook.ts +0 -102
- package/src/oauth-providers/google.ts +0 -146
- package/src/oauth-providers/index.ts +0 -20
- package/src/oauth-providers/instagram.ts +0 -97
- package/src/oauth-providers/oauth-provider.ts +0 -55
- package/src/oauth-providers/spotify.ts +0 -126
- 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 -76
- package/src/routes/data-set.yaml +0 -71
- 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 -76
- package/src/routes/data-transaction-start.yaml +0 -58
- package/src/routes/data-transaction.ts +0 -144
- package/src/routes/data-update.ts +0 -77
- package/src/routes/data-update.yaml +0 -71
- 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 -281
- 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 -416
- package/src/settings/email.ts +0 -18
- package/src/settings/index.ts +0 -240
- 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 -24
- 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 -91
- package/tsconfig-cjs.json +0 -10
- package/tsconfig.json +0 -21
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
/oauth2/{dbname}/refresh:
|
|
2
|
-
get:
|
|
3
|
-
tags:
|
|
4
|
-
- auth
|
|
5
|
-
- oauth2
|
|
6
|
-
summary: Refreshes an OAuth2 access token
|
|
7
|
-
description: Refreshes an OAuth2 provider's access token using the refresh token given by the auth provider at signin.
|
|
8
|
-
parameters:
|
|
9
|
-
- name: dbname
|
|
10
|
-
in: path
|
|
11
|
-
description: name of the database
|
|
12
|
-
required: true
|
|
13
|
-
schema:
|
|
14
|
-
type: string
|
|
15
|
-
example: mydb
|
|
16
|
-
- name: provider
|
|
17
|
-
in: query
|
|
18
|
-
description: External OAuth2 provider name
|
|
19
|
-
required: true
|
|
20
|
-
schema:
|
|
21
|
-
type: string
|
|
22
|
-
example: facebook
|
|
23
|
-
- name: refresh_token
|
|
24
|
-
in: query
|
|
25
|
-
description: A refresh token from the OAuth2 signin result
|
|
26
|
-
required: true
|
|
27
|
-
schema:
|
|
28
|
-
type: string
|
|
29
|
-
example: erfiyh3rfig3fuyb3rjfg3u7refc...
|
|
30
|
-
responses:
|
|
31
|
-
200:
|
|
32
|
-
description: Returns a JSON object containing new access and refresh tokens and expiry information
|
|
33
|
-
content:
|
|
34
|
-
'application/json':
|
|
35
|
-
schema:
|
|
36
|
-
type: object
|
|
37
|
-
properties:
|
|
38
|
-
name:
|
|
39
|
-
type: string
|
|
40
|
-
description: Provider name
|
|
41
|
-
example: facebook
|
|
42
|
-
access_token:
|
|
43
|
-
type: string
|
|
44
|
-
description: New access token
|
|
45
|
-
example: fkw3ygfi74g25foli2n4vo7h2vto72g4...
|
|
46
|
-
refresh_token:
|
|
47
|
-
type: string
|
|
48
|
-
description: New refresh token
|
|
49
|
-
example: 32lo548jnl34tv73h4l5nbb35ithn3jt...
|
|
50
|
-
expires_in:
|
|
51
|
-
type: integer
|
|
52
|
-
description: Amount of seconds these tokens remain valid. A new refresh will have to be done before they expire.
|
|
53
|
-
example: 3600
|
|
54
|
-
example:
|
|
55
|
-
name: facebook
|
|
56
|
-
access_token: fkw3ygfi74g25foli2n4vo7h2vto72g4...
|
|
57
|
-
refresh_token: 32lo548jnl34tv73h4l5nbb35ithn3jt...
|
|
58
|
-
expires_in: 3600
|
|
59
|
-
500:
|
|
60
|
-
description: If anything goes wrong on the server, it returns the actual error text in plain text
|
|
61
|
-
content:
|
|
62
|
-
'text/plain':
|
|
63
|
-
schema:
|
|
64
|
-
type: string
|
|
65
|
-
example: Some error text
|
|
@@ -1,281 +0,0 @@
|
|
|
1
|
-
import { DataSnapshot, ID } from 'acebase-core';
|
|
2
|
-
import { DbUserAccountDetails } from '../schema/user';
|
|
3
|
-
import { AceBaseUserSignInEmailRequest, AceBaseUserSignupEmailRequest } from '../shared/email';
|
|
4
|
-
import { RouteInitEnvironment, RouteRequest } from '../shared/env';
|
|
5
|
-
import { createPasswordHash, generatePassword } from '../shared/password';
|
|
6
|
-
import { createPublicAccessToken, createSignedPublicToken, parseSignedPublicToken } from '../shared/tokens';
|
|
7
|
-
import { fetch } from '../shared/simple-fetch';
|
|
8
|
-
|
|
9
|
-
const socketSignInSuccess = `<html><script>window.close()</script><body>Signed in succesfully. You can <a href="javascript:window.close()">close</a> this page</body></html>`;
|
|
10
|
-
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>`;
|
|
11
|
-
|
|
12
|
-
export type RequestQuery = { state: string; code?: string; error?: string; error_reason?: string; error_description?: string };
|
|
13
|
-
export type RequestBody = null;
|
|
14
|
-
export type ResponseBody = string | { code: 'admin_only', message: string };
|
|
15
|
-
export type Request = RouteRequest<any, ResponseBody, RequestBody, RequestQuery>;
|
|
16
|
-
|
|
17
|
-
export const addRoute = (env: RouteInitEnvironment) => {
|
|
18
|
-
|
|
19
|
-
env.app.get(`/oauth2/${env.db.name}/signin`, async (req: Request, res) => {
|
|
20
|
-
|
|
21
|
-
// This is where the user is redirected to by the provider after signin or error
|
|
22
|
-
try {
|
|
23
|
-
const state = parseSignedPublicToken(req.query.state, env.tokenSalt);
|
|
24
|
-
if (req.query.error) {
|
|
25
|
-
if (state.flow === 'socket') {
|
|
26
|
-
const client = env.clients.get(state.client_id);
|
|
27
|
-
client.socket.emit('oauth2-signin', { error: req.query.error, reason: req.query.error_reason, description: req.query.error_description, provider: state.provider });
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
const callbackUrl = `${state.callbackUrl}?provider=${state.provider}&error=${req.query.error}&reason=${req.query.error_reason}&description=${req.query.error_description}`;
|
|
31
|
-
res.redirect(callbackUrl);
|
|
32
|
-
}
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// Got authorization code
|
|
37
|
-
const authCode = req.query.code;
|
|
38
|
-
const provider = env.authProviders[state.provider];
|
|
39
|
-
|
|
40
|
-
// Get access & refresh tokens
|
|
41
|
-
const tokens = await provider.getAccessToken({ type: 'auth', auth_code: authCode, redirect_url: `${req.protocol}://${req.headers.host}/oauth2/${env.db.name}/signin` });
|
|
42
|
-
|
|
43
|
-
let user_details;
|
|
44
|
-
// TODO: Have we got an id_token?
|
|
45
|
-
// if (tokens.id_token) {
|
|
46
|
-
// // decode, extract user information
|
|
47
|
-
// }
|
|
48
|
-
// else {
|
|
49
|
-
user_details = await provider.getUserInfo(tokens.access_token);
|
|
50
|
-
// }
|
|
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 = await 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
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
const image = await response.arrayBuffer();
|
|
64
|
-
let 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
|
-
catch(err) {
|
|
69
|
-
env.debug.warn(`Could not fetch profile picture from "${best.url}": `, err);
|
|
70
|
-
user_details.picture = null;
|
|
71
|
-
}
|
|
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
|
-
|
|
90
|
-
// Check if this user exists in the database
|
|
91
|
-
let snaps: DataSnapshot[];
|
|
92
|
-
let addToExistingAccount = true;
|
|
93
|
-
if (typeof state.uid === 'string') {
|
|
94
|
-
// Use the signed in uid to link this account to. This allows multiple auth provider
|
|
95
|
-
// accounts (with different email addresses) to be linked to the account the user
|
|
96
|
-
// is signed into, and also allows multiple AceBase users to link to the same provider
|
|
97
|
-
// accounts, eg if a client app allows users to link their own account to a shared
|
|
98
|
-
// family Spotify / Dropbox account.
|
|
99
|
-
let snap = await env.authRef.child(state.uid).get();
|
|
100
|
-
if (!snap.exists()) {
|
|
101
|
-
// This is wrong!
|
|
102
|
-
throw new Error(`Invalid uid`);
|
|
103
|
-
}
|
|
104
|
-
snaps = [snap];
|
|
105
|
-
let user = snap.val();
|
|
106
|
-
addToExistingAccount = user.email === user_details.email;
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
109
|
-
const query = env.authRef.query();
|
|
110
|
-
if (user_details.email) {
|
|
111
|
-
query.filter('email', '==', user_details.email);
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
// User did not allow reading e-mail address, or provider does not have one (eg whatsapp?)
|
|
115
|
-
// Switch to using a generated username such as "facebook-3292389234" instead
|
|
116
|
-
query.filter('username', '==', providerUsername);
|
|
117
|
-
}
|
|
118
|
-
snaps = await query.get();
|
|
119
|
-
}
|
|
120
|
-
if (snaps.length === 0 && user_details.email) {
|
|
121
|
-
// Try again with providerUsername, use might previously have denied access to email,
|
|
122
|
-
// and now has granted access. In that case, we'll already have an account with the
|
|
123
|
-
// generated providerUsername
|
|
124
|
-
snaps = await env.authRef.query().filter('username', '==', providerUsername).get();
|
|
125
|
-
}
|
|
126
|
-
let user: DbUserAccountDetails;
|
|
127
|
-
if (snaps.length === 1) {
|
|
128
|
-
const uid = snaps[0].key as string;
|
|
129
|
-
user = snaps[0].val();
|
|
130
|
-
user.uid = uid;
|
|
131
|
-
|
|
132
|
-
if (addToExistingAccount) {
|
|
133
|
-
// Update user details
|
|
134
|
-
user.email_verified = user.email_verified || user_details.email_verified;
|
|
135
|
-
user.email = user.email || user_details.email;
|
|
136
|
-
if (user_details.picture && user_details.picture.length > 0) {
|
|
137
|
-
user.picture = user_details.picture[0];
|
|
138
|
-
}
|
|
139
|
-
await env.authRef.child(uid).update({
|
|
140
|
-
email: user.email || null,
|
|
141
|
-
email_verified: user.email_verified,
|
|
142
|
-
last_signin: new Date(),
|
|
143
|
-
last_signin_ip: req.ip,
|
|
144
|
-
picture: user.picture
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
// Add provider details
|
|
148
|
-
await env.authRef.child(uid).child('settings').update(getProviderSettings());
|
|
149
|
-
|
|
150
|
-
// Log success
|
|
151
|
-
env.logRef.push({ action: 'oauth2_signin', success: true, ip: req.ip, date: new Date(), uid });
|
|
152
|
-
|
|
153
|
-
// Cache the user
|
|
154
|
-
env.authCache.set(user.uid, user);
|
|
155
|
-
|
|
156
|
-
// Request signin e-mail to be sent
|
|
157
|
-
const request: AceBaseUserSignInEmailRequest = {
|
|
158
|
-
type: 'user_signin',
|
|
159
|
-
user: {
|
|
160
|
-
uid: user.uid,
|
|
161
|
-
username: user.username,
|
|
162
|
-
email: user.email,
|
|
163
|
-
displayName: user.display_name,
|
|
164
|
-
settings: user.settings
|
|
165
|
-
},
|
|
166
|
-
date: user.created,
|
|
167
|
-
ip: req.ip,
|
|
168
|
-
activationCode: user.email_verified ? null : createSignedPublicToken({ uid: user.uid }, env.tokenSalt),
|
|
169
|
-
emailVerified: user.email_verified,
|
|
170
|
-
provider: state.provider
|
|
171
|
-
};
|
|
172
|
-
|
|
173
|
-
env.config.email?.send(request).catch(err => {
|
|
174
|
-
env.logRef.push({ action: 'oauth2_login_email', success: false, code: 'unexpected', ip: req.ip, date: new Date(), error: err.message, request });
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
else if (snaps.length === 0) {
|
|
178
|
-
// User does not exist, create
|
|
179
|
-
|
|
180
|
-
if (!env.config.auth.allowUserSignup) {
|
|
181
|
-
env.logRef.push({ action: 'oauth2_signup', success: false, code: 'user_signup_disabled', provider: state.provider, email: user_details.email, date: new Date() });
|
|
182
|
-
res.statusCode = 403; // Forbidden
|
|
183
|
-
return res.send({ code: 'admin_only', message: 'Only admin is allowed to create users' });
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
// Create user with Generated password
|
|
187
|
-
let pwd = createPasswordHash(generatePassword());
|
|
188
|
-
user = {
|
|
189
|
-
uid: null,
|
|
190
|
-
username: typeof user_details.email === 'undefined' ? providerUsername : null, // provider-accountid usernames for external accounts without email address
|
|
191
|
-
email: user_details.email || null,
|
|
192
|
-
email_verified: user_details.email_verified, // trust provider's verification
|
|
193
|
-
display_name: user_details.display_name,
|
|
194
|
-
password: pwd.hash,
|
|
195
|
-
password_salt: pwd.salt,
|
|
196
|
-
created: new Date(),
|
|
197
|
-
created_ip: req.ip,
|
|
198
|
-
access_token: ID.generate(),
|
|
199
|
-
access_token_created: new Date(),
|
|
200
|
-
last_signin: new Date(),
|
|
201
|
-
last_signin_ip: req.ip,
|
|
202
|
-
picture: user_details.picture && user_details.picture[0],
|
|
203
|
-
settings: getProviderSettings()
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
const userRef = await env.authRef.push(user);
|
|
207
|
-
const uid = userRef.key;
|
|
208
|
-
user.uid = uid;
|
|
209
|
-
|
|
210
|
-
// Log success
|
|
211
|
-
env.logRef.push({ action: 'oauth2_signup', success: true, ip: req.ip, date: new Date(), uid });
|
|
212
|
-
|
|
213
|
-
// Cache the user
|
|
214
|
-
env.authCache.set(user.uid, user);
|
|
215
|
-
|
|
216
|
-
// Request welcome e-mail to be sent
|
|
217
|
-
const request: AceBaseUserSignupEmailRequest = {
|
|
218
|
-
type: 'user_signup',
|
|
219
|
-
user: {
|
|
220
|
-
uid: user.uid,
|
|
221
|
-
username: user.username,
|
|
222
|
-
email: user.email,
|
|
223
|
-
displayName: user.display_name,
|
|
224
|
-
settings: user.settings
|
|
225
|
-
},
|
|
226
|
-
date: user.created,
|
|
227
|
-
ip: user.created_ip,
|
|
228
|
-
activationCode: user.email_verified ? null : createSignedPublicToken({ uid: user.uid }, env.tokenSalt),
|
|
229
|
-
emailVerified: user.email_verified,
|
|
230
|
-
provider: state.provider
|
|
231
|
-
};
|
|
232
|
-
|
|
233
|
-
env.config.email?.send(request).catch(err => {
|
|
234
|
-
env.logRef.push({ action: 'oauth2_signup_email', success: false, code: 'unexpected', ip: req.ip, date: new Date(), error: err.message, request });
|
|
235
|
-
});
|
|
236
|
-
}
|
|
237
|
-
else {
|
|
238
|
-
// More than 1?!!
|
|
239
|
-
if (state.flow === 'socket') {
|
|
240
|
-
const client = env.clients.get(state.client_id);
|
|
241
|
-
client.socket.emit('oauth2-signin', { action: 'error', error: 'account_duplicates' });
|
|
242
|
-
return res.send(socketSignInFailed);
|
|
243
|
-
}
|
|
244
|
-
else {
|
|
245
|
-
const callbackUrl = `${state.callbackUrl}?provider=${state.provider}&error=account_duplicates`;
|
|
246
|
-
return res.redirect(callbackUrl);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
let result = {
|
|
251
|
-
provider: {
|
|
252
|
-
name: state.provider,
|
|
253
|
-
access_token: tokens.access_token,
|
|
254
|
-
refresh_token: tokens.refresh_token,
|
|
255
|
-
expires_in: tokens.expires_in
|
|
256
|
-
},
|
|
257
|
-
access_token: createPublicAccessToken(user.uid, req.ip, user.access_token, env.tokenSalt),
|
|
258
|
-
|
|
259
|
-
// Disabled sending user details because:
|
|
260
|
-
// 1) they might be too big to send as redirect header,
|
|
261
|
-
// 2) client should verify the sent access token and get user details from the server
|
|
262
|
-
// user: getPublicAccountDetails(user)
|
|
263
|
-
};
|
|
264
|
-
|
|
265
|
-
if (state.flow === 'socket') {
|
|
266
|
-
const client = env.clients.get(state.client_id);
|
|
267
|
-
client.socket.emit('oauth2-signin', { action: 'success', result });
|
|
268
|
-
res.send(socketSignInSuccess);
|
|
269
|
-
}
|
|
270
|
-
else {
|
|
271
|
-
const base64Result = Buffer.from(JSON.stringify(result)).toString('base64');
|
|
272
|
-
const callbackUrl = `${state.callbackUrl}?result=${base64Result}`;
|
|
273
|
-
res.redirect(callbackUrl);
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
catch(err) {
|
|
277
|
-
res.status(500).send(err.message);
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
});
|
|
281
|
-
};
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
/oauth2/{dbname}/signin:
|
|
2
|
-
get:
|
|
3
|
-
tags:
|
|
4
|
-
- auth
|
|
5
|
-
- oauth2
|
|
6
|
-
summary: Finishes an OAuth2 flow
|
|
7
|
-
description: Finishes a 3rd party OAuth2 authentication flow. This is the url you have to add to your oAuth2 provider's allowed callback urls; the auth provider will redirect the login result to this address so AceBase auth can handle the signin and then redirect the user to the callbackUrl you provided in the oauth2 init endpoint.
|
|
8
|
-
parameters:
|
|
9
|
-
- name: dbname
|
|
10
|
-
in: path
|
|
11
|
-
description: name of the database
|
|
12
|
-
required: true
|
|
13
|
-
schema:
|
|
14
|
-
type: string
|
|
15
|
-
example: mydb
|
|
16
|
-
- name: state
|
|
17
|
-
in: query
|
|
18
|
-
description: info about the initial oauth2 request (generated by AceBase)
|
|
19
|
-
required: true
|
|
20
|
-
schema:
|
|
21
|
-
type: string
|
|
22
|
-
example: sefwerg44tgwefvq3g4rbqewrgbqebwdfbwsergq34t...
|
|
23
|
-
- name: code
|
|
24
|
-
in: query
|
|
25
|
-
required: false
|
|
26
|
-
description: If signin is successful, contains the authorization code AceBase will use to request access and refresh tokens from the OAuth2 provider.
|
|
27
|
-
schema:
|
|
28
|
-
type: string
|
|
29
|
-
example: erfuihqrf7g3rfgh3rifhwre8f73efuh3eifh...
|
|
30
|
-
- name: error
|
|
31
|
-
in: query
|
|
32
|
-
description: If an error occurred, the error code
|
|
33
|
-
required: false
|
|
34
|
-
schema:
|
|
35
|
-
type: string
|
|
36
|
-
example: canceled
|
|
37
|
-
- name: error_reason
|
|
38
|
-
in: query
|
|
39
|
-
description: If an error occurred, the error reason
|
|
40
|
-
required: false
|
|
41
|
-
schema:
|
|
42
|
-
type: string
|
|
43
|
-
example: user_canceled
|
|
44
|
-
- name: error_description
|
|
45
|
-
in: query
|
|
46
|
-
description: If an error occurred, the error description
|
|
47
|
-
required: false
|
|
48
|
-
schema:
|
|
49
|
-
type: string
|
|
50
|
-
example: The user canceled the auth request
|
|
51
|
-
responses:
|
|
52
|
-
200:
|
|
53
|
-
description: Redirects the user to the callbackUrl you provided in the init endpoint
|
|
54
|
-
headers:
|
|
55
|
-
Location:
|
|
56
|
-
schema:
|
|
57
|
-
type: string
|
|
58
|
-
format: url
|
|
59
|
-
example: https://my.website.com/oauth2/callback?result=er32f32g2erv2erv23g315g..
|
|
60
|
-
500:
|
|
61
|
-
description: If anything goes wrong on the server, it returns the actual error text in plain text
|
|
62
|
-
content:
|
|
63
|
-
'text/plain':
|
|
64
|
-
schema:
|
|
65
|
-
type: string
|
|
66
|
-
example: Some error text
|
package/src/routes/webmanager.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { RouteInitEnvironment } from "../shared/env";
|
|
2
|
-
import { packageRootPath } from "../shared/rootpath";
|
|
3
|
-
import { join as joinPaths } from 'path'
|
|
4
|
-
|
|
5
|
-
export const addRoutes = (env: RouteInitEnvironment) => {
|
|
6
|
-
|
|
7
|
-
const webManagerDir = `/webmanager/`;
|
|
8
|
-
|
|
9
|
-
// Add redirect from root to webmanager
|
|
10
|
-
env.app.get('/', (req, res) => {
|
|
11
|
-
res.redirect(webManagerDir);
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
// Serve static files from webmanager directory
|
|
15
|
-
env.app.get(`${webManagerDir}*`, (req, res) => {
|
|
16
|
-
const filePath = req.path.slice(webManagerDir.length);
|
|
17
|
-
const assetsPath = joinPaths(packageRootPath, '/webmanager');
|
|
18
|
-
if (filePath.length === 0) {
|
|
19
|
-
// Send default file
|
|
20
|
-
res.sendFile(joinPaths(assetsPath, '/index.html'));
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
res.sendFile(joinPaths(assetsPath, '/', filePath));
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export default addRoutes;
|
package/src/rules.ts
DELETED
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
import { AceBase } from 'acebase';
|
|
2
|
-
import { DebugLogger, PathInfo } from 'acebase-core';
|
|
3
|
-
import * as fs from 'fs';
|
|
4
|
-
import { DbUserAccountDetails } from './schema/user';
|
|
5
|
-
import { AUTH_ACCESS_DEFAULT } from './settings';
|
|
6
|
-
|
|
7
|
-
type PathRuleFunction = ((env: any) => boolean) & { getText(): string };
|
|
8
|
-
type PathRules = {} & {
|
|
9
|
-
'.schema'?: string|Object;
|
|
10
|
-
'.read'?: boolean|string|PathRuleFunction;
|
|
11
|
-
'.write'?: boolean|string|PathRuleFunction;
|
|
12
|
-
'.validate'?: string|PathRuleFunction;
|
|
13
|
-
}
|
|
14
|
-
type RulesData = {
|
|
15
|
-
rules: PathRules;
|
|
16
|
-
}
|
|
17
|
-
export type RuleValidationFailCode = 'rule'|'no_rule'|'private'|'exception';
|
|
18
|
-
// type HasAccessResult = { allow: true } | { allow: false; code: RuleValidationFailCode; message: string; rule?: string|boolean; rulePath?: string; details?: any };
|
|
19
|
-
type HasAccessResult = { allow: boolean; code?: RuleValidationFailCode; message?: string; rule?: string|boolean; rulePath?: string; details?: any } & ({ allow: true } | { allow: false; code: RuleValidationFailCode; message: string; rule?: string|boolean; rulePath?: string; details?: any });
|
|
20
|
-
|
|
21
|
-
export class PathBasedRules {
|
|
22
|
-
|
|
23
|
-
private authEnabled: boolean;
|
|
24
|
-
private accessRules: RulesData;
|
|
25
|
-
stop(): void { throw new Error('not started yet'); }
|
|
26
|
-
|
|
27
|
-
constructor(rulesFilePath: string, defaultAccess: AUTH_ACCESS_DEFAULT, env: { debug: DebugLogger, db: AceBase, authEnabled: boolean } ) {
|
|
28
|
-
// Reads rules from a file and monitors it
|
|
29
|
-
// Check if there is a rules file, load it or generate default
|
|
30
|
-
|
|
31
|
-
const readRules = () => {
|
|
32
|
-
try {
|
|
33
|
-
const json = fs.readFileSync(rulesFilePath, 'utf-8');
|
|
34
|
-
const obj = JSON.parse(json);
|
|
35
|
-
if (typeof obj !== 'object' || typeof obj.rules !== 'object') {
|
|
36
|
-
throw new Error(`malformed rules object`);
|
|
37
|
-
}
|
|
38
|
-
return obj;
|
|
39
|
-
}
|
|
40
|
-
catch (err) {
|
|
41
|
-
env.debug.error(`Failed to read rules from "${rulesFilePath}": ${err.message}`);
|
|
42
|
-
return defaultRules;
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
const defaultAccessRule = (def => {
|
|
46
|
-
switch (def) {
|
|
47
|
-
case AUTH_ACCESS_DEFAULT.ALLOW_AUTHENTICATED: {
|
|
48
|
-
return 'auth !== null';
|
|
49
|
-
}
|
|
50
|
-
case AUTH_ACCESS_DEFAULT.ALLOW_ALL: {
|
|
51
|
-
return true;
|
|
52
|
-
}
|
|
53
|
-
case AUTH_ACCESS_DEFAULT.DENY_ALL: {
|
|
54
|
-
return false;
|
|
55
|
-
}
|
|
56
|
-
default: {
|
|
57
|
-
env.debug.error(`Unknown defaultAccessRule "${def}"`);
|
|
58
|
-
return false;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
})(defaultAccess);
|
|
62
|
-
const defaultRules:RulesData = {
|
|
63
|
-
rules: {
|
|
64
|
-
".read": defaultAccessRule,
|
|
65
|
-
".write": defaultAccessRule
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
let accessRules = defaultRules;
|
|
69
|
-
if (!fs.existsSync(rulesFilePath)) {
|
|
70
|
-
// Write defaults
|
|
71
|
-
fs.writeFileSync(rulesFilePath, JSON.stringify(defaultRules, null, 4));
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
accessRules = readRules();
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// Convert string rules to functions that can be executed
|
|
78
|
-
const processRules = (path: string, parent: any, variables: string[]) => {
|
|
79
|
-
Object.keys(parent).forEach(key => {
|
|
80
|
-
let rule = parent[key];
|
|
81
|
-
if (['.read', '.write', '.validate'].includes(key) && typeof rule === 'string') {
|
|
82
|
-
// Convert to function
|
|
83
|
-
const text = rule;
|
|
84
|
-
rule = eval(`(env => { const { now, root, newData, data, auth, ${variables.join(', ')} } = env; return ${text}; })`);
|
|
85
|
-
rule.getText = () => {
|
|
86
|
-
return text;
|
|
87
|
-
}
|
|
88
|
-
parent[key] = rule;
|
|
89
|
-
}
|
|
90
|
-
else if (key === '.schema') {
|
|
91
|
-
// Add schema
|
|
92
|
-
env.db.schema.set(path, rule)
|
|
93
|
-
.catch(err => {
|
|
94
|
-
env.debug.error(`Error parsing ${path}/.schema: ${err.message}`)
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
else if (key.startsWith('$')) {
|
|
98
|
-
variables.push(key);
|
|
99
|
-
}
|
|
100
|
-
if (typeof rule === 'object') {
|
|
101
|
-
processRules(`${path}/${key}`, rule, variables.slice());
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
};
|
|
105
|
-
processRules('', accessRules.rules, []);
|
|
106
|
-
|
|
107
|
-
// Watch file for changes
|
|
108
|
-
const wacthFileListener = () => {
|
|
109
|
-
// Reload access rules
|
|
110
|
-
const accessRules = readRules();
|
|
111
|
-
processRules('', accessRules, []);
|
|
112
|
-
this.accessRules = accessRules;
|
|
113
|
-
};
|
|
114
|
-
fs.watchFile(rulesFilePath, wacthFileListener);
|
|
115
|
-
this.stop = () => {
|
|
116
|
-
fs.unwatchFile(rulesFilePath, wacthFileListener);
|
|
117
|
-
}
|
|
118
|
-
process.on('SIGINT', this.stop);
|
|
119
|
-
|
|
120
|
-
this.authEnabled = env.authEnabled;
|
|
121
|
-
this.accessRules = accessRules;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
*
|
|
126
|
-
* @param {DbUserAccountDetails} user
|
|
127
|
-
* @param {string} path
|
|
128
|
-
* @param {boolean} [write]
|
|
129
|
-
* @param {(details: { code: string, message: string, [key:string]: any }) => void} denyDetailsCallback
|
|
130
|
-
*/
|
|
131
|
-
userHasAccess (user: Pick<DbUserAccountDetails, 'uid'>, path: string, write: boolean = false): HasAccessResult {
|
|
132
|
-
// Process rules, find out if signed in user is allowed to read/write
|
|
133
|
-
// Defaults to false unless a rule is found that tells us otherwise
|
|
134
|
-
|
|
135
|
-
const allow: HasAccessResult = { allow: true };
|
|
136
|
-
if (!this.authEnabled) {
|
|
137
|
-
// Authentication is disabled, anyone can do anything. Not really a smart thing to do!
|
|
138
|
-
return allow;
|
|
139
|
-
}
|
|
140
|
-
else if (user && user.uid === 'admin') {
|
|
141
|
-
// Always allow admin access
|
|
142
|
-
// TODO: implement user.is_admin, so the default admin account can be disabled
|
|
143
|
-
return allow;
|
|
144
|
-
}
|
|
145
|
-
else if (path.startsWith('__')) {
|
|
146
|
-
// NEW: with the auth database now integrated into the main database,
|
|
147
|
-
// deny access to private resources starting with '__' for non-admins
|
|
148
|
-
return { allow: false, code: 'private', message: `Access to private resource "${path}" not allowed` };
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
const env = { now: Date.now(), auth: user || null }; // IDEA: Add functions like "exists" and "value". These will be async (so that requires refactoring) and can be used like "await exists('./shared/' + auth.uid)" and "await value('./writable') === true"
|
|
152
|
-
const pathKeys = PathInfo.getPathKeys(path);
|
|
153
|
-
let rule = this.accessRules.rules;
|
|
154
|
-
let rulePath = [];
|
|
155
|
-
while(true) {
|
|
156
|
-
if (!rule) {
|
|
157
|
-
// TODO: check if this one is redundant with the pathKeys.length === 0 near the end
|
|
158
|
-
return { allow: false, code: 'no_rule', message: `No rules set for requested path "${path}", defaulting to false` };
|
|
159
|
-
}
|
|
160
|
-
let checkRule = write ? rule['.write'] : rule['.read'];
|
|
161
|
-
if (typeof checkRule === 'boolean') {
|
|
162
|
-
if (!checkRule) {
|
|
163
|
-
return { allow: false, code: 'rule', message: `Access denied to path "${path}" by set rule`, rule: checkRule, rulePath: rulePath.join('/') };
|
|
164
|
-
}
|
|
165
|
-
return allow;
|
|
166
|
-
}
|
|
167
|
-
if (typeof checkRule === 'function') {
|
|
168
|
-
try {
|
|
169
|
-
// Execute rule function
|
|
170
|
-
if (!checkRule(env)) {
|
|
171
|
-
return { allow: false, code: 'rule', message: `Access denied to path "${path}" by set rule`, rule: checkRule.getText(), rulePath: rulePath.join('/') };
|
|
172
|
-
}
|
|
173
|
-
return allow;
|
|
174
|
-
}
|
|
175
|
-
catch (err) {
|
|
176
|
-
// If rule execution throws an exception, don't allow. Can happen when rule is "auth.uid === '...'", and auth is null because the user is not signed in
|
|
177
|
-
return { allow: false, code: 'exception', message: `Access denied to path "${path}" by set rule`, rule: checkRule.getText(), rulePath: rulePath.join('/'), details: err };
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
if (pathKeys.length === 0) {
|
|
181
|
-
return { allow: false, code: 'no_rule', message: `No rule found for path ${path}` };
|
|
182
|
-
}
|
|
183
|
-
let nextKey = pathKeys.shift();
|
|
184
|
-
// if nextKey is '*' or '$something', rule[nextKey] will be undefined (or match a variable) so there is no
|
|
185
|
-
// need to change things here for usage of wildcard paths in subscriptions
|
|
186
|
-
if (typeof rule[nextKey] === 'undefined') {
|
|
187
|
-
// Check if current rule has a wildcard child
|
|
188
|
-
const wildcardKey = Object.keys(rule).find(key => key ==='*' || key[0] === '$');
|
|
189
|
-
if (wildcardKey) { env[wildcardKey] = nextKey; }
|
|
190
|
-
nextKey = wildcardKey;
|
|
191
|
-
}
|
|
192
|
-
nextKey && rulePath.push(nextKey);
|
|
193
|
-
rule = rule[nextKey];
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
}
|