Skip to content

Commit 53a506b

Browse files
fix: totp on list-path AuthenticationFactor is now optional to match the API (#1665)
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent ad71216 commit 53a506b

7 files changed

Lines changed: 124 additions & 8 deletions

File tree

src/multi-factor-auth/fixtures/list-factors.json

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,27 @@
1010
"totp": {
1111
"issuer": "WorkOS",
1212
"user": "some_user"
13-
}
13+
},
14+
"user_id": "user_01H5JQDV7R7ATEYZDEG0W5PRYS"
15+
},
16+
{
17+
"object": "authentication_factor",
18+
"id": "auth_factor_5678",
19+
"created_at": "2022-03-15T20:39:19.892Z",
20+
"updated_at": "2022-03-15T20:39:19.892Z",
21+
"type": "sms",
22+
"sms": {
23+
"phone_number": "+15555555555"
24+
},
25+
"user_id": "user_01H5JQDV7R7ATEYZDEG0W5PRYS"
26+
},
27+
{
28+
"object": "authentication_factor",
29+
"id": "auth_factor_9012",
30+
"created_at": "2022-03-15T20:39:19.892Z",
31+
"updated_at": "2022-03-15T20:39:19.892Z",
32+
"type": "generic_otp",
33+
"user_id": "user_01H5JQDV7R7ATEYZDEG0W5PRYS"
1434
}
1535
],
1636
"list_metadata": {

src/multi-factor-auth/interfaces/factor.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
TotpWithSecretsResponse,
77
} from './totp.interface';
88

9-
type FactorType = 'sms' | 'totp' | 'generic_otp';
9+
export type FactorType = 'sms' | 'totp' | 'generic_otp';
1010

1111
export interface Factor {
1212
object: 'authentication_factor';

src/multi-factor-auth/multi-factor-auth.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,26 @@ describe('MFA', () => {
520520
issuer: 'WorkOS',
521521
user: 'some_user',
522522
},
523+
userId: 'user_01H5JQDV7R7ATEYZDEG0W5PRYS',
524+
},
525+
{
526+
object: 'authentication_factor',
527+
id: 'auth_factor_5678',
528+
createdAt: '2022-03-15T20:39:19.892Z',
529+
updatedAt: '2022-03-15T20:39:19.892Z',
530+
type: 'sms',
531+
sms: {
532+
phoneNumber: '+15555555555',
533+
},
534+
userId: 'user_01H5JQDV7R7ATEYZDEG0W5PRYS',
535+
},
536+
{
537+
object: 'authentication_factor',
538+
id: 'auth_factor_9012',
539+
createdAt: '2022-03-15T20:39:19.892Z',
540+
updatedAt: '2022-03-15T20:39:19.892Z',
541+
type: 'generic_otp',
542+
userId: 'user_01H5JQDV7R7ATEYZDEG0W5PRYS',
523543
},
524544
],
525545
listMetadata: {

src/user-management/fixtures/list-factors.json

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,27 @@
1010
"totp": {
1111
"issuer": "WorkOS",
1212
"user": "some_user"
13-
}
13+
},
14+
"user_id": "user_01H5JQDV7R7ATEYZDEG0W5PRYS"
15+
},
16+
{
17+
"object": "authentication_factor",
18+
"id": "auth_factor_5678",
19+
"created_at": "2022-03-15T20:39:19.892Z",
20+
"updated_at": "2022-03-15T20:39:19.892Z",
21+
"type": "sms",
22+
"sms": {
23+
"phone_number": "+15555555555"
24+
},
25+
"user_id": "user_01H5JQDV7R7ATEYZDEG0W5PRYS"
26+
},
27+
{
28+
"object": "authentication_factor",
29+
"id": "auth_factor_9012",
30+
"created_at": "2022-03-15T20:39:19.892Z",
31+
"updated_at": "2022-03-15T20:39:19.892Z",
32+
"type": "generic_otp",
33+
"user_id": "user_01H5JQDV7R7ATEYZDEG0W5PRYS"
1434
}
1535
],
1636
"list_metadata": {

src/user-management/interfaces/authentication-factor.interface.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
1+
import { FactorType } from '../../multi-factor-auth/interfaces/factor.interface';
2+
import {
3+
Sms,
4+
SmsResponse,
5+
} from '../../multi-factor-auth/interfaces/sms.interface';
16
import {
27
Totp,
38
TotpResponse,
49
TotpWithSecrets,
510
TotpWithSecretsResponse,
611
} from '../../multi-factor-auth/interfaces/totp.interface';
712

13+
export type AuthenticationFactorType = FactorType;
14+
815
export interface AuthenticationFactor {
916
object: 'authentication_factor';
1017
id: string;
1118
createdAt: string;
1219
updatedAt: string;
13-
type: 'totp';
14-
totp: Totp;
20+
type: AuthenticationFactorType;
21+
sms?: Sms;
22+
totp?: Totp;
1523
userId: string;
1624
}
1725

@@ -30,8 +38,9 @@ export interface AuthenticationFactorResponse {
3038
id: string;
3139
created_at: string;
3240
updated_at: string;
33-
type: 'totp';
34-
totp: TotpResponse;
41+
type: AuthenticationFactorType;
42+
sms?: SmsResponse;
43+
totp?: TotpResponse;
3544
user_id: string;
3645
}
3746

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { deserializeFactor } from './authentication-factor.serializer';
2+
import { AuthenticationFactorResponse } from '../interfaces/authentication-factor.interface';
3+
import listFactorsFixture from '../fixtures/list-factors.json';
4+
5+
describe('deserializeFactor', () => {
6+
it('deserializes totp, sms, and generic_otp factors', () => {
7+
const factors = listFactorsFixture.data.map((factor) =>
8+
deserializeFactor(factor as AuthenticationFactorResponse),
9+
);
10+
11+
expect(factors).toStrictEqual([
12+
{
13+
object: 'authentication_factor',
14+
id: 'auth_factor_1234',
15+
createdAt: '2022-03-15T20:39:19.892Z',
16+
updatedAt: '2022-03-15T20:39:19.892Z',
17+
type: 'totp',
18+
totp: {
19+
issuer: 'WorkOS',
20+
user: 'some_user',
21+
},
22+
userId: 'user_01H5JQDV7R7ATEYZDEG0W5PRYS',
23+
},
24+
{
25+
object: 'authentication_factor',
26+
id: 'auth_factor_5678',
27+
createdAt: '2022-03-15T20:39:19.892Z',
28+
updatedAt: '2022-03-15T20:39:19.892Z',
29+
type: 'sms',
30+
sms: {
31+
phoneNumber: '+15555555555',
32+
},
33+
userId: 'user_01H5JQDV7R7ATEYZDEG0W5PRYS',
34+
},
35+
{
36+
object: 'authentication_factor',
37+
id: 'auth_factor_9012',
38+
createdAt: '2022-03-15T20:39:19.892Z',
39+
updatedAt: '2022-03-15T20:39:19.892Z',
40+
type: 'generic_otp',
41+
userId: 'user_01H5JQDV7R7ATEYZDEG0W5PRYS',
42+
},
43+
]);
44+
});
45+
});

src/user-management/serializers/authentication-factor.serializer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
AuthenticationFactorWithSecrets,
55
AuthenticationFactorWithSecretsResponse,
66
} from '../interfaces/authentication-factor.interface';
7+
import { deserializeSms } from '../../multi-factor-auth/serializers/sms.serializer';
78
import {
89
deserializeTotp,
910
deserializeTotpWithSecrets,
@@ -17,7 +18,8 @@ export const deserializeFactor = (
1718
createdAt: factor.created_at,
1819
updatedAt: factor.updated_at,
1920
type: factor.type,
20-
totp: deserializeTotp(factor.totp),
21+
...(factor.sms ? { sms: deserializeSms(factor.sms) } : {}),
22+
...(factor.totp ? { totp: deserializeTotp(factor.totp) } : {}),
2123
userId: factor.user_id,
2224
});
2325

0 commit comments

Comments
 (0)