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

Two Factor Authentication #6977

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
79 changes: 79 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"lru-cache": "5.1.1",
"mime": "2.4.6",
"mongodb": "3.6.2",
"otplib": "^12.0.1",
dblythy marked this conversation as resolved.
Show resolved Hide resolved
dblythy marked this conversation as resolved.
Show resolved Hide resolved
"parse": "2.17.0",
"pg-promise": "10.6.2",
"pluralize": "8.0.0",
Expand Down
208 changes: 208 additions & 0 deletions spec/ParseUser.2FA.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
'use strict';

const request = require('../lib/request');
const otplib = require('otplib');

describe('2FA', () => {
function enable2FA(user) {
return request({
method: 'GET',
url: 'http://localhost:8378/1/users/me/enable2FA',
json: true,
headers: {
'X-Parse-Session-Token': user.getSessionToken(),
'X-Parse-Application-Id': Parse.applicationId,
'X-Parse-REST-API-Key': 'rest',
},
});
}

function validate2FA(user, token) {
return request({
method: 'POST',
url: 'http://localhost:8378/1/users/me/verify2FA',
body: {
token,
},
headers: {
'X-Parse-Session-Token': user.getSessionToken(),
'X-Parse-Application-Id': Parse.applicationId,
'X-Parse-REST-API-Key': 'rest',
'Content-Type': 'application/json',
},
});
}

function loginWith2Fa(username, password, token) {
let req = `http://localhost:8378/1/login?username=${username}&password=${password}`;
if (token) {
req += `&token=${token}`;
}
return request({
method: 'POST',
url: req,
headers: {
'X-Parse-Application-Id': Parse.applicationId,
'X-Parse-REST-API-Key': 'rest',
'Content-Type': 'application/json',
},
});
}

it('should enable 2FA tokens', async () => {
await reconfigureServer({
twoFactor: {
enabled: true,
},
appName: 'testApp',
});
const user = await Parse.User.signUp('username', 'password');
const {
data: { secret, qrcodeURL },
} = await enable2FA(user);
expect(qrcodeURL).toBeDefined();
expect(qrcodeURL).toContain('otpauth://totp/testApp');
expect(qrcodeURL).toContain('secret');
expect(qrcodeURL).toContain('username');
expect(qrcodeURL).toContain('period');
expect(qrcodeURL).toContain('digits');
expect(qrcodeURL).toContain('algorithm');
const token = otplib.authenticator.generate(secret);
await validate2FA(user, token);
await Parse.User.logOut();
let verifytoken = '';
const mfaLogin = async () => {
try {
const result = await loginWith2Fa('username', 'password', verifytoken);
if (!verifytoken) {
throw 'Should not have been able to login.';
}
const newUser = result.data;
expect(newUser.objectId).toBe(user.id);
expect(newUser.username).toBe('username');
expect(newUser.createdAt).toBe(user.createdAt.toISOString());
expect(newUser.MFAEnabled).toBe(true);
} catch (err) {
expect(err.text).toMatch('{"code":211,"error":"Please provide your 2FA token."}');
verifytoken = otplib.authenticator.generate(secret);
if (err.text.includes('211')) {
await mfaLogin();
}
}
};
await mfaLogin();
});

it('can reject 2FA', async () => {
await reconfigureServer({
twoFactor: {
enabled: true,
},
});
const user = await Parse.User.signUp('username', 'password');
const {
data: { secret },
} = await enable2FA(user);
const token = otplib.authenticator.generate(secret);
await validate2FA(user, token);
await Parse.User.logOut();
try {
await loginWith2Fa('username', 'password', '123102');
throw 'should not be able to login.';
} catch (e) {
expect(e.text).toBe('{"code":212,"error":"Invalid 2FA token"}');
}
});

it('can encrypt 2FA tokens', async () => {
await reconfigureServer({
twoFactor: {
enabled: true,
encryptionKey: '89E4AFF1-DFE4-4603-9574-BFA16BB446FD',
},
});
const user = await Parse.User.signUp('username', 'password');
const {
data: { secret },
} = await enable2FA(user);
const token = otplib.authenticator.generate(secret);
await validate2FA(user, token);
await Parse.User.logOut();
let verifytoken = '';
const mfaLogin = async () => {
try {
const result = await loginWith2Fa('username', 'password', verifytoken);
if (!verifytoken) {
throw 'Should not have been able to login.';
}
const newUser = result.data;
expect(newUser.objectId).toBe(user.id);
expect(newUser.username).toBe('username');
expect(newUser.createdAt).toBe(user.createdAt.toISOString());
expect(newUser._mfa).toBeUndefined();
} catch (err) {
expect(err.text).toMatch('{"code":211,"error":"Please provide your 2FA token."}');
verifytoken = otplib.authenticator.generate(secret);
if (err.text.includes('211')) {
await mfaLogin();
}
}
};
await mfaLogin();
});
it('cannot set _mfa or mfa', async () => {
await reconfigureServer({
twoFactor: {
enabled: true,
encryptionKey: '89E4AFF1-DFE4-4603-9574-BFA16BB446FD',
},
});
const user = await Parse.User.signUp('username', 'password');
const {
data: { secret },
} = await enable2FA(user);
const token = otplib.authenticator.generate(secret);
await validate2FA(user, token);
user.set('_mfa', 'foo');
user.set('mfa', 'foo');
await user.save(null, { sessionToken: user.getSessionToken() });
await Parse.User.logOut();
let verifytoken = '';
const mfaLogin = async () => {
try {
const result = await loginWith2Fa('username', 'password', verifytoken);
if (!verifytoken) {
throw 'Should not have been able to login.';
}
const newUser = result.data;
expect(newUser.objectId).toBe(user.id);
expect(newUser.username).toBe('username');
expect(newUser.createdAt).toBe(user.createdAt.toISOString());
expect(newUser._mfa).toBeUndefined();
} catch (err) {
expect(err.text).toMatch('{"code":211,"error":"Please provide your 2FA token."}');
verifytoken = otplib.authenticator.generate(secret);
if (err.text.includes('211')) {
await mfaLogin();
}
}
};
await mfaLogin();
});
it('prevent setting on mfw / 2fa tokens', async () => {
const user = await Parse.User.signUp('username', 'password');
user.set('MFAEnabled', true);
user.set('mfa', true);
user.set('_mfa', true);
await user.save(null, { sessionToken: user.getSessionToken() });
await user.fetch({ sessionToken: user.getSessionToken() });
expect(user.get('MFAEnabled')).toBeUndefined();
expect(user.get('mfa')).toBeUndefined();
expect(user.get('_mfa')).toBeUndefined();
await reconfigureServer({
twoFactor: {
enabled: false,
},
});
});
});
3 changes: 3 additions & 0 deletions spec/schemas.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ const userSchema = {
email: { type: 'String' },
emailVerified: { type: 'Boolean' },
authData: { type: 'Object' },
MFAEnabled: { type: 'Boolean' },
dblythy marked this conversation as resolved.
Show resolved Hide resolved
},
classLevelPermissions: defaultClassLevelPermissions,
};
Expand Down Expand Up @@ -1287,6 +1288,7 @@ describe('schemas', () => {
emailVerified: { type: 'Boolean' },
authData: { type: 'Object' },
newField: { type: 'String' },
MFAEnabled: { type: 'Boolean' },
ACL: { type: 'ACL' },
},
classLevelPermissions: {
Expand Down Expand Up @@ -1314,6 +1316,7 @@ describe('schemas', () => {
email: { type: 'String' },
emailVerified: { type: 'Boolean' },
authData: { type: 'Object' },
MFAEnabled: { type: 'Boolean' },
newField: { type: 'String' },
ACL: { type: 'ACL' },
},
Expand Down
Loading