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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
import argon2 from "argon2";
import express from "express";
import { users, linkedDevices } from "../db/schema";
import { db } from "../db/db";
import { Infer, z } from "zod";
import { signJwt } from "../account/jwt";
import { eq } from "drizzle-orm";
import { logger } from "../lib/pino";
import * as jose from "jose";
import { getTransporter } from "../email/email";
/** Validates signin request body with email and password */
const SigninBodySchema = z.object({
email: z
.string()
.transform((val) => val.trim())
.pipe(
z
.email({ error: "Invalid email" })
.nonempty({ error: "Email can't be empty" }),
),
password: z.string(),
});
const ResetPasswordRequestSchema = z.object({
email: z
.string()
.transform((val) => val.trim())
.pipe(
z
.email({ error: "Invalid email" })
.nonempty({ error: "Email can't be empty" }),
),
link: z.url({ error: "Invalid link" }),
});
const ResetPasswordConfirmSchema = z.object({
token: z.string().min(1, { error: "Token can't be empty" }),
password: z.string().min(1, { error: "Password can't be empty" }),
});
const router: express.Router = express.Router();
function getResetPasswordSecret(): Uint8Array {
const secret = process.env.RESET_PASSWORD_JWT_SECRET;
if (!secret) {
logger.error("RESET_PASSWORD_JWT_SECRET environment variable not set");
throw new Error("RESET_PASSWORD_JWT_SECRET not configured");
}
return new TextEncoder().encode(secret);
}
async function signResetPasswordToken(userId: number): Promise<string> {
return new jose.SignJWT({ id: userId, type: "password_reset" })
.setProtectedHeader({ alg: "HS256" })
.setIssuedAt()
.setIssuer("urn:lajosh:buddy")
.setAudience("urn:buddy:password-reset")
.setExpirationTime("1h")
.sign(getResetPasswordSecret());
}
async function verifyResetPasswordToken(token: string): Promise<number> {
const { payload } = await jose.jwtVerify(token, getResetPasswordSecret(), {
issuer: "urn:lajosh:buddy",
audience: "urn:buddy:password-reset",
});
if (payload.type !== "password_reset" || typeof payload.id !== "number") {
throw new Error("Invalid reset token payload");
}
return payload.id;
}
router.post("/resetpassword", async (req, res) => {
const requestResetParsed = ResetPasswordRequestSchema.safeParse(req.body);
if (requestResetParsed.success) {
const { email, link } = requestResetParsed.data;
logger.info({ email }, "Password reset requested");
const existingUser = (
await db.select().from(users).where(eq(users.email, email)).limit(1)
)[0];
if (!existingUser) {
logger.info({ email }, "Password reset requested for unknown email");
return res.send({
success: true,
reason: "",
});
}
try {
const token = await signResetPasswordToken(existingUser.id);
const separator = link.includes("?") ? "&" : "?";
const resetLink = `${link}${separator}token=${encodeURIComponent(token)}`;
await getTransporter().sendMail({
from: `"Buddy 🐶" <${process.env.SMTP_EMAIL}>`,
to: email,
subject: "Buddy password reset",
text: `Your Buddy password reset link is ${resetLink}`,
});
logger.info(
{ email, userId: existingUser.id },
"Password reset email sent",
);
return res.send({
success: true,
reason: "",
});
} catch (error) {
logger.error({ error, email }, "Failed to send password reset email");
return res.status(500).send({
success: false,
reason: "Failed to send password reset email",
});
}
}
const confirmResetParsed = ResetPasswordConfirmSchema.safeParse(req.body);
if (!confirmResetParsed.success) {
logger.warn(
{ error: confirmResetParsed.error },
"Reset password validation failed",
);
return res.status(400).send({
success: false,
reason: confirmResetParsed.error,
});
}
try {
const userId = await verifyResetPasswordToken(confirmResetParsed.data.token);
const hashedPassword = await argon2.hash(confirmResetParsed.data.password);
const updatedUsers = await db
.update(users)
.set({ password: hashedPassword })
.where(eq(users.id, userId))
.returning({ id: users.id });
if (updatedUsers.length === 0) {
logger.warn({ userId }, "Password reset failed: user not found");
return res.status(400).send({
success: false,
reason: "Invalid or expired reset token",
});
}
logger.info({ userId }, "Password reset completed successfully");
return res.send({
success: true,
reason: "",
});
} catch (error) {
logger.warn({ error }, "Password reset failed: invalid token");
return res.status(400).send({
success: false,
reason: "Invalid or expired reset token",
});
}
});
router.post("/signin", async (req, res) => {
const body: Infer<typeof SigninBodySchema> = req.body;
logger.info({ email: body.email }, "Signin attempt initiated");
const parsed = SigninBodySchema.safeParse(body);
if (!parsed.success) {
logger.warn(
{ email: body.email, error: parsed.error },
"Signin validation failed",
);
res.send({
success: false,
reason: parsed.error,
token: "",
});
return;
}
const existingUser = (
await db.select().from(users).where(eq(users.email, body.email)).limit(1)
)[0];
if (!existingUser) {
logger.warn({ email: body.email }, "Signin failed: user not found");
res.send({
success: false,
reason: "Invalid email or password",
});
return;
}
const validPassword = await argon2.verify(
existingUser.password,
body.password,
);
if (!validPassword) {
logger.warn(
{ email: body.email, userId: existingUser.id },
"Signin failed: invalid password",
);
res.send({
success: false,
reason: "Invalid email or password",
});
return;
}
const jwt = await signJwt(
{ id: existingUser.id, type: "parent" },
"urn:buddy:users",
);
logger.info(
{ userId: existingUser.id, email: body.email },
"User signin successful",
);
res.send({
success: true,
token: jwt,
reason: "",
});
});
router.post("/kid/link", async (req, res) => {
const body: Infer<typeof SigninBodySchema> = req.body;
const parsed = SigninBodySchema.safeParse(body);
if (!parsed.success) {
logger.warn({ error: parsed.error }, "Kid link validation failed");
res.send({
success: false,
reason: parsed.error,
token: "",
});
return;
}
logger.info({ email: parsed.data.email }, "Kid link request initiated");
const existingUser = (
await db
.select()
.from(users)
.where(eq(users.email, parsed.data.email))
.limit(1)
)[0];
if (!existingUser) {
logger.warn(
{ email: parsed.data.email },
"Kid link failed: user not found",
);
res.send({
success: false,
reason: "Invalid email or password",
});
return;
}
logger.debug({ email: parsed.data.email }, "User found for kid link");
const validPassword = await argon2.verify(
existingUser.password,
parsed.data.password,
);
if (!validPassword) {
res.send({
success: false,
reason: "Invalid email or password",
});
return;
}
if (!existingUser.emailVerified) {
res.send({
success: false,
reason: "You must verify your email in the parent app before using Buddy",
});
return;
}
const newDevice = (
await db
.insert(linkedDevices)
.values({ parentId: existingUser.id })
.returning({ id: linkedDevices.id })
)[0];
const jwt = await signJwt(
{ id: newDevice!.id, type: "child" },
"urn:buddy:devices",
);
logger.info(
{ deviceId: newDevice!.id, parentId: existingUser.id },
"New child device linked successfully",
);
res.send({
success: true,
token: jwt,
reason: "",
});
});
export default router;
|