-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserValidator.js
112 lines (90 loc) · 2.18 KB
/
UserValidator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// const BaseValidator = require('cyanide-validator').BaseValidator
const BaseValidator = require('./base')
const RateLimiter = require('./RateLimiter')
class UserValidator extends BaseValidator {
get policy_conf() {
return this.target.policy[this.constructor.name]
}
validate(user_info) {
// console.log(user_info)
this.target = user_info.target
this.request = user_info.request
this.user_info = user_info
this.change_pwd_at = null
return super.validate(user_info)
}
constructor(opt){
super(opt)
this.redis = opt.redis
this.REDIS_TIMEOUT = opt.conf.REDIS_TIMEOUT
this.rateLimiter = new RateLimiter({
interval: opt.conf.RATE_LIMIT_INTERVAL
})
}
get headers() {
return this.user_info.headers
}
get uid(){
return this.user_info.payload.aud
}
get app_id(){
return this.user_info.payload.app_id
}
get issued_at(){
return this.user_info.payload.iat
}
get expires_at(){
// console.log(this.user_info.payload)
return this.user_info.payload.exp
}
get last_login_ip(){
return this.user_info.payload.last_login_ip
}
get now(){
return Math.floor(new Date().getTime() / 1000)
}
get_change_pwd_at(policy){
this.redis.get(this.uid, (err, reply) => {
if(err) {
throw err
} else {
this.change_pwd_at = JSON.parse(reply).change_pwd_at
this.next()
// this.resolve(JSON.parse(reply).change_pwd_at)
}
})
setTimeout(()=>{
// FIX ME: Change policy "throwError Method"
let e = { 'name':'userError', 'error': policy.errorThrower.err }
this.reject(e)
}, this.REDIS_TIMEOUT)
}
get_secret(policy) {
}
enforceRateLimit(args) {
args.limits.forEach((constrain)=>{
this.rateLimiter.enforce(
this.buildLimitKey(constrain.key),
{ limit: constrain.limit }
)
console.log(constrain)
})
// this.rateLimiter.enforce(key, options, cb)
}
get ip(){
return UserValidator.getIP(this.request)
}
static getIP(req) {
return req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress;
}
buildLimitKey(key) {
return `${key}-${this[key]}`
}
done() {
return this.resolve(this.user_info)
}
}
module.exports = UserValidator