Repository navigation
fix(discord): the first install's channel picker loads, through a proof the OAuth callback mints #3367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
fix(discord): the first install's channel picker loads, through a proof the OAuth callback mints #3367
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
436df12
fix(discord): the first install's channel picker loads, through a pro…
lilyshen0722 77a4a8d
fix(discord): a failed channel fetch logs its status, not the request…
lilyshen0722 ccf724b
fix(sentry): scrub the Discord install proof from breadcrumb and requ…
lilyshen0722 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,53 @@ | ||
| /** | ||
| * Discord first-install proof (F89). | ||
| * | ||
| * The success page's channel picker reads `GET /api/discord/channels/:guildId`, | ||
| * which answers only for a guild already bound to one of the caller's pods. On a | ||
| * first install nothing is bound yet, so the picker never loaded. The OAuth | ||
| * callback is where Commonly learns which guild the bot was just added to: the | ||
| * code exchange's token response names it. The callback has no session (Discord | ||
| * redirects a browser there), so it hands the page a short-lived signed proof of | ||
| * (pod, guild), and the channels route accepts that proof in place of a binding | ||
| * when the caller belongs to the pod it names. | ||
| * | ||
| * The payload carries neither `id` nor `user`, so `middleware/auth` never reads | ||
| * it as a session. | ||
| */ | ||
| import jwt, { type JwtPayload } from 'jsonwebtoken'; | ||
|
|
||
| import { isDiscordSnowflake } from './discordBinding'; | ||
|
|
||
| const PURPOSE = 'discord-install'; | ||
| export const DISCORD_INSTALL_PROOF_TTL_SECONDS = 30 * 60; | ||
|
|
||
| interface DiscordInstallProofPayload extends JwtPayload { | ||
| pur: typeof PURPOSE; | ||
| pod: string; | ||
| gid: string; | ||
| } | ||
|
|
||
| const secret = (): string => { | ||
| const value = process.env.JWT_SECRET; | ||
| if (!value) throw new Error('JWT_SECRET is not set'); | ||
| return value; | ||
| }; | ||
|
|
||
| export const mintDiscordInstallProof = ( | ||
| { podId, guildId }: { podId: string; guildId: string }, | ||
| ttlSeconds: number = DISCORD_INSTALL_PROOF_TTL_SECONDS, | ||
| ): string => { | ||
| const payload: DiscordInstallProofPayload = { pur: PURPOSE, pod: podId, gid: guildId }; | ||
| return jwt.sign(payload, secret(), { expiresIn: ttlSeconds }); | ||
| }; | ||
|
|
||
| /** The proof's (pod, guild), or null for anything that is not a live proof. */ | ||
| export const readDiscordInstallProof = (token: unknown): { podId: string; guildId: string } | null => { | ||
| if (typeof token !== 'string' || !token) return null; | ||
| try { | ||
| const decoded = jwt.verify(token, secret()) as DiscordInstallProofPayload; | ||
| if (decoded.pur !== PURPOSE || typeof decoded.pod !== 'string' || !isDiscordSnowflake(decoded.gid)) return null; | ||
| return { podId: decoded.pod, guildId: decoded.gid }; | ||
| } catch { | ||
| return null; | ||
| } | ||
| }; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.