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

Remove dependency of RC namespace in rc-2fa #13235

Merged
merged 36 commits into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
6f749bc
Move integrations models to rc-models
MarcosSpessatto Jan 21, 2019
5dec8ce
Move composeMessage function to rc-utils
MarcosSpessatto Jan 21, 2019
583cb87
Move PushNotifications class to push-notifications package
MarcosSpessatto Jan 21, 2019
039e7cd
Import variables to remove dependency of RC namespace
MarcosSpessatto Jan 21, 2019
d49540b
Import variables to remove RC namespace dependency inside rc-lib/serv…
MarcosSpessatto Jan 22, 2019
4cede2e
Import variables to remove RC namespace inside rc-lib/server/methods
MarcosSpessatto Jan 22, 2019
5ecc7b8
Import variables to remove dependency with RC namespace
MarcosSpessatto Jan 22, 2019
f3280a6
Remove duplicated server settings located in rc-lib
MarcosSpessatto Jan 22, 2019
f526680
Merge remote-tracking branch 'origin/depackaging' into globals/rc-lib…
rodrigok Jan 22, 2019
8d4c70f
Merge branch 'globals/rc-lib-wave-5' into globals/rc-lib-wave-6
MarcosSpessatto Jan 22, 2019
fb7666d
Merge branch 'globals/rc-lib-wave-6' into globals/lib-settings
MarcosSpessatto Jan 22, 2019
f457877
Export function and lib function of rc-lib but keep it inside RC name…
MarcosSpessatto Jan 22, 2019
3b6ffa6
Import variables from functions and lib to remove RC namespace depend…
MarcosSpessatto Jan 22, 2019
27043cb
Merge remote-tracking branch 'origin/depackaging' into globals/lib-fu…
rodrigok Jan 22, 2019
6ab7f28
Remove dependency of RC namespace in rc-sandstorm
MarcosSpessatto Jan 22, 2019
f00a322
Merge branch 'globals/lib-functions' into globals/lib-functions-import
MarcosSpessatto Jan 22, 2019
6043a3d
Merge branch 'globals/lib-functions-import' into globals/lib-x-sandstorm
MarcosSpessatto Jan 22, 2019
568ed1f
Remove dependency of RC namespace in chatpal-search
MarcosSpessatto Jan 23, 2019
36c17f2
Merge remote-tracking branch 'origin/depackaging' into globals/lib-fu…
rodrigok Jan 23, 2019
08d8bd1
Merge remote-tracking branch 'origin/globals/lib-functions-import' in…
rodrigok Jan 23, 2019
06a780f
Merge remote-tracking branch 'origin/depackaging' into globals/lib-x-…
rodrigok Jan 23, 2019
5d60447
Move CreadentialTokens model to rc-models
MarcosSpessatto Jan 23, 2019
dd9303f
Move getUsernameSuggestion function and method from server/ to rc-lib
MarcosSpessatto Jan 23, 2019
842a519
Remove dependency of RC namespace in meteor saml
MarcosSpessatto Jan 23, 2019
76b17c8
Merge branch 'globals/lib-x-sandstorm' into globals/lib-x-chatpal-search
rodrigok Jan 23, 2019
9645e02
Merge remote-tracking branch 'origin/depackaging' into globals/lib-x-…
rodrigok Jan 23, 2019
ba81c02
Remove dependency of RC namespace in rc-theme
MarcosSpessatto Jan 23, 2019
c32491e
Merge branch 'globals/lib-x-chatpal-search' into globals/rc-x-saml
rodrigok Jan 23, 2019
df3c3f8
Merge remote-tracking branch 'origin/depackaging' into globals/rc-x-saml
rodrigok Jan 23, 2019
1290be4
Move 2FA Users model to rc-models
MarcosSpessatto Jan 23, 2019
25b121e
Remove dependency of RC namespace in rc-2fa
MarcosSpessatto Jan 23, 2019
9a4ee66
Merge branch 'globals/rc-x-saml' into globals/rc-x-theme
MarcosSpessatto Jan 23, 2019
41de984
Merge branch 'globals/rc-x-theme' into globals/rc-x-2fa
MarcosSpessatto Jan 23, 2019
025eb99
Merge remote-tracking branch 'origin/depackaging' into globals/rc-x-t…
rodrigok Jan 23, 2019
f052849
Merge branch 'globals/rc-x-theme' into globals/rc-x-2fa
rodrigok Jan 23, 2019
d8bcac7
Merge remote-tracking branch 'origin/depackaging' into globals/rc-x-2fa
rodrigok Jan 23, 2019
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
2 changes: 1 addition & 1 deletion packages/rocketchat-2fa/client/TOTPPassword.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
import { modal } from 'meteor/rocketchat:ui';
import { modal } from 'meteor/rocketchat:ui-utils';
import { t } from 'meteor/rocketchat:utils';
import toastr from 'toastr';

Expand Down
6 changes: 3 additions & 3 deletions packages/rocketchat-2fa/client/accountSecurity.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Meteor } from 'meteor/meteor';
import { ReactiveVar } from 'meteor/reactive-var';
import { Template } from 'meteor/templating';
import { modal } from 'meteor/rocketchat:ui';
import { RocketChat } from 'meteor/rocketchat:lib';
import { modal } from 'meteor/rocketchat:ui-utils';
import { settings } from 'meteor/rocketchat:settings';
import { t } from 'meteor/rocketchat:utils';
import toastr from 'toastr';
import qrcode from 'yaqrcode';
Expand All @@ -27,7 +27,7 @@ Template.accountSecurity.helpers({
return Template.instance().state.get() === 'registering';
},
isAllowed() {
return RocketChat.settings.get('Accounts_TwoFactorAuthentication_Enabled');
return settings.get('Accounts_TwoFactorAuthentication_Enabled');
},
codesRemaining() {
if (Template.instance().codesRemaining.get()) {
Expand Down
6 changes: 4 additions & 2 deletions packages/rocketchat-2fa/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ Package.onUse(function(api) {
'accounts-base',
'ecmascript',
'templating',
'rocketchat:lib',
'rocketchat:settings',
'sha',
'random',
'rocketchat:ui',
'rocketchat:ui-utils',
'rocketchat:utils',
'rocketchat:models',
'rocketchat:callbacks',
]);

api.mainModule('client/index.js', 'client');
Expand Down
2 changes: 0 additions & 2 deletions packages/rocketchat-2fa/server/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import './startup/settings';
import './lib/totp';
import './models/users';
import './methods/checkCodesRemaining';
import './methods/disable';
import './methods/enable';
Expand Down
9 changes: 5 additions & 4 deletions packages/rocketchat-2fa/server/lib/totp.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { SHA256 } from 'meteor/sha';
import { Random } from 'meteor/random';
import { RocketChat } from 'meteor/rocketchat:lib';
import { Users } from 'meteor/rocketchat:models';
import { settings } from 'meteor/rocketchat:settings';
import speakeasy from 'speakeasy';

RocketChat.TOTP = {
export const TOTP = {
generateSecret() {
return speakeasy.generateSecret();
},
Expand All @@ -25,14 +26,14 @@ RocketChat.TOTP = {
backupTokens.splice(usedCode, 1);

// mark the code as used (remove it from the list)
RocketChat.models.Users.update2FABackupCodesByUserId(userId, backupTokens);
Users.update2FABackupCodesByUserId(userId, backupTokens);
return true;
}

return false;
}

const maxDelta = RocketChat.settings.get('Accounts_TwoFactorAuthentication_MaxDelta');
const maxDelta = settings.get('Accounts_TwoFactorAuthentication_MaxDelta');
if (maxDelta) {
const verifiedDelta = speakeasy.totp.verifyDelta({
secret,
Expand Down
10 changes: 6 additions & 4 deletions packages/rocketchat-2fa/server/loginHandler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
import { RocketChat } from 'meteor/rocketchat:lib';
import { settings } from 'meteor/rocketchat:settings';
import { callbacks } from 'meteor/rocketchat:callbacks';
import { TOTP } from './lib/totp';

Accounts.registerLoginHandler('totp', function(options) {
if (!options.totp || !options.totp.code) {
Expand All @@ -10,8 +12,8 @@ Accounts.registerLoginHandler('totp', function(options) {
return Accounts._runLoginHandlers(this, options.totp.login);
});

RocketChat.callbacks.add('onValidateLogin', (login) => {
if (!RocketChat.settings.get('Accounts_TwoFactorAuthentication_Enabled')) {
callbacks.add('onValidateLogin', (login) => {
if (!settings.get('Accounts_TwoFactorAuthentication_Enabled')) {
return;
}

Expand All @@ -22,7 +24,7 @@ RocketChat.callbacks.add('onValidateLogin', (login) => {
throw new Meteor.Error('totp-required', 'TOTP Required');
}

const verified = RocketChat.TOTP.verify({
const verified = TOTP.verify({
secret: login.user.services.totp.secret,
token: totp.code,
userId: login.user._id,
Expand Down
7 changes: 4 additions & 3 deletions packages/rocketchat-2fa/server/methods/disable.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Meteor } from 'meteor/meteor';
import { RocketChat } from 'meteor/rocketchat:lib';
import { Users } from 'meteor/rocketchat:models';
import { TOTP } from '../lib/totp';

Meteor.methods({
'2fa:disable'(code) {
Expand All @@ -9,7 +10,7 @@ Meteor.methods({

const user = Meteor.user();

const verified = RocketChat.TOTP.verify({
const verified = TOTP.verify({
secret: user.services.totp.secret,
token: code,
userId: Meteor.userId(),
Expand All @@ -20,6 +21,6 @@ Meteor.methods({
return false;
}

return RocketChat.models.Users.disable2FAByUserId(Meteor.userId());
return Users.disable2FAByUserId(Meteor.userId());
},
});
9 changes: 5 additions & 4 deletions packages/rocketchat-2fa/server/methods/enable.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Meteor } from 'meteor/meteor';
import { RocketChat } from 'meteor/rocketchat:lib';
import { Users } from 'meteor/rocketchat:models';
import { TOTP } from '../lib/totp';

Meteor.methods({
'2fa:enable'() {
Expand All @@ -9,13 +10,13 @@ Meteor.methods({

const user = Meteor.user();

const secret = RocketChat.TOTP.generateSecret();
const secret = TOTP.generateSecret();

RocketChat.models.Users.disable2FAAndSetTempSecretByUserId(Meteor.userId(), secret.base32);
Users.disable2FAAndSetTempSecretByUserId(Meteor.userId(), secret.base32);

return {
secret: secret.base32,
url: RocketChat.TOTP.generateOtpauthURL(secret, user.username),
url: TOTP.generateOtpauthURL(secret, user.username),
};
},
});
9 changes: 5 additions & 4 deletions packages/rocketchat-2fa/server/methods/regenerateCodes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Meteor } from 'meteor/meteor';
import { RocketChat } from 'meteor/rocketchat:lib';
import { Users } from 'meteor/rocketchat:models';
import { TOTP } from '../lib/totp';

Meteor.methods({
'2fa:regenerateCodes'(userToken) {
Expand All @@ -13,17 +14,17 @@ Meteor.methods({
throw new Meteor.Error('invalid-totp');
}

const verified = RocketChat.TOTP.verify({
const verified = TOTP.verify({
secret: user.services.totp.secret,
token: userToken,
userId: Meteor.userId(),
backupTokens: user.services.totp.hashedBackup,
});

if (verified) {
const { codes, hashedCodes } = RocketChat.TOTP.generateCodes();
const { codes, hashedCodes } = TOTP.generateCodes();

RocketChat.models.Users.update2FABackupCodesByUserId(Meteor.userId(), hashedCodes);
Users.update2FABackupCodesByUserId(Meteor.userId(), hashedCodes);
return { codes };
}
},
Expand Down
9 changes: 5 additions & 4 deletions packages/rocketchat-2fa/server/methods/validateTempToken.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Meteor } from 'meteor/meteor';
import { RocketChat } from 'meteor/rocketchat:lib';
import { Users } from 'meteor/rocketchat:models';
import { TOTP } from '../lib/totp';

Meteor.methods({
'2fa:validateTempToken'(userToken) {
Expand All @@ -13,15 +14,15 @@ Meteor.methods({
throw new Meteor.Error('invalid-totp');
}

const verified = RocketChat.TOTP.verify({
const verified = TOTP.verify({
secret: user.services.totp.tempSecret,
token: userToken,
});

if (verified) {
const { codes, hashedCodes } = RocketChat.TOTP.generateCodes();
const { codes, hashedCodes } = TOTP.generateCodes();

RocketChat.models.Users.enable2FAAndSetSecretAndCodesByUserId(Meteor.userId(), user.services.totp.tempSecret, hashedCodes);
Users.enable2FAAndSetSecretAndCodesByUserId(Meteor.userId(), user.services.totp.tempSecret, hashedCodes);
return { codes };
}
},
Expand Down
51 changes: 0 additions & 51 deletions packages/rocketchat-2fa/server/models/users.js

This file was deleted.

4 changes: 2 additions & 2 deletions packages/rocketchat-2fa/server/startup/settings.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RocketChat } from 'meteor/rocketchat:lib';
import { settings } from 'meteor/rocketchat:settings';

RocketChat.settings.addGroup('Accounts', function() {
settings.addGroup('Accounts', function() {
this.section('Two Factor Authentication', function() {
this.add('Accounts_TwoFactorAuthentication_Enabled', true, {
type: 'boolean',
Expand Down
50 changes: 50 additions & 0 deletions packages/rocketchat-models/server/models/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,56 @@ export class Users extends Base {
};
}

disable2FAAndSetTempSecretByUserId(userId, tempToken) {
return this.update({
_id: userId,
}, {
$set: {
'services.totp': {
enabled: false,
tempSecret: tempToken,
},
},
});
}

enable2FAAndSetSecretAndCodesByUserId(userId, secret, backupCodes) {
return this.update({
_id: userId,
}, {
$set: {
'services.totp.enabled': true,
'services.totp.secret': secret,
'services.totp.hashedBackup': backupCodes,
},
$unset: {
'services.totp.tempSecret': 1,
},
});
}

disable2FAByUserId(userId) {
return this.update({
_id: userId,
}, {
$set: {
'services.totp': {
enabled: false,
},
},
});
}

update2FABackupCodesByUserId(userId, backupCodes) {
return this.update({
_id: userId,
}, {
$set: {
'services.totp.hashedBackup': backupCodes,
},
});
}

findByIdsWithPublicE2EKey(ids, options) {
const query = {
_id: {
Expand Down