Skip to content

Commit

Permalink
enhancement: use cache discovery data when request discovery error at…
Browse files Browse the repository at this point in the history
… login (#181)
  • Loading branch information
embbnux authored Apr 13, 2022
1 parent ed5a310 commit 3f0fbee
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
2 changes: 1 addition & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
39 changes: 39 additions & 0 deletions sdk/src/platform/Platform-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
29 changes: 19 additions & 10 deletions sdk/src/platform/Platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '',
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 3f0fbee

Please sign in to comment.