-
-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/discourse forum sso route #860
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
Open
jona159
wants to merge
10
commits into
dev
Choose a base branch
from
feat/discourse-forum-sso-route
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a25b156
feat: add api route for discourse connect
jona159 a2b92e2
fix: temporarily comment out use effect that cleans up params
jona159 3fb293d
fix: import order
jona159 5658ba4
feat: return redirect url from login loader
jona159 ded62e8
fix: rm logs
jona159 0ad6268
feat: add discourse connect secret to example env
jona159 9043cef
Merge branch 'dev' into feat/discourse-forum-sso-route
jona159 46b21fc
feat: require actiation if email not yet verified
jona159 2d7e3a7
Merge branch 'dev' into feat/discourse-forum-sso-route
jona159 b7288e2
fix: rm comment
jona159 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
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,105 @@ | ||
| import { createHmac, timingSafeEqual } from 'node:crypto' | ||
| import { redirect, type LoaderFunctionArgs } from 'react-router' | ||
| import invariant from 'tiny-invariant' | ||
| import { getUser } from '~/utils/session.server' | ||
|
|
||
| invariant( | ||
| process.env.DISCOURSE_CONNECT_SECRET, | ||
| 'DISCOURSE_CONNECT_SECRET must be set', | ||
| ) | ||
|
|
||
| const DISCOURSE_CONNECT_SECRET = process.env.DISCOURSE_CONNECT_SECRET | ||
|
|
||
| function signPayload(payload: string) { | ||
| return createHmac('sha256', DISCOURSE_CONNECT_SECRET).update(payload).digest('hex') | ||
| } | ||
|
|
||
| function safeEqualHex(a: string, b: string) { | ||
| try { | ||
| const ab = Buffer.from(a, 'hex') | ||
| const bb = Buffer.from(b, 'hex') | ||
| return ab.length === bb.length && timingSafeEqual(ab, bb) | ||
| } catch { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| function decodeDiscoursePayload(sso: string) { | ||
| const decoded = Buffer.from(sso, 'base64').toString('utf8') | ||
| return new URLSearchParams(decoded) | ||
| } | ||
|
|
||
| function encodeDiscoursePayload(params: URLSearchParams) { | ||
| return Buffer.from(params.toString(), 'utf8').toString('base64') | ||
| } | ||
|
|
||
| function buildAbsoluteLoginRedirect(request: Request) { | ||
| const url = new URL(request.url) | ||
| const redirectTo = `${url.pathname}${url.search}` | ||
|
|
||
| return `/explore/login?${new URLSearchParams({ redirectTo }).toString()}` | ||
| } | ||
|
|
||
|
|
||
| export async function loader({ request }: LoaderFunctionArgs) { | ||
| const url = new URL(request.url) | ||
| const sso = url.searchParams.get('sso') | ||
| const sig = url.searchParams.get('sig') | ||
|
|
||
| if (!sso || !sig) { | ||
| throw new Response('Missing sso or sig', { status: 400 }) | ||
| } | ||
|
|
||
| const expectedSig = signPayload(sso) | ||
| if (!safeEqualHex(sig, expectedSig)) { | ||
| throw new Response('Invalid DiscourseConnect signature', { status: 403 }) | ||
| } | ||
|
|
||
| const incoming = decodeDiscoursePayload(sso) | ||
| const nonce = incoming.get('nonce') | ||
| const returnSsoUrl = incoming.get('return_sso_url') | ||
|
|
||
| if (!nonce || !returnSsoUrl) { | ||
| throw new Response('Missing nonce or return_sso_url', { status: 400 }) | ||
| } | ||
|
|
||
| const user = await getUser(request) | ||
|
|
||
| if (!user) { | ||
| const loginRedirect = buildAbsoluteLoginRedirect(request) | ||
| throw redirect(loginRedirect) | ||
| } | ||
|
|
||
| if (!user.email) { | ||
| throw new Response('User has no email', { status: 400 }) | ||
| } | ||
|
|
||
| const username = | ||
| user.name ?? | ||
| `user_${user.id}` | ||
|
|
||
| const outgoing = new URLSearchParams() | ||
| outgoing.set('nonce', nonce) | ||
| outgoing.set('external_id', String(user.id)) | ||
| outgoing.set('email', user.email) | ||
| outgoing.set('username', username) | ||
|
|
||
| if (user.name) { | ||
| outgoing.set('name', user.name) | ||
| } | ||
|
|
||
| if (!user.emailIsConfirmed) { | ||
| outgoing.set('require_activation', 'true') | ||
| } | ||
|
|
||
|
|
||
| const responsePayload = encodeDiscoursePayload(outgoing) | ||
| const responseSig = signPayload(responsePayload) | ||
|
|
||
| const redirectUrl = new URL(returnSsoUrl) | ||
| redirectUrl.searchParams.set('sso', responsePayload) | ||
| redirectUrl.searchParams.set('sig', responseSig) | ||
|
|
||
|
|
||
| throw redirect(redirectUrl.toString()) | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this might break existing functionality right?