Skip to content

Commit

Permalink
fix: ID-1202 Fix deviceflow error
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenfowler authored Nov 16, 2023
1 parent 5ab007a commit 17c1179
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
50 changes: 50 additions & 0 deletions packages/passport/sdk/src/authManager.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { Environment, ImmutableConfiguration } from '@imtbl/config';
import { User as OidcUser, UserManager, WebStorageStateStore } from 'oidc-client-ts';
import jwt_decode from 'jwt-decode';
import DeviceCredentialsManager from 'storage/device_credentials_manager';
import AuthManager from './authManager';
import { PassportError, PassportErrorType } from './errors/passportError';
import { PassportConfiguration } from './config';
import { mockUser, mockUserImx, mockUserZkEvm } from './test/mocks';
import { isTokenExpired } from './utils/token';
import { DeviceTokenResponse } from './types';

jest.mock('oidc-client-ts');
jest.mock('storage/device_credentials_manager');
jest.mock('jwt-decode');
jest.mock('./utils/token');

const baseConfig = new ImmutableConfiguration({
Expand Down Expand Up @@ -73,6 +78,8 @@ describe('AuthManager', () => {
let mockGetUser: jest.Mock;
let mockSigninSilent: jest.Mock;
let mockSignoutSilent: jest.Mock;
let mockAreValid: jest.Mock;
let mockGetCredentials: jest.Mock;

beforeEach(() => {
mockSignIn = jest.fn();
Expand All @@ -81,6 +88,8 @@ describe('AuthManager', () => {
mockGetUser = jest.fn();
mockSigninSilent = jest.fn();
mockSignoutSilent = jest.fn();
mockAreValid = jest.fn();
mockGetCredentials = jest.fn();
(UserManager as jest.Mock).mockReturnValue({
signinPopup: mockSignIn,
signinPopupCallback: mockSigninPopupCallback,
Expand All @@ -89,6 +98,10 @@ describe('AuthManager', () => {
getUser: mockGetUser,
signinSilent: mockSigninSilent,
});
(DeviceCredentialsManager as jest.Mock).mockReturnValue({
areValid: mockAreValid,
getCredentials: mockGetCredentials,
});
authManager = new AuthManager(config);
});

Expand Down Expand Up @@ -393,4 +406,41 @@ describe('AuthManager', () => {
});
});
});

describe('connectImxWithCredentials', () => {
describe('when the user has not registered for any rollup', () => {
it('should return a User', async () => {
mockAreValid.mockReturnValue(true);
(jwt_decode as jest.Mock).mockReturnValue({
email: mockUser.profile.email,
nickname: mockUser.profile.nickname,
aud: 'audience123',
sub: 'subject123',
exp: 1234567890,
});

const tokenResponse: DeviceTokenResponse = {
access_token: mockUser.accessToken,
refresh_token: mockUser.refreshToken,
id_token: mockUser.idToken!,
token_type: 'Bearer',
expires_in: 167222,
};

const result = await authManager.connectImxWithCredentials(tokenResponse);

expect(mockAreValid).toHaveBeenCalledWith(tokenResponse);
expect(result).toEqual({
idToken: mockUser.idToken,
accessToken: mockUser.accessToken,
refreshToken: mockUser.refreshToken,
profile: {
email: mockUser.profile.email,
nickname: mockUser.profile.nickname,
sub: 'subject123',
},
});
});
});
});
});
2 changes: 1 addition & 1 deletion packages/passport/sdk/src/authManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export default class AuthManager {
nickname: idTokenPayload.nickname,
},
};
if (idTokenPayload?.passport.imx_eth_address) {
if (idTokenPayload?.passport?.imx_eth_address) {
user.imx = {
ethAddress: idTokenPayload.passport.imx_eth_address,
starkAddress: idTokenPayload.passport.imx_stark_address,
Expand Down
2 changes: 1 addition & 1 deletion packages/passport/sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export type TokenPayload = {
};

export type IdTokenPayload = {
passport: PassportMetadata;
passport?: PassportMetadata;
email: string;
nickname: string;
aud: string;
Expand Down

0 comments on commit 17c1179

Please sign in to comment.