Skip to content

Commit

Permalink
enhancement: check and fetch discovery data at login (#201)
Browse files Browse the repository at this point in the history
* enhancement: check discovery data at login

* feat: fetch init discovery again when not init data at login
  • Loading branch information
embbnux committed Feb 10, 2023
1 parent 25223f1 commit dcb63a6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
23 changes: 23 additions & 0 deletions sdk/src/platform/Platform-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,29 @@ describe('RingCentral.platform.Platform', () => {
expect(await platform.discovery().externalDataExpired()).to.equal(false);
});

it('should fetch external discovery successfully when login without initDiscovery data', async () => {
// mock
const initialDiscoveryData = getInitialDiscoveryMockData();
const externalDiscoveryData = getExternalDiscoveryMockData();
apiCall('GET', '/.well-known/entry-points/initial?clientId=whatever', initialDiscoveryData);
apiCall('GET', '/.well-known/entry-points/external', externalDiscoveryData);
authentication();

const sdk = createSdk({
enableDiscovery: true,
discoveryServer: 'http://whatever',
discoveryAutoInit: false,
server: '',
});
const platform = sdk.platform();
await platform.login({
code: 'whatever',
});
const externalData = await platform.discovery().externalData();
expect(externalData.coreApi.baseUri).to.equal(externalDiscoveryData.coreApi.baseUri);
expect(await platform.discovery().externalDataExpired()).to.equal(false);
});

it('should fetch external discovery when login without discovery_uri and token_uri', async () => {
// mock
const initialDiscoveryData = getInitialDiscoveryMockData();
Expand Down
7 changes: 6 additions & 1 deletion sdk/src/platform/Platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,12 @@ export default class Platform extends EventEmitter {
if (this._discoveryInitPromise) {
await this._discoveryInitPromise;
}
const discoveryData = await this._discovery.initialData();
let discoveryData = await this._discovery.initialData();
// check if discovery data is initialized successfully
if (!discoveryData) {
// discovery request fail in previous init, try re-fetch discovery
discoveryData = await this._discovery.fetchInitialData();
}
if (!tokenEndpoint) {
tokenEndpoint = discoveryData.authApi.defaultTokenUri;
}
Expand Down

0 comments on commit dcb63a6

Please sign in to comment.