2026-10-23 19:56:38 -07:00
|
|
|
import jwt from "jsonwebtoken";
|
2026-10-23 11:43:49 -05:00
|
|
|
|
|
|
|
|
export interface ClientTokenPayload {
|
|
|
|
|
clientId: string;
|
|
|
|
|
name?: string;
|
|
|
|
|
rateLimit?: number;
|
|
|
|
|
iat?: number;
|
|
|
|
|
exp?: number;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-18 05:18:55 +06:00
|
|
|
const DEV_DEFAULT_JWT_SECRET =
|
2026-11-16 21:58:18 +06:00
|
|
|
"datastack-jwt-default-secret-key-32-chars-long";
|
2026-10-23 11:43:49 -05:00
|
|
|
|
2026-11-18 05:18:55 +06:00
|
|
|
export function resolveDefaultJwtSecret(): string {
|
|
|
|
|
const configured = process.env.DATASTACK_JWT_SECRET?.trim() || "";
|
|
|
|
|
if (process.env.NODE_ENV === "production") {
|
|
|
|
|
return configured.length >= 32 ? configured : "";
|
|
|
|
|
}
|
|
|
|
|
if (configured.length > 0) {
|
|
|
|
|
return configured.length >= 32 ? configured : "";
|
|
|
|
|
}
|
|
|
|
|
return DEV_DEFAULT_JWT_SECRET;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-23 11:43:49 -05:00
|
|
|
export class AuthService {
|
2026-11-18 05:18:55 +06:00
|
|
|
private static secret: string = resolveDefaultJwtSecret();
|
2026-10-23 11:43:49 -05:00
|
|
|
|
|
|
|
|
public static setSecret(newSecret: string) {
|
2026-11-18 05:18:55 +06:00
|
|
|
const trimmed = newSecret ? newSecret.trim() : "";
|
|
|
|
|
if (trimmed.length >= 32) {
|
|
|
|
|
this.secret = trimmed;
|
|
|
|
|
} else if (process.env.NODE_ENV !== "production" && trimmed.length > 0) {
|
|
|
|
|
this.secret = trimmed;
|
|
|
|
|
} else if (process.env.NODE_ENV === "production") {
|
|
|
|
|
this.secret = "";
|
2026-10-23 11:43:49 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static getSecret(): string {
|
|
|
|
|
return this.secret;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-18 05:18:55 +06:00
|
|
|
public static isConfigured(): boolean {
|
|
|
|
|
const minLen = process.env.NODE_ENV === "production" ? 32 : 1;
|
|
|
|
|
return this.secret.length >= minLen;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-23 11:43:49 -05:00
|
|
|
/**
|
|
|
|
|
* Generates a signed JWT for client SDK authentication.
|
|
|
|
|
*/
|
2026-10-23 19:56:38 -07:00
|
|
|
public static generateClientToken(
|
|
|
|
|
payload: { clientId: string; name?: string; rateLimit?: number },
|
|
|
|
|
expiresIn = "30d",
|
|
|
|
|
): string {
|
2026-11-18 05:18:55 +06:00
|
|
|
if (!this.isConfigured()) {
|
2026-10-23 19:56:38 -07:00
|
|
|
throw new Error(
|
2026-11-18 05:18:55 +06:00
|
|
|
"DATASTACK_JWT_SECRET (>= 32 characters) must be configured before generating client tokens.",
|
2026-10-23 19:56:38 -07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-23 11:43:49 -05:00
|
|
|
return jwt.sign(
|
|
|
|
|
{
|
|
|
|
|
clientId: payload.clientId,
|
|
|
|
|
name: payload.name || payload.clientId,
|
|
|
|
|
rateLimit: payload.rateLimit || 100,
|
|
|
|
|
},
|
|
|
|
|
this.secret,
|
2026-10-23 19:56:38 -07:00
|
|
|
{ expiresIn: expiresIn as jwt.SignOptions["expiresIn"] },
|
2026-10-23 11:43:49 -05:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-11-18 05:18:55 +06:00
|
|
|
* Verifies and decodes incoming token from Socket.IO handshake or HTTP ingestion.
|
2026-10-23 11:43:49 -05:00
|
|
|
*/
|
2026-10-23 19:56:38 -07:00
|
|
|
public static verifyClientToken(token: string): {
|
|
|
|
|
valid: boolean;
|
|
|
|
|
payload?: ClientTokenPayload;
|
|
|
|
|
error?: string;
|
|
|
|
|
} {
|
2026-11-18 05:18:55 +06:00
|
|
|
if (!this.isConfigured()) {
|
|
|
|
|
return {
|
|
|
|
|
valid: false,
|
|
|
|
|
error: "DATASTACK_JWT_SECRET (>= 32 characters) is not configured.",
|
|
|
|
|
};
|
2026-10-23 19:56:38 -07:00
|
|
|
}
|
|
|
|
|
|
2026-10-23 11:43:49 -05:00
|
|
|
try {
|
2026-10-23 19:56:38 -07:00
|
|
|
const cleanToken = token.startsWith("Bearer ")
|
|
|
|
|
? token.slice(7).trim()
|
|
|
|
|
: token.trim();
|
2026-10-23 11:43:49 -05:00
|
|
|
const decoded = jwt.verify(cleanToken, this.secret) as ClientTokenPayload;
|
|
|
|
|
return { valid: true, payload: decoded };
|
|
|
|
|
} catch (err: unknown) {
|
2026-10-23 19:56:38 -07:00
|
|
|
const message = err instanceof Error ? err.message : "Invalid token";
|
2026-10-23 11:43:49 -05:00
|
|
|
return { valid: false, error: message };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default AuthService;
|