Skip to content

Commit

Permalink
Add listAuthFactors method (#857)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
jonatascastro12 authored Sep 5, 2023
1 parent 387d60b commit a7e1013
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 20 deletions.
14 changes: 14 additions & 0 deletions src/users/fixtures/factor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"object": "authentication_factor",
"id": "auth_factor_1234",
"created_at": "2022-03-15T20:39:19.892Z",
"updated_at": "2022-03-15T20:39:19.892Z",
"type": "totp",
"totp": {
"issuer": "WorkOS",
"qr_code": "qr-code-test",
"secret": "secret-test",
"uri": "uri-test",
"user": "some_user"
}
}
2 changes: 2 additions & 0 deletions src/users/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ export * from './add-user-to-organization-options.interface';
export * from './authenticate-with-magic-auth-options.interface';
export * from './authenticate-with-password-options.interface';
export * from './authenticate-with-code-options.interface';
export * from './authenticate-with-totp-options.interface';
export * from './authentication-response.interface';
export * from './reset-password-options.interface';
export * from './send-password-reset-options.interface';
export * from './create-user-options.interface';
export * from './delete-user-options.interface';
export * from './enroll-user-in-mfa-factor.interface';
export * from './list-users-options.interface';
export * from './list-auth-factors-options.interface';
export * from './remove-user-from-organization-options.interface';
export * from './send-magic-auth-code-options.interface';
export * from './send-verification-email-options';
Expand Down
3 changes: 3 additions & 0 deletions src/users/interfaces/list-auth-factors-options.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface ListAuthFactorsOptions {
userId: string;
}
2 changes: 2 additions & 0 deletions src/users/serializers/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export * from './authenticate-with-code-options.serializer';
export * from './authenticate-with-magic-auth-options.serializer';
export * from './authenticate-with-password-options.serializer';
export * from './authenticate-with-totp-options.serializer';
export * from './authentication-response.serializer';
export * from './enroll-user-in-mfa-factor-options.serializer';
export * from './reset-password-options.serializer';
export * from './send-password-reset-email.serializer';
export * from './create-user-options.serializer';
Expand Down
28 changes: 14 additions & 14 deletions src/users/users.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import MockAdapter from 'axios-mock-adapter';
import { WorkOS } from '../workos';
import userFixture from './fixtures/user.json';
import listUsersFixture from './fixtures/list-users.json';
import factorFixture from './fixtures/factor.json';

const mock = new MockAdapter(axios);
const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');
Expand Down Expand Up @@ -409,20 +410,7 @@ describe('UserManagement', () => {
describe('enrollUserInMfaFactor', () => {
it('sends an enrollUserInMfaFactor request', async () => {
mock.onPost(`/users/${userId}/auth/factors`).reply(200, {
authentication_factor: {
object: 'authentication_factor',
id: 'auth_factor_1234',
created_at: '2022-03-15T20:39:19.892Z',
updated_at: '2022-03-15T20:39:19.892Z',
type: 'totp',
totp: {
issuer: 'WorkOS',
qr_code: 'qr-code-test',
secret: 'secret-test',
uri: 'uri-test',
user: 'some_user',
},
},
authentication_factor: factorFixture,
authentication_challenge: {
object: 'authentication_challenge',
id: 'auth_challenge_1234',
Expand Down Expand Up @@ -470,6 +458,18 @@ describe('UserManagement', () => {
});
});

describe('listAuthFactors', () => {
it('sends a listAuthFactors request', async () => {
mock.onGet(`/users/${userId}/auth/factors`).reply(200, [factorFixture]);

const resp = await workos.users.listAuthFactors({ userId });

expect(mock.history.get[0].url).toEqual(`/users/${userId}/auth/factors`);

expect(resp[0]).toMatchObject({ id: factorFixture.id });
});
});

describe('deleteUser', () => {
it('sends a deleteUser request', async () => {
mock.onDelete(`/users/${userId}`).reply(200);
Expand Down
19 changes: 13 additions & 6 deletions src/users/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
AuthenticateWithCodeOptions,
AuthenticateWithMagicAuthOptions,
AuthenticateWithPasswordOptions,
AuthenticateWithTotpOptions,
AuthenticationResponse,
AuthenticationResponseResponse,
ResetPasswordOptions,
Expand All @@ -13,6 +14,7 @@ import {
CreateUserOptions,
DeleteUserOptions,
EnrollUserInMfaFactorOptions,
ListAuthFactorsOptions,
ListUsersOptions,
RemoveUserFromOrganizationOptions,
SendMagicAuthCodeOptions,
Expand All @@ -21,6 +23,7 @@ import {
SerializedAuthenticateWithCodeOptions,
SerializedAuthenticateWithMagicAuthOptions,
SerializedAuthenticateWithPasswordOptions,
SerializedAuthenticateWithTotpOptions,
SerializedResetPasswordOptions,
SerializedSendPasswordResetEmailOptions,
SerializedCreateUserOptions,
Expand All @@ -33,17 +36,15 @@ import {
VerifyEmailCodeOptions,
SendPasswordResetEmailResponseResponse,
} from './interfaces';
import {
AuthenticateWithTotpOptions,
SerializedAuthenticateWithTotpOptions,
} from './interfaces/authenticate-with-totp-options.interface';
import {
deserializeAuthenticationResponse,
deserializeSendPasswordResetEmailResponse,
deserializeUser,
serializeAuthenticateWithMagicAuthOptions,
serializeAuthenticateWithPasswordOptions,
serializeAuthenticateWithCodeOptions,
serializeAuthenticateWithTotpOptions,
serializeEnrollUserInMfaFactorOptions,
serializeResetPasswordOptions,
serializeSendPasswordResetEmailOptions,
serializeCreateUserOptions,
Expand All @@ -59,8 +60,6 @@ import {
FactorResponse,
} from '../mfa/interfaces';
import { deserializeChallenge, deserializeFactor } from '../mfa/serializers';
import { serializeAuthenticateWithTotpOptions } from './serializers/authenticate-with-totp-options.serializer';
import { serializeEnrollUserInMfaFactorOptions } from './serializers/enroll-user-in-mfa-factor-options.serializer';

export class Users {
constructor(private readonly workos: WorkOS) {}
Expand Down Expand Up @@ -299,6 +298,14 @@ export class Users {
};
}

async listAuthFactors(payload: ListAuthFactorsOptions): Promise<Factor[]> {
const { data } = await this.workos.get<FactorResponse[]>(
`/users/${payload.userId}/auth/factors`,
);

return data.map(deserializeFactor);
}

async deleteUser(payload: DeleteUserOptions) {
await this.workos.delete(`/users/${payload.userId}`);
}
Expand Down

0 comments on commit a7e1013

Please sign in to comment.