Skip to content

Commit 67cf523

Browse files
authored
fix: API key authentication and REST handler errors (#446)
1 parent 0b45cef commit 67cf523

4 files changed

Lines changed: 18 additions & 14 deletions

File tree

apps/docs/api-reference/introduction.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ https://kan.bn/api/v1
1111

1212
## Authentication
1313

14-
Most endpoints require authentication using your API key. You can create one in the [settings page](https://kan.bn/settings) of your account. Include this key in the `x-api-key` header of each request.
14+
Most endpoints require authentication using your API key. You can create one in the [settings page](https://kan.bn/settings) of your account. Include this key as a Bearer token in the `Authorization` header of each request.
1515

1616
```
17-
'x-api-key': kan_123456789
17+
'Authorization': 'Bearer kan_123456789'
1818
```
1919

2020
## Response codes

packages/api/src/routers/integration.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { encryptToken } from "../utils/encryption";
1919
export const integrationRouter = createTRPCRouter({
2020
saveGitHubToken: protectedProcedure
2121
.input(z.object({ token: z.string() }))
22+
.output(z.object({ success: z.boolean() }))
2223
.mutation(async ({ ctx, input }) => {
2324
const user = ctx.user;
2425

@@ -43,7 +44,9 @@ export const integrationRouter = createTRPCRouter({
4344
return { success: true };
4445
}),
4546

46-
disconnectGitHub: protectedProcedure.mutation(async ({ ctx }) => {
47+
disconnectGitHub: protectedProcedure
48+
.output(z.object({ success: z.boolean() }))
49+
.mutation(async ({ ctx }) => {
4750
const user = ctx.user;
4851

4952
if (!user)
@@ -56,7 +59,9 @@ export const integrationRouter = createTRPCRouter({
5659
return { success: true };
5760
}),
5861

59-
getGitHubStatus: protectedProcedure.query(async ({ ctx }) => {
62+
getGitHubStatus: protectedProcedure
63+
.output(z.object({ connected: z.boolean() }))
64+
.query(async ({ ctx }) => {
6065
const user = ctx.user;
6166

6267
if (!user)

packages/api/src/trpc.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,7 @@ const loggingMiddleware = t.middleware(async ({ path, type, next, ctx }) => {
137137
return result;
138138
});
139139

140-
export const publicProcedure = t.procedure.use(loggingMiddleware).meta({
141-
openapi: { method: "GET", path: "/public" },
142-
});
140+
export const publicProcedure = t.procedure.use(loggingMiddleware);
143141

144142
const enforceUserIsAuthed = t.middleware(async ({ ctx, next }) => {
145143
if (!ctx.user) {
@@ -163,13 +161,7 @@ const enforceUserIsAdmin = t.middleware(async ({ ctx, next }) => {
163161

164162
export const protectedProcedure = t.procedure
165163
.use(loggingMiddleware)
166-
.use(enforceUserIsAuthed)
167-
.meta({
168-
openapi: {
169-
method: "GET",
170-
path: "/protected",
171-
},
172-
});
164+
.use(enforceUserIsAuthed);
173165

174166
export const adminProtectedProcedure = t.procedure
175167
.use(loggingMiddleware)

packages/auth/src/plugins.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ export function createPlugins(db: dbClient) {
165165
: []),
166166
apiKey({
167167
enableSessionForAPIKeys: true,
168+
customAPIKeyGetter: (ctx) => {
169+
const authorization = ctx.headers?.get("authorization");
170+
if (authorization?.startsWith("Bearer ")) {
171+
return authorization.slice(7);
172+
}
173+
return ctx.headers?.get("x-api-key") ?? undefined;
174+
},
168175
rateLimit: {
169176
enabled: true,
170177
timeWindow: 1000 * 60, // 1 minute

0 commit comments

Comments
 (0)