Skip to content

Commit

Permalink
feat: Improve verification token security
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-dalmet committed Sep 26, 2023
1 parent 1ba870a commit 6632675
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
23 changes: 16 additions & 7 deletions src/server/api/routers/auth.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { TRPCError } from '@trpc/server';
import bcrypt from 'bcrypt';
import { randomUUID } from 'crypto';
import dayjs from 'dayjs';
import jwt from 'jsonwebtoken';
import { cookies } from 'next/headers';
Expand All @@ -10,7 +9,7 @@ import EmailActivateAccount from '@/emails/templates/activate-account';
import EmailResetPassword from '@/emails/templates/reset-password';
import i18n from '@/lib/i18n/server';
import { createTRPCRouter, publicProcedure } from '@/server/api/trpc';
import { AUTH_COOKIE_NAME } from '@/server/auth';
import { AUTH_COOKIE_NAME, decodeJwt } from '@/server/auth';
import { prismaThrowFormatedTRPCError } from '@/server/db';
import { sendEmail } from '@/server/email';

Expand Down Expand Up @@ -122,7 +121,7 @@ export const authRouter = createTRPCRouter({
});
}

const token = randomUUID();
const token = jwt.sign({ id: user.id }, process.env.AUTH_SECRET);
await ctx.db.verificationToken.create({
data: {
userId: user.id,
Expand Down Expand Up @@ -170,8 +169,13 @@ export const authRouter = createTRPCRouter({
throw new TRPCError({ code: 'BAD_REQUEST' });
}

const jwtDecoded = decodeJwt(input.token);
if (!jwtDecoded?.id) {
throw new TRPCError({ code: 'BAD_REQUEST' });
}

const verificationToken = await ctx.db.verificationToken.findUnique({
where: { token: input.token },
where: { token: input.token, userId: jwtDecoded.id },
});

if (!verificationToken) {
Expand Down Expand Up @@ -206,8 +210,6 @@ export const authRouter = createTRPCRouter({
.input(z.object({ email: z.string().email() }))
.output(z.void())
.mutation(async ({ ctx, input }) => {
const token = randomUUID();

const user = await ctx.db.user.findFirst({
where: { email: input.email },
});
Expand All @@ -217,6 +219,8 @@ export const authRouter = createTRPCRouter({
return undefined;
}

const token = jwt.sign({ id: user.id }, process.env.AUTH_SECRET);

await ctx.db.verificationToken.create({
data: {
userId: user.id,
Expand Down Expand Up @@ -257,8 +261,13 @@ export const authRouter = createTRPCRouter({
where: { expires: { lt: new Date() } },
});

const jwtDecoded = decodeJwt(input.token);
if (!jwtDecoded?.id) {
throw new TRPCError({ code: 'BAD_REQUEST' });
}

const verificationToken = await ctx.db.verificationToken.findUnique({
where: { token: input.token },
where: { token: input.token, userId: jwtDecoded.id },
});

if (!verificationToken) {
Expand Down
13 changes: 11 additions & 2 deletions src/server/auth.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TRPCError } from '@trpc/server';
import jwt from 'jsonwebtoken';
import { cookies, headers } from 'next/headers';

Expand All @@ -19,9 +20,9 @@ export const getServerAuthSession = async () => {
return null;
}

const jwtDecoded = jwt.verify(token, process.env.AUTH_SECRET);
const jwtDecoded = decodeJwt(token);

if (!jwtDecoded || typeof jwtDecoded !== 'object' || !('id' in jwtDecoded)) {
if (!jwtDecoded?.id) {
return null;
}

Expand All @@ -38,3 +39,11 @@ export const getServerAuthSession = async () => {
},
});
};

export const decodeJwt = (token: string) => {
const jwtDecoded = jwt.verify(token, process.env.AUTH_SECRET);
if (!jwtDecoded || typeof jwtDecoded !== 'object' || !('id' in jwtDecoded)) {
return null;
}
return jwtDecoded;
};

0 comments on commit 6632675

Please sign in to comment.