From 3f0fbee8e5a31bca9e43a9f6fa7470fbfaf92b49 Mon Sep 17 00:00:00 2001 From: Embbnux Ji Date: Thu, 14 Apr 2022 06:00:27 +0800 Subject: [PATCH] enhancement: use cache discovery data when request discovery error at login (#181) --- sdk/package.json | 2 +- sdk/src/platform/Platform-spec.ts | 39 +++++++++++++++++++++++++++++++ sdk/src/platform/Platform.ts | 29 +++++++++++++++-------- 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/sdk/package.json b/sdk/package.json index 873b61d..6059ec7 100644 --- a/sdk/package.json +++ b/sdk/package.json @@ -27,7 +27,7 @@ "@ringcentral/sdk-utils": "*", "@types/mocha": "5.2.5", "@types/node": "10.12.0", - "karma": "6.3.14", + "karma": "6.3.16", "mocha": "5.2.0", "npm-run-all": "4.1.3", "nyc": "13.1.0", diff --git a/sdk/src/platform/Platform-spec.ts b/sdk/src/platform/Platform-spec.ts index 10e7f4b..fea1237 100644 --- a/sdk/src/platform/Platform-spec.ts +++ b/sdk/src/platform/Platform-spec.ts @@ -1008,6 +1008,45 @@ describe('RingCentral.platform.Platform', () => { ).to.equal(0); }); + it('should not throw error when fetch initial discovery error with cache data on loginUrlWithDiscovery', async function() { + this.timeout(20000); + const initialDiscoveryData = getInitialDiscoveryMockData(); + apiCall('GET', '/.well-known/entry-points/initial?clientId=whatever', initialDiscoveryData); + const sdk = createSdk({enableDiscovery: true, discoveryServer: 'http://whatever', server: ''}); + const platform = sdk.platform(); + if (platform.discoveryInitPromise) { + await platform.discoveryInitPromise; + } + apiCall('GET', '/.well-known/entry-points/initial?clientId=whatever', {description: 'Fail'}, 500); + apiCall('GET', '/.well-known/entry-points/initial?clientId=whatever', {description: 'Fail'}, 500); + apiCall('GET', '/.well-known/entry-points/initial?clientId=whatever', {description: 'Fail'}, 500); + expect( + (await platform.loginUrlWithDiscovery()).indexOf(initialDiscoveryData.authApi.authorizationUri), + ).to.equal(0); + }); + + it('should throw error when fetch initial discovery error without cache data on loginUrlWithDiscovery', async function() { + this.timeout(20000); + const initialDiscoveryData = getInitialDiscoveryMockData(); + apiCall('GET', '/.well-known/entry-points/initial?clientId=whatever', initialDiscoveryData); + const sdk = createSdk({enableDiscovery: true, discoveryServer: 'http://whatever', server: ''}); + const platform = sdk.platform(); + if (platform.discoveryInitPromise) { + await platform.discoveryInitPromise; + } + await platform.discovery().removeInitialData(); + apiCall('GET', '/.well-known/entry-points/initial?clientId=whatever', {description: 'Fail'}, 500); + apiCall('GET', '/.well-known/entry-points/initial?clientId=whatever', {description: 'Fail'}, 500); + apiCall('GET', '/.well-known/entry-points/initial?clientId=whatever', {description: 'Fail'}, 500); + let error; + try { + await platform.loginUrlWithDiscovery(); + } catch (e) { + error = e; + } + expect(!!error).to.equal(true); + }); + it('should fetch external discovery when login with discovery_uri and token_uri', async () => { // mock const initialDiscoveryData = getInitialDiscoveryMockData(); diff --git a/sdk/src/platform/Platform.ts b/sdk/src/platform/Platform.ts index 2e0d42d..a86f84c 100644 --- a/sdk/src/platform/Platform.ts +++ b/sdk/src/platform/Platform.ts @@ -105,7 +105,7 @@ export default class Platform extends EventEmitter { authorizeEndpoint = '/restapi/oauth/authorize', enableDiscovery = false, discoveryServer, - discoveryInitalEndpoint = '/.well-known/entry-points/initial', + discoveryInitialEndpoint = '/.well-known/entry-points/initial', discoveryAutoInit = true, authProxy = false, urlPrefix = '', @@ -143,8 +143,8 @@ export default class Platform extends EventEmitter { this._codeVerifier = ''; if (enableDiscovery) { const initialEndpoint = discoveryServer - ? `${discoveryServer}${discoveryInitalEndpoint}` - : discoveryInitalEndpoint; + ? `${discoveryServer}${discoveryInitialEndpoint}` + : discoveryInitialEndpoint; this._discovery = new Discovery({ clientId, brandId, @@ -217,12 +217,21 @@ export default class Platform extends EventEmitter { public async loginUrlWithDiscovery(options: LoginUrlOptions = {}) { if (this._discovery) { - // fetch new discovery when generate login url - const discoveryData = await this._discovery.fetchInitialData(); - this._authorizeEndpoint = discoveryData.authApi.authorizationUri; - if (this._discoveryInitPromise) { - // await init discovery if it's not initialized - await this._discoveryInitPromise; + try { + // fetch new discovery when generate login url + const discoveryData = await this._discovery.fetchInitialData(); + this._authorizeEndpoint = discoveryData.authApi.authorizationUri; + if (this._discoveryInitPromise) { + // await init discovery if it's not initialized + await this._discoveryInitPromise; + } + } catch (e) { + const discoveryData = await this._discovery.initialData(); + if (!discoveryData) { + throw e; + } + // feedback to use the cached data + this._authorizeEndpoint = discoveryData.authApi.authorizationUri; } } return this.loginUrl(options); @@ -846,7 +855,7 @@ export interface PlatformOptions extends AuthOptions { handleRateLimit?: boolean | number; enableDiscovery?: boolean; discoveryServer?: string; - discoveryInitalEndpoint?: string; + discoveryInitialEndpoint?: string; discoveryAuthorizedEndpoint?: string; discoveryAutoInit?: boolean; brandId?: string;