Compare commits
1 Commits
fix/react-
...
fix/api-ke
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d3687cfb10 |
@@ -11,10 +11,10 @@ https://kan.bn/api/v1
|
|||||||
|
|
||||||
## Authentication
|
## Authentication
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
```
|
```
|
||||||
'x-api-key': kan_123456789
|
'Authorization': 'Bearer kan_123456789'
|
||||||
```
|
```
|
||||||
|
|
||||||
## Response codes
|
## Response codes
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import { encryptToken } from "../utils/encryption";
|
|||||||
export const integrationRouter = createTRPCRouter({
|
export const integrationRouter = createTRPCRouter({
|
||||||
saveGitHubToken: protectedProcedure
|
saveGitHubToken: protectedProcedure
|
||||||
.input(z.object({ token: z.string() }))
|
.input(z.object({ token: z.string() }))
|
||||||
|
.output(z.object({ success: z.boolean() }))
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(async ({ ctx, input }) => {
|
||||||
const user = ctx.user;
|
const user = ctx.user;
|
||||||
|
|
||||||
@@ -43,7 +44,9 @@ export const integrationRouter = createTRPCRouter({
|
|||||||
return { success: true };
|
return { success: true };
|
||||||
}),
|
}),
|
||||||
|
|
||||||
disconnectGitHub: protectedProcedure.mutation(async ({ ctx }) => {
|
disconnectGitHub: protectedProcedure
|
||||||
|
.output(z.object({ success: z.boolean() }))
|
||||||
|
.mutation(async ({ ctx }) => {
|
||||||
const user = ctx.user;
|
const user = ctx.user;
|
||||||
|
|
||||||
if (!user)
|
if (!user)
|
||||||
@@ -56,7 +59,9 @@ export const integrationRouter = createTRPCRouter({
|
|||||||
return { success: true };
|
return { success: true };
|
||||||
}),
|
}),
|
||||||
|
|
||||||
getGitHubStatus: protectedProcedure.query(async ({ ctx }) => {
|
getGitHubStatus: protectedProcedure
|
||||||
|
.output(z.object({ connected: z.boolean() }))
|
||||||
|
.query(async ({ ctx }) => {
|
||||||
const user = ctx.user;
|
const user = ctx.user;
|
||||||
|
|
||||||
if (!user)
|
if (!user)
|
||||||
|
|||||||
@@ -137,9 +137,7 @@ const loggingMiddleware = t.middleware(async ({ path, type, next, ctx }) => {
|
|||||||
return result;
|
return result;
|
||||||
});
|
});
|
||||||
|
|
||||||
export const publicProcedure = t.procedure.use(loggingMiddleware).meta({
|
export const publicProcedure = t.procedure.use(loggingMiddleware);
|
||||||
openapi: { method: "GET", path: "/public" },
|
|
||||||
});
|
|
||||||
|
|
||||||
const enforceUserIsAuthed = t.middleware(async ({ ctx, next }) => {
|
const enforceUserIsAuthed = t.middleware(async ({ ctx, next }) => {
|
||||||
if (!ctx.user) {
|
if (!ctx.user) {
|
||||||
@@ -163,13 +161,7 @@ const enforceUserIsAdmin = t.middleware(async ({ ctx, next }) => {
|
|||||||
|
|
||||||
export const protectedProcedure = t.procedure
|
export const protectedProcedure = t.procedure
|
||||||
.use(loggingMiddleware)
|
.use(loggingMiddleware)
|
||||||
.use(enforceUserIsAuthed)
|
.use(enforceUserIsAuthed);
|
||||||
.meta({
|
|
||||||
openapi: {
|
|
||||||
method: "GET",
|
|
||||||
path: "/protected",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
export const adminProtectedProcedure = t.procedure
|
export const adminProtectedProcedure = t.procedure
|
||||||
.use(loggingMiddleware)
|
.use(loggingMiddleware)
|
||||||
|
|||||||
@@ -165,6 +165,13 @@ export function createPlugins(db: dbClient) {
|
|||||||
: []),
|
: []),
|
||||||
apiKey({
|
apiKey({
|
||||||
enableSessionForAPIKeys: true,
|
enableSessionForAPIKeys: true,
|
||||||
|
customAPIKeyGetter: (ctx) => {
|
||||||
|
const authorization = ctx.headers?.get("authorization");
|
||||||
|
if (authorization?.startsWith("Bearer ")) {
|
||||||
|
return authorization.slice(7);
|
||||||
|
}
|
||||||
|
return ctx.headers?.get("x-api-key") ?? undefined;
|
||||||
|
},
|
||||||
rateLimit: {
|
rateLimit: {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
timeWindow: 1000 * 60, // 1 minute
|
timeWindow: 1000 * 60, // 1 minute
|
||||||
|
|||||||
Reference in New Issue
Block a user