-
-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
172 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,63 @@ | ||
import { FastifyRequest } from "fastify"; | ||
import jwt from "jsonwebtoken"; | ||
import { prisma } from "../prisma"; | ||
|
||
// Checks session token and returns user object | ||
export async function checkSession(request: any) { | ||
const token = request.headers.authorization!.split(" ")[1]; | ||
export async function checkSession(request: FastifyRequest) { | ||
try { | ||
const bearer = request.headers.authorization?.split(" ")[1]; | ||
if (!bearer) { | ||
return null; | ||
} | ||
|
||
let session = await prisma.session.findUnique({ | ||
where: { | ||
sessionToken: token, | ||
}, | ||
}); | ||
// Verify JWT token is valid | ||
var b64string = process.env.SECRET; | ||
var secret = Buffer.from(b64string!, "base64"); | ||
|
||
let user = await prisma.user.findUnique({ | ||
where: { id: session!.userId }, | ||
}); | ||
try { | ||
jwt.verify(bearer, secret); | ||
} catch (e) { | ||
// Token is invalid or expired | ||
await prisma.session.delete({ | ||
where: { sessionToken: bearer }, | ||
}); | ||
return null; | ||
} | ||
|
||
return user; | ||
// Check if session exists and is not expired | ||
const session = await prisma.session.findUnique({ | ||
where: { sessionToken: bearer }, | ||
include: { user: true }, | ||
}); | ||
|
||
if (!session || session.expires < new Date()) { | ||
// Session expired or doesn't exist | ||
if (session) { | ||
await prisma.session.delete({ | ||
where: { id: session.id }, | ||
}); | ||
} | ||
return null; | ||
} | ||
|
||
// Verify the request is coming from the same client | ||
const currentUserAgent = request.headers["user-agent"]; | ||
const currentIp = request.ip; | ||
|
||
if ( | ||
session.userAgent !== currentUserAgent && | ||
session.ipAddress !== currentIp | ||
) { | ||
// Potential session hijacking attempt - invalidate the session | ||
await prisma.session.delete({ | ||
where: { id: session.id }, | ||
}); | ||
|
||
return null; | ||
} | ||
|
||
return session.user; | ||
} catch (error) { | ||
return null; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
apps/api/src/prisma/migrations/20241114164206_sessions/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-- AlterTable | ||
ALTER TABLE "Session" ADD COLUMN "apiKey" BOOLEAN NOT NULL DEFAULT false, | ||
ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
ADD COLUMN "ipAddress" TEXT, | ||
ADD COLUMN "userAgent" TEXT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters