Skip to content

Commit

Permalink
Merge pull request #150 from forio/epicenter-1590
Browse files Browse the repository at this point in the history
Epicenter 1590
  • Loading branch information
mmrj committed Feb 27, 2016
2 parents 1a89a26 + 921c0bf commit 5eb8507
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 57 deletions.
62 changes: 35 additions & 27 deletions dist/epicenter.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/epicenter.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/epicenter.min.js.map

Large diffs are not rendered by default.

54 changes: 31 additions & 23 deletions src/managers/auth-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ var defaults = {

var EPI_COOKIE_KEY = keyNames.EPI_COOKIE_KEY;
var EPI_SESSION_KEY = keyNames.EPI_SESSION_KEY;
var store;
var token;
var session;

function saveSession(userInfo) {
function saveSession(userInfo, store) {
var serialized = JSON.stringify(userInfo);
store.set(EPI_SESSION_KEY, serialized);

Expand All @@ -61,7 +60,7 @@ function saveSession(userInfo) {
store.set(EPI_COOKIE_KEY, userInfo.auth_token);
}

function getSession() {
function getSession(store) {
var session = store.get(EPI_SESSION_KEY) || '{}';
return JSON.parse(session);
}
Expand All @@ -70,6 +69,8 @@ function AuthManager(options) {
this.options = $.extend(true, {}, defaults, options);

var urlConfig = new ConfigService(this.options).get('server');
this.isLocal = urlConfig.isLocalhost();

if (!this.options.account) {
this.options.account = urlConfig.accountPath;
}
Expand All @@ -79,9 +80,9 @@ function AuthManager(options) {
this.options.project = urlConfig.projectPath;
}

store = new StorageFactory(this.options.store);
session = getSession();
token = store.get(EPI_COOKIE_KEY) || '';
this.store = new StorageFactory(this.options.store);
session = getSession(this.store);
token = this.store.get(EPI_COOKIE_KEY) || '';
//jshint camelcase: false
//jscs:disable
this.authAdapter = new AuthAdapter(this.options, { token: session.auth_token });
Expand All @@ -107,18 +108,18 @@ AuthManager.prototype = $.extend(AuthManager.prototype, {
*
* authMgr.login({
* account: 'acme-simulations',
* project: 'supply-chain-game',
* project: 'supply-chain-game',
* userName: 'enduser1',
* password: 'passw0rd'
* password: 'passw0rd'
* })
* .then(function(statusObj) {
* // if enduser1 belongs to exactly one group
* // (or if the login() call is modified to include the group id)
* // continue here
* })
* .fail(function(statusObj) {
* // if enduser1 belongs to multiple groups,
* // the login() call fails
* // if enduser1 belongs to multiple groups,
* // the login() call fails
* // and returns all groups of which the user is a member
* for (var i=0; i < statusObj.userGroups.length; i++) {
* console.log(statusObj.userGroups[i].name, statusObj.userGroups[i].groupId);
Expand All @@ -127,12 +128,12 @@ AuthManager.prototype = $.extend(AuthManager.prototype, {
*
* **Parameters**
*
* @param {Object} `options` (Optional) Overrides for configuration options. If not passed in when creating an instance of the manager (`F.manager.AuthManager()`), these options should include:
* @param {Object} `options` (Optional) Overrides for configuration options. If not passed in when creating an instance of the manager (`F.manager.AuthManager()`), these options should include:
* @param {string} `options.account` The account id for this `userName`. In the Epicenter UI, this is the **Team ID** (for team projects) or the **User ID** (for personal projects).
* @param {string} `options.userName` Email or username to use for logging in.
* @param {string} `options.password` Password for specified `userName`.
* @param {string} `options.project` (Optional) The **Project ID** for the project to log this user into.
* @param {string} `options.groupId` The id of the group to which `userName` belongs. Required for [end users](../../../glossary/#users) if the `project` is specified and if the end users are members of multiple [groups](../../../glossary/#groups), otherwise optional.
* @param {string} `options.groupId` The id of the group to which `userName` belongs. Required for [end users](../../../glossary/#users) if the `project` is specified and if the end users are members of multiple [groups](../../../glossary/#groups), otherwise optional.
*/
login: function (options) {
var _this = this;
Expand All @@ -142,6 +143,13 @@ AuthManager.prototype = $.extend(AuthManager.prototype, {
var outError = adapterOptions.error;
var groupId = adapterOptions.groupId;

var accountName = (options && options.account) ? options.account : this.options.account;
var projectName = (options && options.project) ? options.project : this.options.project;

if (this.options.store.root === undefined && accountName && projectName) {
this.store.serviceOptions.root = this.isLocal ? '/' : '/app/' + accountName + '/' + projectName;
}

var decodeToken = function (token) {
var encoded = token.split('.')[1];
while (encoded.length % 4 !== 0) {
Expand All @@ -165,7 +173,6 @@ AuthManager.prototype = $.extend(AuthManager.prototype, {
//jshint camelcase: false
//jscs:disable
token = response.access_token;

var userInfo = decodeToken(token);
var userGroupOpts = $.extend(true, {}, adapterOptions, { success: $.noop, token: token });
_this.getUserGroups({ userId: userInfo.user_id, token: token }, userGroupOpts).done( function (memberInfo) {
Expand All @@ -179,7 +186,7 @@ AuthManager.prototype = $.extend(AuthManager.prototype, {
};
// The group is not required if the user is not logging into a project
if (!adapterOptions.project) {
saveSession(sessionInfo);
saveSession(sessionInfo, _this.store);
outSuccess.apply(this, [data]);
$d.resolve(data);
return;
Expand Down Expand Up @@ -209,7 +216,7 @@ AuthManager.prototype = $.extend(AuthManager.prototype, {
'groupName': group.name,
'isFac': _findUserInGroup(group.members, userInfo.user_id).role === 'facilitator'
});
saveSession(sessionInfoWithGroup);
saveSession(sessionInfoWithGroup, _this.store);
outSuccess.apply(this, [data]);
$d.resolve(data);
} else {
Expand Down Expand Up @@ -252,11 +259,12 @@ AuthManager.prototype = $.extend(AuthManager.prototype, {
* @param {Object} `options` (Optional) Overrides for configuration options.
*/
logout: function (options) {
var _this = this;
var adapterOptions = $.extend(true, { token: token }, this.options, options);

var removeCookieFn = function (response) {
store.remove(EPI_COOKIE_KEY, adapterOptions);
store.remove(EPI_SESSION_KEY, adapterOptions);
_this.store.remove(EPI_COOKIE_KEY, adapterOptions);
_this.store.remove(EPI_SESSION_KEY, adapterOptions);
token = '';
};

Expand All @@ -269,8 +277,8 @@ AuthManager.prototype = $.extend(AuthManager.prototype, {
* **Example**
*
* authMgr.getToken()
* .then(function (token) {
* console.log('My token is ', token);
* .then(function (token) {
* console.log('My token is ', token);
* });
*
* **Parameters**
Expand Down Expand Up @@ -298,8 +306,8 @@ AuthManager.prototype = $.extend(AuthManager.prototype, {
* // get groups for current user
* var sessionObj = authMgr.getCurrentUserSessionInfo();
* authMgr.getUserGroups({ userId: sessionObj.userId, token: sessionObj.auth_token })
* .then(function (groups) {
* for (var i=0; i < groups.length; i++)
* .then(function (groups) {
* for (var i=0; i < groups.length; i++)
* { console.log(groups[i].name); }
* });
*
Expand Down Expand Up @@ -339,7 +347,7 @@ AuthManager.prototype = $.extend(AuthManager.prototype, {
*
* *Important*: This method is synchronous. The session information is returned immediately in an object; no callbacks or promises are needed.
*
* By default, session information is stored in a cookie in the browser. You can change this with the `store` configuration option.
* By default, session information is stored in a cookie in the browser. You can change this with the `store` configuration option.
*
* **Example**
*
Expand All @@ -349,7 +357,7 @@ AuthManager.prototype = $.extend(AuthManager.prototype, {
* @param {Object} `options` (Optional) Overrides for configuration options.
*/
getCurrentUserSessionInfo: function (options) {
return getSession(options);
return getSession(this.store, options);
}
});

Expand Down
6 changes: 3 additions & 3 deletions src/store/cookie-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = function (config) {

domain: '.forio.com'
};
var serviceOptions = $.extend({}, defaults, config);
this.serviceOptions = $.extend({}, defaults, config);

var publicAPI = {
// * TBD
Expand Down Expand Up @@ -54,7 +54,7 @@ module.exports = function (config) {
* cs.set({ name:'smith', age:'32' });
*/
set: function (key, value, options) {
var setOptions = $.extend(true, {}, serviceOptions, options);
var setOptions = $.extend(true, {}, this.serviceOptions, options);

var domain = setOptions.domain;
var path = setOptions.root;
Expand Down Expand Up @@ -91,7 +91,7 @@ module.exports = function (config) {
* cs.remove('person');
*/
remove: function (key, options) {
var remOptions = $.extend(true, {}, serviceOptions, options);
var remOptions = $.extend(true, {}, this.serviceOptions, options);

var domain = remOptions.domain;
var path = remOptions.root;
Expand Down
24 changes: 23 additions & 1 deletion tests/spec/test-auth-manager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(function () {
'use strict';

describe('Auth Manager', function () {
var server, token;
before(function () {
Expand Down Expand Up @@ -47,5 +46,28 @@
});

// TODO: Create some test, find a way for the fake server to auto respond synchronously inside a respond callback
describe('#setting cookies', function () {
it ('creates cookie with the correct path name when passing in account info in consructor', function () {
var am = new F.manager.AuthManager({
account: 'accountName',
project: 'projectName'
});
am.isLocal = false ;
am.login();
am.store.serviceOptions.root.should.equal('/app/accountName/projectName');
});
});
describe('#setting cookies', function () {
it ('creates cookie with the correct path name when passing in account info in login', function () {
var am = new F.manager.AuthManager();
am.isLocal = false;
am.login({
account: 'accountName',
project: 'projectName'
});
am.store.serviceOptions.root.should.equal('/app/accountName/projectName');
});
});

});
}());

0 comments on commit 5eb8507

Please sign in to comment.