sitepaige-mcp-server 1.2.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/components/slideshow.tsx +1 -9
  2. package/defaultapp/api/Auth/resend-verification/route.ts +2 -2
  3. package/defaultapp/api/Auth/route.ts +2 -2
  4. package/defaultapp/api/Auth/signup/route.ts +2 -2
  5. package/defaultapp/api/Auth/verify-email/route.ts +3 -3
  6. package/defaultapp/api/admin/users/route.ts +3 -3
  7. package/dist/components/slideshow.tsx +1 -9
  8. package/dist/defaultapp/api/Auth/resend-verification/route.ts +2 -2
  9. package/dist/defaultapp/api/Auth/route.ts +2 -2
  10. package/dist/defaultapp/api/Auth/signup/route.ts +2 -2
  11. package/dist/defaultapp/api/Auth/verify-email/route.ts +3 -3
  12. package/dist/defaultapp/api/admin/users/route.ts +3 -3
  13. package/dist/defaultapp/api/csrf.ts +111 -0
  14. package/dist/defaultapp/api/db-mysql.ts +183 -0
  15. package/dist/defaultapp/api/db-password-auth.ts +362 -0
  16. package/dist/defaultapp/api/db-postgres.ts +189 -0
  17. package/dist/defaultapp/api/db-sqlite.ts +335 -0
  18. package/dist/defaultapp/api/db-users.ts +520 -0
  19. package/dist/defaultapp/api/db.ts +149 -0
  20. package/dist/defaultapp/api/storage/email.ts +162 -0
  21. package/dist/defaultapp/api/storage/files.ts +160 -0
  22. package/dist/generators/skeleton.js +3 -5
  23. package/dist/generators/skeleton.js.map +1 -1
  24. package/dist/generators/sql.js +60 -0
  25. package/dist/generators/sql.js.map +1 -1
  26. package/package.json +1 -1
  27. package/defaultapp/api/example-secure/route.ts +0 -100
  28. package/defaultapp/migrate.ts +0 -142
  29. /package/defaultapp/{csrf.ts → api/csrf.ts} +0 -0
  30. /package/defaultapp/{db-mysql.ts → api/db-mysql.ts} +0 -0
  31. /package/defaultapp/{db-password-auth.ts → api/db-password-auth.ts} +0 -0
  32. /package/defaultapp/{db-postgres.ts → api/db-postgres.ts} +0 -0
  33. /package/defaultapp/{db-sqlite.ts → api/db-sqlite.ts} +0 -0
  34. /package/defaultapp/{db-users.ts → api/db-users.ts} +0 -0
  35. /package/defaultapp/{db.ts → api/db.ts} +0 -0
  36. /package/defaultapp/{storage → api/storage}/email.ts +0 -0
  37. /package/defaultapp/{storage → api/storage}/files.ts +0 -0
@@ -0,0 +1,362 @@
1
+ /*
2
+ * Username/Password Authentication Database Utilities
3
+ * Handles all database operations related to username/password authentication
4
+ */
5
+
6
+ import { db_init, db_query } from './db';
7
+ import type { DatabaseClient } from './db';
8
+ import * as crypto from 'node:crypto';
9
+ import { scrypt, randomBytes } from 'node:crypto';
10
+ import { promisify } from 'node:util';
11
+
12
+ const scryptAsync = promisify(scrypt);
13
+
14
+ export interface PasswordAuth {
15
+ id: string;
16
+ email: string; // Email serves as username
17
+ passwordhash: string;
18
+ salt: string;
19
+ verificationtoken?: string;
20
+ verificationtokenexpires?: string;
21
+ emailverified: boolean;
22
+ resettoken?: string;
23
+ resettokenexpires?: string;
24
+ createdat: string;
25
+ updatedat: string;
26
+ }
27
+
28
+ /**
29
+ * Hash a password using scrypt
30
+ */
31
+ export async function hashPassword(password: string): Promise<{ hash: string; salt: string }> {
32
+ const salt = randomBytes(32).toString('base64');
33
+ const hash = (await scryptAsync(password, salt, 64)) as Buffer;
34
+ return {
35
+ hash: hash.toString('base64'),
36
+ salt
37
+ };
38
+ }
39
+
40
+ /**
41
+ * Verify a password against its hash
42
+ */
43
+ export async function verifyPassword(password: string, hash: string, salt: string): Promise<boolean> {
44
+ const derivedKey = (await scryptAsync(password, salt, 64)) as Buffer;
45
+ return derivedKey.toString('base64') === hash;
46
+ }
47
+
48
+ /**
49
+ * Create the password authentication table
50
+ */
51
+ export async function createPasswordAuthTable(): Promise<void> {
52
+ const client = await db_init();
53
+
54
+ const createTableQuery = `
55
+ CREATE TABLE IF NOT EXISTS passwordauth (
56
+ id TEXT PRIMARY KEY,
57
+ email TEXT NOT NULL UNIQUE,
58
+ passwordhash TEXT NOT NULL,
59
+ salt TEXT NOT NULL,
60
+ verificationtoken TEXT,
61
+ verificationtokenexpires TIMESTAMP,
62
+ emailverified BOOLEAN DEFAULT FALSE,
63
+ resettoken TEXT,
64
+ resettokenexpires TIMESTAMP,
65
+ createdat TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
66
+ updatedat TIMESTAMP DEFAULT CURRENT_TIMESTAMP
67
+ )
68
+ `;
69
+
70
+ await db_query(client, createTableQuery);
71
+
72
+ // Create index on email for faster lookups
73
+ await db_query(client, 'CREATE INDEX IF NOT EXISTS idx_passwordauth_email ON passwordauth(email)');
74
+ await db_query(client, 'CREATE INDEX IF NOT EXISTS idx_passwordauth_verification_token ON passwordauth(verificationtoken)');
75
+ await db_query(client, 'CREATE INDEX IF NOT EXISTS idx_passwordauth_reset_token ON passwordauth(resettoken)');
76
+ }
77
+
78
+ /**
79
+ * Get password auth record by email
80
+ */
81
+ export async function getPasswordAuthByEmail(email: string): Promise<PasswordAuth | null> {
82
+ const client = await db_init();
83
+
84
+ const results = await db_query(client,
85
+ "SELECT * FROM passwordauth WHERE email = ?",
86
+ [email.toLowerCase()]
87
+ );
88
+
89
+ return results.length > 0 ? results[0] as PasswordAuth : null;
90
+ }
91
+
92
+ /**
93
+ * Create a new password auth record (for signup)
94
+ */
95
+ export async function createPasswordAuth(
96
+ email: string,
97
+ password: string
98
+ ): Promise<{ passwordAuth: PasswordAuth; verificationToken: string }> {
99
+ const client = await db_init();
100
+
101
+ // Check if email already exists
102
+ const existing = await getPasswordAuthByEmail(email);
103
+ if (existing) {
104
+ throw new Error('Email already registered');
105
+ }
106
+
107
+ // Hash the password
108
+ const { hash, salt } = await hashPassword(password);
109
+
110
+ // Generate verification token
111
+ const verificationToken = randomBytes(32).toString('base64url');
112
+ const verificationTokenExpires = new Date(Date.now() + 24 * 60 * 60 * 1000); // 24 hours
113
+
114
+ const id = crypto.randomUUID();
115
+ const now = new Date().toISOString();
116
+
117
+ await db_query(client,
118
+ `INSERT INTO passwordauth
119
+ (id, email, passwordhash, salt, verificationtoken, verificationtokenexpires, emailverified, createdat, updatedat)
120
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
121
+ [id, email.toLowerCase(), hash, salt, verificationToken, verificationTokenExpires.toISOString(), false, now, now]
122
+ );
123
+
124
+ const passwordAuth = await getPasswordAuthByEmail(email);
125
+ if (!passwordAuth) {
126
+ throw new Error('Failed to create password auth record');
127
+ }
128
+
129
+ return { passwordAuth, verificationToken };
130
+ }
131
+
132
+ /**
133
+ * Verify email with token
134
+ */
135
+ export async function verifyEmailWithToken(token: string): Promise<PasswordAuth | null> {
136
+ const client = await db_init();
137
+
138
+ // Find the auth record with this token
139
+ const results = await db_query(client,
140
+ `SELECT * FROM passwordauth
141
+ WHERE verificationtoken = ?
142
+ AND verificationtokenexpires > CURRENT_TIMESTAMP
143
+ AND emailverified = false`,
144
+ [token]
145
+ );
146
+
147
+ if (results.length === 0) {
148
+ return null;
149
+ }
150
+
151
+ const authRecord = results[0];
152
+
153
+ // Mark email as verified and clear token
154
+ await db_query(client,
155
+ `UPDATE passwordauth
156
+ SET emailverified = true,
157
+ verificationtoken = NULL,
158
+ verificationtokenexpires = NULL,
159
+ updatedat = CURRENT_TIMESTAMP
160
+ WHERE id = ?`,
161
+ [authRecord.id]
162
+ );
163
+
164
+ return await getPasswordAuthByEmail(authRecord.email);
165
+ }
166
+
167
+ /**
168
+ * Authenticate user with email and password
169
+ */
170
+ export async function authenticateUser(email: string, password: string): Promise<PasswordAuth | null> {
171
+ const authRecord = await getPasswordAuthByEmail(email);
172
+
173
+ if (!authRecord) {
174
+ return null;
175
+ }
176
+
177
+ // Check if email is verified
178
+ if (!authRecord.emailverified) {
179
+ throw new Error('Email not verified');
180
+ }
181
+
182
+ // Verify password
183
+ const isValid = await verifyPassword(password, authRecord.passwordhash, authRecord.salt);
184
+
185
+ if (!isValid) {
186
+ return null;
187
+ }
188
+
189
+ return authRecord;
190
+ }
191
+
192
+ /**
193
+ * Generate password reset token
194
+ */
195
+ export async function generatePasswordResetToken(email: string): Promise<string | null> {
196
+ const client = await db_init();
197
+
198
+ const authRecord = await getPasswordAuthByEmail(email);
199
+ if (!authRecord) {
200
+ return null;
201
+ }
202
+
203
+ const resetToken = randomBytes(32).toString('base64url');
204
+ const resetTokenExpires = new Date(Date.now() + 60 * 60 * 1000); // 1 hour
205
+
206
+ await db_query(client,
207
+ `UPDATE passwordauth
208
+ SET resettoken = ?,
209
+ resettokenexpires = ?,
210
+ updatedat = CURRENT_TIMESTAMP
211
+ WHERE id = ?`,
212
+ [resetToken, resetTokenExpires.toISOString(), authRecord.id]
213
+ );
214
+
215
+ return resetToken;
216
+ }
217
+
218
+ /**
219
+ * Reset password with token
220
+ */
221
+ export async function resetPasswordWithToken(token: string, newPassword: string): Promise<boolean> {
222
+ const client = await db_init();
223
+
224
+ // Find the auth record with this token
225
+ const results = await db_query(client,
226
+ `SELECT * FROM passwordauth
227
+ WHERE resettoken = ?
228
+ AND resettokenexpires > CURRENT_TIMESTAMP`,
229
+ [token]
230
+ );
231
+
232
+ if (results.length === 0) {
233
+ return false;
234
+ }
235
+
236
+ const authRecord = results[0];
237
+
238
+ // Hash the new password
239
+ const { hash, salt } = await hashPassword(newPassword);
240
+
241
+ // Update password and clear reset token
242
+ await db_query(client,
243
+ `UPDATE passwordauth
244
+ SET passwordhash = ?,
245
+ salt = ?,
246
+ resettoken = NULL,
247
+ resettokenexpires = NULL,
248
+ updatedat = CURRENT_TIMESTAMP
249
+ WHERE id = ?`,
250
+ [hash, salt, authRecord.id]
251
+ );
252
+
253
+ return true;
254
+ }
255
+
256
+ /**
257
+ * Delete unverified accounts older than 7 days
258
+ */
259
+ export async function cleanupUnverifiedAccounts(): Promise<number> {
260
+ const client = await db_init();
261
+
262
+ const result = await db_query(client,
263
+ `DELETE FROM passwordauth
264
+ WHERE emailverified = false
265
+ AND createdat < datetime('now', '-7 days')`
266
+ );
267
+
268
+ return result[0]?.changes || 0;
269
+ }
270
+
271
+ /**
272
+ * Update password for authenticated user
273
+ */
274
+ export async function updatePassword(email: string, currentPassword: string, newPassword: string): Promise<boolean> {
275
+ // First verify the current password
276
+ const authRecord = await authenticateUser(email, currentPassword);
277
+ if (!authRecord) {
278
+ return false;
279
+ }
280
+
281
+ // Hash the new password
282
+ const { hash, salt } = await hashPassword(newPassword);
283
+ const client = await db_init();
284
+
285
+ // Update password
286
+ await db_query(client,
287
+ `UPDATE passwordauth
288
+ SET passwordhash = ?,
289
+ salt = ?,
290
+ updatedat = CURRENT_TIMESTAMP
291
+ WHERE id = ?`,
292
+ [hash, salt, authRecord.id]
293
+ );
294
+
295
+ return true;
296
+ }
297
+
298
+ /**
299
+ * Regenerate email verification token for unverified accounts
300
+ */
301
+ export async function regenerateVerificationToken(email: string): Promise<{ passwordAuth: PasswordAuth; verificationToken: string } | null> {
302
+ const client = await db_init();
303
+
304
+ const authRecord = await getPasswordAuthByEmail(email);
305
+ if (!authRecord) {
306
+ return null;
307
+ }
308
+
309
+ // Only regenerate for unverified accounts
310
+ if (authRecord.emailverified) {
311
+ throw new Error('Email is already verified');
312
+ }
313
+
314
+ // Generate new verification token
315
+ const verificationToken = randomBytes(32).toString('base64url');
316
+ const verificationTokenExpires = new Date(Date.now() + 24 * 60 * 60 * 1000); // 24 hours
317
+
318
+ await db_query(client,
319
+ `UPDATE passwordauth
320
+ SET verificationtoken = ?,
321
+ verificationtokenexpires = ?,
322
+ updatedat = CURRENT_TIMESTAMP
323
+ WHERE id = ?`,
324
+ [verificationToken, verificationTokenExpires.toISOString(), authRecord.id]
325
+ );
326
+
327
+ const updatedAuth = await getPasswordAuthByEmail(email);
328
+ if (!updatedAuth) {
329
+ throw new Error('Failed to update verification token');
330
+ }
331
+
332
+ return { passwordAuth: updatedAuth, verificationToken };
333
+ }
334
+
335
+ /**
336
+ * Check if an email is already registered
337
+ */
338
+ export async function isEmailRegistered(email: string): Promise<boolean> {
339
+ const authRecord = await getPasswordAuthByEmail(email);
340
+ return authRecord !== null;
341
+ }
342
+
343
+ /**
344
+ * Get statistics about password auth users
345
+ */
346
+ export async function getPasswordAuthStats(): Promise<{
347
+ totalUsers: number;
348
+ verifiedUsers: number;
349
+ unverifiedUsers: number;
350
+ }> {
351
+ const client = await db_init();
352
+
353
+ const stats = await db_query(client, `
354
+ SELECT
355
+ COUNT(*) as totalUsers,
356
+ SUM(CASE WHEN emailverified = true THEN 1 ELSE 0 END) as verifiedUsers,
357
+ SUM(CASE WHEN emailverified = false THEN 1 ELSE 0 END) as unverifiedUsers
358
+ FROM passwordauth
359
+ `);
360
+
361
+ return stats[0];
362
+ }
@@ -0,0 +1,189 @@
1
+ /*
2
+ Sitepaige v1.0.0
3
+ PostgreSQL database implementation
4
+ WARNING: This file is automatically generated and should not be modified.
5
+ */
6
+
7
+ import { Client, Pool } from 'pg';
8
+ import type { Model, ModelField } from './db';
9
+
10
+ // Connection pool for better performance
11
+ let pool: Pool | null = null;
12
+
13
+ /**
14
+ * Initialize database connection
15
+ * @returns Database client (Pool)
16
+ */
17
+ export async function db_init(): Promise<Pool> {
18
+
19
+ try {
20
+ // Create connection configuration
21
+ const poolConfig: any = {};
22
+
23
+ // Read configuration from environment variables
24
+ poolConfig.host = process.env.DB_HOST || 'localhost';
25
+ poolConfig.port = parseInt(process.env.DB_PORT || '5432');
26
+ poolConfig.user = process.env.DB_USER || 'postgres';
27
+ poolConfig.password = process.env.DB_PASSWORD;
28
+ poolConfig.database = process.env.DB_NAME || 'app';
29
+
30
+ // SSL configuration for secure connections (e.g., AWS RDS)
31
+ // Always enable SSL for PostgreSQL connections
32
+ poolConfig.ssl = {
33
+ rejectUnauthorized: false, // For self-signed certificates
34
+ require: true // Equivalent to sslmode=require
35
+ };
36
+
37
+ // Additional pool configuration
38
+ poolConfig.max = 20; // Maximum number of clients in the pool
39
+ poolConfig.idleTimeoutMillis = 30000;
40
+ poolConfig.connectionTimeoutMillis = 2000;
41
+
42
+ // Create the pool
43
+ pool = new Pool(poolConfig);
44
+
45
+
46
+ return pool;
47
+ } catch (error) {
48
+ console.error(error);
49
+ return null;
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Execute a SQL query using the provided PostgreSQL client
55
+ * @param client Database client from db_init()
56
+ * @param query SQL query string (PostgreSQL syntax with $1, $2... parameter placeholders)
57
+ * @param params Optional array of parameters for the query
58
+ * @returns Array of selected rows or execution results
59
+ */
60
+ export async function db_query(
61
+ client: Pool,
62
+ query: string,
63
+ params?: any[]
64
+ ): Promise<any[]> {
65
+
66
+ try {
67
+ // Convert SQLite ? placeholders to PostgreSQL $1, $2, etc.
68
+ let pgQuery = query;
69
+ let paramIndex = 1;
70
+ while (pgQuery.includes('?')) {
71
+ pgQuery = pgQuery.replace('?', `$${paramIndex}`);
72
+ paramIndex++;
73
+ }
74
+
75
+ // Execute the query
76
+ const result = await client.query(pgQuery, params);
77
+
78
+ // Return rows for SELECT queries, or affected rows info for others
79
+ if (result.command === 'SELECT') {
80
+ return result.rows;
81
+ } else {
82
+ return [{
83
+ changes: result.rowCount,
84
+ command: result.command
85
+ }];
86
+ }
87
+
88
+ } catch (error) {
89
+ // If table doesn't exist for SELECT, return empty array
90
+ console.error(error);
91
+ return [];
92
+
93
+ }
94
+ }
95
+
96
+ /**
97
+ * Generates a CREATE TABLE SQL string for the specified table and fields
98
+ * @param model The model definition
99
+ * @param dbType Database type
100
+ * @returns SQL string for creating the table
101
+ */
102
+ export function db_migrate(model: Model, dbType: string): string {
103
+
104
+ const sanitizedTableName = model.name.toLowerCase().replace(/\s+/g, '_');
105
+
106
+ // Start with the model's fields
107
+ let fields = [...model.fields];
108
+
109
+ // Add userid field if data is user specific
110
+ if (model.data_is_user_specific === "true") {
111
+ fields.push({
112
+ name: 'userid',
113
+ datatype: 'UUID',
114
+ datatypesize: null,
115
+ key: 'foreign',
116
+ required: 'true',
117
+ default: undefined
118
+ });
119
+ }
120
+
121
+ // Map SQL data types for PostgreSQL
122
+ const sqlTypeMap: { [key: string]: string } = {
123
+ 'UUID': 'UUID',
124
+ 'TINYINT': 'SMALLINT',
125
+ 'SMALLINT': 'SMALLINT',
126
+ 'BIGINT': 'BIGINT',
127
+ 'INT128': 'NUMERIC(39,0)',
128
+ 'VARCHAR': 'VARCHAR',
129
+ 'TEXT': 'TEXT',
130
+ 'BINARY': 'BYTEA',
131
+ 'DATE': 'DATE',
132
+ 'TIME': 'TIME',
133
+ 'DATETIME': 'TIMESTAMP',
134
+ 'DOUBLE': 'DOUBLE PRECISION',
135
+ 'FLOAT': 'REAL',
136
+ 'BOOLEAN': 'BOOLEAN'
137
+ };
138
+
139
+ // Build column definitions
140
+ const columnDefs = fields.sort((a, b) => (b.key === 'primary' ? 1 : 0) - (a.key === 'primary' ? 1 : 0)).map(field => {
141
+ const dataType = field.datatype.toUpperCase();
142
+ let sqlType = sqlTypeMap[dataType] || dataType;
143
+
144
+ // Add size for VARCHAR
145
+ if (dataType === 'VARCHAR' && field.datatypesize) {
146
+ sqlType = `VARCHAR(${field.datatypesize})`;
147
+ }
148
+
149
+ let def = `"${field.name}" ${sqlType}`;
150
+
151
+ // Add NOT NULL if required
152
+ if (field.required === "true") {
153
+ def += ' NOT NULL';
154
+ }
155
+
156
+ // Add default if specified
157
+ if (field.default !== undefined) {
158
+ if (field.datatype === 'VARCHAR' || field.datatype === 'TEXT') {
159
+ def += ` DEFAULT '${field.default}'`;
160
+ } else if (field.datatype === 'BOOLEAN') {
161
+ def += ` DEFAULT ${field.default === 'true' || field.default === true}`;
162
+ } else {
163
+ def += ` DEFAULT ${field.default}`;
164
+ }
165
+ }
166
+
167
+ // Add primary key constraint
168
+ if (field.key === 'primary') {
169
+ def += ' PRIMARY KEY';
170
+ }
171
+
172
+ return def;
173
+ }).join(',\n ');
174
+
175
+ // Add foreign key constraints at the end
176
+ const foreignKeys: string[] = [];
177
+ if (model.data_is_user_specific === "true") {
178
+ foreignKeys.push(`FOREIGN KEY ("userid") REFERENCES "users" ("userid")`);
179
+ }
180
+
181
+ const allConstraints = foreignKeys.length > 0
182
+ ? columnDefs + ',\n ' + foreignKeys.join(',\n ')
183
+ : columnDefs;
184
+
185
+ // Build the CREATE TABLE statement
186
+ const sql = `CREATE TABLE IF NOT EXISTS "${sanitizedTableName}" (\n ${allConstraints}\n);`;
187
+
188
+ return sql;
189
+ }