Skip to content

Commit

Permalink
Fix casing of MFA API arguments (#586)
Browse files Browse the repository at this point in the history
  • Loading branch information
robframpton authored Mar 31, 2022
1 parent e135387 commit bbc79f0
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 41 deletions.
6 changes: 3 additions & 3 deletions src/mfa/interfaces/challenge-factor-options.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export type ChallengeFactorOptions =
| {
authentication_factor_id: string;
authenticationFactorId: string;
}
| {
authentication_factor_id: string;
sms_template: string;
authenticationFactorId: string;
smsTemplate: string;
};
2 changes: 1 addition & 1 deletion src/mfa/interfaces/verify-factor-options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type VerifyFactorOptions = {
authentication_challenge_id: string;
authenticationChallengeId: string;
code: string;
};
84 changes: 50 additions & 34 deletions src/mfa/mfa.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,26 @@ describe('MFA', () => {
describe('with no sms template', () => {
it('challenge a factor with no sms template', async () => {
const mock = new MockAdapter(axios);
mock.onPost('/auth/factors/challenge').reply(200, {
object: 'authentication_challenge',
id: 'auth_challenge_1234',
created_at: '2022-03-15T20:39:19.892Z',
updated_at: '2022-03-15T20:39:19.892Z',
expires_at: '2022-03-15T21:39:19.892Z',
code: '12345',
authentication_factor_id: 'auth_factor_1234',
});
mock
.onPost('/auth/factors/challenge', {
authentication_factor_id: 'auth_factor_1234',
})
.reply(200, {
object: 'authentication_challenge',
id: 'auth_challenge_1234',
created_at: '2022-03-15T20:39:19.892Z',
updated_at: '2022-03-15T20:39:19.892Z',
expires_at: '2022-03-15T21:39:19.892Z',
code: '12345',
authentication_factor_id: 'auth_factor_1234',
});

const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU', {
apiHostname: 'api.workos.dev',
});

const challengeResponse = await workos.mfa.challengeFactor({
authentication_factor_id: 'auth_factor_1234',
authenticationFactorId: 'auth_factor_1234',
});

expect(challengeResponse).toMatchInlineSnapshot(`
Expand All @@ -172,25 +177,31 @@ describe('MFA', () => {
`);
});
});
describe('with totp', () => {

describe('with sms template', () => {
it('challenge a factor with sms template', async () => {
const mock = new MockAdapter(axios);
mock.onPost('/auth/factors/challenge').reply(200, {
object: 'authentication_challenge',
id: 'auth_challenge_1234',
created_at: '2022-03-15T20:39:19.892Z',
updated_at: '2022-03-15T20:39:19.892Z',
expires_at: '2022-03-15T21:39:19.892Z',
code: '12345',
authentication_factor_id: 'auth_factor_1234',
});
mock
.onPost('/auth/factors/challenge', {
authentication_factor_id: 'auth_factor_1234',
sms_template: 'This is your code: 12345',
})
.reply(200, {
object: 'authentication_challenge',
id: 'auth_challenge_1234',
created_at: '2022-03-15T20:39:19.892Z',
updated_at: '2022-03-15T20:39:19.892Z',
expires_at: '2022-03-15T21:39:19.892Z',
code: '12345',
authentication_factor_id: 'auth_factor_1234',
});
const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU', {
apiHostname: 'api.workos.dev',
});

const challengeResponse = await workos.mfa.challengeFactor({
authentication_factor_id: 'auth_factor_1234',
sms_template: 'This is your code: 12345',
authenticationFactorId: 'auth_factor_1234',
smsTemplate: 'This is your code: 12345',
});

expect(challengeResponse).toMatchInlineSnapshot(`
Expand All @@ -212,24 +223,29 @@ describe('MFA', () => {
describe('verify with successful response', () => {
it('verifies a successful factor', async () => {
const mock = new MockAdapter(axios);
mock.onPost('/auth/factors/verify').reply(200, {
challenge: {
object: 'authentication_challenge',
id: 'auth_challenge_1234',
created_at: '2022-03-15T20:39:19.892Z',
updated_at: '2022-03-15T20:39:19.892Z',
expires_at: '2022-03-15T21:39:19.892Z',
mock
.onPost('/auth/factors/verify', {
authentication_challenge_id: 'auth_challenge_1234',
code: '12345',
authentication_factor_id: 'auth_factor_1234',
},
valid: true,
});
})
.reply(200, {
challenge: {
object: 'authentication_challenge',
id: 'auth_challenge_1234',
created_at: '2022-03-15T20:39:19.892Z',
updated_at: '2022-03-15T20:39:19.892Z',
expires_at: '2022-03-15T21:39:19.892Z',
code: '12345',
authentication_factor_id: 'auth_factor_1234',
},
valid: true,
});
const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU', {
apiHostname: 'api.workos.dev',
});

const verifyResponse = await workos.mfa.verifyFactor({
authentication_challenge_id: 'auth_challenge_1234',
authenticationChallengeId: 'auth_challenge_1234',
code: '12345',
});
expect(verifyResponse).toMatchInlineSnapshot(`
Expand Down
32 changes: 29 additions & 3 deletions src/mfa/mfa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,43 @@ export class Mfa {
}

async enrollFactor(options: EnrollFactorOptions): Promise<Factor> {
const { data } = await this.workos.post('/auth/factors/enroll', options);
const { data } = await this.workos.post('/auth/factors/enroll', {
type: options.type,
...(() => {
switch (options.type) {
case 'sms':
return {
phone_number: options.phoneNumber,
};
case 'totp':
return {
totp_issuer: options.issuer,
totp_user: options.user,
};
default:
return {};
}
})(),
});

return data;
}

async challengeFactor(options: ChallengeFactorOptions): Promise<Challenge> {
const { data } = await this.workos.post('/auth/factors/challenge', options);
const { data } = await this.workos.post('/auth/factors/challenge', {
authentication_factor_id: options.authenticationFactorId,
sms_template: 'smsTemplate' in options ? options.smsTemplate : undefined,
});

return data;
}

async verifyFactor(options: VerifyFactorOptions): Promise<VerifyResponse> {
const { data } = await this.workos.post('/auth/factors/verify', options);
const { data } = await this.workos.post('/auth/factors/verify', {
authentication_challenge_id: options.authenticationChallengeId,
code: options.code,
});

return data;
}
}

0 comments on commit bbc79f0

Please sign in to comment.