Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
@@ -0,0 +1,47 @@
|
||||
import { createHash, timingSafeEqual } from "node:crypto";
|
||||
import type { ApiConfig } from "@toolctrl/config";
|
||||
|
||||
export type ServicePrincipal = {
|
||||
type: "service-token";
|
||||
};
|
||||
|
||||
export function extractBearerToken(request: Request) {
|
||||
return request.headers.get("authorization")?.match(/^Bearer\s+(.+)$/i)?.[1];
|
||||
}
|
||||
|
||||
export function extractApiKeyFromRequest(request: Request, body: Record<string, unknown> | undefined) {
|
||||
if (typeof body?.apiKey === "string" && body.apiKey) {
|
||||
return body.apiKey;
|
||||
}
|
||||
|
||||
return request.headers.get("x-api-key");
|
||||
}
|
||||
|
||||
export function isValidServiceToken(token: string, configOrToken: Pick<ApiConfig, "apiAccessToken"> | string | undefined) {
|
||||
const configuredToken = typeof configOrToken === "string" ? configOrToken : configOrToken?.apiAccessToken;
|
||||
if (!configuredToken || !token) {
|
||||
return false;
|
||||
}
|
||||
// Constant-time comparison: hash both sides to a fixed length first so
|
||||
// timingSafeEqual never throws on length mismatch and the comparison leaks
|
||||
// neither the token contents nor its length.
|
||||
const provided = createHash("sha256").update(token).digest();
|
||||
const expected = createHash("sha256").update(configuredToken).digest();
|
||||
return timingSafeEqual(provided, expected);
|
||||
}
|
||||
|
||||
export function createServicePrincipal(): ServicePrincipal {
|
||||
return {
|
||||
type: "service-token",
|
||||
};
|
||||
}
|
||||
|
||||
export function authenticateServiceRequest(request: Request, configOrToken: Pick<ApiConfig, "apiAccessToken"> | string | undefined) {
|
||||
const token = extractBearerToken(request);
|
||||
|
||||
if (!token || !isValidServiceToken(token, configOrToken)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return createServicePrincipal();
|
||||
}
|
||||
Reference in new issue
Block a user