@cloudbase/oauth 0.1.1-alpha → 0.1.1-alpha.2
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/Dockerfile +15 -0
- package/README.md +1 -1
- package/_exmaple/assets/scripts/function/function.ts +99 -0
- package/_exmaple/assets/scripts/index.ts +99 -0
- package/_exmaple/assets/scripts/request.ts +11 -0
- package/_exmaple/index.html +15 -0
- package/_exmaple/package.json +33 -0
- package/_exmaple/tsconfig.json +71 -0
- package/_exmaple/typings.d.ts +0 -0
- package/_exmaple/webpack.config.js +42 -0
- package/cdnjs/1.0.0/cloudbase.auth.js +1 -0
- package/cdnjs/1.0.0/cloudbase.database.js +8 -0
- package/cdnjs/1.0.0/cloudbase.full.js +8 -0
- package/cdnjs/1.0.0/cloudbase.functions.js +1 -0
- package/cdnjs/1.0.0/cloudbase.js +1 -0
- package/cdnjs/1.0.0/cloudbase.storage.js +1 -0
- package/cdnjs/1.0.1/cloudbase.auth.js +1 -0
- package/cdnjs/1.0.1/cloudbase.database.js +8 -0
- package/cdnjs/1.0.1/cloudbase.full.js +8 -0
- package/cdnjs/1.0.1/cloudbase.functions.js +1 -0
- package/cdnjs/1.0.1/cloudbase.js +1 -0
- package/cdnjs/1.0.1/cloudbase.storage.js +1 -0
- package/cdnjs/1.0.2/cloudbase.auth.js +1 -0
- package/cdnjs/1.0.2/cloudbase.database.js +8 -0
- package/cdnjs/1.0.2/cloudbase.full.js +8 -0
- package/cdnjs/1.0.2/cloudbase.functions.js +1 -0
- package/cdnjs/1.0.2/cloudbase.js +1 -0
- package/cdnjs/1.0.2/cloudbase.storage.js +1 -0
- package/cdnjs/1.0.3/cloudbase.auth.js +1 -0
- package/cdnjs/1.0.3/cloudbase.database.js +8 -0
- package/cdnjs/1.0.3/cloudbase.full.js +8 -0
- package/cdnjs/1.0.3/cloudbase.functions.js +1 -0
- package/cdnjs/1.0.3/cloudbase.js +1 -0
- package/cdnjs/1.0.3/cloudbase.storage.js +1 -0
- package/cdnjs/1.0.4/cloudbase.auth.js +1 -0
- package/cdnjs/1.0.4/cloudbase.database.js +8 -0
- package/cdnjs/1.0.4/cloudbase.full.js +8 -0
- package/cdnjs/1.0.4/cloudbase.functions.js +1 -0
- package/cdnjs/1.0.4/cloudbase.js +1 -0
- package/cdnjs/1.0.4/cloudbase.storage.js +1 -0
- package/cdnjs/1.1.0/cloudbase.auth.js +1 -0
- package/cdnjs/1.1.0/cloudbase.database.js +8 -0
- package/cdnjs/1.1.0/cloudbase.full.js +8 -0
- package/cdnjs/1.1.0/cloudbase.functions.js +1 -0
- package/cdnjs/1.1.0/cloudbase.js +1 -0
- package/cdnjs/1.1.0/cloudbase.storage.js +1 -0
- package/cdnjs/1.1.1/cloudbase.auth.js +1 -0
- package/cdnjs/1.1.1/cloudbase.database.js +8 -0
- package/cdnjs/1.1.1/cloudbase.full.js +8 -0
- package/cdnjs/1.1.1/cloudbase.functions.js +1 -0
- package/cdnjs/1.1.1/cloudbase.js +1 -0
- package/cdnjs/1.1.1/cloudbase.storage.js +1 -0
- package/cdnjs/1.1.2/cloudbase.auth.js +1 -0
- package/cdnjs/1.1.2/cloudbase.database.js +8 -0
- package/cdnjs/1.1.2/cloudbase.full.js +8 -0
- package/cdnjs/1.1.2/cloudbase.functions.js +1 -0
- package/cdnjs/1.1.2/cloudbase.js +1 -0
- package/cdnjs/1.1.2/cloudbase.storage.js +1 -0
- package/cdnjs/1.1.3/cloudbase.auth.js +1 -0
- package/cdnjs/1.1.3/cloudbase.database.js +8 -0
- package/cdnjs/1.1.3/cloudbase.full.js +8 -0
- package/cdnjs/1.1.3/cloudbase.functions.js +1 -0
- package/cdnjs/1.1.3/cloudbase.js +1 -0
- package/cdnjs/1.1.3/cloudbase.storage.js +1 -0
- package/cdnjs/1.1.4/cloudbase.auth.js +1 -0
- package/cdnjs/1.1.4/cloudbase.database.js +8 -0
- package/cdnjs/1.1.4/cloudbase.full.js +8 -0
- package/cdnjs/1.1.4/cloudbase.functions.js +1 -0
- package/cdnjs/1.1.4/cloudbase.js +1 -0
- package/cdnjs/1.1.4/cloudbase.storage.js +1 -0
- package/cdnjs/1.2.1/cloudbase.auth.js +1 -0
- package/cdnjs/1.2.1/cloudbase.database.js +8 -0
- package/cdnjs/1.2.1/cloudbase.full.js +8 -0
- package/cdnjs/1.2.1/cloudbase.functions.js +1 -0
- package/cdnjs/1.2.1/cloudbase.js +1 -0
- package/cdnjs/1.2.1/cloudbase.storage.js +1 -0
- package/cdnjs/1.2.2/cloudbase.auth.js +1 -0
- package/cdnjs/1.2.2/cloudbase.database.js +8 -0
- package/cdnjs/1.2.2/cloudbase.full.js +8 -0
- package/cdnjs/1.2.2/cloudbase.functions.js +1 -0
- package/cdnjs/1.2.2/cloudbase.js +1 -0
- package/cdnjs/1.2.2/cloudbase.storage.js +1 -0
- package/cdnjs/1.2.3/cloudbase.auth.js +1 -0
- package/cdnjs/1.2.3/cloudbase.database.js +8 -0
- package/cdnjs/1.2.3/cloudbase.full.js +8 -0
- package/cdnjs/1.2.3/cloudbase.functions.js +1 -0
- package/cdnjs/1.2.3/cloudbase.js +1 -0
- package/cdnjs/1.2.3/cloudbase.storage.js +1 -0
- package/cdnjs/1.3.0/cloudbase.auth.js +1 -0
- package/cdnjs/1.3.0/cloudbase.database.js +8 -0
- package/cdnjs/1.3.0/cloudbase.full.js +8 -0
- package/cdnjs/1.3.0/cloudbase.functions.js +1 -0
- package/cdnjs/1.3.0/cloudbase.js +1 -0
- package/cdnjs/1.3.0/cloudbase.realtime.js +1 -0
- package/cdnjs/1.3.0/cloudbase.storage.js +1 -0
- package/cdnjs/1.3.1/cloudbase.auth.js +1 -0
- package/cdnjs/1.3.1/cloudbase.database.js +8 -0
- package/cdnjs/1.3.1/cloudbase.full.js +8 -0
- package/cdnjs/1.3.1/cloudbase.functions.js +1 -0
- package/cdnjs/1.3.1/cloudbase.js +1 -0
- package/cdnjs/1.3.1/cloudbase.realtime.js +1 -0
- package/cdnjs/1.3.1/cloudbase.storage.js +1 -0
- package/cdnjs/1.3.2/cloudbase.auth.js +1 -0
- package/cdnjs/1.3.2/cloudbase.database.js +8 -0
- package/cdnjs/1.3.2/cloudbase.full.js +8 -0
- package/cdnjs/1.3.2/cloudbase.functions.js +1 -0
- package/cdnjs/1.3.2/cloudbase.js +1 -0
- package/cdnjs/1.3.2/cloudbase.realtime.js +1 -0
- package/cdnjs/1.3.2/cloudbase.storage.js +1 -0
- package/cdnjs/1.3.3/cloudbase.auth.js +1 -0
- package/cdnjs/1.3.3/cloudbase.database.js +8 -0
- package/cdnjs/1.3.3/cloudbase.full.js +8 -0
- package/cdnjs/1.3.3/cloudbase.functions.js +1 -0
- package/cdnjs/1.3.3/cloudbase.js +1 -0
- package/cdnjs/1.3.3/cloudbase.realtime.js +1 -0
- package/cdnjs/1.3.3/cloudbase.storage.js +1 -0
- package/cdnjs/1.3.4-alpha.0/cloudbase.auth.js +1 -0
- package/cdnjs/1.3.4-alpha.0/cloudbase.database.js +8 -0
- package/cdnjs/1.3.4-alpha.0/cloudbase.full.js +8 -0
- package/cdnjs/1.3.4-alpha.0/cloudbase.functions.js +1 -0
- package/cdnjs/1.3.4-alpha.0/cloudbase.js +1 -0
- package/cdnjs/1.3.4-alpha.0/cloudbase.realtime.js +1 -0
- package/cdnjs/1.3.4-alpha.0/cloudbase.storage.js +1 -0
- package/cdnjs/1.3.5/cloudbase.analytics.js +1 -0
- package/cdnjs/1.3.5/cloudbase.auth.js +1 -0
- package/cdnjs/1.3.5/cloudbase.database.js +10 -0
- package/cdnjs/1.3.5/cloudbase.full.js +10 -0
- package/cdnjs/1.3.5/cloudbase.functions.js +1 -0
- package/cdnjs/1.3.5/cloudbase.js +1 -0
- package/cdnjs/1.3.5/cloudbase.realtime.js +1 -0
- package/cdnjs/1.3.5/cloudbase.storage.js +1 -0
- package/cdnjs/1.3.5-alpha.0/cloudbase.auth.js +1 -0
- package/cdnjs/1.3.5-alpha.0/cloudbase.database.js +10 -0
- package/cdnjs/1.3.5-alpha.0/cloudbase.full.js +10 -0
- package/cdnjs/1.3.5-alpha.0/cloudbase.functions.js +1 -0
- package/cdnjs/1.3.5-alpha.0/cloudbase.js +1 -0
- package/cdnjs/1.3.5-alpha.0/cloudbase.realtime.js +1 -0
- package/cdnjs/1.3.5-alpha.0/cloudbase.storage.js +1 -0
- package/cdnjs/1.4.0/cloudbase.analytics.js +1 -0
- package/cdnjs/1.4.0/cloudbase.auth.js +1 -0
- package/cdnjs/1.4.0/cloudbase.database.js +10 -0
- package/cdnjs/1.4.0/cloudbase.full.js +10 -0
- package/cdnjs/1.4.0/cloudbase.functions.js +1 -0
- package/cdnjs/1.4.0/cloudbase.js +1 -0
- package/cdnjs/1.4.0/cloudbase.realtime.js +1 -0
- package/cdnjs/1.4.0/cloudbase.storage.js +1 -0
- package/cdnjs/1.4.1/cloudbase.analytics.js +1 -0
- package/cdnjs/1.4.1/cloudbase.auth.js +1 -0
- package/cdnjs/1.4.1/cloudbase.database.js +10 -0
- package/cdnjs/1.4.1/cloudbase.full.js +10 -0
- package/cdnjs/1.4.1/cloudbase.functions.js +1 -0
- package/cdnjs/1.4.1/cloudbase.js +1 -0
- package/cdnjs/1.4.1/cloudbase.realtime.js +1 -0
- package/cdnjs/1.4.1/cloudbase.storage.js +1 -0
- package/cdnjs/1.4.1-alpha.0/cloudbase.analytics.js +1 -0
- package/cdnjs/1.4.1-alpha.0/cloudbase.auth.js +1 -0
- package/cdnjs/1.4.1-alpha.0/cloudbase.database.js +10 -0
- package/cdnjs/1.4.1-alpha.0/cloudbase.full.js +10 -0
- package/cdnjs/1.4.1-alpha.0/cloudbase.functions.js +1 -0
- package/cdnjs/1.4.1-alpha.0/cloudbase.js +1 -0
- package/cdnjs/1.4.1-alpha.0/cloudbase.realtime.js +1 -0
- package/cdnjs/1.4.1-alpha.0/cloudbase.storage.js +1 -0
- package/cdnjs/1.4.2-alpha.0/cloudbase.analytics.js +1 -0
- package/cdnjs/1.4.2-alpha.0/cloudbase.auth.js +1 -0
- package/cdnjs/1.4.2-alpha.0/cloudbase.database.js +10 -0
- package/cdnjs/1.4.2-alpha.0/cloudbase.full.js +10 -0
- package/cdnjs/1.4.2-alpha.0/cloudbase.functions.js +1 -0
- package/cdnjs/1.4.2-alpha.0/cloudbase.js +1 -0
- package/cdnjs/1.4.2-alpha.0/cloudbase.realtime.js +1 -0
- package/cdnjs/1.4.2-alpha.0/cloudbase.storage.js +1 -0
- package/cdnjs/1.5.0/cloudbase.analytics.js +1 -0
- package/cdnjs/1.5.0/cloudbase.auth.js +1 -0
- package/cdnjs/1.5.0/cloudbase.database.js +10 -0
- package/cdnjs/1.5.0/cloudbase.full.js +10 -0
- package/cdnjs/1.5.0/cloudbase.functions.js +1 -0
- package/cdnjs/1.5.0/cloudbase.js +1 -0
- package/cdnjs/1.5.0/cloudbase.realtime.js +1 -0
- package/cdnjs/1.5.0/cloudbase.storage.js +1 -0
- package/cdnjs/1.5.1/cloudbase.analytics.js +1 -0
- package/cdnjs/1.5.1/cloudbase.auth.js +1 -0
- package/cdnjs/1.5.1/cloudbase.database.js +26 -0
- package/cdnjs/1.5.1/cloudbase.full.js +26 -0
- package/cdnjs/1.5.1/cloudbase.functions.js +1 -0
- package/cdnjs/1.5.1/cloudbase.js +1 -0
- package/cdnjs/1.5.1/cloudbase.realtime.js +1 -0
- package/cdnjs/1.5.1/cloudbase.storage.js +1 -0
- package/cdnjs/1.5.1-alpha.0/cloudbase.analytics.js +1 -0
- package/cdnjs/1.5.1-alpha.0/cloudbase.auth.js +1 -0
- package/cdnjs/1.5.1-alpha.0/cloudbase.database.js +10 -0
- package/cdnjs/1.5.1-alpha.0/cloudbase.full.js +10 -0
- package/cdnjs/1.5.1-alpha.0/cloudbase.functions.js +1 -0
- package/cdnjs/1.5.1-alpha.0/cloudbase.js +1 -0
- package/cdnjs/1.5.1-alpha.0/cloudbase.realtime.js +1 -0
- package/cdnjs/1.5.1-alpha.0/cloudbase.storage.js +1 -0
- package/cdnjs/1.5.3-alpha.0/cloudbase.analytics.js +1 -0
- package/cdnjs/1.5.3-alpha.0/cloudbase.auth.js +1 -0
- package/cdnjs/1.5.3-alpha.0/cloudbase.database.js +26 -0
- package/cdnjs/1.5.3-alpha.0/cloudbase.full.js +26 -0
- package/cdnjs/1.5.3-alpha.0/cloudbase.functions.js +1 -0
- package/cdnjs/1.5.3-alpha.0/cloudbase.js +1 -0
- package/cdnjs/1.5.3-alpha.0/cloudbase.realtime.js +1 -0
- package/cdnjs/1.5.3-alpha.0/cloudbase.storage.js +1 -0
- package/cdnjs/1.6.0/cloudbase.analytics.js +1 -0
- package/cdnjs/1.6.0/cloudbase.auth.js +1 -0
- package/cdnjs/1.6.0/cloudbase.database.js +26 -0
- package/cdnjs/1.6.0/cloudbase.full.js +26 -0
- package/cdnjs/1.6.0/cloudbase.functions.js +1 -0
- package/cdnjs/1.6.0/cloudbase.js +1 -0
- package/cdnjs/1.6.0/cloudbase.realtime.js +1 -0
- package/cdnjs/1.6.0/cloudbase.storage.js +1 -0
- package/{CHANGELOG.md → changelog.md} +0 -0
- package/dist/README.md +153 -0
- package/{app → dist/app}/index.d.ts +1 -0
- package/{app → dist/app}/index.js +0 -0
- package/dist/app/index.js.map +1 -0
- package/{app → dist/app}/internal.d.ts +0 -0
- package/{app → dist/app}/internal.js +0 -0
- package/{app → dist/app}/internal.js.map +0 -0
- package/{app → dist/app}/openuri.d.ts +0 -0
- package/{app → dist/app}/openuri.js +2 -1
- package/{app → dist/app}/openuri.js.map +1 -1
- package/{app → dist/app}/request.d.ts +4 -3
- package/{app → dist/app}/request.js +20 -12
- package/dist/app/request.js.map +1 -0
- package/{app → dist/app}/storage.d.ts +0 -0
- package/{app → dist/app}/storage.js +0 -0
- package/{app → dist/app}/storage.js.map +0 -0
- package/{auth → dist/auth}/consts.d.ts +21 -4
- package/{auth → dist/auth}/consts.js +27 -5
- package/dist/auth/consts.js.map +1 -0
- package/{auth → dist/auth}/index.d.ts +83 -34
- package/{auth → dist/auth}/index.js +239 -92
- package/dist/auth/index.js.map +1 -0
- package/dist/auth/models.d.ts +284 -0
- package/{oauthclient → dist/auth}/models.js +1 -0
- package/dist/auth/models.js.map +1 -0
- package/{captcha → dist/captcha}/index.d.ts +0 -0
- package/{captcha → dist/captcha}/index.js +4 -2
- package/dist/captcha/index.js.map +1 -0
- package/dist/changelog.md +30 -0
- package/dist/function/index.d.ts +38 -0
- package/dist/function/index.js +80 -0
- package/dist/function/index.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +24 -0
- package/dist/index.js.map +1 -0
- package/{oauthclient → dist/oauthclient}/consts.d.ts +0 -0
- package/{oauthclient → dist/oauthclient}/consts.js +0 -0
- package/{oauthclient → dist/oauthclient}/consts.js.map +0 -0
- package/{oauthclient → dist/oauthclient}/index.d.ts +0 -0
- package/{oauthclient → dist/oauthclient}/index.js +2 -2
- package/dist/oauthclient/index.js.map +1 -0
- package/{oauthclient → dist/oauthclient}/interface.d.ts +4 -0
- package/{oauthclient → dist/oauthclient}/interface.js +0 -0
- package/{oauthclient → dist/oauthclient}/interface.js.map +1 -1
- package/{oauthclient → dist/oauthclient}/models.d.ts +12 -11
- package/{auth → dist/oauthclient}/models.js +0 -0
- package/{oauthclient → dist/oauthclient}/models.js.map +0 -0
- package/{oauthclient → dist/oauthclient}/oauthclient.d.ts +11 -7
- package/{oauthclient → dist/oauthclient}/oauthclient.js +27 -12
- package/dist/oauthclient/oauthclient.js.map +1 -0
- package/dist/package.json +32 -0
- package/{utils/single-promise.d.ts → dist/utils/promise.d.ts} +3 -3
- package/{utils/single-promise.js → dist/utils/promise.js} +6 -6
- package/dist/utils/promise.js.map +1 -0
- package/{utils → dist/utils}/uuid.d.ts +0 -0
- package/{utils → dist/utils}/uuid.js +0 -0
- package/{utils → dist/utils}/uuid.js.map +0 -0
- package/docs/dev.md +71 -0
- package/docs/errlog.md +79 -0
- package/package.json +3 -2
- package/publish.sh +2 -0
- package/src/app/index.ts +83 -0
- package/src/app/internal.ts +26 -0
- package/src/app/openuri.ts +111 -0
- package/src/app/request.ts +66 -0
- package/src/app/storage.ts +58 -0
- package/src/auth/consts.ts +74 -0
- package/src/auth/index.ts +718 -0
- package/src/auth/models.ts +352 -0
- package/src/captcha/index.ts +170 -0
- package/src/function/index.ts +103 -0
- package/src/index.ts +32 -0
- package/src/oauthclient/consts.ts +48 -0
- package/src/oauthclient/index.ts +33 -0
- package/src/oauthclient/interface.ts +34 -0
- package/src/oauthclient/models.ts +50 -0
- package/src/oauthclient/oauthclient.ts +487 -0
- package/src/utils/promise.ts +41 -0
- package/src/utils/uuid.ts +11 -0
- package/test/e2e/cases/custom-login.test.js +122 -0
- package/test/e2e/cases/index.test.js +61 -0
- package/test/e2e/cases/login-state.test.js +89 -0
- package/test/e2e/environment.js +39 -0
- package/test/e2e/global_setup.js +33 -0
- package/test/e2e/global_teardown.js +15 -0
- package/test/e2e/setup.js +37 -0
- package/test/unit/index.test.js +89 -0
- package/test/web/cases/auth/index.js +256 -0
- package/test/web/cases/database/collection.js +173 -0
- package/test/web/cases/database/command.js +754 -0
- package/test/web/cases/database/date.js +122 -0
- package/test/web/cases/database/db.js +20 -0
- package/test/web/cases/database/document.js +160 -0
- package/test/web/cases/database/geo-advanced.js +327 -0
- package/test/web/cases/database/geo.js +105 -0
- package/test/web/cases/database/index.js +315 -0
- package/test/web/cases/database/order.js +83 -0
- package/test/web/cases/database/realtime.js +102 -0
- package/test/web/cases/database/regex.js +140 -0
- package/test/web/cases/ext/index.js +88 -0
- package/test/web/cases/function/index.js +77 -0
- package/test/web/cases/storage/cos.jpeg +0 -0
- package/test/web/cases/storage/index.js +207 -0
- package/test/web/index.html +63 -0
- package/test/web/index.js +109 -0
- package/test/web/util.js +80 -0
- package/tsconfig.json +35 -0
- package/wiki/README.md +75 -0
- package/app/index.js.map +0 -1
- package/app/request.js.map +0 -1
- package/auth/consts.js.map +0 -1
- package/auth/index.js.map +0 -1
- package/auth/models.d.ts +0 -158
- package/auth/models.js.map +0 -1
- package/captcha/index.js.map +0 -1
- package/index.d.ts +0 -8
- package/index.js +0 -17
- package/index.js.map +0 -1
- package/oauthclient/index.js.map +0 -1
- package/oauthclient/oauthclient.js.map +0 -1
- package/utils/single-promise.js.map +0 -1
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import {App} from "../app";
|
|
2
|
+
import {_getComponent} from "../app/internal";
|
|
3
|
+
import {OAuth2Client} from "./oauthclient";
|
|
4
|
+
import {Credentials as credentials, ResponseError as responseError} from './models';
|
|
5
|
+
import {AuthClient as authClient, RequestFn as requestFn} from "./interface";
|
|
6
|
+
export {ErrorType} from './consts';
|
|
7
|
+
|
|
8
|
+
export interface InitOptions {
|
|
9
|
+
request?: RequestFn;
|
|
10
|
+
retry?: number;
|
|
11
|
+
refreshTokenFunc?: (refreshToken?: string) => Promise<Credentials>;
|
|
12
|
+
tokenInURL?: boolean;
|
|
13
|
+
headers?: { [key: string]: string };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function getOAuthClient(app: App, opts?: InitOptions): authClient {
|
|
17
|
+
return _getComponent<authClient>(app, "oauthclient", (): authClient => {
|
|
18
|
+
const appOpts = app.options
|
|
19
|
+
const oauthOpts = {
|
|
20
|
+
clientId: appOpts.clientId,
|
|
21
|
+
clientSecret: appOpts.clientSecret,
|
|
22
|
+
request: appOpts.request,
|
|
23
|
+
storage: appOpts.storage,
|
|
24
|
+
}
|
|
25
|
+
return (new OAuth2Client(oauthOpts))
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
export type Credentials = credentials
|
|
31
|
+
export type AuthClient = authClient
|
|
32
|
+
export type RequestFn = requestFn
|
|
33
|
+
export type ResponseError = responseError
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {Credentials, AuthClientRequestOptions} from './models';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* the interface for the Oauth2Client
|
|
5
|
+
*/
|
|
6
|
+
export abstract class AuthClient {
|
|
7
|
+
/**
|
|
8
|
+
* Sets the auth credentials.
|
|
9
|
+
*/
|
|
10
|
+
abstract setCredentials(credentials?: Credentials): void;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Provides an alternative fetch api request implementation with auth credentials
|
|
14
|
+
* if options.withCredentials:true, the request will auto add Authorization: Bearer <AccessToken> in the request
|
|
15
|
+
* error:
|
|
16
|
+
* - unreachable, the network error or response is not json
|
|
17
|
+
* - unauthenticated: has no validate access token
|
|
18
|
+
*/
|
|
19
|
+
abstract request: RequestFn;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* get the current accessToken from AuthClient, you can use this to detect login status
|
|
23
|
+
* error:
|
|
24
|
+
* - unauthenticated: has no validate access token
|
|
25
|
+
*/
|
|
26
|
+
abstract getAccessToken(): Promise<string>;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* get the current token scope
|
|
30
|
+
*/
|
|
31
|
+
abstract getScope(): Promise<string>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type RequestFn = <T>(url: string, options?: AuthClientRequestOptions) => Promise<T>;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import {RequestFn, RequestOptions, Storage} from "../app";
|
|
2
|
+
import {ErrorType} from './consts';
|
|
3
|
+
|
|
4
|
+
// Credentials
|
|
5
|
+
export interface Credentials {
|
|
6
|
+
token_type?: string;
|
|
7
|
+
access_token?: string;
|
|
8
|
+
refresh_token?: string;
|
|
9
|
+
scope?: string;
|
|
10
|
+
expires_in?: number;
|
|
11
|
+
expires_at?: Date;
|
|
12
|
+
sub?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// An Error For all concern
|
|
16
|
+
export interface ResponseError {
|
|
17
|
+
error: ErrorType;
|
|
18
|
+
error_description?: string;
|
|
19
|
+
error_uri?: string;
|
|
20
|
+
details?: any;
|
|
21
|
+
request_id?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// refer https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Authentication
|
|
25
|
+
export interface AuthClientRequestOptions extends RequestOptions {
|
|
26
|
+
headers?: {
|
|
27
|
+
'x-request-id'?: string;
|
|
28
|
+
[key: string]: any;
|
|
29
|
+
};
|
|
30
|
+
// Bearer https://datatracker.ietf.org/doc/html/rfc6750
|
|
31
|
+
withCredentials?: boolean;
|
|
32
|
+
// Basic https://datatracker.ietf.org/doc/html/rfc7617
|
|
33
|
+
withBasicAuth?: boolean;
|
|
34
|
+
retry?: number;
|
|
35
|
+
|
|
36
|
+
[key: string]: any;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface OAuth2ClientOptions {
|
|
40
|
+
clientId: string; // Storage, default is localStorage, setItem(k, v), getItem(k),removeItem(k)
|
|
41
|
+
storage: Storage;
|
|
42
|
+
request: RequestFn;
|
|
43
|
+
clientSecret?: string;
|
|
44
|
+
// default value is 1,min value is 0, max value is 5
|
|
45
|
+
retry?: number;
|
|
46
|
+
refreshTokenFunc?: (refreshToken?: string) => Promise<Credentials>;
|
|
47
|
+
// set the token in url query instead of header
|
|
48
|
+
tokenInURL?: boolean;
|
|
49
|
+
headers?: { [key: string]: string };
|
|
50
|
+
}
|
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
import {ErrorType} from './consts';
|
|
2
|
+
|
|
3
|
+
import {AuthClient} from './interface';
|
|
4
|
+
import {RequestFn, Storage} from "../app";
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
Credentials,
|
|
8
|
+
ResponseError,
|
|
9
|
+
OAuth2ClientOptions,
|
|
10
|
+
AuthClientRequestOptions,
|
|
11
|
+
} from './models';
|
|
12
|
+
|
|
13
|
+
import {uuidv4} from '../utils/uuid';
|
|
14
|
+
|
|
15
|
+
import {PromiseOnce} from '../utils/promise';
|
|
16
|
+
|
|
17
|
+
const RequestIdHeaderName = 'x-request-id';
|
|
18
|
+
const DeviceIdHeaderName = 'x-device-id';
|
|
19
|
+
const DeviceIdSectionName = 'device_';
|
|
20
|
+
|
|
21
|
+
export interface ToResponseErrorOptions {
|
|
22
|
+
error?: ErrorType;
|
|
23
|
+
error_description?: string;
|
|
24
|
+
error_uri?: string;
|
|
25
|
+
details?: any;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
export const toResponseError = (
|
|
30
|
+
error: ResponseError | Error,
|
|
31
|
+
options?: ToResponseErrorOptions,
|
|
32
|
+
): ResponseError => {
|
|
33
|
+
let responseError: ResponseError;
|
|
34
|
+
const formatOptions: ToResponseErrorOptions = options || {};
|
|
35
|
+
if (error instanceof Error) {
|
|
36
|
+
responseError = {
|
|
37
|
+
error: formatOptions.error || ErrorType.LOCAL,
|
|
38
|
+
error_description: formatOptions.error_description || error.message,
|
|
39
|
+
error_uri: formatOptions.error_uri,
|
|
40
|
+
details: formatOptions.details || error.stack,
|
|
41
|
+
};
|
|
42
|
+
} else {
|
|
43
|
+
const formatError: ToResponseErrorOptions = error || {};
|
|
44
|
+
responseError = {
|
|
45
|
+
error: formatOptions.error || formatError.error || ErrorType.LOCAL,
|
|
46
|
+
error_description:
|
|
47
|
+
formatOptions.error_description || formatError.error_description,
|
|
48
|
+
error_uri: formatOptions.error_uri || formatError.error_uri,
|
|
49
|
+
details: formatOptions.details || formatError.details,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
return responseError;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Generate request id.
|
|
57
|
+
* @return {string}
|
|
58
|
+
*/
|
|
59
|
+
export function generateRequestId(): string {
|
|
60
|
+
return uuidv4();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
interface LocalCredentialsOptions {
|
|
65
|
+
tokenSectionName: string;
|
|
66
|
+
storage: Storage;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Check if credentials is expired.
|
|
71
|
+
* @param {Credentials} credentials
|
|
72
|
+
* @return {boolean}
|
|
73
|
+
*/
|
|
74
|
+
function isCredentialsExpired(credentials: Credentials): boolean {
|
|
75
|
+
let isExpired = true;
|
|
76
|
+
if (credentials && credentials.expires_at && credentials.access_token) {
|
|
77
|
+
isExpired = credentials.expires_at < new Date();
|
|
78
|
+
}
|
|
79
|
+
return isExpired;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Local credentials.
|
|
84
|
+
* Local credentials, with memory cache and storage cache.
|
|
85
|
+
* If the memory cache expires, the storage cache is automatically loaded.
|
|
86
|
+
*/
|
|
87
|
+
export class LocalCredentials {
|
|
88
|
+
private _tokenSectionName: string;
|
|
89
|
+
|
|
90
|
+
private _storage: Storage;
|
|
91
|
+
|
|
92
|
+
private _credentials: Credentials = null;
|
|
93
|
+
|
|
94
|
+
private _promiseOnce: PromiseOnce = new PromiseOnce();
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* constructor
|
|
98
|
+
* @param {LocalCredentialsOptions} options
|
|
99
|
+
*/
|
|
100
|
+
constructor(options: LocalCredentialsOptions) {
|
|
101
|
+
this._tokenSectionName = options.tokenSectionName;
|
|
102
|
+
this._storage = options.storage;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* setCredentials Provides an alternative fetch api request implementation with auth credentials
|
|
107
|
+
* @param {Credentials} credentials
|
|
108
|
+
*/
|
|
109
|
+
public async setCredentials(credentials?: Credentials): Promise<void> {
|
|
110
|
+
if (credentials && credentials.expires_in) {
|
|
111
|
+
credentials.expires_at = new Date(
|
|
112
|
+
Date.now() + (credentials.expires_in - 30) * 1000,
|
|
113
|
+
);
|
|
114
|
+
if (this._storage) {
|
|
115
|
+
const tokenStr: string = JSON.stringify(credentials);
|
|
116
|
+
await this._storage.setItem(this._tokenSectionName, tokenStr);
|
|
117
|
+
}
|
|
118
|
+
this._credentials = credentials;
|
|
119
|
+
} else {
|
|
120
|
+
if (this._storage) {
|
|
121
|
+
await this._storage.removeItem(this._tokenSectionName);
|
|
122
|
+
}
|
|
123
|
+
this._credentials = null;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Get credentials.
|
|
129
|
+
* @return {Promise<Credentials>}
|
|
130
|
+
*/
|
|
131
|
+
public async getCredentials(): Promise<Credentials> {
|
|
132
|
+
return this._promiseOnce.run('getCredentials', async () => {
|
|
133
|
+
if (isCredentialsExpired(this._credentials)) {
|
|
134
|
+
this._credentials = await this._getStorageCredentials();
|
|
135
|
+
}
|
|
136
|
+
return this._credentials;
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Get storage credentials.
|
|
142
|
+
*/
|
|
143
|
+
private async _getStorageCredentials(): Promise<Credentials> {
|
|
144
|
+
return this._promiseOnce.run('_getStorageCredentials', async () => {
|
|
145
|
+
let credentials: Credentials = null;
|
|
146
|
+
const tokenStr: string = await this._storage.getItem(
|
|
147
|
+
this._tokenSectionName,
|
|
148
|
+
);
|
|
149
|
+
if (tokenStr !== undefined && tokenStr !== null) {
|
|
150
|
+
try {
|
|
151
|
+
credentials = JSON.parse(tokenStr);
|
|
152
|
+
if (credentials && credentials.expires_at) {
|
|
153
|
+
credentials.expires_at = new Date(credentials.expires_at);
|
|
154
|
+
}
|
|
155
|
+
} catch (error) {
|
|
156
|
+
await this._storage.removeItem(this._tokenSectionName);
|
|
157
|
+
credentials = null;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return credentials;
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* OAuth2Client
|
|
167
|
+
*/
|
|
168
|
+
export class OAuth2Client implements AuthClient {
|
|
169
|
+
private static _defaultRetry = 2;
|
|
170
|
+
private static _minRetry = 0;
|
|
171
|
+
private static _maxRetry = 5;
|
|
172
|
+
private static _retryInterval = 1000;
|
|
173
|
+
private _retry: number;
|
|
174
|
+
private _baseRequest: RequestFn;
|
|
175
|
+
private _basicAuth?: string;
|
|
176
|
+
private _localCredentials: LocalCredentials;
|
|
177
|
+
private _storage: Storage;
|
|
178
|
+
private _deviceID?: string;
|
|
179
|
+
private _tokenInURL?: boolean;
|
|
180
|
+
private _refreshTokenFunc: (refreshToken?: string) => Promise<Credentials>;
|
|
181
|
+
private _headers?: { [key: string]: string };
|
|
182
|
+
private _promiseOnce: PromiseOnce = new PromiseOnce();
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* constructor
|
|
186
|
+
* @param {OAuth2ClientOptions} options
|
|
187
|
+
*/
|
|
188
|
+
constructor(options: OAuth2ClientOptions) {
|
|
189
|
+
this._retry = this._formatRetry(options.retry, OAuth2Client._defaultRetry);
|
|
190
|
+
this._baseRequest = options.request
|
|
191
|
+
if (!options.clientSecret) {
|
|
192
|
+
options.clientSecret = ""
|
|
193
|
+
}
|
|
194
|
+
if (options.clientId !== '') {
|
|
195
|
+
this._basicAuth = "Basic " + btoa(options.clientId + ":" + options.clientSecret);
|
|
196
|
+
}
|
|
197
|
+
this._tokenInURL = options.tokenInURL;
|
|
198
|
+
this._headers = options.headers;
|
|
199
|
+
// @ts-ignore
|
|
200
|
+
this._storage = options.storage || defaultStorage;
|
|
201
|
+
this._localCredentials = new LocalCredentials({
|
|
202
|
+
tokenSectionName: 'credentials_',
|
|
203
|
+
storage: this._storage,
|
|
204
|
+
});
|
|
205
|
+
this._refreshTokenFunc =
|
|
206
|
+
options.refreshTokenFunc || this._defaultRefreshTokenFunc;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* setCredentials Provides an alternative fetch api request implementation with auth credentials
|
|
211
|
+
* @param {Credentials} credentials
|
|
212
|
+
* @return {Promise<void>}
|
|
213
|
+
*/
|
|
214
|
+
public setCredentials(credentials?: Credentials): Promise<void> {
|
|
215
|
+
return this._localCredentials.setCredentials(credentials);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* getAccessToken return a validate access token
|
|
220
|
+
*/
|
|
221
|
+
public async getAccessToken(): Promise<string> {
|
|
222
|
+
const credentials: Credentials = await this._getCredentials();
|
|
223
|
+
if (credentials && credentials.access_token) {
|
|
224
|
+
return Promise.resolve(credentials.access_token);
|
|
225
|
+
}
|
|
226
|
+
return Promise.reject({error: ErrorType.UNAUTHENTICATED} as ResponseError);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* getScope return a validate access token
|
|
231
|
+
*/
|
|
232
|
+
public async getScope(): Promise<string> {
|
|
233
|
+
let credentials: Credentials = await this._localCredentials.getCredentials();
|
|
234
|
+
if (credentials == null) {
|
|
235
|
+
return this._unAuthenticatedError("credentials not found")
|
|
236
|
+
}
|
|
237
|
+
return credentials.scope;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* request http like simple fetch api, exp:request('/v1/user/me', {withCredentials:true})
|
|
243
|
+
* @param {string} url
|
|
244
|
+
* @param {AuthClientRequestOptions} options
|
|
245
|
+
*/
|
|
246
|
+
public async request<T>(
|
|
247
|
+
url: string,
|
|
248
|
+
options?: AuthClientRequestOptions,
|
|
249
|
+
): Promise<T> {
|
|
250
|
+
if (!options) {
|
|
251
|
+
options = {};
|
|
252
|
+
}
|
|
253
|
+
const retry: number = this._formatRetry(options.retry, this._retry);
|
|
254
|
+
options.headers = options.headers || {};
|
|
255
|
+
if (this._headers) {
|
|
256
|
+
options.headers = {
|
|
257
|
+
...this._headers,
|
|
258
|
+
...options.headers,
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
if (!options.headers[RequestIdHeaderName]) {
|
|
262
|
+
options.headers[RequestIdHeaderName] = generateRequestId();
|
|
263
|
+
}
|
|
264
|
+
if (!options.headers[DeviceIdHeaderName]) {
|
|
265
|
+
const deviceId = await this._getDeviceId();
|
|
266
|
+
options.headers[DeviceIdHeaderName] = deviceId;
|
|
267
|
+
}
|
|
268
|
+
if (options && options.withBasicAuth && this._basicAuth) {
|
|
269
|
+
options.headers['Authorization'] = this._basicAuth
|
|
270
|
+
}
|
|
271
|
+
if (options && options.withCredentials) {
|
|
272
|
+
const credentials = await this._getCredentials();
|
|
273
|
+
if (credentials) {
|
|
274
|
+
if (this._tokenInURL) {
|
|
275
|
+
if (url.indexOf('?') < 0) {
|
|
276
|
+
url += '?';
|
|
277
|
+
}
|
|
278
|
+
url += 'access_token=' + credentials.access_token;
|
|
279
|
+
} else {
|
|
280
|
+
options.headers['Authorization'] =
|
|
281
|
+
credentials.token_type + ' ' + credentials.access_token;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
let response: T;
|
|
286
|
+
const maxRequestTimes: number = retry + 1;
|
|
287
|
+
for (
|
|
288
|
+
let requestTime = 0;
|
|
289
|
+
requestTime < maxRequestTimes;
|
|
290
|
+
requestTime++
|
|
291
|
+
) {
|
|
292
|
+
try {
|
|
293
|
+
response = await this._baseRequest<T>(url, options);
|
|
294
|
+
break;
|
|
295
|
+
} catch (responseError) {
|
|
296
|
+
if (options.withCredentials && responseError && responseError.error === ErrorType.UNAUTHENTICATED) {
|
|
297
|
+
await this.setCredentials(null);
|
|
298
|
+
return Promise.reject(responseError);
|
|
299
|
+
}
|
|
300
|
+
if (
|
|
301
|
+
requestTime === retry ||
|
|
302
|
+
!responseError ||
|
|
303
|
+
responseError.error !== 'unreachable'
|
|
304
|
+
) {
|
|
305
|
+
return Promise.reject(responseError);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
await this._sleep(OAuth2Client._retryInterval);
|
|
309
|
+
}
|
|
310
|
+
return response;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Check retry value.
|
|
315
|
+
* @param {number} retry
|
|
316
|
+
* @return {number}
|
|
317
|
+
*/
|
|
318
|
+
private _checkRetry(retry: number): number {
|
|
319
|
+
let responseError: ResponseError = null;
|
|
320
|
+
if (
|
|
321
|
+
typeof retry !== 'number' ||
|
|
322
|
+
retry < OAuth2Client._minRetry ||
|
|
323
|
+
retry > OAuth2Client._maxRetry
|
|
324
|
+
) {
|
|
325
|
+
responseError = {
|
|
326
|
+
error: ErrorType.UNREACHABLE,
|
|
327
|
+
error_description: 'wrong options param: retry',
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
if (responseError) {
|
|
331
|
+
throw responseError;
|
|
332
|
+
}
|
|
333
|
+
return retry;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Format retry value.
|
|
338
|
+
* @param {number} retry
|
|
339
|
+
* @param {number} defaultVale
|
|
340
|
+
* @return {number}
|
|
341
|
+
*/
|
|
342
|
+
private _formatRetry(retry: number, defaultVale: number): number {
|
|
343
|
+
if (typeof retry === 'undefined') {
|
|
344
|
+
return defaultVale;
|
|
345
|
+
} else {
|
|
346
|
+
return this._checkRetry(retry);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Sleep.
|
|
352
|
+
* @param {number} ms
|
|
353
|
+
* @return {Promise<void>}
|
|
354
|
+
*/
|
|
355
|
+
private async _sleep(ms: number): Promise<void> {
|
|
356
|
+
return new Promise<void>((resolve) => {
|
|
357
|
+
setTimeout(() => {
|
|
358
|
+
resolve();
|
|
359
|
+
}, ms);
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Refresh expired token.
|
|
365
|
+
* @param {Credentials} credentials
|
|
366
|
+
* @return {Promise<Credentials>}
|
|
367
|
+
*/
|
|
368
|
+
private async _refreshToken(credentials: Credentials): Promise<Credentials> {
|
|
369
|
+
return this._promiseOnce.run('_refreshToken', async () => {
|
|
370
|
+
if (!credentials || !credentials.refresh_token) {
|
|
371
|
+
return this._unAuthenticatedError('no refresh token found in credentials');
|
|
372
|
+
}
|
|
373
|
+
try {
|
|
374
|
+
const newCredentials: Credentials = await this._refreshTokenFunc(
|
|
375
|
+
credentials.refresh_token,
|
|
376
|
+
);
|
|
377
|
+
await this._localCredentials.setCredentials(newCredentials);
|
|
378
|
+
return newCredentials
|
|
379
|
+
} catch (error) {
|
|
380
|
+
if (error.error === ErrorType.INVALID_GRANT) {
|
|
381
|
+
await this._localCredentials.setCredentials(null);
|
|
382
|
+
return this._unAuthenticatedError(error.error_description);
|
|
383
|
+
}
|
|
384
|
+
return Promise.reject(error);
|
|
385
|
+
}
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* anonymous signIn
|
|
391
|
+
* @param {Credentials} credentials
|
|
392
|
+
* @return {Promise<Credentials>}
|
|
393
|
+
*/
|
|
394
|
+
private async _anonymousSignIn(credentials: Credentials): Promise<Credentials> {
|
|
395
|
+
return this._promiseOnce.run('_anonymous', async () => {
|
|
396
|
+
if (!credentials || credentials.scope !== 'anonymous') {
|
|
397
|
+
return this._unAuthenticatedError('no anonymous in credentials');
|
|
398
|
+
}
|
|
399
|
+
try {
|
|
400
|
+
const newCredentials: Credentials = await this.request('/auth/v1/signin/anonymously', {
|
|
401
|
+
method: 'POST',
|
|
402
|
+
withBasicAuth: true,
|
|
403
|
+
body: {}
|
|
404
|
+
});
|
|
405
|
+
await this._localCredentials.setCredentials(newCredentials);
|
|
406
|
+
return newCredentials
|
|
407
|
+
} catch (error) {
|
|
408
|
+
if (error.error === ErrorType.INVALID_GRANT) {
|
|
409
|
+
await this._localCredentials.setCredentials(null);
|
|
410
|
+
return this._unAuthenticatedError(error.error_description);
|
|
411
|
+
}
|
|
412
|
+
return Promise.reject(error);
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Default refresh token function.
|
|
419
|
+
* @param {string} refreshToken
|
|
420
|
+
* @return {Promise<Credentials>}
|
|
421
|
+
*/
|
|
422
|
+
private _defaultRefreshTokenFunc(
|
|
423
|
+
refreshToken?: string,
|
|
424
|
+
): Promise<Credentials> {
|
|
425
|
+
if (refreshToken === undefined || refreshToken === '') {
|
|
426
|
+
return this._unAuthenticatedError('refresh token not found');
|
|
427
|
+
}
|
|
428
|
+
return this.request('/auth/v1/token', {
|
|
429
|
+
method: 'POST',
|
|
430
|
+
withBasicAuth: true,
|
|
431
|
+
body: {
|
|
432
|
+
grant_type: 'refresh_token',
|
|
433
|
+
refresh_token: refreshToken,
|
|
434
|
+
},
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Get credentials.
|
|
440
|
+
*/
|
|
441
|
+
private async _getCredentials(): Promise<Credentials> {
|
|
442
|
+
let credentials: Credentials = await this._localCredentials.getCredentials();
|
|
443
|
+
if (credentials == null) {
|
|
444
|
+
return this._unAuthenticatedError("credentials not found")
|
|
445
|
+
}
|
|
446
|
+
if (isCredentialsExpired(credentials)) {
|
|
447
|
+
if (credentials && credentials.scope === 'anonymous') {
|
|
448
|
+
credentials = await this._anonymousSignIn(credentials)
|
|
449
|
+
} else {
|
|
450
|
+
credentials = await this._refreshToken(credentials);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
return credentials;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* Get deviceId
|
|
458
|
+
*/
|
|
459
|
+
private async _getDeviceId(): Promise<string> {
|
|
460
|
+
if (this._deviceID) {
|
|
461
|
+
return this._deviceID;
|
|
462
|
+
}
|
|
463
|
+
let deviceId: string = await this._storage.getItem(
|
|
464
|
+
DeviceIdSectionName,
|
|
465
|
+
);
|
|
466
|
+
if (!(typeof deviceId === 'string' &&
|
|
467
|
+
deviceId.length >= 16 &&
|
|
468
|
+
deviceId.length <= 48)) {
|
|
469
|
+
deviceId = uuidv4();
|
|
470
|
+
await this._storage.setItem(DeviceIdSectionName, deviceId);
|
|
471
|
+
}
|
|
472
|
+
this._deviceID = deviceId;
|
|
473
|
+
return deviceId;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* Generate unAuthenticated error.
|
|
478
|
+
* @param {string} err
|
|
479
|
+
* @return {Promise<T>}
|
|
480
|
+
*/
|
|
481
|
+
private _unAuthenticatedError<T>(err?: string): Promise<T> {
|
|
482
|
+
return Promise.reject({
|
|
483
|
+
error: ErrorType.UNAUTHENTICATED,
|
|
484
|
+
error_description: err,
|
|
485
|
+
} as ResponseError);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Promise Once
|
|
3
|
+
*/
|
|
4
|
+
export class PromiseOnce {
|
|
5
|
+
/**
|
|
6
|
+
* Run Once promise.
|
|
7
|
+
* @param {string} key
|
|
8
|
+
* @param {Function} fn
|
|
9
|
+
* @return {Promise<T>}
|
|
10
|
+
*/
|
|
11
|
+
async run<T>(key: string, fn: () => Promise<T>): Promise<T> {
|
|
12
|
+
let result: Promise<any> = this._fnPromiseMap.get(key);
|
|
13
|
+
if (!result) {
|
|
14
|
+
result = new Promise<any>(async (resolve, reject) => {
|
|
15
|
+
try {
|
|
16
|
+
// The idle promise must be run to prevent _fnPromiseMap from
|
|
17
|
+
// storing the current promise function.
|
|
18
|
+
await this._runIdlePromise();
|
|
19
|
+
const fnResult: Promise<T> = fn();
|
|
20
|
+
resolve(await fnResult);
|
|
21
|
+
} catch (error) {
|
|
22
|
+
reject(error);
|
|
23
|
+
} finally {
|
|
24
|
+
this._fnPromiseMap.delete(key);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
this._fnPromiseMap.set(key, result);
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Run idle promise.
|
|
34
|
+
* @return {Promise<void>}
|
|
35
|
+
*/
|
|
36
|
+
private _runIdlePromise(): Promise<void> {
|
|
37
|
+
return Promise.resolve();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
private _fnPromiseMap: Map<string, Promise<any>> = new Map();
|
|
41
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generate uuidv4 string.
|
|
3
|
+
* @return {string}
|
|
4
|
+
*/
|
|
5
|
+
export function uuidv4(): string {
|
|
6
|
+
return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
|
7
|
+
const r = (Math.random() * 16) | 0;
|
|
8
|
+
const v = c == 'x' ? r : (r & 0x3) | 0x8;
|
|
9
|
+
return v.toString(16);
|
|
10
|
+
});
|
|
11
|
+
}
|