Skip to content

Commit 4164e99

Browse files
authored
fix(passport): game sdk pkce logout url (#2659)
1 parent 3e1336d commit 4164e99

4 files changed

Lines changed: 72 additions & 0 deletions

File tree

packages/game-bridge/src/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const PASSPORT_FUNCTIONS = {
3838
connectPKCE: 'connectPKCE',
3939
getAccessToken: 'getAccessToken',
4040
getIdToken: 'getIdToken',
41+
logout: 'logout',
4142
getEmail: 'getEmail',
4243
getPassportId: 'getPassportId',
4344
getLinkedAddresses: 'getLinkedAddresses',
@@ -402,6 +403,20 @@ window.callFunction = async (jsonData: string) => {
402403
});
403404
break;
404405
}
406+
case PASSPORT_FUNCTIONS.logout: {
407+
const logoutUrl = await getPassportClient().getLogoutUrl();
408+
providerInstance = null;
409+
zkEvmProviderInstance = null;
410+
trackDuration(moduleName, 'performedGetLogoutUrl', mt(markStart));
411+
callbackToGame({
412+
responseFor: fxName,
413+
requestId,
414+
success: true,
415+
error: null,
416+
result: logoutUrl,
417+
});
418+
break;
419+
}
405420
case PASSPORT_FUNCTIONS.getAccessToken: {
406421
const accessToken = await getPassportClient().getAccessToken();
407422
const success = accessToken !== undefined;

packages/passport/sdk/src/Passport.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,19 @@ export class Passport {
314314
}, 'logout');
315315
}
316316

317+
/**
318+
* Returns the logout URL for the current user.
319+
* @returns {Promise<string>} The logout URL
320+
*/
321+
public async getLogoutUrl(): Promise<string> {
322+
return withMetricsAsync(async () => {
323+
await this.authManager.removeUser();
324+
await this.magicAdapter.logout();
325+
this.passportEventEmitter.emit(PassportEvents.LOGGED_OUT);
326+
return await this.authManager.getLogoutUrl();
327+
}, 'getLogoutUrl');
328+
}
329+
317330
/**
318331
* Handles the silent logout callback.
319332
* @param {string} url - The callback URL to process

packages/passport/sdk/src/authManager.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,4 +659,37 @@ describe('AuthManager', () => {
659659
);
660660
});
661661
});
662+
663+
describe('getLogoutUrl', () => {
664+
describe('with a logged in user', () => {
665+
describe('when a logoutRedirectUri is specified', () => {
666+
it('should set the endSessionEndpoint `returnTo` and `client_id` query string params', async () => {
667+
mockGetUser.mockReturnValue(mockOidcUser);
668+
669+
const am = new AuthManager(getConfig({ logoutRedirectUri }));
670+
const result = await am.getLogoutUrl();
671+
const uri = new URL(result);
672+
673+
expect(uri.hostname).toEqual(authenticationDomain);
674+
expect(uri.pathname).toEqual(logoutEndpoint);
675+
expect(uri.searchParams.get('client_id')).toEqual(clientId);
676+
expect(uri.searchParams.get('returnTo')).toEqual(logoutRedirectUri);
677+
});
678+
});
679+
680+
describe('when no post_logout_redirect_uri is specified', () => {
681+
it('should return the endSessionEndpoint without a `returnTo` or `client_id` query string params', async () => {
682+
mockGetUser.mockReturnValue(mockOidcUser);
683+
684+
const am = new AuthManager(getConfig());
685+
const result = await am.getLogoutUrl();
686+
const uri = new URL(result);
687+
688+
expect(uri.hostname).toEqual(authenticationDomain);
689+
expect(uri.pathname).toEqual(logoutEndpoint);
690+
expect(uri.searchParams.get('client_id')).toEqual(clientId);
691+
});
692+
});
693+
});
694+
});
662695
});

packages/passport/sdk/src/authManager.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,17 @@ export default class AuthManager {
364364
return this.userManager.removeUser();
365365
}
366366

367+
public async getLogoutUrl(): Promise<string> {
368+
const { authenticationDomain, oidcConfiguration } = this.config;
369+
370+
const endSessionEndpoint = new URL(logoutEndpoint, authenticationDomain);
371+
endSessionEndpoint.searchParams.set('client_id', oidcConfiguration.clientId);
372+
373+
if (oidcConfiguration.logoutRedirectUri) endSessionEndpoint.searchParams.set('returnTo', oidcConfiguration.logoutRedirectUri);
374+
375+
return endSessionEndpoint.toString();
376+
}
377+
367378
public forceUserRefreshInBackground() {
368379
this.refreshTokenAndUpdatePromise().catch((error) => {
369380
logger.warn('Failed to refresh user token', error);

0 commit comments

Comments
 (0)