-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(golinks): simplify auth logic using bult-in bearerToken middleware
Also in this commit we added debug API classes. Signed-off-by: Andrei Jiroh Halili <[email protected]>
- Loading branch information
1 parent
437d779
commit 874e2ec
Showing
4 changed files
with
177 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { OpenAPIRoute, Str } from "chanfana"; | ||
import { Context } from "hono"; | ||
import { jwtVerify, SignJWT } from "jose"; | ||
import { z } from "zod"; | ||
|
||
export class debugApiGenerateJwt extends OpenAPIRoute { | ||
schema = { | ||
tags: ["debug"], | ||
summary: "Generate a example signed JWT or validate a JWT generated from this service.", | ||
request: { | ||
query: z.object({ | ||
jwt: Str({ | ||
description: "JWT to validate its signature against", | ||
}), | ||
}), | ||
}, | ||
security: [{ userApiKey: [] }], | ||
}; | ||
async handle(c) { | ||
const { token } = c.req.query(); | ||
const secret = new TextEncoder().encode(c.env.JWT_SIGNING_KEY); | ||
const payload = { | ||
slack: { | ||
teamId: "T1234", | ||
userId: "U1234", | ||
enterpriseId: "E1234", | ||
isEnterpriseInstall: true, | ||
}, | ||
example_jwt: true, | ||
}; | ||
|
||
if (token == null) { | ||
const exampleToken = await new SignJWT(payload) | ||
.setProtectedHeader({ alg: "HS256" }) | ||
.setAudience("challenge_1234abcd") | ||
.setIssuer(c.env.BASE_URL) | ||
.setIssuedAt() | ||
.setExpirationTime("15 minutes") | ||
.sign(secret); | ||
return c.json({ ok: true, result: exampleToken }); | ||
} | ||
|
||
const result = await jwtVerify(token, secret, { | ||
issuer: c.env.BASE_URL, | ||
clockTolerance: 30, | ||
}); | ||
return c.json({ ok: true, result }); | ||
} | ||
} | ||
|
||
export class debugApiGetBindings extends OpenAPIRoute { | ||
schema = { | ||
summary: "Show all Worker bindings associated with this instance, including secrets.", | ||
security: [ | ||
{userApiKey: []} | ||
] | ||
} | ||
async handle(c: Context) { | ||
return c.json({ ok: true, result: c.env }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,27 @@ | ||
import { EnvBindings, Env } from "types"; | ||
import { EnvBindings } from "types"; | ||
|
||
export const adminApiKey = { | ||
type: "apiKey", | ||
name: "X-Golinks-Admin-Key", | ||
in: "header", | ||
description: "Superadmin API key. This is temporary while we're working on support for managing API tokens in the database.", | ||
externalDocs: { | ||
description: "Learn more about admin access", | ||
url: homepage | ||
} | ||
description: "This is being deprecated for the use of bearer token-based `userApiKey` instead", | ||
externalDocs: { | ||
description: "Learn more about admin access", | ||
url: homepage, | ||
}, | ||
}; | ||
|
||
export const userApiKey = { | ||
type: "http", | ||
scheme: "bearer", | ||
format: "JWT", | ||
description: "User bearer token in JWT format. The token will be checked server-side for expiration status and if it is revoked manually.", | ||
externalDocs: { | ||
description: "Request API access", | ||
url: "https://go.andreijiroh.xyz/request-api-access" | ||
} | ||
} | ||
type: "http", | ||
scheme: "bearer", | ||
format: "JWT", | ||
description: | ||
"User bearer token in JWT format. The token will be checked server-side for expiration status and if it is revoked manually.", | ||
externalDocs: { | ||
description: "Request API access", | ||
url: "https://go.andreijiroh.xyz/request-api-access", | ||
}, | ||
}; | ||
|
||
export const homepage = "https://wiki.andreijiroh.xyz/golinks"; | ||
export const sources = "https://github.com/andreijiroh-dev/api-servers/tree/main/apps/golinks-v2"; | ||
|
@@ -31,7 +32,7 @@ export const contact = { | |
email: "[email protected]", | ||
}; | ||
|
||
export function getWorkersDashboardUrl(env: EnvBindings<Env>["DEPLOY_ENV"]) { | ||
export function getWorkersDashboardUrl(env: EnvBindings["DEPLOY_ENV"]) { | ||
if (env == "production") { | ||
return "https://dash.cloudflare.com/cf0bd808c6a294fd8c4d8f6d2cdeca05/workers/services/view/golinks-next/production"; | ||
} else { | ||
|
@@ -75,19 +76,32 @@ export const tags = [ | |
url: "https://go.andreijiroh.xyz/feedback/add-discord-invite", | ||
}, | ||
}, | ||
{ | ||
name: "meta", | ||
description: "Utility API endpoints to check API availability and get the commit hash of latest deploy", | ||
}, | ||
{ | ||
name: "debug", | ||
description: "Requires admin API key (aka the `ADMIN_KEY` secret) to access them.", | ||
}, | ||
]; | ||
|
||
export const discordServerNotFound = (url?: string) => ` | ||
Either that server is not on our records (perhaps the slug is just renamed) or | ||
export const discordServerNotFound = (url?: string) => `\ | ||
Either that server is not on our records (perhaps the slug is just renamed) or \ | ||
something went wrong on our side. | ||
Still seeing this? Submit a ticket in our issue tracker using the following URL: | ||
https://go.andreijiroh.xyz/feedback/broken-link${url !== undefined ? `?url=${url}` : ""}`; | ||
|
||
export const golinkNotFound = (url?: string) => `\ | ||
Either that golink is not on our records (perhaps the slug is just renamed) or something | ||
Either that golink is not on our records (perhaps the slug is just renamed) or something \ | ||
went wrong on our side. | ||
Still seeing this? Submit a ticket in our issue tracker using the following URL: | ||
https://go.andreijiroh.xyz/feedback/broken-link${url !== undefined ? `?url=${url}` : ""}`; | ||
|
||
export const wikilinkNotAvailable = `\ | ||
Golink-styled wikilinks are available in andreijiroh.xyz subdomains (and friends \ | ||
at the moment, especially in the main website and digital garden.` |