diff --git a/src/multi-factor-auth/fixtures/list-factors.json b/src/multi-factor-auth/fixtures/list-factors.json index 288833151..1135a300e 100644 --- a/src/multi-factor-auth/fixtures/list-factors.json +++ b/src/multi-factor-auth/fixtures/list-factors.json @@ -10,7 +10,27 @@ "totp": { "issuer": "WorkOS", "user": "some_user" - } + }, + "user_id": "user_01H5JQDV7R7ATEYZDEG0W5PRYS" + }, + { + "object": "authentication_factor", + "id": "auth_factor_5678", + "created_at": "2022-03-15T20:39:19.892Z", + "updated_at": "2022-03-15T20:39:19.892Z", + "type": "sms", + "sms": { + "phone_number": "+15555555555" + }, + "user_id": "user_01H5JQDV7R7ATEYZDEG0W5PRYS" + }, + { + "object": "authentication_factor", + "id": "auth_factor_9012", + "created_at": "2022-03-15T20:39:19.892Z", + "updated_at": "2022-03-15T20:39:19.892Z", + "type": "generic_otp", + "user_id": "user_01H5JQDV7R7ATEYZDEG0W5PRYS" } ], "list_metadata": { diff --git a/src/multi-factor-auth/interfaces/factor.interface.ts b/src/multi-factor-auth/interfaces/factor.interface.ts index 98b31aa95..9d618fc84 100644 --- a/src/multi-factor-auth/interfaces/factor.interface.ts +++ b/src/multi-factor-auth/interfaces/factor.interface.ts @@ -6,7 +6,7 @@ import { TotpWithSecretsResponse, } from './totp.interface'; -type FactorType = 'sms' | 'totp' | 'generic_otp'; +export type FactorType = 'sms' | 'totp' | 'generic_otp'; export interface Factor { object: 'authentication_factor'; diff --git a/src/multi-factor-auth/multi-factor-auth.spec.ts b/src/multi-factor-auth/multi-factor-auth.spec.ts index 2f04c5ade..f2676d5f7 100644 --- a/src/multi-factor-auth/multi-factor-auth.spec.ts +++ b/src/multi-factor-auth/multi-factor-auth.spec.ts @@ -520,6 +520,26 @@ describe('MFA', () => { issuer: 'WorkOS', user: 'some_user', }, + userId: 'user_01H5JQDV7R7ATEYZDEG0W5PRYS', + }, + { + object: 'authentication_factor', + id: 'auth_factor_5678', + createdAt: '2022-03-15T20:39:19.892Z', + updatedAt: '2022-03-15T20:39:19.892Z', + type: 'sms', + sms: { + phoneNumber: '+15555555555', + }, + userId: 'user_01H5JQDV7R7ATEYZDEG0W5PRYS', + }, + { + object: 'authentication_factor', + id: 'auth_factor_9012', + createdAt: '2022-03-15T20:39:19.892Z', + updatedAt: '2022-03-15T20:39:19.892Z', + type: 'generic_otp', + userId: 'user_01H5JQDV7R7ATEYZDEG0W5PRYS', }, ], listMetadata: { diff --git a/src/user-management/fixtures/list-factors.json b/src/user-management/fixtures/list-factors.json index 288833151..1135a300e 100644 --- a/src/user-management/fixtures/list-factors.json +++ b/src/user-management/fixtures/list-factors.json @@ -10,7 +10,27 @@ "totp": { "issuer": "WorkOS", "user": "some_user" - } + }, + "user_id": "user_01H5JQDV7R7ATEYZDEG0W5PRYS" + }, + { + "object": "authentication_factor", + "id": "auth_factor_5678", + "created_at": "2022-03-15T20:39:19.892Z", + "updated_at": "2022-03-15T20:39:19.892Z", + "type": "sms", + "sms": { + "phone_number": "+15555555555" + }, + "user_id": "user_01H5JQDV7R7ATEYZDEG0W5PRYS" + }, + { + "object": "authentication_factor", + "id": "auth_factor_9012", + "created_at": "2022-03-15T20:39:19.892Z", + "updated_at": "2022-03-15T20:39:19.892Z", + "type": "generic_otp", + "user_id": "user_01H5JQDV7R7ATEYZDEG0W5PRYS" } ], "list_metadata": { diff --git a/src/user-management/interfaces/authentication-factor.interface.ts b/src/user-management/interfaces/authentication-factor.interface.ts index fb1732e04..fa47744a6 100644 --- a/src/user-management/interfaces/authentication-factor.interface.ts +++ b/src/user-management/interfaces/authentication-factor.interface.ts @@ -1,3 +1,8 @@ +import { FactorType } from '../../multi-factor-auth/interfaces/factor.interface'; +import { + Sms, + SmsResponse, +} from '../../multi-factor-auth/interfaces/sms.interface'; import { Totp, TotpResponse, @@ -5,13 +10,16 @@ import { TotpWithSecretsResponse, } from '../../multi-factor-auth/interfaces/totp.interface'; +export type AuthenticationFactorType = FactorType; + export interface AuthenticationFactor { object: 'authentication_factor'; id: string; createdAt: string; updatedAt: string; - type: 'totp'; - totp: Totp; + type: AuthenticationFactorType; + sms?: Sms; + totp?: Totp; userId: string; } @@ -30,8 +38,9 @@ export interface AuthenticationFactorResponse { id: string; created_at: string; updated_at: string; - type: 'totp'; - totp: TotpResponse; + type: AuthenticationFactorType; + sms?: SmsResponse; + totp?: TotpResponse; user_id: string; } diff --git a/src/user-management/serializers/authentication-factor.serializer.spec.ts b/src/user-management/serializers/authentication-factor.serializer.spec.ts new file mode 100644 index 000000000..0b1a56afc --- /dev/null +++ b/src/user-management/serializers/authentication-factor.serializer.spec.ts @@ -0,0 +1,45 @@ +import { deserializeFactor } from './authentication-factor.serializer'; +import { AuthenticationFactorResponse } from '../interfaces/authentication-factor.interface'; +import listFactorsFixture from '../fixtures/list-factors.json'; + +describe('deserializeFactor', () => { + it('deserializes totp, sms, and generic_otp factors', () => { + const factors = listFactorsFixture.data.map((factor) => + deserializeFactor(factor as AuthenticationFactorResponse), + ); + + expect(factors).toEqual([ + { + object: 'authentication_factor', + id: 'auth_factor_1234', + createdAt: '2022-03-15T20:39:19.892Z', + updatedAt: '2022-03-15T20:39:19.892Z', + type: 'totp', + totp: { + issuer: 'WorkOS', + user: 'some_user', + }, + userId: 'user_01H5JQDV7R7ATEYZDEG0W5PRYS', + }, + { + object: 'authentication_factor', + id: 'auth_factor_5678', + createdAt: '2022-03-15T20:39:19.892Z', + updatedAt: '2022-03-15T20:39:19.892Z', + type: 'sms', + sms: { + phoneNumber: '+15555555555', + }, + userId: 'user_01H5JQDV7R7ATEYZDEG0W5PRYS', + }, + { + object: 'authentication_factor', + id: 'auth_factor_9012', + createdAt: '2022-03-15T20:39:19.892Z', + updatedAt: '2022-03-15T20:39:19.892Z', + type: 'generic_otp', + userId: 'user_01H5JQDV7R7ATEYZDEG0W5PRYS', + }, + ]); + }); +}); diff --git a/src/user-management/serializers/authentication-factor.serializer.ts b/src/user-management/serializers/authentication-factor.serializer.ts index 9c93d635c..ce85ba3c0 100644 --- a/src/user-management/serializers/authentication-factor.serializer.ts +++ b/src/user-management/serializers/authentication-factor.serializer.ts @@ -4,6 +4,7 @@ import { AuthenticationFactorWithSecrets, AuthenticationFactorWithSecretsResponse, } from '../interfaces/authentication-factor.interface'; +import { deserializeSms } from '../../multi-factor-auth/serializers/sms.serializer'; import { deserializeTotp, deserializeTotpWithSecrets, @@ -17,7 +18,8 @@ export const deserializeFactor = ( createdAt: factor.created_at, updatedAt: factor.updated_at, type: factor.type, - totp: deserializeTotp(factor.totp), + ...(factor.sms ? { sms: deserializeSms(factor.sms) } : {}), + ...(factor.totp ? { totp: deserializeTotp(factor.totp) } : {}), userId: factor.user_id, });