Skip to content
This repository was archived by the owner on Mar 18, 2026. It is now read-only.

Commit 4bdb2fc

Browse files
committed
feat: Implement Bearer token authentication for /post-event endpoint
1 parent 235537e commit 4bdb2fc

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

src/durable-objects/chainhooks-do.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ export class ChainhooksDO extends DurableObject<Env> {
5050
});
5151
}
5252

53+
// Check authentication
54+
if (!this.validateAuthToken(request)) {
55+
throw new ApiError(ErrorCode.UNAUTHORIZED, {
56+
reason: 'Invalid or missing authentication token',
57+
});
58+
}
59+
5360
return await this.handlePostEvent(request);
5461
}
5562

@@ -187,4 +194,26 @@ export class ChainhooksDO extends DurableObject<Env> {
187194
});
188195
}
189196
}
197+
198+
/**
199+
* Validates the authentication token from the request
200+
*
201+
* @param request - The incoming request
202+
* @returns boolean indicating if the token is valid
203+
*/
204+
private validateAuthToken(request: Request): boolean {
205+
// Extract the Authorization header
206+
const authHeader = request.headers.get('Authorization');
207+
208+
// Check if the header exists and has the correct format
209+
if (!authHeader || !authHeader.startsWith('Bearer ')) {
210+
return false;
211+
}
212+
213+
// Extract the token
214+
const token = authHeader.replace('Bearer ', '');
215+
216+
// Compare with the stored token
217+
return token === this.env.CHAINHOOKS_AUTH_TOKEN;
218+
}
190219
}

worker-configuration.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export interface Env {
55
HIRO_API_KEY: string;
66
SUPABASE_URL: string;
77
SUPABASE_SERVICE_KEY: string;
8+
CHAINHOOKS_AUTH_TOKEN: string; // Auth token for chainhooks POST endpoint
89
BNS_API_DO: DurableObjectNamespace<import('./src/index').BnsApiDO>;
910
HIRO_API_DO: DurableObjectNamespace<import('./src/index').HiroApiDO>;
1011
STX_CITY_DO: DurableObjectNamespace<import('./src/index').StxCityDO>;

0 commit comments

Comments
 (0)