Skip to content

Commit

Permalink
added a CredentialTokens model to store user info in mongodb instead …
Browse files Browse the repository at this point in the history
…of process to fix CAS when operating multiple server instances
  • Loading branch information
AmShaegar13 committed Dec 8, 2017
1 parent b444159 commit d6ee11b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
1 change: 1 addition & 0 deletions packages/rocketchat-cas/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Package.onUse(function(api) {
// Server files
api.add_files('server/cas_rocketchat.js', 'server');
api.add_files('server/cas_server.js', 'server');
api.add_files('server/models/CredentialTokens.js', 'server');

// Client files
api.add_files('client/cas_client.js', 'client');
Expand Down
26 changes: 6 additions & 20 deletions packages/rocketchat-cas/server/cas_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ const fiber = Npm.require('fibers');
const url = Npm.require('url');
const CAS = Npm.require('cas');

const _casCredentialTokens = {};

RoutePolicy.declare('/_cas/', 'network');

const closePopup = function(res) {
Expand Down Expand Up @@ -38,7 +36,7 @@ const casTicket = function(req, token, callback) {
service: `${ appUrl }/_cas/${ token }`
});

cas.validate(ticketId, function(err, status, username, details) {
cas.validate(ticketId, Meteor.bindEnvironment(function(err, status, username, details) {
if (err) {
logger.error(`error when trying to validate: ${ err.message }`);
} else if (status) {
Expand All @@ -49,14 +47,14 @@ const casTicket = function(req, token, callback) {
if (details && details.attributes) {
_.extend(user_info, { attributes: details.attributes });
}
_casCredentialTokens[token] = user_info;
RocketChat.models.CredentialTokens.create(token, user_info);
} else {
logger.error(`Unable to validate ticket: ${ ticketId }`);
}
//logger.debug("Receveied response: " + JSON.stringify(details, null , 4));

callback();
});
}));

return;
};
Expand Down Expand Up @@ -102,19 +100,6 @@ WebApp.connectHandlers.use(function(req, res, next) {
}).run();
});

const _hasCredential = function(credentialToken) {
return _.has(_casCredentialTokens, credentialToken);
};

/*
* Retrieve token and delete it to avoid replaying it.
*/
const _retrieveCredential = function(credentialToken) {
const result = _casCredentialTokens[credentialToken];
delete _casCredentialTokens[credentialToken];
return result;
};

/*
* Register a server-side login handle.
* It is call after Accounts.callLoginMethod() is call from client.
Expand All @@ -126,12 +111,13 @@ Accounts.registerLoginHandler(function(options) {
return undefined;
}

if (!_hasCredential(options.cas.credentialToken)) {
const credentials = RocketChat.models.CredentialTokens.findOneById(options.cas.credentialToken);
if (credentials === undefined) {
throw new Meteor.Error(Accounts.LoginCancelledError.numericError,
'no matching login attempt found');
}

const result = _retrieveCredential(options.cas.credentialToken);
const result = credentials.userInfo;
const syncUserDataFieldMap = RocketChat.settings.get('CAS_Sync_User_Data_FieldMap').trim();
const cas_version = parseFloat(RocketChat.settings.get('CAS_version'));
const sync_enabled = RocketChat.settings.get('CAS_Sync_User_Data_Enabled');
Expand Down
28 changes: 28 additions & 0 deletions packages/rocketchat-cas/server/models/CredentialTokens.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
RocketChat.models.CredentialTokens = new class extends RocketChat.models._Base {
constructor() {
super('credential_tokens');

this.tryEnsureIndex({ 'expireAt': 1 }, { sparse: 1, expireAfterSeconds: 0 });
}

create(_id, userInfo) {
const validForMilliseconds = 60000; // Valid for 60 seconds
const token = {
_id,
userInfo,
expireAt: new Date(Date.now() + validForMilliseconds)
};

this.insert(token);
return token;
}

findOneById(_id) {
const query = {
_id,
expireAt: { $gt: new Date() }
};

return this.findOne(query);
}
};

0 comments on commit d6ee11b

Please sign in to comment.