Skip to content

Commit

Permalink
chore: ID-1423 Remove redundant userInfo auth request (#1494)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-Alonso authored Feb 18, 2024
1 parent 6e74cb7 commit 204998e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 42 deletions.
32 changes: 17 additions & 15 deletions packages/passport/sdk/src/Passport.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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');

Expand All @@ -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 = {
Expand Down Expand Up @@ -77,6 +77,7 @@ describe('Passport', () => {

beforeEach(() => {
jest.resetAllMocks();

(UserManager as jest.Mock).mockImplementation(() => ({
signinPopup: mockSigninPopup,
signinSilent: mockSigninSilent,
Expand All @@ -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(),
}));
});
Expand All @@ -109,21 +106,25 @@ 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');
});
});

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');
});
});
Expand Down Expand Up @@ -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);
});
});
Expand All @@ -199,6 +200,7 @@ describe('Passport', () => {
mockSigninPopup.mockResolvedValue(mockOidcUser);
mockSigninSilent.mockResolvedValueOnce(mockOidcUserZkevm);
useMswHandlers([
mswHandlers.jsonRpcProvider.success,
mswHandlers.counterfactualAddress.success,
mswHandlers.api.chains.success,
]);
Expand All @@ -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);
});

Expand All @@ -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,
Expand Down
44 changes: 21 additions & 23 deletions packages/passport/sdk/src/authManager.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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`,
Expand Down Expand Up @@ -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,
},
});

Expand All @@ -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,
},
});

Expand All @@ -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,
},
});

Expand Down
7 changes: 5 additions & 2 deletions packages/passport/sdk/src/authManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const getAuthConfiguration = (config: PassportConfiguration): UserManagerSetting
end_session_endpoint: endSessionEndpoint,
},
mergeClaims: true,
loadUserInfo: true,
scope: oidcConfiguration.scope,
userStore,
};
Expand Down Expand Up @@ -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<IdTokenPayload>(oidcUser.id_token)?.passport;
}

const user: User = {
expired: oidcUser.expired,
idToken: oidcUser.id_token,
Expand Down
4 changes: 2 additions & 2 deletions packages/passport/sdk/src/test/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export const mockUserImx: UserImx = {
export const mockUserZkEvm: UserZkEvm = {
...mockUser,
zkEvm: {
ethAddress: 'zkevmEthAddress123',
userAdminAddress: 'zkevmUserAdminAddress123',
ethAddress: '0x0000000000000000000000000000000000000001',
userAdminAddress: '0x0000000000000000000000000000000000000002',
},
};

Expand Down

0 comments on commit 204998e

Please sign in to comment.