Skip to content

Commit a7e1013

Browse files
Add listAuthFactors method (#857)
## Description ## Documentation Does this require changes to the WorkOS Docs? E.g. the [API Reference](https://workos.com/docs/reference) or code snippets need updates. ``` [ ] Yes ``` If yes, link a related docs PR and add a docs maintainer as a reviewer. Their approval is required.
1 parent 387d60b commit a7e1013

File tree

6 files changed

+48
-20
lines changed

6 files changed

+48
-20
lines changed

src/users/fixtures/factor.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"object": "authentication_factor",
3+
"id": "auth_factor_1234",
4+
"created_at": "2022-03-15T20:39:19.892Z",
5+
"updated_at": "2022-03-15T20:39:19.892Z",
6+
"type": "totp",
7+
"totp": {
8+
"issuer": "WorkOS",
9+
"qr_code": "qr-code-test",
10+
"secret": "secret-test",
11+
"uri": "uri-test",
12+
"user": "some_user"
13+
}
14+
}

src/users/interfaces/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ export * from './add-user-to-organization-options.interface';
22
export * from './authenticate-with-magic-auth-options.interface';
33
export * from './authenticate-with-password-options.interface';
44
export * from './authenticate-with-code-options.interface';
5+
export * from './authenticate-with-totp-options.interface';
56
export * from './authentication-response.interface';
67
export * from './reset-password-options.interface';
78
export * from './send-password-reset-options.interface';
89
export * from './create-user-options.interface';
910
export * from './delete-user-options.interface';
1011
export * from './enroll-user-in-mfa-factor.interface';
1112
export * from './list-users-options.interface';
13+
export * from './list-auth-factors-options.interface';
1214
export * from './remove-user-from-organization-options.interface';
1315
export * from './send-magic-auth-code-options.interface';
1416
export * from './send-verification-email-options';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface ListAuthFactorsOptions {
2+
userId: string;
3+
}

src/users/serializers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
export * from './authenticate-with-code-options.serializer';
22
export * from './authenticate-with-magic-auth-options.serializer';
33
export * from './authenticate-with-password-options.serializer';
4+
export * from './authenticate-with-totp-options.serializer';
45
export * from './authentication-response.serializer';
6+
export * from './enroll-user-in-mfa-factor-options.serializer';
57
export * from './reset-password-options.serializer';
68
export * from './send-password-reset-email.serializer';
79
export * from './create-user-options.serializer';

src/users/users.spec.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import MockAdapter from 'axios-mock-adapter';
33
import { WorkOS } from '../workos';
44
import userFixture from './fixtures/user.json';
55
import listUsersFixture from './fixtures/list-users.json';
6+
import factorFixture from './fixtures/factor.json';
67

78
const mock = new MockAdapter(axios);
89
const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');
@@ -409,20 +410,7 @@ describe('UserManagement', () => {
409410
describe('enrollUserInMfaFactor', () => {
410411
it('sends an enrollUserInMfaFactor request', async () => {
411412
mock.onPost(`/users/${userId}/auth/factors`).reply(200, {
412-
authentication_factor: {
413-
object: 'authentication_factor',
414-
id: 'auth_factor_1234',
415-
created_at: '2022-03-15T20:39:19.892Z',
416-
updated_at: '2022-03-15T20:39:19.892Z',
417-
type: 'totp',
418-
totp: {
419-
issuer: 'WorkOS',
420-
qr_code: 'qr-code-test',
421-
secret: 'secret-test',
422-
uri: 'uri-test',
423-
user: 'some_user',
424-
},
425-
},
413+
authentication_factor: factorFixture,
426414
authentication_challenge: {
427415
object: 'authentication_challenge',
428416
id: 'auth_challenge_1234',
@@ -470,6 +458,18 @@ describe('UserManagement', () => {
470458
});
471459
});
472460

461+
describe('listAuthFactors', () => {
462+
it('sends a listAuthFactors request', async () => {
463+
mock.onGet(`/users/${userId}/auth/factors`).reply(200, [factorFixture]);
464+
465+
const resp = await workos.users.listAuthFactors({ userId });
466+
467+
expect(mock.history.get[0].url).toEqual(`/users/${userId}/auth/factors`);
468+
469+
expect(resp[0]).toMatchObject({ id: factorFixture.id });
470+
});
471+
});
472+
473473
describe('deleteUser', () => {
474474
it('sends a deleteUser request', async () => {
475475
mock.onDelete(`/users/${userId}`).reply(200);

src/users/users.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
AuthenticateWithCodeOptions,
66
AuthenticateWithMagicAuthOptions,
77
AuthenticateWithPasswordOptions,
8+
AuthenticateWithTotpOptions,
89
AuthenticationResponse,
910
AuthenticationResponseResponse,
1011
ResetPasswordOptions,
@@ -13,6 +14,7 @@ import {
1314
CreateUserOptions,
1415
DeleteUserOptions,
1516
EnrollUserInMfaFactorOptions,
17+
ListAuthFactorsOptions,
1618
ListUsersOptions,
1719
RemoveUserFromOrganizationOptions,
1820
SendMagicAuthCodeOptions,
@@ -21,6 +23,7 @@ import {
2123
SerializedAuthenticateWithCodeOptions,
2224
SerializedAuthenticateWithMagicAuthOptions,
2325
SerializedAuthenticateWithPasswordOptions,
26+
SerializedAuthenticateWithTotpOptions,
2427
SerializedResetPasswordOptions,
2528
SerializedSendPasswordResetEmailOptions,
2629
SerializedCreateUserOptions,
@@ -33,17 +36,15 @@ import {
3336
VerifyEmailCodeOptions,
3437
SendPasswordResetEmailResponseResponse,
3538
} from './interfaces';
36-
import {
37-
AuthenticateWithTotpOptions,
38-
SerializedAuthenticateWithTotpOptions,
39-
} from './interfaces/authenticate-with-totp-options.interface';
4039
import {
4140
deserializeAuthenticationResponse,
4241
deserializeSendPasswordResetEmailResponse,
4342
deserializeUser,
4443
serializeAuthenticateWithMagicAuthOptions,
4544
serializeAuthenticateWithPasswordOptions,
4645
serializeAuthenticateWithCodeOptions,
46+
serializeAuthenticateWithTotpOptions,
47+
serializeEnrollUserInMfaFactorOptions,
4748
serializeResetPasswordOptions,
4849
serializeSendPasswordResetEmailOptions,
4950
serializeCreateUserOptions,
@@ -59,8 +60,6 @@ import {
5960
FactorResponse,
6061
} from '../mfa/interfaces';
6162
import { deserializeChallenge, deserializeFactor } from '../mfa/serializers';
62-
import { serializeAuthenticateWithTotpOptions } from './serializers/authenticate-with-totp-options.serializer';
63-
import { serializeEnrollUserInMfaFactorOptions } from './serializers/enroll-user-in-mfa-factor-options.serializer';
6463

6564
export class Users {
6665
constructor(private readonly workos: WorkOS) {}
@@ -299,6 +298,14 @@ export class Users {
299298
};
300299
}
301300

301+
async listAuthFactors(payload: ListAuthFactorsOptions): Promise<Factor[]> {
302+
const { data } = await this.workos.get<FactorResponse[]>(
303+
`/users/${payload.userId}/auth/factors`,
304+
);
305+
306+
return data.map(deserializeFactor);
307+
}
308+
302309
async deleteUser(payload: DeleteUserOptions) {
303310
await this.workos.delete(`/users/${payload.userId}`);
304311
}

0 commit comments

Comments
 (0)