Skip to content

Commit a90dbba

Browse files
fix(auth): improve API key validation and authorization handling
feat(pulse-notify): add token support in event configuration and URL generation
1 parent 82f9875 commit a90dbba

3 files changed

Lines changed: 13 additions & 10 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ dist
44
build
55
.next
66
**/.env.local
7-
**/.env.development.local
7+
**/.env.development.local
8+
.claude

apps/server/src/auth.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ export function requireApiKey(req: Request, res: Response, next: NextFunction):
88
return;
99
}
1010

11+
// Accept key via Authorization header (REST) or ?token= query param (EventSource/SSE)
1112
const authHeader = req.headers["authorization"];
12-
if (!authHeader || !authHeader.startsWith("Bearer ")) {
13-
res.status(401).json({ error: "Unauthorized" });
14-
return;
15-
}
13+
const headerKey = authHeader?.startsWith("Bearer ") ? authHeader.slice("Bearer ".length) : null;
14+
const queryKey = typeof req.query.token === "string" ? req.query.token : null;
15+
const provided = headerKey ?? queryKey;
1616

17-
const provided = authHeader.slice("Bearer ".length);
18-
if (provided !== apiKey) {
17+
if (!provided || provided !== apiKey) {
1918
res.status(401).json({ error: "Unauthorized" });
2019
return;
2120
}

packages/pulse-notify/src/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export type UseEventConfig = {
77
serverUrl: string;
88
address: string;
99
event?: string; // defaults to "*" — all events
10+
/** API key forwarded as ?token= query param — required when the server has authentication enabled */
11+
token?: string;
1012
};
1113

1214
export type EventState = {
@@ -26,8 +28,9 @@ export function useStellarEvent(config: UseEventConfig): EventState {
2628
});
2729

2830
useEffect(() => {
29-
const { serverUrl, address, event: eventType = "*" } = config;
30-
const url = `${serverUrl}/events/${address}`;
31+
const { serverUrl, address, event: eventType = "*", token } = config;
32+
const base = `${serverUrl}/events/${address}`;
33+
const url = token ? `${base}?token=${encodeURIComponent(token)}` : base;
3134

3235
const source = new EventSource(url);
3336

@@ -60,7 +63,7 @@ export function useStellarEvent(config: UseEventConfig): EventState {
6063
return () => {
6164
source.close();
6265
};
63-
}, [config.serverUrl, config.address, config.event]);
66+
}, [config.serverUrl, config.address, config.event, config.token]);
6467

6568
return state;
6669
}

0 commit comments

Comments
 (0)