diff --git a/packages/passport/sdk/src/Passport.int.test.ts b/packages/passport/sdk/src/Passport.int.test.ts index dee683c7ec..3c671aa87f 100644 --- a/packages/passport/sdk/src/Passport.int.test.ts +++ b/packages/passport/sdk/src/Passport.int.test.ts @@ -4,6 +4,7 @@ import { TransactionRequest } from '@ethersproject/providers'; import { Environment, ImmutableConfiguration } from '@imtbl/config'; import { OidcConfiguration } from 'types'; import { IMXClient } from '@imtbl/x-client'; +import encode from 'jwt-encode'; import { mockValidIdToken } from './utils/token.test'; import { buildPrivateVars, Passport } from './Passport'; import { RequestArguments } from './zkEvm/types'; @@ -16,7 +17,7 @@ import { } from './mocks/zkEvm/msw'; import { JsonRpcError, RpcErrorCode } from './zkEvm/JsonRpcError'; import GuardianClient from './guardian'; -import { chainIdHex } from './test/mocks'; +import { chainIdHex, mockUserZkEvm } from './test/mocks'; jest.mock('./guardian'); @@ -38,13 +39,12 @@ const mockOidcUser = { const mockOidcUserZkevm = { ...mockOidcUser, - profile: { - ...mockOidcUser.profile, + id_token: encode({ passport: { - zkevm_eth_address: '0x7EEC32793414aAb720a90073607733d9e7B0ecD0', - zkevm_user_admin_address: '0x123', + zkevm_eth_address: mockUserZkEvm.zkEvm.ethAddress, + zkevm_user_admin_address: mockUserZkEvm.zkEvm.userAdminAddress, }, - }, + }, 'secret'), }; const oidcConfiguration: OidcConfiguration = { @@ -77,6 +77,7 @@ describe('Passport', () => { beforeEach(() => { jest.resetAllMocks(); + (UserManager as jest.Mock).mockImplementation(() => ({ signinPopup: mockSigninPopup, signinSilent: mockSigninSilent, @@ -87,12 +88,8 @@ describe('Passport', () => { withConfirmationScreen: () => (task: () => void) => task(), })); (Magic as jest.Mock).mockImplementation(() => ({ - openid: { - loginWithOIDC: mockLoginWithOidc, - }, - rpcProvider: { - request: mockMagicRequest, - }, + openid: { loginWithOIDC: mockLoginWithOidc }, + rpcProvider: { request: mockMagicRequest }, preload: jest.fn(), })); }); @@ -109,10 +106,12 @@ describe('Passport', () => { describe('when the env is prod', () => { it('sets the prod x URL as the basePath on imxApiClients', () => { const baseConfig = new ImmutableConfiguration({ environment: Environment.PRODUCTION }); + const privateVars = buildPrivateVars({ baseConfig, ...oidcConfiguration, }); + expect(privateVars.passportImxProviderFactory.imxApiClients.config.basePath).toEqual('https://api.x.immutable.com'); }); }); @@ -120,10 +119,12 @@ describe('Passport', () => { describe('when the env is sandbox', () => { it('sets the sandbox x URL as the basePath on imxApiClients', () => { const baseConfig = new ImmutableConfiguration({ environment: Environment.SANDBOX }); + const privateVars = buildPrivateVars({ baseConfig, ...oidcConfiguration, }); + expect(privateVars.passportImxProviderFactory.imxApiClients.config.basePath).toEqual('https://api.sandbox.x.immutable.com'); }); }); @@ -174,7 +175,7 @@ describe('Passport', () => { method: 'eth_requestAccounts', }); - expect(accounts).toEqual([mockOidcUserZkevm.profile.passport.zkevm_eth_address]); + expect(accounts).toEqual([mockUserZkEvm.zkEvm.ethAddress]); expect(mockGetUser).toHaveBeenCalledTimes(1); }); }); @@ -199,6 +200,7 @@ describe('Passport', () => { mockSigninPopup.mockResolvedValue(mockOidcUser); mockSigninSilent.mockResolvedValueOnce(mockOidcUserZkevm); useMswHandlers([ + mswHandlers.jsonRpcProvider.success, mswHandlers.counterfactualAddress.success, mswHandlers.api.chains.success, ]); @@ -209,7 +211,7 @@ describe('Passport', () => { method: 'eth_requestAccounts', }); - expect(accounts).toEqual([mockOidcUserZkevm.profile.passport.zkevm_eth_address]); + expect(accounts).toEqual([mockUserZkEvm.zkEvm.ethAddress]); expect(mockGetUser).toHaveBeenCalledTimes(1); }); @@ -218,7 +220,7 @@ describe('Passport', () => { mockSigninPopup.mockResolvedValue(mockOidcUser); mockGetUser.mockResolvedValueOnce(null); mockGetUser.mockResolvedValueOnce(mockOidcUser); - mockSigninSilent.mockResolvedValue(mockOidcUserZkevm); + mockSigninSilent.mockResolvedValue(mockOidcUser); useMswHandlers([ mswHandlers.counterfactualAddress.internalServerError, mswHandlers.api.chains.success, diff --git a/packages/passport/sdk/src/authManager.test.ts b/packages/passport/sdk/src/authManager.test.ts index 46353fd727..3acec6efa5 100644 --- a/packages/passport/sdk/src/authManager.test.ts +++ b/packages/passport/sdk/src/authManager.test.ts @@ -1,5 +1,6 @@ import { Environment, ImmutableConfiguration } from '@imtbl/config'; import { User as OidcUser, UserManager, WebStorageStateStore } from 'oidc-client-ts'; +import jwt_decode from 'jwt-decode'; import AuthManager from './authManager'; import { PassportError, PassportErrorType } from './errors/passportError'; import { PassportConfiguration } from './config'; @@ -109,7 +110,6 @@ describe('AuthManager', () => { expect(UserManager).toBeCalledWith({ authority: config.authenticationDomain, client_id: config.oidcConfiguration.clientId, - loadUserInfo: true, mergeClaims: true, metadata: { authorization_endpoint: `${config.authenticationDomain}/authorize`, @@ -170,13 +170,12 @@ describe('AuthManager', () => { describe('when the user has registered for imx', () => { it('should populate the imx object', async () => { - mockSigninPopup.mockResolvedValue({ - ...mockOidcUser, - profile: { - ...mockOidcUser.profile, - passport: { - ...imxProfileData, - }, + mockSigninPopup.mockResolvedValue(mockOidcUser); + (jwt_decode as jest.Mock).mockReturnValue({ + passport: { + imx_eth_address: mockUserImx.imx.ethAddress, + imx_stark_address: mockUserImx.imx.starkAddress, + imx_user_admin_address: mockUserImx.imx.userAdminAddress, }, }); @@ -188,13 +187,12 @@ describe('AuthManager', () => { describe('when the user has registered for zkEvm', () => { it('should populate the zkEvm object', async () => { - mockSigninPopup.mockResolvedValue({ - ...mockOidcUser, - profile: { - ...mockOidcUser.profile, - passport: { - ...zkEvmProfileData, - }, + mockSigninPopup.mockResolvedValue(mockOidcUser); + + (jwt_decode as jest.Mock).mockReturnValue({ + passport: { + zkevm_eth_address: mockUserZkEvm.zkEvm.ethAddress, + zkevm_user_admin_address: mockUserZkEvm.zkEvm.userAdminAddress, }, }); @@ -206,14 +204,14 @@ describe('AuthManager', () => { describe('when the user has registered for imx & zkEvm', () => { it('should populate the imx & zkEvm objects', async () => { - mockSigninPopup.mockResolvedValue({ - ...mockOidcUser, - profile: { - ...mockOidcUser.profile, - passport: { - ...zkEvmProfileData, - ...imxProfileData, - }, + mockSigninPopup.mockResolvedValue(mockOidcUser); + (jwt_decode as jest.Mock).mockReturnValue({ + passport: { + zkevm_eth_address: mockUserZkEvm.zkEvm.ethAddress, + zkevm_user_admin_address: mockUserZkEvm.zkEvm.userAdminAddress, + imx_eth_address: mockUserImx.imx.ethAddress, + imx_stark_address: mockUserImx.imx.starkAddress, + imx_user_admin_address: mockUserImx.imx.userAdminAddress, }, }); diff --git a/packages/passport/sdk/src/authManager.ts b/packages/passport/sdk/src/authManager.ts index f624537d0f..915c31ab8a 100644 --- a/packages/passport/sdk/src/authManager.ts +++ b/packages/passport/sdk/src/authManager.ts @@ -53,7 +53,6 @@ const getAuthConfiguration = (config: PassportConfiguration): UserManagerSetting end_session_endpoint: endSessionEndpoint, }, mergeClaims: true, - loadUserInfo: true, scope: oidcConfiguration.scope, userStore, }; @@ -105,7 +104,11 @@ export default class AuthManager { } private static mapOidcUserToDomainModel = (oidcUser: OidcUser): User => { - const passport = oidcUser.profile?.passport as PassportMetadata; + let passport: PassportMetadata | undefined; + if (oidcUser.id_token) { + passport = jwt_decode(oidcUser.id_token)?.passport; + } + const user: User = { expired: oidcUser.expired, idToken: oidcUser.id_token, diff --git a/packages/passport/sdk/src/test/mocks.ts b/packages/passport/sdk/src/test/mocks.ts index 75a481393f..bd8a0879f3 100644 --- a/packages/passport/sdk/src/test/mocks.ts +++ b/packages/passport/sdk/src/test/mocks.ts @@ -45,8 +45,8 @@ export const mockUserImx: UserImx = { export const mockUserZkEvm: UserZkEvm = { ...mockUser, zkEvm: { - ethAddress: 'zkevmEthAddress123', - userAdminAddress: 'zkevmUserAdminAddress123', + ethAddress: '0x0000000000000000000000000000000000000001', + userAdminAddress: '0x0000000000000000000000000000000000000002', }, };