Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
757b931
feat(Adapter Class): The realisation of adapter class and Redis data …
stainwoortsel Jun 3, 2016
76cc264
feat(actions, adapter, redisstorage): Updating few actions with new s…
stainwoortsel Jun 3, 2016
fd2a23f
feat(adapter, redisstorage, actions): Making abstraction layer betwee…
stainwoortsel Jun 3, 2016
c4ebf71
style(gitignore):
stainwoortsel Jun 3, 2016
4b7a5c6
style(delete compiled and map files):
stainwoortsel Jun 3, 2016
a334384
feat: actions: login, alias, list
stainwoortsel Jun 4, 2016
98cf306
feat: register action
stainwoortsel Jun 5, 2016
17ba3bf
feat: add new utils: verifyGoogleCaptcha and mapMetaResponse
stainwoortsel Jun 5, 2016
e04d2c7
feat: remove action
stainwoortsel Jun 5, 2016
e113733
feat: actions: requestPassword, updateMetadata
stainwoortsel Jun 5, 2016
0b51598
feat: actions: updatePassword, verify
stainwoortsel Jun 6, 2016
55c47af
style: actions, adapter, redisstorage, utils, docker.sh
stainwoortsel Jun 8, 2016
b18ac68
feat: new UserModer class abstraction, redisstorage, new modelError m…
stainwoortsel Jun 18, 2016
821deb6
feat: renewed modelError methods, usermodal, redisstodage; utils: jwt…
stainwoortsel Jun 20, 2016
8a5de75
feat: usermodel & redisstorage: throttle state and token methods, uti…
stainwoortsel Jun 21, 2016
3829f5e
style: some linter fixes: util/scrypt, util/send-emal, model/redissto…
stainwoortsel Jun 21, 2016
60746e9
fix: fixing docker.sh
stainwoortsel Jun 23, 2016
6ba80a6
Merge branch 'master' into adapter
AVVS Jun 26, 2016
0dfc8c9
fix: fixing 'this'-context trouble, fixing loginAttempts variable
stainwoortsel Jun 26, 2016
e4c8b44
fix: fixing bugs through the tests
stainwoortsel Jul 1, 2016
ac18064
fix: linting!
stainwoortsel Jul 1, 2016
71aa982
fix: fixing bugs through tests
stainwoortsel Jul 1, 2016
2ee6993
fix: linting!
stainwoortsel Jul 1, 2016
275a7bb
fix: fixing register bug with catchReturn wrong error code
stainwoortsel Jul 1, 2016
7d5a1f2
fix: fixing bugs through the tests
stainwoortsel Jul 2, 2016
8b7263d
fix: fixing bugs through the tests
stainwoortsel Jul 3, 2016
0860363
fix: fixing bugs through the tests: usermodel, login, updateMeta, tok…
stainwoortsel Jul 4, 2016
12c5df1
fix: jwt logout decodedTokens, arrow-function better
stainwoortsel Jul 4, 2016
7d7bfb4
fix: fixing bugs through the tests: jwt/send-mail
stainwoortsel Jul 5, 2016
785ccfd
fix: fixing bugs through the test (hope, in last time): jwt, send-mail
stainwoortsel Jul 5, 2016
8ea411f
fix: some cosmetic fixes
stainwoortsel Jul 7, 2016
d6cff19
fix: some another cosmetic fixes
stainwoortsel Jul 7, 2016
7cc68c8
refactor: merging with master
stainwoortsel Jul 8, 2016
1cabd9c
fix: UNIX-style end of strings in scripts (to avoid bash errors)
stainwoortsel Jul 8, 2016
71c46a5
fix: some lint sugar
stainwoortsel Jul 8, 2016
db222e4
fix: and little fix for math operation with page of list in redisstor…
stainwoortsel Jul 8, 2016
aaad1a4
fix: please linter, please...
stainwoortsel Jul 8, 2016
98b5770
feat: mongo docker-compose
stainwoortsel Jul 18, 2016
20fa21d
feat: making test suites a bit more independed from storages
stainwoortsel Aug 1, 2016
24e3674
feat: methods in mongo adapter, before HUGE refactoring commit
stainwoortsel Aug 10, 2016
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
4 changes: 2 additions & 2 deletions src/actions/activate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const Promise = require('bluebird');
const emailVerification = require('../utils/send-email.js');
const jwt = require('../utils/jwt.js');
const { User } = require('../model/usermodel');
const { ModelError } = require('../model/modelError');
const { httpErrorMapper } = require('../model/modelError');

module.exports = function verifyChallenge(opts) {
// TODO: add security logs
Expand All @@ -26,5 +26,5 @@ module.exports = function verifyChallenge(opts) {
.tap(hook)
.then(user => [user, audience])
.spread(jwt.login)
.catch(e => { throw (e instanceof ModelError ? e : e.mapToHttp); });
.catch(e => { throw httpErrorMapper(e); });
};
4 changes: 2 additions & 2 deletions src/actions/alias.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const isActive = require('../utils/isActive');
const isBanned = require('../utils/isBanned');

const { User } = require('../model/usermodel');
const { ModelError } = require('../model/modelError');
const { httpErrorMapper } = require('../model/modelError');


module.exports = function assignAlias(opts) {
Expand All @@ -16,5 +16,5 @@ module.exports = function assignAlias(opts) {
.tap(isBanned)
.then(data => ({ username, alias, data }))
.then(User.User.setAlias)
.catch(e => { throw (e instanceof ModelError ? e : e.mapToHttp); });
.catch(e => { throw httpErrorMapper(e); });
};
4 changes: 2 additions & 2 deletions src/actions/ban.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Promise = require('bluebird');
const { User } = require('../model/usermodel');
const { ModelError } = require('../model/modelError');
const { httpErrorMapper } = require('../model/modelError');

/**
* Bans/unbans existing user
Expand All @@ -13,5 +13,5 @@ module.exports = function banUser(opts) {
.then(User.getUsername)
.then(username => ({ username, opts }))
.then(opts.ban ? User.lock : User.unlock)
.catch(e => { throw (e instanceof ModelError ? e : e.mapToHttp); });
.catch(e => { throw httpErrorMapper(e); });
};
4 changes: 2 additions & 2 deletions src/actions/challenge.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const Promise = require('bluebird');
const emailChallenge = require('../utils/send-email.js');
const isActive = require('../utils/isActive');
const { User } = require('../model/usermodel');
const { ModelError, ERR_USERNAME_ALREADY_ACTIVE } = require('../model/modelError');
const { ModelError, httpErrorMapper, ERR_USERNAME_ALREADY_ACTIVE } = require('../model/modelError');

module.exports = function sendChallenge(message) {
const { username } = message;
Expand All @@ -17,5 +17,5 @@ module.exports = function sendChallenge(message) {
.throw(new ModelError(ERR_USERNAME_ALREADY_ACTIVE, username))
.catchReturn({ statusCode: 412 }, username)
.then(emailChallenge.send)
.catch(e => { throw (e instanceof ModelError ? e : e.mapToHttp); });
.catch(e => { throw httpErrorMapper(e); });
};
4 changes: 2 additions & 2 deletions src/actions/getInternalData.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const Promise = require('bluebird');
const pick = require('lodash/pick');

const { User } = require('../model/usermodel');
const { ModelError } = require('../model/modelError');
const { httpErrorMapper } = require('../model/modelError');

module.exports = function internalData(message) {
const { fields } = message;
Expand All @@ -13,5 +13,5 @@ module.exports = function internalData(message) {
.then(data => {
return fields ? pick(data, fields) : data;
})
.catch(e => { throw (e instanceof ModelError ? e : e.mapToHttp); });
.catch(e => { throw httpErrorMapper(e); });
};
4 changes: 2 additions & 2 deletions src/actions/getMetadata.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Promise = require('bluebird');
const { User } = require('../model/usermodel');
const { ModelError } = require('../model/modelError');
const { httpErrorMapper } = require('../model/modelError');

module.exports = function getMetadataAction(message) {
const { audience, username, fields } = message;
Expand All @@ -10,5 +10,5 @@ module.exports = function getMetadataAction(message) {
.then(User.getUsername)
.then(realUsername => [realUsername, audience, fields, message.public])
.spread(User.getMeta)
.catch(e => { throw (e instanceof ModelError ? e : e.mapToHttp); });
.catch(e => { throw httpErrorMapper(e); });
};
4 changes: 2 additions & 2 deletions src/actions/list.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const Promise = require('bluebird');
const { User } = require('../model/usermodel');
const { ModelError } = require('../model/modelError');
const { httpErrorMapper } = require('../model/modelError');


module.exports = function iterateOverActiveUsers(opts) {
return Promise
.bind(this, opts)
.then(User.getList)
.catch(e => { throw (e instanceof ModelError ? e : e.mapToHttp); });
.catch(e => { throw httpErrorMapper(e); });
};
23 changes: 12 additions & 11 deletions src/actions/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ const Promise = require('bluebird');
const scrypt = require('../utils/scrypt.js');
const jwt = require('../utils/jwt.js');
const noop = require('lodash/noop');

const Users = require('../db/adapter');

const isActive = require('../utils/isActive');
const isBanned = require('../utils/isBanned');
const { User, Attempts } = require('../model/usermodel');
const { httpErrorMapper } = require('../model/modelError');

module.exports = function login(opts) {
const config = this.config.jwt;
Expand All @@ -25,21 +26,21 @@ module.exports = function login(opts) {

function enrichError(err) {
if (remoteip) {
err.loginAttempts = Users.getAttempts();
err.loginAttempts = Attempts.count();
}

throw err;
return err;
}

return Promise
.bind(this, opts.username)
.then(Users.getUser)
.then(User.getOne)
.then(data => [data, remoteip])
.tap(verifyIp ? Users.checkLoginAttempts : noop)
.tap(verifyIp ? Attempts.check : noop)
.tap(verifyHash)
.tap(verifyIp ? Users.dropAttempts : noop)
.tap(Users.isActive)
.tap(Users.isBanned)
.tap(verifyIp ? Attempts.drop : noop)
.tap(isActive)
.tap(isBanned)
.then(getUserInfo)
.catch(verifyIp ? enrichError : e => { throw e; });
.catch(e => { throw httpErrorMapper(verifyIp ? enrichError(e) : e); });
};
16 changes: 9 additions & 7 deletions src/actions/remove.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
const Promise = require('bluebird');
const Errors = require('common-errors');
const { USERS_ADMIN_ROLE } = require('../constants');
const Users = require('../db/adapter');
const { User } = require('../model/usermodel');
const { ModelError, httpErrorMapper, ERR_ADMIN_IS_UNTOUCHABLE } = require('../model/modelError');


module.exports = function removeUser({ username }) {
const audience = this.config.jwt.defaultAudience;

return Promise.props({
internal: Users.getUser(username),
meta: Users.getMetadata(username, audience),
internal: User.getOne.call(this, username),
meta: User.getMeta.call(this, username, audience),
})
.then(({ internal, meta }) => {
const isAdmin = (meta[audience].roles || []).indexOf(USERS_ADMIN_ROLE) >= 0;
if (isAdmin) {
throw new Errors.HttpStatusError(400, 'can\'t remove admin user from the system');
throw new ModelError(ERR_ADMIN_IS_UNTOUCHABLE);
}

return Users.removeUser(username, internal);
});
return User.remove.call(this, username, internal);
})
.catch(e => { throw httpErrorMapper(e); });
};
15 changes: 10 additions & 5 deletions src/actions/requestPassword.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const Promise = require('bluebird');
const emailValidation = require('../utils/send-email.js');
const Users = require('../db/adapter');
const isActive = require('../utils/isActive');
const isBanned = require('../utils/isBanned');
const { User } = require('../model/usermodel');
const { httpErrorMapper } = require('../model/modelError');


module.exports = function requestPassword(opts) {
const { username, generateNewPassword } = opts;
Expand All @@ -11,9 +15,10 @@ module.exports = function requestPassword(opts) {

return Promise
.bind(this, username)
.then(Users.getUser)
.tap(Users.isActive)
.tap(Users.isBanned)
.then(User.getOne)
.tap(isActive)
.tap(isBanned)
.then(() => emailValidation.send.call(this, username, action))
.return({ success: true });
.return({ success: true })
.catch(e => { throw httpErrorMapper(e); });
};
8 changes: 5 additions & 3 deletions src/actions/updateMetadata.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const Promise = require('bluebird');
const Users = require('../db/adapter');
const { User } = require('../model/usermodel');
const { httpErrorMapper } = require('../model/modelError');

module.exports = function updateMetadataAction(message) {
return Promise
.bind(this, message.username)
.then(Users.isExists)
.then(User.getUsername)
.then(username => ({ ...message, username }))
.then(Users.updateMetadata);
.then(message.script ? User.executeUpdateMetaScript : User.setMeta)
.catch(e => { throw httpErrorMapper(e); });
};
24 changes: 16 additions & 8 deletions src/actions/updatePassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ const Promise = require('bluebird');
const scrypt = require('../utils/scrypt.js');
const jwt = require('../utils/jwt.js');
const emailChallenge = require('../utils/send-email.js');
const Users = require('../db/adapter');

const isActive = require('../utils/isActive');
const isBanned = require('../utils/isBanned');

const { User, Attempts } = require('../model/usermodel');
const { httpErrorMapper } = require('../model/modelError');


/**
* Verifies token and deletes it if it matches
Expand All @@ -20,11 +26,12 @@ function tokenReset(token) {
function usernamePasswordReset(username, password) {
return Promise
.bind(this, username)
.then(Users.getUser)
.tap(Users.isActive)
.tap(Users.isBanned)
.then(User.getOne)
.tap(isActive)
.tap(isBanned)
.tap(data => scrypt.verify(data.password, password))
.return(username);
.return(username)
.catch(e => { throw httpErrorMapper(e); });
}

/**
Expand All @@ -35,12 +42,13 @@ function usernamePasswordReset(username, password) {
function setPassword(_username, password) {
return Promise
.bind(this, _username)
.then(Users.isExists)
.then(User.getUsername)
.then(username => Promise.props({
username,
hash: scrypt.hash(password),
}))
.then(Users.setPassword);
.then(User.setPassword)
.catch(e => { throw httpErrorMapper(e); });
}

module.exports = exports = function updatePassword(opts) {
Expand All @@ -65,7 +73,7 @@ module.exports = exports = function updatePassword(opts) {

if (remoteip) {
promise = promise.tap(function resetLock(username) {
return Users.resetIPLock(username, remoteip);
return Attempts.drop(username, remoteip);
});
}

Expand Down
8 changes: 5 additions & 3 deletions src/actions/verify.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Promise = require('bluebird');
const jwt = require('../utils/jwt.js');
const Users = require('../db/adapter');
const { User } = require('../model/usermodel');
const { httpErrorMapper } = require('../model/modelError');

/**
* Verifies that passed token is signed correctly, returns associated metadata with it
Expand All @@ -25,7 +26,8 @@ module.exports = function verify(opts) {
const username = decoded.username;
return Promise.props({
username,
metadata: Users.getMetadata(username, audience),
metadata: User.getMeta.call(this, username, audience),
});
});
})
.catch(e => { throw httpErrorMapper(e); });
};
Loading