-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
auth.ts
170 lines (154 loc) · 4.92 KB
/
auth.ts
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import Scrypt from "scrypt-kdf"
import { AuthenticateResult } from "../types/auth"
import { Customer, User } from "../models"
import { TransactionBaseService } from "../interfaces"
import UserService from "./user"
import CustomerService from "./customer"
import { EntityManager } from "typeorm"
type InjectedDependencies = {
manager: EntityManager
userService: UserService
customerService: CustomerService
}
/**
* Can authenticate a user based on email password combination
*/
class AuthService extends TransactionBaseService {
protected readonly userService_: UserService
protected readonly customerService_: CustomerService
constructor({ userService, customerService }: InjectedDependencies) {
// eslint-disable-next-line prefer-rest-params
super(arguments[0])
this.userService_ = userService
this.customerService_ = customerService
}
/**
* Verifies if a password is valid given the provided password hash
* @param {string} password - the raw password to check
* @param {string} hash - the hash to compare against
* @return {bool} the result of the comparison
*/
protected async comparePassword_(
password: string,
hash: string
): Promise<boolean> {
const buf = Buffer.from(hash, "base64")
return Scrypt.verify(buf, password)
}
/**
* Authenticates a given user with an API token
* @param {string} token - the api_token of the user to authenticate
* @return {AuthenticateResult}
* success: whether authentication succeeded
* user: the user document if authentication succeeded
* error: a string with the error message
*/
async authenticateAPIToken(token: string): Promise<AuthenticateResult> {
return await this.atomicPhase_(async (transactionManager) => {
try {
const user: User = await this.userService_
.withTransaction(transactionManager)
.retrieveByApiToken(token)
return {
success: true,
user,
}
} catch (error) {
return {
success: false,
error: "Invalid API Token",
}
}
})
}
/**
* Authenticates a given user based on an email, password combination. Uses
* scrypt to match password with hashed value.
* @param {string} email - the email of the user
* @param {string} password - the password of the user
* @return {AuthenticateResult}
* success: whether authentication succeeded
* user: the user document if authentication succeeded
* error: a string with the error message
*/
async authenticate(
email: string,
password: string
): Promise<AuthenticateResult> {
return await this.atomicPhase_(async (transactionManager) => {
try {
const userPasswordHash: User = await this.userService_
.withTransaction(transactionManager)
.retrieveByEmail(email, {
select: ["password_hash"],
})
const passwordsMatch = await this.comparePassword_(
password,
userPasswordHash.password_hash
)
if (passwordsMatch) {
const user = await this.userService_
.withTransaction(transactionManager)
.retrieveByEmail(email)
return {
success: true,
user: user,
}
}
} catch (error) {
console.log("error ->", error)
// ignore
}
return {
success: false,
error: "Invalid email or password",
}
})
}
/**
* Authenticates a customer based on an email, password combination. Uses
* scrypt to match password with hashed value.
* @param {string} email - the email of the user
* @param {string} password - the password of the user
* @return {{ success: (bool), customer: (object | undefined) }}
* success: whether authentication succeeded
* customer: the customer document if authentication succeded
* error: a string with the error message
*/
async authenticateCustomer(
email: string,
password: string
): Promise<AuthenticateResult> {
return await this.atomicPhase_(async (transactionManager) => {
try {
const customer: Customer = await this.customerService_
.withTransaction(transactionManager)
.retrieveRegisteredByEmail(email, {
select: ["id", "password_hash"],
})
if (customer.password_hash) {
const passwordsMatch = await this.comparePassword_(
password,
customer.password_hash
)
if (passwordsMatch) {
const customer = await this.customerService_
.withTransaction(transactionManager)
.retrieveRegisteredByEmail(email)
return {
success: true,
customer,
}
}
}
} catch (error) {
// ignore
}
return {
success: false,
error: "Invalid email or password",
}
})
}
}
export default AuthService