Files
DataStack/core/auth/jwt.ts
T

100 lines
2.7 KiB
TypeScript
Raw Normal View History

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;
}
const DEV_DEFAULT_JWT_SECRET =
"datastack-jwt-default-secret-key-32-chars-long";
2026-10-23 11:43:49 -05: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 {
private static secret: string = resolveDefaultJwtSecret();
2026-10-23 11:43:49 -05:00
public static setSecret(newSecret: string) {
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;
}
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 {
if (!this.isConfigured()) {
2026-10-23 19:56:38 -07:00
throw new Error(
"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
);
}
/**
* 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;
} {
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;