Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Make sure mfa secret and recovery codes are not returned on login #7936

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/cli/src/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ export class UserService {
user: User,
options?: { withInviteUrl?: boolean; posthog?: PostHogClient; withScopes?: boolean },
) {
const { password, updatedAt, apiKey, authIdentities, ...rest } = user;
const { password, updatedAt, apiKey, authIdentities, mfaRecoveryCodes, mfaSecret, ...rest } =
user;

const ldapIdentity = authIdentities?.find((i) => i.providerType === 'ldap');

Expand Down
53 changes: 53 additions & 0 deletions packages/cli/test/integration/auth.api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as utils from './shared/utils/';
import { getGlobalMemberRole, getGlobalOwnerRole } from './shared/db/roles';
import { createUser, createUserShell } from './shared/db/users';
import { UserRepository } from '@db/repositories/user.repository';
import { MfaService } from '@/Mfa/mfa.service';

let globalOwnerRole: Role;
let globalMemberRole: Role;
Expand All @@ -22,9 +23,12 @@ const ownerPassword = randomValidPassword();
const testServer = utils.setupTestServer({ endpointGroups: ['auth'] });
const license = testServer.license;

let mfaService: MfaService;

beforeAll(async () => {
globalOwnerRole = await getGlobalOwnerRole();
globalMemberRole = await getGlobalMemberRole();
mfaService = Container.get(MfaService);
});

beforeEach(async () => {
Expand Down Expand Up @@ -59,6 +63,8 @@ describe('POST /login', () => {
globalRole,
apiKey,
globalScopes,
mfaSecret,
mfaRecoveryCodes,
} = response.body.data;

expect(validator.isUUID(id)).toBe(true);
Expand All @@ -73,6 +79,53 @@ describe('POST /login', () => {
expect(globalRole.scope).toBe('global');
expect(apiKey).toBeUndefined();
expect(globalScopes).toBeDefined();
expect(mfaRecoveryCodes).toBeUndefined();
expect(mfaSecret).toBeUndefined();

const authToken = utils.getAuthToken(response);
expect(authToken).toBeDefined();
});

test('should log user with MFA enabled', async () => {
const secret = 'test';
const recoveryCodes = ['1'];
await mfaService.saveSecretAndRecoveryCodes(owner.id, secret, recoveryCodes);
await mfaService.enableMfa(owner.id);

const response = await testServer.authlessAgent.post('/login').send({
email: owner.email,
password: ownerPassword,
mfaToken: mfaService.totp.generateTOTP(secret),
});

expect(response.statusCode).toBe(200);

const {
id,
email,
firstName,
lastName,
password,
personalizationAnswers,
globalRole,
apiKey,
mfaRecoveryCodes,
mfaSecret,
} = response.body.data;

expect(validator.isUUID(id)).toBe(true);
expect(email).toBe(owner.email);
expect(firstName).toBe(owner.firstName);
expect(lastName).toBe(owner.lastName);
expect(password).toBeUndefined();
expect(personalizationAnswers).toBeNull();
expect(password).toBeUndefined();
expect(globalRole).toBeDefined();
expect(globalRole.name).toBe('owner');
expect(globalRole.scope).toBe('global');
expect(apiKey).toBeUndefined();
expect(mfaRecoveryCodes).toBeUndefined();
expect(mfaSecret).toBeUndefined();

const authToken = utils.getAuthToken(response);
expect(authToken).toBeDefined();
Expand Down
Loading