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,126 +0,0 @@
|
|
|
1
|
-
import { IOAuth2Provider, IOAuth2ProviderSettings, IOAuth2AuthCodeParams, IOAuth2RefreshTokenParams } from "./oauth-provider";
|
|
2
|
-
import { fetch } from '../shared/simple-fetch';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Details of your app to access the Spotify API
|
|
6
|
-
*/
|
|
7
|
-
export interface ISpotifyAuthSettings extends IOAuth2ProviderSettings {
|
|
8
|
-
/** 'user-read-email', 'user-read-private' and additional scopes you want to access. See https://developer.spotify.com/documentation/general/guides/scopes/ */
|
|
9
|
-
scopes?: string[]
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
interface ISpotifyAuthToken { access_token: string, expires_in: number, expires: Date, refresh_token: string, scope: string, token_type: string }
|
|
13
|
-
interface ISpotifyClientAuthToken { access_token: string, token_type: string, expires_in: number }
|
|
14
|
-
interface ISpotifyExternalUrl { key: string, value: string }
|
|
15
|
-
interface ISpotifyFollowers { href: string, total: number }
|
|
16
|
-
interface ISpotifyImage { width: number, height: number, url: string }
|
|
17
|
-
interface ISpotifyError { status: number, message: string }
|
|
18
|
-
interface ISpotifyUser {
|
|
19
|
-
country: string,
|
|
20
|
-
display_name: string,
|
|
21
|
-
email: string, // NOT VERIFIED! See https://developer.spotify.com/documentation/web-api/reference/users-profile/get-current-users-profile/
|
|
22
|
-
external_urls: ISpotifyExternalUrl,
|
|
23
|
-
followers: ISpotifyFollowers,
|
|
24
|
-
href: string,
|
|
25
|
-
id: string,
|
|
26
|
-
images: Array<ISpotifyImage>,
|
|
27
|
-
product: 'premium'|'free'|'open',
|
|
28
|
-
type: 'user',
|
|
29
|
-
uri: string
|
|
30
|
-
}
|
|
31
|
-
export class SpotifyAuthProvider implements IOAuth2Provider {
|
|
32
|
-
|
|
33
|
-
constructor(private settings: ISpotifyAuthSettings) {
|
|
34
|
-
if (!settings.scopes) { settings.scopes = []; }
|
|
35
|
-
if (!settings.scopes.includes('user-read-email')) { settings.scopes.push('user-read-email'); }
|
|
36
|
-
if (!settings.scopes.includes('user-read-private')) { settings.scopes.push('user-read-private'); }
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Starts auth flow by getting the url the user should be redirected to
|
|
41
|
-
* @param info.redirectUrl Url spotify will redirect to after authorizing
|
|
42
|
-
* @param info.state Optional state that will be passed to redirectUri by spotify
|
|
43
|
-
*/
|
|
44
|
-
async init(info: { redirect_url: string, state?: string }) {
|
|
45
|
-
// Return url to get authorization code with
|
|
46
|
-
const authUrl = `https://accounts.spotify.com/authorize?response_type=code&client_id=${this.settings.client_id}&scope=${encodeURIComponent(this.settings.scopes.join(' '))}&redirect_uri=${encodeURIComponent(info.redirect_url)}&state=${encodeURIComponent(info.state)}`;
|
|
47
|
-
return authUrl;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
getAccessToken(params: IOAuth2AuthCodeParams|IOAuth2RefreshTokenParams) {
|
|
51
|
-
// Request access & refresh tokens with authorization code
|
|
52
|
-
return fetch('https://accounts.spotify.com/api/token', {
|
|
53
|
-
method: 'POST',
|
|
54
|
-
headers: {
|
|
55
|
-
"Content-Type": "application/x-www-form-urlencoded"
|
|
56
|
-
},
|
|
57
|
-
body: `client_id=${this.settings.client_id}&client_secret=${this.settings.client_secret}` +
|
|
58
|
-
(params.type === 'refresh'
|
|
59
|
-
? `&grant_type=refresh_token&refresh_token=${params.refresh_token}`
|
|
60
|
-
: `&grant_type=authorization_code&code=${params.auth_code}&redirect_uri=${encodeURIComponent(params.redirect_url)}`)
|
|
61
|
-
})
|
|
62
|
-
.then(response => response.json())
|
|
63
|
-
.then((result: ISpotifyAuthToken) => {
|
|
64
|
-
if ((result as any).error) {
|
|
65
|
-
throw new Error((result as any).error);
|
|
66
|
-
}
|
|
67
|
-
const secondsToExpiry = result.expires_in;
|
|
68
|
-
result.expires = new Date(Date.now() + (secondsToExpiry * 1000));
|
|
69
|
-
return result;
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
getClientAccessToken() {
|
|
74
|
-
// Gets client only access to Spotify API, without signed in user
|
|
75
|
-
return fetch('https://accounts.spotify.com/api/token', {
|
|
76
|
-
method: 'POST',
|
|
77
|
-
headers: {
|
|
78
|
-
"Content-Type": "application/x-www-form-urlencoded",
|
|
79
|
-
"Authorization": `Basic ${Buffer.from(`${this.settings.client_id}:${this.settings.client_secret}`).toString('base64')}`
|
|
80
|
-
},
|
|
81
|
-
body: `grant_type=client_credentials`
|
|
82
|
-
})
|
|
83
|
-
.then(response => response.json())
|
|
84
|
-
.then((result: ISpotifyClientAuthToken) => {
|
|
85
|
-
if ((result as any).error) {
|
|
86
|
-
throw new Error((result as any).error);
|
|
87
|
-
}
|
|
88
|
-
return result;
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
getUserInfo(access_token: string) {
|
|
93
|
-
return fetch('https://api.spotify.com/v1/me', {
|
|
94
|
-
method: 'GET',
|
|
95
|
-
headers: {
|
|
96
|
-
"Authorization": `Bearer ${access_token}`
|
|
97
|
-
},
|
|
98
|
-
})
|
|
99
|
-
.then(async response => {
|
|
100
|
-
const result = await response.json();
|
|
101
|
-
if (response.status !== 200) {
|
|
102
|
-
const error = result as ISpotifyError;
|
|
103
|
-
throw new Error(`${error.status}: ${error.message}`);
|
|
104
|
-
}
|
|
105
|
-
const user = result as ISpotifyUser;
|
|
106
|
-
return {
|
|
107
|
-
id: user.id,
|
|
108
|
-
name: user.display_name,
|
|
109
|
-
display_name: user.display_name,
|
|
110
|
-
picture: user.images,
|
|
111
|
-
email: user.email,
|
|
112
|
-
email_verified: false,
|
|
113
|
-
other: {
|
|
114
|
-
premium: user.product === 'premium',
|
|
115
|
-
followers: user.followers ? user.followers.total : null,
|
|
116
|
-
country: user.country,
|
|
117
|
-
// external_urls: user.external_urls,
|
|
118
|
-
uri: user.uri
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
export const AuthProvider = SpotifyAuthProvider;
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { RouteInitEnvironment, RouteRequest } from '../shared/env';
|
|
2
|
-
import { DbUserAccountDetails } from '../schema/user';
|
|
3
|
-
import { sendBadRequestError, sendUnexpectedError } from '../shared/error';
|
|
4
|
-
import { createPasswordHash, getOldPasswordHash, getPasswordHash } from '../shared/password';
|
|
5
|
-
import { ID } from 'acebase-core';
|
|
6
|
-
import { createPublicAccessToken } from '../shared/tokens';
|
|
7
|
-
|
|
8
|
-
export class ChangePasswordError extends Error {
|
|
9
|
-
constructor(public code: 'unknown_uid'|'wrong_password'|'wrong_access_token', message: string) {
|
|
10
|
-
super(message);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export type RequestQuery = {};
|
|
15
|
-
export type RequestBody = { uid: string; password: string; new_password: string };
|
|
16
|
-
export type ResponseBody = { access_token: string } | { code: string; message: string } | string;
|
|
17
|
-
export type Request = RouteRequest<any, ResponseBody, RequestBody, RequestQuery>;
|
|
18
|
-
|
|
19
|
-
export const addRoute = (env: RouteInitEnvironment) => {
|
|
20
|
-
env.app.post(`/auth/${env.db.name}/change_password`, async (req: Request, res) => {
|
|
21
|
-
let access_token = req.user?.access_token;
|
|
22
|
-
const details = req.body;
|
|
23
|
-
|
|
24
|
-
if (typeof details !== 'object' || typeof details.uid !== 'string' || typeof details.password !== 'string' || typeof details.new_password !== 'string') {
|
|
25
|
-
env.logRef.push({ action: 'change_password', success: false, code: 'invalid_details', ip: req.ip, date: new Date() });
|
|
26
|
-
res.status(400).send('Bad Request'); // Bad Request
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
if (details.new_password.length < 8 || details.new_password.includes(' ') || !/[0-9]/.test(details.new_password) || !/[a-z]/.test(details.new_password) || !/[A-Z]/.test(details.new_password)) {
|
|
30
|
-
env.logRef.push({ action: 'change_password', success: false, code: 'new_password_denied', ip: req.ip, date: new Date(), uid: details.uid });
|
|
31
|
-
const err = 'Invalid new password, must be at least 8 characters and contain a combination of numbers and letters (both lower and uppercase)';
|
|
32
|
-
res.status(422).send(err);// Unprocessable Entity
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
try {
|
|
37
|
-
let publicAccessToken: string;
|
|
38
|
-
await env.authRef.child(details.uid).transaction(snap => {
|
|
39
|
-
if (!snap.exists()) {
|
|
40
|
-
throw new ChangePasswordError('unknown_uid', `Unknown uid`);
|
|
41
|
-
}
|
|
42
|
-
let user: DbUserAccountDetails = snap.val();
|
|
43
|
-
user.uid = snap.key as string;
|
|
44
|
-
|
|
45
|
-
let hash = user.password_salt ? getPasswordHash(details.password, user.password_salt) : getOldPasswordHash(details.password);
|
|
46
|
-
|
|
47
|
-
if (user.password !== hash) {
|
|
48
|
-
throw new ChangePasswordError('wrong_password', `Wrong password`);
|
|
49
|
-
}
|
|
50
|
-
if (access_token && access_token !== user.access_token) {
|
|
51
|
-
throw new ChangePasswordError('wrong_access_token', `Cannot change password while signed in as other user, or with an old token`);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
let pwd = createPasswordHash(details.new_password);
|
|
55
|
-
const updates = {
|
|
56
|
-
access_token: ID.generate(),
|
|
57
|
-
access_token_created: new Date(),
|
|
58
|
-
password: pwd.hash,
|
|
59
|
-
password_salt: pwd.salt
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
// Update user object
|
|
63
|
-
Object.assign(user, updates);
|
|
64
|
-
|
|
65
|
-
// Set or update cache
|
|
66
|
-
env.authCache.set(user.uid, user);
|
|
67
|
-
|
|
68
|
-
// Create new public access token
|
|
69
|
-
publicAccessToken = createPublicAccessToken(user.uid, req.ip, user.access_token, env.tokenSalt);
|
|
70
|
-
|
|
71
|
-
return user; // Update db
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
res.send({ access_token: publicAccessToken }); // Client must use this new access token from now on
|
|
75
|
-
}
|
|
76
|
-
catch(err) {
|
|
77
|
-
env.logRef.push({ action: 'change_pwd', success: false, code: err.code, ip: req.ip, date: new Date(), uid: details.uid });
|
|
78
|
-
if (err.code) {
|
|
79
|
-
sendBadRequestError(res, err);
|
|
80
|
-
}
|
|
81
|
-
else {
|
|
82
|
-
sendUnexpectedError(res, err);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
});
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
export default addRoute;
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
/auth/{dbname}/change_password:
|
|
2
|
-
post:
|
|
3
|
-
tags:
|
|
4
|
-
- auth
|
|
5
|
-
summary: Change a user's password
|
|
6
|
-
description: Changes a user account's password by providing the current and new passwords
|
|
7
|
-
parameters:
|
|
8
|
-
- name: dbname
|
|
9
|
-
in: path
|
|
10
|
-
description: name of the database
|
|
11
|
-
required: true
|
|
12
|
-
schema:
|
|
13
|
-
type: string
|
|
14
|
-
example: mydb
|
|
15
|
-
requestBody:
|
|
16
|
-
description: request object
|
|
17
|
-
content:
|
|
18
|
-
'application/json':
|
|
19
|
-
schema:
|
|
20
|
-
type: object
|
|
21
|
-
properties:
|
|
22
|
-
uid:
|
|
23
|
-
type: string
|
|
24
|
-
description: Account's uid
|
|
25
|
-
password:
|
|
26
|
-
type: string
|
|
27
|
-
description: Current password set on the account
|
|
28
|
-
new_password:
|
|
29
|
-
type: string
|
|
30
|
-
description: New password to set on the account
|
|
31
|
-
required:
|
|
32
|
-
- uid
|
|
33
|
-
- password
|
|
34
|
-
- new_password
|
|
35
|
-
responses:
|
|
36
|
-
200:
|
|
37
|
-
description: If the password change is successful, returns an object with a new access token
|
|
38
|
-
content:
|
|
39
|
-
'application/json':
|
|
40
|
-
schema:
|
|
41
|
-
type: object
|
|
42
|
-
properties:
|
|
43
|
-
access_token:
|
|
44
|
-
description: New access token that provides access to API calls that require authentication.
|
|
45
|
-
type: string
|
|
46
|
-
example: ewufh387f3fyubqwekrjbcqwkuiec7gq3euyfbqwkuyebqjk..
|
|
47
|
-
400:
|
|
48
|
-
description: Returns a `"400 Bad Request"` if the sent request is invalid. If the request is missing details, response is the string `"Bad Request"`. Otherwise, it's an object with `code` and `message` properties. Possible codes are `"unknown_uid"`, `"wrong_password"`, `"wrong_access_token"` (if not signed in as the user the password change was requested for)
|
|
49
|
-
content:
|
|
50
|
-
'text/plain':
|
|
51
|
-
schema:
|
|
52
|
-
type: string
|
|
53
|
-
value: Bad Request
|
|
54
|
-
example: Bad Request
|
|
55
|
-
'application/json':
|
|
56
|
-
schema:
|
|
57
|
-
$ref: '#/components/schemas/Error'
|
|
58
|
-
example:
|
|
59
|
-
code: wrong_access_token
|
|
60
|
-
message: "Cannot change password while signed in as other user, or with an old token"
|
|
61
|
-
422:
|
|
62
|
-
description: New password does not meet the criteria
|
|
63
|
-
content:
|
|
64
|
-
'text/plain':
|
|
65
|
-
schema:
|
|
66
|
-
type: string
|
|
67
|
-
value: "Invalid new password, must be at least 8 characters and contain a combination of numbers and letters (both lower and uppercase)"
|
|
68
|
-
example: "Invalid new password, must be at least 8 characters and contain a combination of numbers and letters (both lower and uppercase)"
|
|
69
|
-
500:
|
|
70
|
-
description: An unexpected error occurred
|
|
71
|
-
content:
|
|
72
|
-
'application/json':
|
|
73
|
-
schema:
|
|
74
|
-
$ref: '#/components/schemas/UnexpectedError'
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { RouteInitEnvironment, RouteRequest } from '../shared/env';
|
|
2
|
-
import { sendNotAuthenticatedError, sendUnauthorizedError, sendUnexpectedError } from '../shared/error';
|
|
3
|
-
|
|
4
|
-
export class DeleteError extends Error {
|
|
5
|
-
constructor(public code: 'unauthenticated_delete'|'unauthorized_delete', message: string) {
|
|
6
|
-
super(message);
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export type RequestQuery = {};
|
|
11
|
-
export type RequestBody = { uid: string };
|
|
12
|
-
export type ResponseBody = 'Farewell' | { code: DeleteError['code']; message: string };
|
|
13
|
-
export type Request = RouteRequest<any, ResponseBody, RequestBody, RequestQuery>;
|
|
14
|
-
|
|
15
|
-
export const addRoute = (env: RouteInitEnvironment) => {
|
|
16
|
-
env.app.post(`/auth/${env.db.name}/delete`, async (req: Request, res) => {
|
|
17
|
-
let details = req.body;
|
|
18
|
-
|
|
19
|
-
if (!req.user) {
|
|
20
|
-
env.logRef.push({ action: 'delete', success: false, code: 'unauthenticated_delete', delete_uid: details.uid, ip: req.ip, date: new Date() });
|
|
21
|
-
return sendNotAuthenticatedError(res, 'unauthenticated_delete', 'You are not authorized to perform this operation, your attempt has been logged');
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
if (req.user.uid !== 'admin' && details.uid !== req.user.uid) {
|
|
25
|
-
env.logRef.push({ action: 'delete', success: false, code: 'unauthorized_delete', auth_uid: req.user.uid, delete_uid: details.uid, ip: req.ip, date: new Date() });
|
|
26
|
-
return sendUnauthorizedError(res, 'unauthorized_delete', 'You are not authorized to perform this operation, your attempt has been logged');
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const uid = details.uid ?? req.user.uid;
|
|
30
|
-
if (uid === 'admin') {
|
|
31
|
-
env.logRef.push({ action: 'delete', success: false, code: 'unauthorized_delete', auth_uid: req.user.uid, delete_uid: details.uid, ip: req.ip, date: new Date() });
|
|
32
|
-
return sendUnauthorizedError(res, 'unauthorized_delete', 'The admin account cannot be deleted, your attempt has been logged');
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
try {
|
|
36
|
-
await env.authRef.child(uid).remove();
|
|
37
|
-
env.logRef.push({ action: 'delete', success: true, auth_uid: req.user.uid, delete_uid: details.uid, ip: req.ip, date: new Date() });
|
|
38
|
-
res.send('Farewell');
|
|
39
|
-
}
|
|
40
|
-
catch (err) {
|
|
41
|
-
env.logRef.push({ action: 'delete', success: false, code: 'unexpected', auth_uid: req.user.uid, delete_uid: details.uid, ip: req.ip, date: new Date() });
|
|
42
|
-
sendUnexpectedError(res, err);
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
export default addRoute;
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
/auth/{dbname}/delete:
|
|
2
|
-
post:
|
|
3
|
-
tags:
|
|
4
|
-
- auth
|
|
5
|
-
summary: Delete a user account
|
|
6
|
-
description: Deletes an existing user account
|
|
7
|
-
parameters:
|
|
8
|
-
- name: dbname
|
|
9
|
-
in: path
|
|
10
|
-
description: name of the database
|
|
11
|
-
required: true
|
|
12
|
-
schema:
|
|
13
|
-
type: string
|
|
14
|
-
example: mydb
|
|
15
|
-
requestBody:
|
|
16
|
-
description: Account to delete
|
|
17
|
-
content:
|
|
18
|
-
'text/plain':
|
|
19
|
-
schema:
|
|
20
|
-
type: string
|
|
21
|
-
value: ''
|
|
22
|
-
example: ''
|
|
23
|
-
description: Empty content to delete signed in user account
|
|
24
|
-
'application/json':
|
|
25
|
-
schema:
|
|
26
|
-
type: object
|
|
27
|
-
properties:
|
|
28
|
-
uid:
|
|
29
|
-
type: string
|
|
30
|
-
description: (admin user only) to delete a specific user account, the target account `uid` must be passed with the request
|
|
31
|
-
example: jpx0k53u0002ecr7s354c51l
|
|
32
|
-
responses:
|
|
33
|
-
200:
|
|
34
|
-
description: When delete succeeds, it returns the string `"Farewell"`
|
|
35
|
-
content:
|
|
36
|
-
'text/plain':
|
|
37
|
-
schema:
|
|
38
|
-
type: text
|
|
39
|
-
value: Farewell
|
|
40
|
-
example: Farewell
|
|
41
|
-
401:
|
|
42
|
-
description: If this is an unauthenticated request (not signed in), `"401 Unauthorized"` is returned
|
|
43
|
-
content:
|
|
44
|
-
'application/json':
|
|
45
|
-
schema:
|
|
46
|
-
type: object
|
|
47
|
-
properties:
|
|
48
|
-
code:
|
|
49
|
-
type: string
|
|
50
|
-
value: unauthenticated_delete
|
|
51
|
-
message:
|
|
52
|
-
type: string
|
|
53
|
-
value: You are not authorized to perform this operation, your attempt has been logged
|
|
54
|
-
example:
|
|
55
|
-
code: unauthenticated_delete
|
|
56
|
-
message: You are not authorized to perform this operation, your attempt has been logged
|
|
57
|
-
403:
|
|
58
|
-
description: If a non-admin user tries to update a user's account details, `"403 Forbidden"` is returned
|
|
59
|
-
content:
|
|
60
|
-
'application/json':
|
|
61
|
-
schema:
|
|
62
|
-
type: object
|
|
63
|
-
properties:
|
|
64
|
-
code:
|
|
65
|
-
type: string
|
|
66
|
-
value: unauthorized_delete
|
|
67
|
-
message:
|
|
68
|
-
type: string
|
|
69
|
-
value: You are not authorized to perform this operation. This attempt has been logged.
|
|
70
|
-
example:
|
|
71
|
-
code: unauthorized_delete
|
|
72
|
-
message: You are not authorized to perform this operation. This attempt has been logged.
|
|
73
|
-
500:
|
|
74
|
-
description: An unenexpected error occurred on the server
|
|
75
|
-
content:
|
|
76
|
-
'application/json':
|
|
77
|
-
schema:
|
|
78
|
-
$ref: '#/components/schemas/UnexpectedError'
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { RouteInitEnvironment, RouteRequest } from '../shared/env';
|
|
2
|
-
import { DbUserAccountDetails } from '../schema/user';
|
|
3
|
-
import { createSignedPublicToken } from '../shared/tokens';
|
|
4
|
-
import { ID } from 'acebase-core';
|
|
5
|
-
import { AceBaseUserResetPasswordEmailRequest } from '../shared/email';
|
|
6
|
-
import { sendBadRequestError, sendUnexpectedError } from '../shared/error';
|
|
7
|
-
|
|
8
|
-
export class ForgotPasswordError extends Error {
|
|
9
|
-
constructor(public code: 'server_email_config'|'invalid_details'|'invalid_email', message: string) {
|
|
10
|
-
super(message);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export type RequestQuery = {};
|
|
15
|
-
export type RequestBody = { email: string };
|
|
16
|
-
export type ResponseBody = 'OK' | { code: string; message: string };
|
|
17
|
-
export type Request = RouteRequest<any, ResponseBody, RequestBody, RequestQuery>;
|
|
18
|
-
|
|
19
|
-
export const addRoute = (env: RouteInitEnvironment) => {
|
|
20
|
-
|
|
21
|
-
env.app.post(`/auth/${env.db.name}/forgot_password`, async (req: Request, res) => {
|
|
22
|
-
|
|
23
|
-
const details = req.body;
|
|
24
|
-
try {
|
|
25
|
-
if (!env.config.email || typeof env.config.email.send !== 'function') {
|
|
26
|
-
throw new ForgotPasswordError('server_email_config', 'Server email settings have not been configured');
|
|
27
|
-
}
|
|
28
|
-
if (typeof details !== 'object' || typeof details.email !== 'string' || details.email.length === 0) {
|
|
29
|
-
throw new ForgotPasswordError('invalid_details', 'Invalid details');
|
|
30
|
-
}
|
|
31
|
-
const snaps = await env.authRef.query().filter('email', '==', details.email).get();
|
|
32
|
-
if (snaps.length !== 1) {
|
|
33
|
-
throw new ForgotPasswordError('invalid_email', 'Email address not found, or duplicate entries found');
|
|
34
|
-
}
|
|
35
|
-
const snap = snaps[0];
|
|
36
|
-
const user: DbUserAccountDetails = snap.val();
|
|
37
|
-
user.uid = snap.key as string;
|
|
38
|
-
user.password_reset_code = ID.generate();
|
|
39
|
-
|
|
40
|
-
// Request a password reset email to be sent:
|
|
41
|
-
const request: AceBaseUserResetPasswordEmailRequest = {
|
|
42
|
-
type: 'user_reset_password',
|
|
43
|
-
date: new Date(),
|
|
44
|
-
ip: req.ip,
|
|
45
|
-
resetCode: createSignedPublicToken({ uid: user.uid, code: user.password_reset_code }, env.tokenSalt),
|
|
46
|
-
user: {
|
|
47
|
-
email: user.email,
|
|
48
|
-
uid: user.uid,
|
|
49
|
-
username: user.username,
|
|
50
|
-
settings: user.settings,
|
|
51
|
-
displayName: user.display_name
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
await Promise.all([
|
|
55
|
-
env.config.email.send(request),
|
|
56
|
-
snap.ref.update({ password_reset_code: user.password_reset_code })
|
|
57
|
-
])
|
|
58
|
-
env.logRef.push({ action: 'forgot_password', success: true, email: details.email, ip: req.ip, date: new Date() });
|
|
59
|
-
res.send('OK');
|
|
60
|
-
}
|
|
61
|
-
catch (err) {
|
|
62
|
-
env.logRef.push({ action: 'forgot_password', success: false, code: err.code || 'unexpected', message: err.code ? null : err.message, email: details.email, ip: req.ip, date: new Date() });
|
|
63
|
-
if (err.code) {
|
|
64
|
-
sendBadRequestError(res, err);
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
sendUnexpectedError(res, err);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
export default addRoute;
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
/auth/{dbname}/forgot_password:
|
|
2
|
-
post:
|
|
3
|
-
tags:
|
|
4
|
-
- auth
|
|
5
|
-
summary: Issue a password reset
|
|
6
|
-
description: Issues a password reset for a user that forgot their password. User must have an email address in the database.
|
|
7
|
-
parameters:
|
|
8
|
-
- name: dbname
|
|
9
|
-
in: path
|
|
10
|
-
description: name of the database
|
|
11
|
-
required: true
|
|
12
|
-
schema:
|
|
13
|
-
type: string
|
|
14
|
-
example: mydb
|
|
15
|
-
requestBody:
|
|
16
|
-
description: user details
|
|
17
|
-
content:
|
|
18
|
-
'application/json':
|
|
19
|
-
schema:
|
|
20
|
-
type: object
|
|
21
|
-
properties:
|
|
22
|
-
email:
|
|
23
|
-
type: string
|
|
24
|
-
description: User's email address. A password reset email will be sent to this address
|
|
25
|
-
required:
|
|
26
|
-
- email
|
|
27
|
-
responses:
|
|
28
|
-
200:
|
|
29
|
-
description: If a password reset email has been successfully sent, returns the string `"OK"`
|
|
30
|
-
content:
|
|
31
|
-
'text/plain':
|
|
32
|
-
schema:
|
|
33
|
-
type: 'string'
|
|
34
|
-
value: OK
|
|
35
|
-
description: The string `"OK"`
|
|
36
|
-
400:
|
|
37
|
-
description: Returns a `"400 Bad Request"` if the server has not been configured properly, or if no or invalid email address was sent with the request
|
|
38
|
-
content:
|
|
39
|
-
'application/json':
|
|
40
|
-
schema:
|
|
41
|
-
$ref: '#/components/schemas/Error'
|
|
42
|
-
example:
|
|
43
|
-
code: invalid_email
|
|
44
|
-
message: Email address not found, or duplicate entries found
|
|
45
|
-
500:
|
|
46
|
-
description: An unexpected error occurred
|
|
47
|
-
content:
|
|
48
|
-
'application/json':
|
|
49
|
-
schema:
|
|
50
|
-
$ref: '#/components/schemas/UnexpectedError'
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import { RouteInitEnvironment, RouteRequest } from '../shared/env';
|
|
2
|
-
import { DbUserAccountDetails } from '../schema/user';
|
|
3
|
-
import { parseSignedPublicToken } from '../shared/tokens';
|
|
4
|
-
import { sendBadRequestError, sendUnexpectedError } from '../shared/error';
|
|
5
|
-
import { createPasswordHash } from '../shared/password';
|
|
6
|
-
import { AceBaseUserResetPasswordSuccessEmailRequest } from '../shared/email';
|
|
7
|
-
|
|
8
|
-
export class ResetPasswordError extends Error {
|
|
9
|
-
constructor(public code: 'invalid_code'|'unknown_user'|'password_requirement_mismatch', message: string) {
|
|
10
|
-
super(message);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export type RequestQuery = {};
|
|
15
|
-
export type RequestBody = { code: string; password: string };
|
|
16
|
-
export type ResponseBody = 'OK' | { code: string; message: string };
|
|
17
|
-
export type Request = RouteRequest<any, ResponseBody, RequestBody, RequestQuery>;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Adds the reset_password route and returns the reset function that can be used to manually reset a password
|
|
21
|
-
* @param env environment
|
|
22
|
-
* @returns returns the reset function
|
|
23
|
-
*/
|
|
24
|
-
export const addRoute = (env: RouteInitEnvironment) => {
|
|
25
|
-
|
|
26
|
-
const resetPassword = async (clientIp: string, code: string, newPassword: string) => {
|
|
27
|
-
try {
|
|
28
|
-
var verification = parseSignedPublicToken(code, env.tokenSalt);
|
|
29
|
-
}
|
|
30
|
-
catch(err) {
|
|
31
|
-
throw new ResetPasswordError('invalid_code', err.message);
|
|
32
|
-
}
|
|
33
|
-
const snap = await env.authRef.child(verification.uid).get();
|
|
34
|
-
if (!snap.exists()) {
|
|
35
|
-
throw new ResetPasswordError('unknown_user', 'Uknown user');
|
|
36
|
-
}
|
|
37
|
-
const user: DbUserAccountDetails = snap.val();
|
|
38
|
-
user.uid = snap.key as string;
|
|
39
|
-
|
|
40
|
-
if (user.password_reset_code !== verification.code) {
|
|
41
|
-
throw new ResetPasswordError('invalid_code', 'Invalid code');
|
|
42
|
-
}
|
|
43
|
-
if (newPassword.length < 8 || newPassword.includes(' ')) {
|
|
44
|
-
throw new ResetPasswordError('password_requirement_mismatch', 'Password must be at least 8 characters, and cannot contain spaces');
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// Ok to change password
|
|
48
|
-
const pwd = createPasswordHash(newPassword);
|
|
49
|
-
await snap.ref.update({
|
|
50
|
-
password: pwd.hash,
|
|
51
|
-
password_salt: pwd.salt,
|
|
52
|
-
password_reset_code: null
|
|
53
|
-
})
|
|
54
|
-
// Send confirmation email
|
|
55
|
-
const request: AceBaseUserResetPasswordSuccessEmailRequest = {
|
|
56
|
-
type: 'user_reset_password_success',
|
|
57
|
-
date: new Date(),
|
|
58
|
-
ip: clientIp,
|
|
59
|
-
user: {
|
|
60
|
-
uid: user.uid,
|
|
61
|
-
email: user.email,
|
|
62
|
-
username: user.username,
|
|
63
|
-
displayName: user.display_name,
|
|
64
|
-
settings: user.settings
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
env.config.email?.send(request);
|
|
68
|
-
return user;
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
env.app.post(`/auth/${env.db.name}/reset_password`, async (req: Request, res) => {
|
|
72
|
-
|
|
73
|
-
const details = req.body;
|
|
74
|
-
try {
|
|
75
|
-
const user = await resetPassword(req.ip, details.code, details.password);
|
|
76
|
-
env.logRef.push({ action: 'reset_password', success: true, ip: req.ip, date: new Date(), uid: user.uid });
|
|
77
|
-
res.send('OK');
|
|
78
|
-
}
|
|
79
|
-
catch (err) {
|
|
80
|
-
env.logRef.push({ action: 'reset_password', success: false, code: err.code, message: err.message, ip: req.ip, date: new Date(), uid: req.user?.uid ?? null });
|
|
81
|
-
if (err.code) {
|
|
82
|
-
sendBadRequestError(res, err);
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
sendUnexpectedError(res, err);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
return resetPassword;
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
export default addRoute;
|