-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser-auth.js
136 lines (110 loc) · 4.44 KB
/
user-auth.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
class UserAuth {
/**
*
* @param {*} secret private key for jsonwebtoken script (see https://www.npmjs.com/package/jsonwebtoken)
* @param {*} logCallback function to log errors, by default it's console.log
* @param {*} saltRounds salt rounds for bcrypt encryption
* @param {*} jwtEncryptOptions options for jwt.sign() function, defaults to empty object
* @param {*} jwtDecryptOptions options for jwt.verify() function, defaults to empty object
*/
constructor( secret, logCallback = console.log, saltRounds = 10, jwtEncryptOptions = {}, jwtDecryptOptions={} ){
this.saltRounds = saltRounds
this.secret = secret
this.logCallback = logCallback
this.jwtEncryptOptions = jwtEncryptOptions
this.jwtDecryptOptions = jwtDecryptOptions
}
/**
* Log callback wrapper. Creates an object of passed message and error and calls logCallback function
* @param {*} message
* @param {*} error
*/
log( message, error ){
this.logCallback( { message, error } )
}
/**
* Creates a password hash to store in DB *
* @param {*} password
* @usage const hash = await uauth.hash("qwerty12345") (in async context)
*/
async hash( password ){
try{
return await bcrypt.hash( password, this.saltRounds)
} catch( e ){
this.log( "Error hashing password", e )
return false
}
}
/**
* Checks if password corresponds to password hash from DB and returns JWT token with given payload on success or false otherwise.
* @param {*} password
* @param {*} passwordHash
* @param {*} payload JWT.sign payload, basically anything to store within the token (see https://www.npmjs.com/package/jsonwebtoken for details )
* @param {*} expireMinutes - minutes to expire the token within, defaults to 30
*/
async auth( password, passwordHash, payload = {}, expireMinutes = 30){
try{
if( await bcrypt.compare( password, passwordHash) ){
return this.getToken(payload, expireMinutes)
} else {
this.log("Password didn't match", {} )
// console.debug("shouldAuthenticate:", shouldAuthenticate)
// this.error = shouldAuthenticate.error || "Password didn't match"
return false
}
} catch( e ){
this.log( "Error comparing password", e )
return false
}
}
decodeToken(token, options = {}){
const verifyOptions = {...this.jwtDecryptOptions, ...options }
return jwt.decode(token, verifyOptions)
}
verifyToken( token, options = {}){
try {
// mixin the passed verification options
const verifyOptions = {...this.jwtDecryptOptions, ...options }
return jwt.verify(token, this.secret, verifyOptions )
} catch( e ){
let error = "Unknown token verification error"
let errorDetails = {}
try {
const{name, message} = e
errorDetails = {name, message}
switch( e.name ){
case "TokenExpiredError":
error = "Token expired"
errorDetails.expiredAt = e.expiredAt
break
case "JsonWebTokenError":
error = "Token invalid!"
break;
case "NotBeforeError":
error = "Token is not active yet"
errorDetails.date = e.date
break;
}
} catch ( e1 ){}
return {error, errorDetails }
}
}
getToken( payload, expireMinutes = 30 ){
const options = {...this.jwtEncryptOptions, expiresIn: this.expireInMinutes(expireMinutes) }
try{
return jwt.sign(payload, this.secret, options )
} catch( e ){
this.log("Error signing JWT token", e )
}
}
/**
* returns timestamp offset for {minutes} minutes
* @param {int} minutes amount of minutes
*/
expireInMinutes( minutes ){
return 60 * minutes
}
}
module.exports = UserAuth