Spaces:
Running
Running
Isitha Tennakoon
commited on
Commit
·
e0c5640
1
Parent(s):
0383459
Key gen backend works
Browse files
keygenServer/server/.dockerignore
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
node_modules
|
2 |
+
dist
|
3 |
+
.env
|
4 |
+
.git
|
5 |
+
.gitignore
|
6 |
+
.dockerignore
|
7 |
+
Dockerfile
|
8 |
+
README.md
|
keygenServer/server/Dockerfile
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Build stage
|
2 |
+
FROM node:20-alpine AS builder
|
3 |
+
|
4 |
+
WORKDIR /app
|
5 |
+
|
6 |
+
# Copy package files
|
7 |
+
COPY package*.json ./
|
8 |
+
COPY prisma ./prisma/
|
9 |
+
|
10 |
+
# Install dependencies
|
11 |
+
RUN npm ci
|
12 |
+
RUN npx prisma generate
|
13 |
+
|
14 |
+
# Copy source code
|
15 |
+
COPY . .
|
16 |
+
|
17 |
+
# Build TypeScript code
|
18 |
+
RUN npm run build
|
19 |
+
|
20 |
+
# Production stage
|
21 |
+
FROM node:20-alpine AS runner
|
22 |
+
|
23 |
+
WORKDIR /app
|
24 |
+
|
25 |
+
# Set to production environment
|
26 |
+
ENV NODE_ENV=production
|
27 |
+
|
28 |
+
# Copy necessary files from builder
|
29 |
+
COPY --from=builder /app/package*.json ./
|
30 |
+
COPY --from=builder /app/dist ./dist
|
31 |
+
COPY --from=builder /app/prisma ./prisma
|
32 |
+
COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma
|
33 |
+
COPY --from=builder /app/node_modules/@prisma ./node_modules/@prisma
|
34 |
+
|
35 |
+
# Install production dependencies only
|
36 |
+
RUN npm ci --omit=dev
|
37 |
+
|
38 |
+
# Expose the port your server runs on
|
39 |
+
EXPOSE 5000
|
40 |
+
|
41 |
+
# Start the application
|
42 |
+
CMD ["npm", "start"]
|
keygenServer/server/prisma_client.ts
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { PrismaClient } from '@prisma/client'
|
2 |
+
|
3 |
+
const prisma = new PrismaClient()
|
4 |
+
|
5 |
+
export default prisma
|
keygenServer/server/server.ts
CHANGED
@@ -1,11 +1,14 @@
|
|
1 |
-
import express from 'express';
|
2 |
import cors from 'cors';
|
3 |
import dotenv from 'dotenv';
|
|
|
|
|
|
|
4 |
|
5 |
dotenv.config();
|
6 |
|
7 |
const app = express();
|
8 |
-
const port = process.env.PORT ||
|
9 |
|
10 |
// Whitelist of allowed client origins
|
11 |
const allowedOrigins = [
|
@@ -33,19 +36,82 @@ const openCorsOptions = {
|
|
33 |
credentials: true
|
34 |
};
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
// Middleware
|
37 |
app.use(express.json());
|
38 |
|
|
|
|
|
|
|
|
|
|
|
39 |
// Public route example (no CORS restrictions)
|
40 |
-
app.
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
// Protected route with strict CORS and authentication
|
45 |
app.post('/api/addKey',
|
46 |
cors(strictCorsOptions),
|
47 |
-
(req, res) => {
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
}
|
50 |
);
|
51 |
|
|
|
1 |
+
import express, { Request, Response } from 'express';
|
2 |
import cors from 'cors';
|
3 |
import dotenv from 'dotenv';
|
4 |
+
import prisma from './prisma_client';
|
5 |
+
import crypto from 'crypto';
|
6 |
+
|
7 |
|
8 |
dotenv.config();
|
9 |
|
10 |
const app = express();
|
11 |
+
const port = process.env.PORT || 3001;
|
12 |
|
13 |
// Whitelist of allowed client origins
|
14 |
const allowedOrigins = [
|
|
|
36 |
credentials: true
|
37 |
};
|
38 |
|
39 |
+
app.use(cors({
|
40 |
+
origin: allowedOrigins,
|
41 |
+
credentials: true,
|
42 |
+
methods: ['GET', 'POST', 'OPTIONS']
|
43 |
+
}));
|
44 |
+
|
45 |
// Middleware
|
46 |
app.use(express.json());
|
47 |
|
48 |
+
|
49 |
+
function generateApiKey(): string {
|
50 |
+
return crypto.randomBytes(32).toString('hex');
|
51 |
+
}
|
52 |
+
|
53 |
// Public route example (no CORS restrictions)
|
54 |
+
app.post('/api/verifyKey',
|
55 |
+
cors(openCorsOptions),
|
56 |
+
async (req: Request, res: Response) => {
|
57 |
+
try {
|
58 |
+
const { apiKey } = req.body;
|
59 |
+
|
60 |
+
if (!apiKey) {
|
61 |
+
res.status(400).json({ error: 'API key is required' });
|
62 |
+
return;
|
63 |
+
}
|
64 |
+
|
65 |
+
const key = await prisma.key.findFirst({
|
66 |
+
where: {
|
67 |
+
apiKey
|
68 |
+
}
|
69 |
+
});
|
70 |
+
|
71 |
+
if (!key) {
|
72 |
+
res.status(401).json({ valid: false });
|
73 |
+
return;
|
74 |
+
}
|
75 |
+
else {
|
76 |
+
res.json({ valid: true });
|
77 |
+
}
|
78 |
+
|
79 |
+
} catch (error) {
|
80 |
+
console.error('Error verifying API key:', error);
|
81 |
+
res.status(500).json({ error: 'Failed to verify API key' });
|
82 |
+
}
|
83 |
+
}
|
84 |
+
);
|
85 |
|
86 |
// Protected route with strict CORS and authentication
|
87 |
app.post('/api/addKey',
|
88 |
cors(strictCorsOptions),
|
89 |
+
async (req: Request, res: Response): Promise<void> => {
|
90 |
+
try {
|
91 |
+
const { email } = req.body;
|
92 |
+
|
93 |
+
if (!email) {
|
94 |
+
res.status(400).json({ error: 'Email is required' });
|
95 |
+
return;
|
96 |
+
}
|
97 |
+
|
98 |
+
const apiKey = generateApiKey();
|
99 |
+
|
100 |
+
const newKey = await prisma.key.create({
|
101 |
+
data: {
|
102 |
+
email,
|
103 |
+
apiKey,
|
104 |
+
}
|
105 |
+
});
|
106 |
+
|
107 |
+
res.json({
|
108 |
+
message: 'API key generated successfully',
|
109 |
+
apiKey: newKey.apiKey
|
110 |
+
});
|
111 |
+
} catch (error) {
|
112 |
+
console.error('Error creating API key:', error);
|
113 |
+
res.status(500).json({ error: 'Failed to create API key' });
|
114 |
+
}
|
115 |
}
|
116 |
);
|
117 |
|
keygenServer/server/tsconfig.json
CHANGED
@@ -3,7 +3,7 @@
|
|
3 |
"target": "es2017",
|
4 |
"module": "commonjs",
|
5 |
"outDir": "./dist",
|
6 |
-
"rootDir": "./
|
7 |
"strict": true,
|
8 |
"esModuleInterop": true,
|
9 |
"skipLibCheck": true,
|
|
|
3 |
"target": "es2017",
|
4 |
"module": "commonjs",
|
5 |
"outDir": "./dist",
|
6 |
+
"rootDir": "./",
|
7 |
"strict": true,
|
8 |
"esModuleInterop": true,
|
9 |
"skipLibCheck": true,
|