Skip to content

Commit 89427fc

Browse files
authored
Merge pull request #101 from MeshJS/API
2 parents 8ce8c13 + cf82bc0 commit 89427fc

File tree

14 files changed

+380
-119
lines changed

14 files changed

+380
-119
lines changed

package-lock.json

Lines changed: 132 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"geist": "^1.3.0",
6262
"idb-keyval": "^6.2.1",
6363
"jsonld": "^8.3.3",
64+
"jsonwebtoken": "^9.0.2",
6465
"lucide-react": "^0.439.0",
6566
"next": "^14.2.4",
6667
"next-auth": "^4.24.7",
@@ -86,6 +87,7 @@
8687
"@types/eslint": "^8.56.10",
8788
"@types/formidable": "^3.4.5",
8889
"@types/jsonld": "^1.5.15",
90+
"@types/jsonwebtoken": "^9.0.9",
8991
"@types/node": "^20.14.10",
9092
"@types/react": "^18.3.3",
9193
"@types/react-dom": "^18.3.0",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- CreateTable
2+
CREATE TABLE "Nonce" (
3+
"id" TEXT NOT NULL,
4+
"address" TEXT NOT NULL,
5+
"value" TEXT NOT NULL,
6+
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
7+
8+
CONSTRAINT "Nonce_pkey" PRIMARY KEY ("id")
9+
);
10+
11+
-- CreateIndex
12+
CREATE UNIQUE INDEX "Nonce_address_key" ON "Nonce"("address");

prisma/schema.prisma

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,10 @@ model NewWallet {
7272
numRequiredSigners Int?
7373
ownerAddress String
7474
}
75+
76+
model Nonce {
77+
id String @id @default(cuid())
78+
address String @unique
79+
value String
80+
createdAt DateTime @default(now())
81+
}

src/lib/verifyJwt.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { verify } from "jsonwebtoken";
2+
3+
export function verifyJwt(token: string): { address: string } | null {
4+
const secret = process.env.JWT_SECRET;
5+
if (!secret) throw new Error("JWT_SECRET not set");
6+
7+
try {
8+
const payload = verify(token, secret) as { address: string };
9+
return payload;
10+
} catch (err) {
11+
return null;
12+
}
13+
}

src/pages/api/v1/README.md

Lines changed: 0 additions & 66 deletions
This file was deleted.

src/pages/api/v1/addTransaction.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
import type { NextApiRequest, NextApiResponse } from "next";
2-
import { getServerAuthSession } from "@/server/auth";
32
import { db } from "@/server/db";
3+
import { verifyJwt } from "@/lib/verifyJwt";
44

55
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
66
if (req.method !== "POST") {
77
return res.status(405).json({ error: "Method Not Allowed" });
88
}
99

10-
const session = await getServerAuthSession({ req, res });
11-
if (!session || !session.user) {
12-
return res.status(401).json({ error: "Unauthorized." });
10+
const authHeader = req.headers.authorization;
11+
const token = authHeader?.startsWith("Bearer ") ? authHeader.slice(7) : null;
12+
13+
if (!token) {
14+
return res.status(401).json({ error: "Unauthorized - Missing token" });
15+
}
16+
17+
const payload = verifyJwt(token);
18+
if (!payload) {
19+
return res.status(401).json({ error: "Invalid or expired token" });
1320
}
1421

22+
const session = {
23+
user: { id: payload.address },
24+
expires: new Date(Date.now() + 60 * 60 * 1000).toISOString(),
25+
};
26+
1527

1628
//JSON.stringify(txBuilder.meshTxBuilderBody),
1729

@@ -26,6 +38,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
2638
if (!address ) {
2739
return res.status(400).json({ error: "Missing required field address!" });
2840
}
41+
// Optionally check that the address matches the session user.id for security
42+
if (session.user.id !== address) {
43+
return res.status(403).json({ error: "Address mismatch" });
44+
}
2945
if (!txCbor ) {
3046
return res.status(400).json({ error: "Missing required field txCbor!" });
3147
}

0 commit comments

Comments
 (0)