-
Notifications
You must be signed in to change notification settings - Fork 217
Expand file tree
/
Copy pathindex.ts
More file actions
269 lines (231 loc) · 7.88 KB
/
index.ts
File metadata and controls
269 lines (231 loc) · 7.88 KB
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
import dotenv from "dotenv";
import express from "express";
import type { Request, Response } from "express";
import * as trpcExpress from "@trpc/server/adapters/express";
import { appRouter } from "./routers/_app.js";
import { createContext } from "./context.js";
import prismaModule from "./prisma.js";
import cors from "cors";
import type { CorsOptions as CorsOptionsType } from "cors";
import rateLimit from "express-rate-limit";
import helmet from "helmet";
import ipBlocker from "./middleware/ipBlock.js";
import crypto from "crypto";
import { paymentService } from "./services/payment.service.js";
import { verifyToken } from "./utils/auth.js";
import { SUBSCRIPTION_STATUS } from "./constants/subscription.js";
dotenv.config();
const app = express();
const PORT = process.env.PORT || 4000;
const CORS_ORIGINS = process.env.CORS_ORIGINS
? process.env.CORS_ORIGINS.split(",")
: ["http://localhost:3000", "http://localhost:5000"];
// Security headers
app.use(helmet());
app.use(
helmet.contentSecurityPolicy({
directives: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'", "'unsafe-inline'"],
styleSrc: ["'self'", "'unsafe-inline'"],
imgSrc: ["'self'", "data:", "https:"],
},
})
);
// Apply IP blocking middleware first
app.use(ipBlocker.middleware);
// Different rate limits for different endpoints
const authLimiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 5,
message: "Too many login attempts, please try again later",
standardHeaders: true,
legacyHeaders: false,
});
const apiLimiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 100,
message: "Too many requests from this IP",
standardHeaders: true,
legacyHeaders: false,
handler: (req, res) => {
console.log(`[RATE LIMIT] IP ${req.ip} hit API rate limit`);
res.status(429).json({ error: "Too many requests from this IP" });
}
});
// Request size limits (except for webhook - needs raw body)
app.use("/webhook/razorpay", express.raw({ type: "application/json" }));
app.use(express.json({ limit: "10kb" }));
app.use(express.urlencoded({ limit: "10kb", extended: true }));
// CORS configuration
const corsOptions: CorsOptionsType = {
origin: (origin, callback) => {
if (!origin || CORS_ORIGINS.includes(origin)) {
callback(null, origin);
} else {
callback(new Error("Not allowed by CORS"));
}
},
methods: ["GET", "POST"],
allowedHeaders: ["Content-Type", "Authorization"],
credentials: true,
maxAge: 86400, // 24 hours
};
app.use(cors(corsOptions));
// Blocked IPs endpoint (admin endpoint)
app.get("/admin/blocked-ips", (req: Request, res: Response) => {
const blockedIPs = ipBlocker.getBlockedIPs();
res.json({
blockedIPs: blockedIPs.map((ip) => ({
...ip,
blockedUntil: new Date(ip.blockedUntil).toISOString(),
})),
});
});
// Test endpoint
app.get("/test", apiLimiter, (req: Request, res: Response) => {
res.status(200).json({ status: "ok", message: "Test endpoint is working" });
});
// Slack Community Invite Endpoint (Protected)
app.get("/join-community", apiLimiter, async (req: Request, res: Response) => {
try {
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith("Bearer ")) {
return res.status(401).json({
error: "Unauthorized - Authorization header with Bearer token required",
});
}
const token = authHeader.substring(7);
let user;
try {
user = await verifyToken(token);
} catch (error) {
return res.status(401).json({ error: "Unauthorized - Invalid token" });
}
if (!user.isPaidUser || !user.subscription) {
return res.status(403).json({
error: "Forbidden - Active subscription required to join community",
});
}
// Get Slack invite URL from environment
const slackInviteUrl = process.env.SLACK_INVITE_URL;
if (!slackInviteUrl) {
console.error("SLACK_INVITE_URL not configured");
return res.status(500).json({ error: "Community invite not configured" });
}
return res.status(200).json({
slackInviteUrl,
message: "Subscription verified. You can join the community.",
});
} catch (error: any) {
console.error("Community invite error:", error);
return res.status(500).json({ error: "Internal server error" });
}
});
// Razorpay Webhook Handler (Backup Flow)
app.post("/webhook/razorpay", async (req: Request, res: Response) => {
try {
const webhookSecret = process.env.RAZORPAY_WEBHOOK_SECRET;
if (!webhookSecret) {
console.error("RAZORPAY_WEBHOOK_SECRET not configured");
return res.status(500).json({ error: "Webhook not configured" });
}
// Get signature from headers
const signature = req.headers["x-razorpay-signature"] as string;
if (!signature) {
return res.status(400).json({ error: "Missing signature" });
}
// Verify webhook signature
const body = req.body.toString();
const expectedSignature = crypto
.createHmac("sha256", webhookSecret)
.update(body)
.digest("hex");
const isValidSignature = crypto.timingSafeEqual(
Buffer.from(signature),
Buffer.from(expectedSignature)
);
if (!isValidSignature) {
console.error("Invalid webhook signature");
return res.status(400).json({ error: "Invalid signature" });
}
// Parse the event
const event = JSON.parse(body);
const eventType = event.event;
// Handle payment.captured event
if (eventType === "payment.captured") {
const payment = event.payload.payment.entity;
// Extract payment details
const razorpayPaymentId = payment.id;
const razorpayOrderId = payment.order_id;
const amount = payment.amount;
const currency = payment.currency;
// Get user ID from order notes (should be stored when creating order)
const notes = payment.notes || {};
const userId = notes.user_id;
if (!userId) {
console.error("User ID not found in payment notes");
return res.status(400).json({ error: "User ID not found" });
}
// Get plan ID from notes
const planId = notes.plan_id;
if (!planId) {
console.error("Plan ID not found in payment notes");
return res.status(400).json({ error: "Plan ID not found" });
}
try {
// Create payment record (with idempotency check)
const paymentRecord = await paymentService.createPaymentRecord(userId, {
razorpayPaymentId,
razorpayOrderId,
amount,
currency,
});
// Create subscription (with idempotency check)
await paymentService.createSubscription(
userId,
planId,
paymentRecord.id
);
console.log(
`✅ Webhook: Payment ${razorpayPaymentId} processed successfully`
);
return res.status(200).json({ status: "ok" });
} catch (error: any) {
console.error("Webhook payment processing error:", error);
// Return 200 to prevent Razorpay retries for application errors
return res
.status(200)
.json({ status: "ok", note: "Already processed" });
}
}
// Acknowledge other events
return res.status(200).json({ status: "ok" });
} catch (error: any) {
console.error("Webhook error:", error);
return res.status(500).json({ error: "Internal server error" });
}
});
// Connect to database
prismaModule.connectDB();
// Apply rate limiting to tRPC endpoints
app.use("/trpc", apiLimiter);
// tRPC middleware
app.use(
"/trpc",
trpcExpress.createExpressMiddleware({
router: appRouter,
createContext,
})
);
// Global error handling
app.use((err: Error, req: Request, res: Response, next: Function) => {
console.error(err.stack);
res.status(500).json({
error: "Internal Server Error",
message: process.env.NODE_ENV === "development" ? err.message : undefined,
});
});
app.listen(PORT, () => {
console.log(`tRPC server running on http://localhost:${PORT}`);
});