Skip to content

Commit ca49c4e

Browse files
committed
conflicts
2 parents 583b130 + 8249f83 commit ca49c4e

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

packages/auth/src/Auth.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Auth } from './Auth';
22
import { AuthEvents, User } from './types';
33
import { withMetricsAsync } from './utils/metrics';
4+
import jwt_decode from 'jwt-decode';
45

56
const trackFlowMock = jest.fn();
67
const trackErrorMock = jest.fn();
@@ -17,12 +18,15 @@ jest.mock('@imtbl/metrics', () => ({
1718
getDetail: (...args: any[]) => getDetailMock(...args),
1819
}));
1920

21+
jest.mock('jwt-decode', () => jest.fn());
22+
2023
beforeEach(() => {
2124
trackFlowMock.mockReset();
2225
trackErrorMock.mockReset();
2326
identifyMock.mockReset();
2427
trackMock.mockReset();
2528
getDetailMock.mockReset();
29+
(jwt_decode as jest.Mock).mockReset();
2630
});
2731

2832
describe('withMetricsAsync', () => {
@@ -130,4 +134,53 @@ describe('Auth', () => {
130134
expect(params.rid).toEqual('runtime-id-value');
131135
});
132136
});
137+
138+
describe('username extraction', () => {
139+
it('extracts username from id token when present', () => {
140+
const mockOidcUser = {
141+
id_token: 'token',
142+
access_token: 'access',
143+
refresh_token: 'refresh',
144+
expired: false,
145+
profile: { sub: 'user-123', email: 'test@example.com', nickname: 'tester' },
146+
};
147+
148+
(jwt_decode as jest.Mock).mockReturnValue({
149+
username: 'username123',
150+
passport: undefined,
151+
});
152+
153+
const result = (Auth as any).mapOidcUserToDomainModel(mockOidcUser);
154+
155+
expect(jwt_decode).toHaveBeenCalledWith('token');
156+
expect(result.profile.username).toEqual('username123');
157+
});
158+
159+
it('maps username when creating OIDC user from device tokens', () => {
160+
const tokenResponse = {
161+
id_token: 'token',
162+
access_token: 'access',
163+
refresh_token: 'refresh',
164+
token_type: 'Bearer',
165+
expires_in: 3600,
166+
};
167+
168+
(jwt_decode as jest.Mock).mockReturnValue({
169+
sub: 'user-123',
170+
iss: 'issuer',
171+
aud: 'audience',
172+
exp: 1,
173+
iat: 0,
174+
email: 'test@example.com',
175+
nickname: 'tester',
176+
username: 'username123',
177+
passport: undefined,
178+
});
179+
180+
const oidcUser = (Auth as any).mapDeviceTokenResponseToOidcUser(tokenResponse);
181+
182+
expect(jwt_decode).toHaveBeenCalledWith('token');
183+
expect(oidcUser.profile.username).toEqual('username123');
184+
});
185+
});
133186
});

packages/auth/src/Auth.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ export class Auth {
252252
* @returns Promise that resolves with the user or null if not authenticated
253253
*/
254254
async getUser(): Promise<User | null> {
255-
return withMetricsAsync(async () => this.getUserInternal(), 'getUserInfo', false);
255+
return this.getUserInternal();
256256
}
257257

258258
/**
@@ -521,8 +521,13 @@ export class Auth {
521521

522522
private static mapOidcUserToDomainModel = (oidcUser: OidcUser): User => {
523523
let passport: PassportMetadata | undefined;
524+
let username: string | undefined;
524525
if (oidcUser.id_token) {
525-
passport = jwt_decode<IdTokenPayload>(oidcUser.id_token)?.passport;
526+
const idTokenPayload = jwt_decode<IdTokenPayload>(oidcUser.id_token);
527+
passport = idTokenPayload?.passport;
528+
if (idTokenPayload?.username) {
529+
username = idTokenPayload?.username;
530+
}
526531
}
527532

528533
const user: User = {
@@ -534,6 +539,7 @@ export class Auth {
534539
sub: oidcUser.profile.sub,
535540
email: oidcUser.profile.email,
536541
nickname: oidcUser.profile.nickname,
542+
username,
537543
},
538544
};
539545
if (passport?.zkevm_eth_address && passport?.zkevm_user_admin_address) {
@@ -561,6 +567,7 @@ export class Auth {
561567
email: idTokenPayload.email,
562568
nickname: idTokenPayload.nickname,
563569
passport: idTokenPayload.passport,
570+
...(idTokenPayload.username ? { username: idTokenPayload.username } : {}),
564571
},
565572
});
566573
};

packages/auth/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type UserProfile = {
99
email?: string;
1010
nickname?: string;
1111
sub: string;
12+
username?: string;
1213
};
1314

1415
export enum RollupType {
@@ -91,6 +92,7 @@ export type TokenPayload = {
9192

9293
export type IdTokenPayload = {
9394
passport?: PassportMetadata;
95+
username?: string;
9496
email: string;
9597
nickname: string;
9698
aud: string;

packages/passport/sdk/src/Passport.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,10 @@ export class Passport {
326326
* @returns {Promise<UserProfile | undefined>} A promise that resolves to the user profile if logged in, undefined otherwise
327327
*/
328328
public async getUserInfo(): Promise<UserProfile | undefined> {
329-
const user = await this.auth.getUser();
330-
return user?.profile;
329+
return withMetricsAsync(async () => {
330+
const user = await this.auth.getUser();
331+
return user?.profile;
332+
}, 'getUserInfo', false);
331333
}
332334

333335
/**

0 commit comments

Comments
 (0)