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

Concept: MFA draft #1271

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 19 additions & 0 deletions integration/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ const api = new ParseServer({
},
});

const mfa = new ParseServer({
databaseURI: 'mongodb://localhost:27017/integration',
appId: 'integration',
masterKey: 'notsosecret',
serverURL: 'http://localhost:1337/mfa',
multiFactorAuth: {
enableMfa: true,
encryptionKey: '89E4AFF1-DFE4-4603-9574-BFA16BB446FD',
},
appName: 'testApp',
verbose: false,
silent: true,
serverStartComplete: () => {
/* Ignore ParseError: schema class name does not revalidate */
},
});

app.use('/mfa', mfa);

app.use('/parse', api);

const TestUtils = require('parse-server').TestUtils;
Expand Down
74 changes: 74 additions & 0 deletions integration/test/ParseUserTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const assert = require('assert');
const clear = require('./clear');
const Parse = require('../../node');
const otplib = require('otplib');

const TestObject = Parse.Object.extend('TestObject');

Expand Down Expand Up @@ -119,6 +120,79 @@ describe('Parse User', () => {
});
});

it("can enable MFA", async () => {
Parse.User.enableUnsafeCurrentUser();
Parse.serverURL = 'http://localhost:1337/mfa'
const user = await Parse.User.signUp("asdf", "zxcv");
const { secret, qrcodeURL } = await user.enableMfa();
expect(qrcodeURL).toBeDefined();
expect(qrcodeURL).toContain('otpauth://totp/testApp');
expect(qrcodeURL).toContain('secret');
expect(qrcodeURL).toContain('asdf');
expect(qrcodeURL).toContain('period');
expect(qrcodeURL).toContain('digits');
expect(qrcodeURL).toContain('algorithm');
expect(secret).toBeDefined();
// show UI: in order to confirm your MFA, you need to add it to a TPA app, and enter the code below to verify.
// Next step is window.open(qrcodeURL) if mobile, or render a QR code pointing to the qrcodeURL.
const token = otplib.authenticator.generate(secret);
// submit the token generated from TPA app
const { recoveryKeys } = await user.verifyMfa(token);
// 'great, MFA is enabled. Don't get locked out, here are your recovery keys. Keep them in a safe place offline.'
expect(recoveryKeys).toBeDefined();
expect(recoveryKeys[0]).toBeDefined();
expect(recoveryKeys[1]).toBeDefined();
expect(recoveryKeys[0].length).toBe(20);
expect(recoveryKeys[1].length).toBe(20);
expect(Parse.User.current().get('mfaEnabled')).toBe(true);
Parse.serverURL = 'http://localhost:1337/parse'
});

it("can login with MFA", async () => {
Parse.User.enableUnsafeCurrentUser();
Parse.serverURL = 'http://localhost:1337/mfa'
const user = await Parse.User.signUp("asdf", "zxcv");
const { secret } = await user.enableMfa();
let token = otplib.authenticator.generate(secret);
await user.verifyMfa(token);
await Parse.User.logOut();
token = null;
const login = async () => {
try {
const current = await Parse.User.logIn("asdf", "zxcv", {token});
if (!token) {
throw 'should not have been able to login without providing a MFA token';
}
expect(current.get('mfaEnabled')).toBe(true);
expect(current.get('username')).toBe('asdf');
} catch (e) {
if (e.code == Parse.Error.MFA_TOKEN_REQUIRED) {
// please enter your TPA code in order to login. Can't access your account? Enter recovery codes.
token = otplib.authenticator.generate(secret);
await login();
return;
}
throw e;
}
}
await login();
Parse.serverURL = 'http://localhost:1337/parse'
});

it("can recover MFA", async () => {
Parse.User.enableUnsafeCurrentUser();
Parse.serverURL = 'http://localhost:1337/mfa'
const user = await Parse.User.signUp("asdf", "zxcv");
const { secret } = await user.enableMfa();
const token = otplib.authenticator.generate(secret);
const {recoveryKeys} = await user.verifyMfa(token);
await Parse.User.logOut();
const current = await Parse.User.logIn("asdf", "zxcv", {recoveryKeys});
expect(current.get('mfaEnabled')).toBe(false);
expect(current.get('username')).toBe('asdf');
Parse.serverURL = 'http://localhost:1337/parse'
});

it('can login users with installationId', async () => {
Parse.User.enableUnsafeCurrentUser();
const currentInstallation = await Parse.CoreManager.getInstallationController().currentInstallationId();
Expand Down
Loading