|
|
@@ -1,10 +1,40 @@
|
|
1
|
1
|
'use strict'
|
|
2
|
|
-
|
|
|
2
|
+require('dotenv').config()
|
|
3
|
3
|
const Util = require('util')
|
|
4
|
4
|
const Jwt = require('@hapi/jwt')
|
|
5
|
5
|
const Schmervice = require('@hapipal/schmervice')
|
|
6
|
6
|
const SecurePassword = require('secure-password')
|
|
7
|
7
|
|
|
|
8
|
+const hasher = async (pwd, steak) => {
|
|
|
9
|
+ const hash = await pwd.hash(steak)
|
|
|
10
|
+ const result = await pwd.verify(steak, hash)
|
|
|
11
|
+ let squirtle = null
|
|
|
12
|
+
|
|
|
13
|
+ switch (result) {
|
|
|
14
|
+ case SecurePassword.INVALID_UNRECOGNIZED_HASH:
|
|
|
15
|
+ return console.error(
|
|
|
16
|
+ 'This hash was not made with secure-password. Attempt legacy algorithm',
|
|
|
17
|
+ )
|
|
|
18
|
+ case SecurePassword.INVALID:
|
|
|
19
|
+ return console.log('Invalid password')
|
|
|
20
|
+ case SecurePassword.VALID:
|
|
|
21
|
+ return result
|
|
|
22
|
+ case SecurePassword.VALID_NEEDS_REHASH:
|
|
|
23
|
+ console.log('Yay you made it, wait for us to improve your safety')
|
|
|
24
|
+ try {
|
|
|
25
|
+ squirtle = await pwd.hash(steak)
|
|
|
26
|
+ // console.log('improvedHash', squirtle)
|
|
|
27
|
+ // const saveHash = Auth.insert({user_email: matchingEmails}, ).into('token')
|
|
|
28
|
+ return squirtle
|
|
|
29
|
+ } catch (err) {
|
|
|
30
|
+ console.error(
|
|
|
31
|
+ 'You are authenticated, but we could not improve your safety this time around',
|
|
|
32
|
+ )
|
|
|
33
|
+ }
|
|
|
34
|
+ break
|
|
|
35
|
+ }
|
|
|
36
|
+}
|
|
|
37
|
+
|
|
8
|
38
|
/** Class for methods used in the User plugin */
|
|
9
|
39
|
module.exports = class UserService extends Schmervice.Service {
|
|
10
|
40
|
/**
|
|
|
@@ -56,21 +86,33 @@ module.exports = class UserService extends Schmervice.Service {
|
|
56
|
86
|
* @param {*} txn
|
|
57
|
87
|
* @returns
|
|
58
|
88
|
*/
|
|
59
|
|
- async signup({ password, userInfo }, txn) {
|
|
60
|
|
- const { User } = this.server.models()
|
|
|
89
|
+ async signup({ password, userInfo, created_at }, txn) {
|
|
|
90
|
+ const { User, Auth } = this.server.models()
|
|
61
|
91
|
const matchingEmails = await User.query().where(
|
|
62
|
92
|
'user_email',
|
|
63
|
93
|
userInfo.user_email,
|
|
64
|
94
|
)
|
|
65
|
|
-
|
|
66
|
95
|
if (matchingEmails.length > 0) {
|
|
67
|
96
|
throw `User ${userInfo.user_email} already exists: Cannot create a user without a unique email`
|
|
68
|
97
|
}
|
|
69
|
|
- const user = await User.query(txn).insert(userInfo)
|
|
70
|
|
- user.user_id = user.id
|
|
71
|
|
- delete user.id
|
|
72
|
|
- // await this.changePassword(id, password, txn)
|
|
73
|
|
- return user
|
|
|
98
|
+ // Insert User Info to User table
|
|
|
99
|
+ const insertUser = await User.query().insert(userInfo)
|
|
|
100
|
+ // insert a row with blank password to be updated by changePassword()
|
|
|
101
|
+ await Auth.query().insert({
|
|
|
102
|
+ user_email: insertUser.user_email,
|
|
|
103
|
+ created_at: created_at,
|
|
|
104
|
+ token: null,
|
|
|
105
|
+ })
|
|
|
106
|
+ // update null token with hashed password
|
|
|
107
|
+ await this.changePassword(insertUser.user_email, password, txn)
|
|
|
108
|
+ return {
|
|
|
109
|
+ user_id: insertUser.id,
|
|
|
110
|
+ user_name: insertUser.user_name,
|
|
|
111
|
+ user_email: insertUser.user_email,
|
|
|
112
|
+ is_poster: insertUser.is_poster,
|
|
|
113
|
+ is_admin: insertUser.is_admin,
|
|
|
114
|
+ is_verified: insertUser.is_verified,
|
|
|
115
|
+ }
|
|
74
|
116
|
}
|
|
75
|
117
|
|
|
76
|
118
|
/**
|
|
|
@@ -104,21 +146,26 @@ module.exports = class UserService extends Schmervice.Service {
|
|
104
|
146
|
* @returns
|
|
105
|
147
|
*/
|
|
106
|
148
|
async login({ email, password }, txn) {
|
|
107
|
|
- const { User } = this.server.models()
|
|
|
149
|
+ const { User, Auth } = this.server.models()
|
|
108
|
150
|
|
|
109
|
|
- const user = await User.query(txn)
|
|
|
151
|
+
|
|
|
152
|
+
|
|
|
153
|
+ const user = await Auth.query(txn)
|
|
110
|
154
|
.throwIfNotFound()
|
|
111
|
155
|
.first()
|
|
112
|
156
|
.where({ user_email: email })
|
|
113
|
157
|
|
|
|
158
|
+ const bufferPepper = Buffer.from(process.env.PEPPER + password)
|
|
|
159
|
+
|
|
114
|
160
|
/** Uncomment to run password check using SecurePassword */
|
|
115
|
|
- // const passwordCheck = await this.pwd.verify(Buffer.from(password), user.password)
|
|
116
|
|
- // if (passwordCheck === SecurePassword.VALID_NEEDS_REHASH) {
|
|
117
|
|
- // await this.changePassword(user.id, password, txn)
|
|
118
|
|
- // }
|
|
119
|
|
- // else if (passwordCheck !== SecurePassword.VALID) {
|
|
120
|
|
- // throw User.createNotFoundError()
|
|
121
|
|
- // }
|
|
|
161
|
+ const passwordCheck = await this.pwd.verify(bufferPepper, user.token)
|
|
|
162
|
+ console.log("passwordCheck", passwordCheck)
|
|
|
163
|
+ if (passwordCheck === SecurePassword.VALID_NEEDS_REHASH) {
|
|
|
164
|
+ await this.changePassword(user.user_email, password, txn)
|
|
|
165
|
+ }
|
|
|
166
|
+ else if (passwordCheck !== SecurePassword.VALID) {
|
|
|
167
|
+ throw User.createNotFoundError()
|
|
|
168
|
+ }
|
|
122
|
169
|
|
|
123
|
170
|
return user
|
|
124
|
171
|
}
|
|
|
@@ -154,9 +201,23 @@ module.exports = class UserService extends Schmervice.Service {
|
|
154
|
201
|
* @param {*} txn
|
|
155
|
202
|
* @returns {number}
|
|
156
|
203
|
*/
|
|
157
|
|
- async changePassword(id, password, txn) {
|
|
158
|
|
- const { User } = this.server.models()
|
|
159
|
|
- return 'done'
|
|
|
204
|
+ async changePassword(email, password, txn) {
|
|
|
205
|
+ const { User, Auth } = this.server.models()
|
|
|
206
|
+
|
|
|
207
|
+ console.log('email passed to changePassword', email)
|
|
|
208
|
+
|
|
|
209
|
+ const hashed = await this.pwd.hash(Buffer.from(process.env.PEPPER + password))
|
|
|
210
|
+ console.log('hashed', hashed)
|
|
|
211
|
+
|
|
|
212
|
+ await Auth.query(txn)
|
|
|
213
|
+ .throwIfNotFound()
|
|
|
214
|
+ .where({ user_email: email })
|
|
|
215
|
+ .patch({
|
|
|
216
|
+ // user_email: email,
|
|
|
217
|
+ token: hashed
|
|
|
218
|
+ })
|
|
|
219
|
+ console.log('changePassword query completed')
|
|
|
220
|
+ return email
|
|
160
|
221
|
|
|
161
|
222
|
// await User.query(txn)
|
|
162
|
223
|
// .throwIfNotFound()
|
|
|
@@ -166,4 +227,14 @@ module.exports = class UserService extends Schmervice.Service {
|
|
166
|
227
|
// })
|
|
167
|
228
|
// return id
|
|
168
|
229
|
}
|
|
|
230
|
+
|
|
|
231
|
+ async getPassword(email, txn) {
|
|
|
232
|
+ const { Auth } = this.server.models()
|
|
|
233
|
+
|
|
|
234
|
+ const passwordRow = await Auth.query(txn)
|
|
|
235
|
+ .where('user_email', email)
|
|
|
236
|
+ .first()
|
|
|
237
|
+
|
|
|
238
|
+ return passwordRow ? passwordRow.token : null
|
|
|
239
|
+ }
|
|
169
|
240
|
}
|