chore: publish current DataStack updates
Publish the current application, design, and Phase 1 deployment updates. Excludes ignored personal notes, local MCP config, and generated TypeScript build info.
This commit is contained in:
1 parent
d39d9f92f9
commit
fcc4f4db2d
88 files changed
+21248
-2213
No files matched your search
@@ -0,0 +1,73 @@
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
interface AdminSessionPayload {
|
||||
email: string;
|
||||
expiresAt: number;
|
||||
}
|
||||
|
||||
function encodeBase64Url(bytes: Uint8Array) {
|
||||
let binary = "";
|
||||
for (let index = 0; index < bytes.length; index += 1) {
|
||||
binary += String.fromCharCode(bytes[index]);
|
||||
}
|
||||
return btoa(binary).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
|
||||
}
|
||||
|
||||
function decodeBase64Url(value: string) {
|
||||
const base64 = value.replace(/-/g, "+").replace(/_/g, "/");
|
||||
const padded = base64.padEnd(Math.ceil(base64.length / 4) * 4, "=");
|
||||
const binary = atob(padded);
|
||||
return Uint8Array.from(binary, (character) => character.charCodeAt(0));
|
||||
}
|
||||
|
||||
async function importKey(secret: string) {
|
||||
return crypto.subtle.importKey(
|
||||
"raw",
|
||||
encoder.encode(secret),
|
||||
{ name: "HMAC", hash: "SHA-256" },
|
||||
false,
|
||||
["sign", "verify"],
|
||||
);
|
||||
}
|
||||
|
||||
export async function createAdminSessionToken(
|
||||
email: string,
|
||||
secret: string,
|
||||
expiresAt: number,
|
||||
) {
|
||||
const payload = encodeBase64Url(
|
||||
encoder.encode(JSON.stringify({ email, expiresAt })),
|
||||
);
|
||||
const signature = await crypto.subtle.sign(
|
||||
"HMAC",
|
||||
await importKey(secret),
|
||||
encoder.encode(payload),
|
||||
);
|
||||
return `${payload}.${encodeBase64Url(new Uint8Array(signature))}`;
|
||||
}
|
||||
|
||||
export async function verifyAdminSessionToken(token: string, secret: string) {
|
||||
const [payload, signature, extra] = token.split(".");
|
||||
if (!payload || !signature || extra) return false;
|
||||
|
||||
try {
|
||||
const valid = await crypto.subtle.verify(
|
||||
"HMAC",
|
||||
await importKey(secret),
|
||||
decodeBase64Url(signature),
|
||||
encoder.encode(payload),
|
||||
);
|
||||
if (!valid) return false;
|
||||
|
||||
const session = JSON.parse(
|
||||
new TextDecoder().decode(decodeBase64Url(payload)),
|
||||
) as AdminSessionPayload;
|
||||
return (
|
||||
typeof session.email === "string" &&
|
||||
typeof session.expiresAt === "number" &&
|
||||
session.expiresAt > Date.now()
|
||||
);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+26
-7
@@ -1,4 +1,4 @@
|
||||
import jwt from 'jsonwebtoken';
|
||||
import jwt from "jsonwebtoken";
|
||||
|
||||
export interface ClientTokenPayload {
|
||||
clientId: string;
|
||||
@@ -8,7 +8,7 @@ export interface ClientTokenPayload {
|
||||
exp?: number;
|
||||
}
|
||||
|
||||
const DEFAULT_SECRET = process.env.DATASTACK_JWT_SECRET || 'datastack-universal-secret-key-2026';
|
||||
const DEFAULT_SECRET = process.env.DATASTACK_JWT_SECRET || "";
|
||||
|
||||
export class AuthService {
|
||||
private static secret: string = DEFAULT_SECRET;
|
||||
@@ -26,7 +26,16 @@ export class AuthService {
|
||||
/**
|
||||
* Generates a signed JWT for client SDK authentication.
|
||||
*/
|
||||
public static generateClientToken(payload: { clientId: string; name?: string; rateLimit?: number }, expiresIn = '30d'): string {
|
||||
public static generateClientToken(
|
||||
payload: { clientId: string; name?: string; rateLimit?: number },
|
||||
expiresIn = "30d",
|
||||
): string {
|
||||
if (!this.secret) {
|
||||
throw new Error(
|
||||
"DATASTACK_JWT_SECRET must be configured before generating client tokens.",
|
||||
);
|
||||
}
|
||||
|
||||
return jwt.sign(
|
||||
{
|
||||
clientId: payload.clientId,
|
||||
@@ -34,20 +43,30 @@ export class AuthService {
|
||||
rateLimit: payload.rateLimit || 100,
|
||||
},
|
||||
this.secret,
|
||||
{ expiresIn: expiresIn as jwt.SignOptions['expiresIn'] }
|
||||
{ expiresIn: expiresIn as jwt.SignOptions["expiresIn"] },
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies and decodes incoming token from Socket.IO handshake.
|
||||
*/
|
||||
public static verifyClientToken(token: string): { valid: boolean; payload?: ClientTokenPayload; error?: string } {
|
||||
public static verifyClientToken(token: string): {
|
||||
valid: boolean;
|
||||
payload?: ClientTokenPayload;
|
||||
error?: string;
|
||||
} {
|
||||
if (!this.secret) {
|
||||
return { valid: false, error: "DATASTACK_JWT_SECRET is not configured." };
|
||||
}
|
||||
|
||||
try {
|
||||
const cleanToken = token.startsWith('Bearer ') ? token.slice(7).trim() : token.trim();
|
||||
const cleanToken = token.startsWith("Bearer ")
|
||||
? token.slice(7).trim()
|
||||
: token.trim();
|
||||
const decoded = jwt.verify(cleanToken, this.secret) as ClientTokenPayload;
|
||||
return { valid: true, payload: decoded };
|
||||
} catch (err: unknown) {
|
||||
const message = err instanceof Error ? err.message : 'Invalid token';
|
||||
const message = err instanceof Error ? err.message : "Invalid token";
|
||||
return { valid: false, error: message };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user