Skip to content

Commit babf85d

Browse files
committed
add login limit error times then lock the account
1 parent 590319a commit babf85d

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

core/services/account-manager.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ proto.createAccessKey = function (uid, newAccessKey, isSession, ttl, friendlyNam
8888
});
8989
};
9090

91+
const LOGIN_LIMIT_PRE = 'LOGIN_LIMIT_PRE_';
92+
9193
proto.login = function (account, password) {
9294
if (_.isEmpty(account)) {
9395
return Promise.reject(new Error("Please input Account."))
@@ -105,12 +107,37 @@ proto.login = function (account, password) {
105107
.then(function(users) {
106108
if (_.isEmpty(users)) {
107109
throw new Error("account or password error.");
108-
} else {
109-
if (!security.passwordVerifySync(password, users.password)) {
110-
throw new Error("account or password error.");
111-
}else {
112-
return users;
110+
}
111+
return users;
112+
})
113+
.then(function (users) {
114+
var loginKey = `${LOGIN_LIMIT_PRE}${users.id}`;
115+
return factory.getRedisClient("default").getAsync(loginKey)
116+
.then(function (loginErrorTimes) {
117+
if (loginErrorTimes > 10) {
118+
throw new Error(`您输入密码错误次数超过限制,帐户已经锁定`);
113119
}
120+
return users;
121+
});
122+
})
123+
.then(function (users) {
124+
if (!security.passwordVerifySync(password, users.password)) {
125+
var loginKey = `${LOGIN_LIMIT_PRE}${users.id}`;
126+
var client = factory.getRedisClient("default");
127+
client.existsAsync(loginKey)
128+
.then(function (isExists) {
129+
if (!isExists) {
130+
var expires = moment().endOf('day').format('X') - moment().format('X');
131+
return client.setexAsync(loginKey, expires, 0);
132+
}
133+
return isExists;
134+
})
135+
.then(function () {
136+
return client.incrAsync(loginKey);
137+
})
138+
throw new Error("account or password error.");
139+
} else {
140+
return users;
114141
}
115142
});
116143
};

routes/users.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ router.post('/', function (req, res, next) {
1818
return accountManager.checkRegisterCode(email, token)
1919
.then(function (u) {
2020
if (_.isString(password) && password.length < 6) {
21-
throw new ERROR('password length must gt ');6
21+
throw new ERROR('password length must gt ');
2222
}
2323
return accountManager.register(email, password);
2424
})

0 commit comments

Comments
 (0)