Skip to content

Commit a65b82f

Browse files
authored
feat: add optional authentication logic for worker (#331)
Signed-off-by: Ravindra Meena <rmeena840@gmail.com>
1 parent f3de548 commit a65b82f

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

packages/btcindexer/src/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,26 @@ export { RPCMock } from "./rpc-mock";
1818

1919
const router = new HttpRouter(undefined);
2020

21+
/**
22+
* Validates the Authorization header against the AUTH_BEARER_TOKEN env var.
23+
*/
24+
function isAuthorized(req: Request, env: Env): boolean {
25+
// If the token isn't set in the environment, we assume the endpoint is public
26+
if (!env.AUTH_BEARER_TOKEN) return true;
27+
28+
const authHeader = req.headers.get("Authorization");
29+
if (!authHeader || !authHeader.startsWith("Bearer ")) return false;
30+
31+
const token = authHeader.substring(7);
32+
return token === env.AUTH_BEARER_TOKEN;
33+
}
34+
2135
export default {
2236
async fetch(req: Request, env: Env, _ctx: ExecutionContext): Promise<Response> {
2337
try {
38+
if (!isAuthorized(req, env)) {
39+
return new Response("Unauthorized", { status: 401 });
40+
}
2441
const indexer = await indexerFromEnv(env);
2542
return await router.fetch(req, env, indexer);
2643
} catch (e) {

packages/btcindexer/worker-configuration.d.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable */
2-
// Generated by Wrangler by running `wrangler types` (hash: d1762921ce9ce60578e7e61df0f8e97d)
2+
// Generated by Wrangler by running `wrangler types` (hash: 0f6ea38d8b9ed234c528c82214007cdc)
33
// Runtime types generated with workerd@1.20251210.0 2025-06-20 nodejs_compat
44
declare namespace Cloudflare {
55
interface GlobalProps {
@@ -11,6 +11,7 @@ declare namespace Cloudflare {
1111
CONFIRMATION_DEPTH: "4";
1212
MAX_NBTC_MINT_TX_RETRIES: "1";
1313
BTC_BLOCK_PROCESSING_BATCH_SIZE: "100";
14+
AUTH_BEARER_TOKEN: "";
1415
DB: D1Database;
1516
NBTC_MINTING_SIGNER_MNEMONIC: SecretsStoreSecret;
1617
SuiIndexer: Fetcher /* sui-indexer */;
@@ -21,7 +22,7 @@ type StringifyValues<EnvType extends Record<string, unknown>> = {
2122
[Binding in keyof EnvType]: EnvType[Binding] extends string ? EnvType[Binding] : string;
2223
};
2324
declare namespace NodeJS {
24-
interface ProcessEnv extends StringifyValues<Pick<Cloudflare.Env, "CONFIRMATION_DEPTH" | "MAX_NBTC_MINT_TX_RETRIES" | "BTC_BLOCK_PROCESSING_BATCH_SIZE">> {}
25+
interface ProcessEnv extends StringifyValues<Pick<Cloudflare.Env, "CONFIRMATION_DEPTH" | "MAX_NBTC_MINT_TX_RETRIES" | "BTC_BLOCK_PROCESSING_BATCH_SIZE" | "AUTH_BEARER_TOKEN">> {}
2526
}
2627

2728
// Begin runtime types
@@ -9383,7 +9384,7 @@ interface IncomingRequestCfPropertiesTLSClientAuthPlaceholder {
93839384
certNotAfter: "";
93849385
}
93859386
/** Possible outcomes of TLS verification */
9386-
declare type CertVerificationStatus =
9387+
declare type CertVerificationStatus =
93879388
/** Authentication succeeded */
93889389
"SUCCESS"
93899390
/** No certificate was presented */
@@ -9447,7 +9448,7 @@ interface D1ExecResult {
94479448
count: number;
94489449
duration: number;
94499450
}
9450-
type D1SessionConstraint =
9451+
type D1SessionConstraint =
94519452
// Indicates that the first query should go to the primary, and the rest queries
94529453
// using the same D1DatabaseSession will go to any replica that is consistent with
94539454
// the bookmark maintained by the session (returned by the first query).
@@ -10028,7 +10029,7 @@ declare namespace Rpc {
1002810029
// The reason for using a generic type here is to build a serializable subset of structured
1002910030
// cloneable composite types. This allows types defined with the "interface" keyword to pass the
1003010031
// serializable check as well. Otherwise, only types defined with the "type" keyword would pass.
10031-
type Serializable<T> =
10032+
type Serializable<T> =
1003210033
// Structured cloneables
1003310034
BaseType
1003410035
// Structured cloneable composites

packages/btcindexer/wrangler.jsonc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"CONFIRMATION_DEPTH": "4",
3535
"MAX_NBTC_MINT_TX_RETRIES": "1",
3636
"BTC_BLOCK_PROCESSING_BATCH_SIZE": "100",
37+
"AUTH_BEARER_TOKEN": "",
3738
},
3839
"secrets_store_secrets": [
3940
{

0 commit comments

Comments
 (0)