feat(endorser): add client helper to check JWT via rateLimits
Introduce checkAuth so callers can verify Endorser acceptance of a JWT without dealing with HTTP details.
This commit is contained in:
44
src/services/endorserClient.ts
Normal file
44
src/services/endorserClient.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { ENDORSER_URL } from "../env.js";
|
||||
import { errorMessage } from "../util/formatElapsed.js";
|
||||
|
||||
const RATE_LIMITS_PATH = "/api/report/rateLimits";
|
||||
|
||||
function rateLimitsUrl(): string {
|
||||
const base = ENDORSER_URL.replace(/\/+$/, "");
|
||||
return `${base}${RATE_LIMITS_PATH}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirms a JWT is accepted by the Endorser server.
|
||||
*
|
||||
* Calls GET /api/report/rateLimits with the JWT as a Bearer token.
|
||||
* Returns true on success, false on auth or request failure.
|
||||
* Does not expose HTTP status or response bodies to callers.
|
||||
*/
|
||||
export async function checkAuth(jwt: string): Promise<boolean> {
|
||||
let response: Response;
|
||||
try {
|
||||
response = await fetch(rateLimitsUrl(), {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${jwt}`,
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(
|
||||
"[Endorser] Auth check request failed:",
|
||||
errorMessage(err)
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
console.error(
|
||||
"[Endorser] Auth check failed with status",
|
||||
response.status
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user