-
Notifications
You must be signed in to change notification settings - Fork 14
feat(apps/chat): LTI semi-anonymous guest access #5083
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
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3137d42
feat(apps/chat): LTI semi-anonymous guest access (Phase A)
rschlaefli a2ca2cc
fix(apps/chat): defer guest secret/seed assertion to first use
rschlaefli 8eb0910
Merge origin/v3-ai into claude/condescending-swartz-993a42
rschlaefli e2a1d1e
fix(apps/chat): address PR review for LTI guest access (Phase A)
rschlaefli 41dc58e
feat(apps/chat): CHIPS + sessionStorage fallback for iframe embedding
rschlaefli 3c938b9
docs(project): plan CHIPS + sessionStorage iframe-auth rollout
rschlaefli a083387
add mint script for testing
rschlaefli f6c60c9
refactor(util): share iframe auth helpers
rschlaefli 3b204d9
feat(auth): share iframe client auth helpers
rschlaefli b65219f
ignore scheduled tasks
rschlaefli 22aa17b
fix(auth): address iframe auth review findings
rschlaefli 2dfd42b
docs(auth): plan LTI course binding follow-up
rschlaefli 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| import { | ||
| resolveLtiAuthDecision, | ||
| signChatGuestToken, | ||
| verifyLtiToken, | ||
| } from '@/src/lib/server/ltiGuest' | ||
| import { prisma } from '@klicker-uzh/prisma' | ||
| import { jwtVerify } from 'jose' | ||
| import { NextRequest, NextResponse } from 'next/server' | ||
| import { z } from 'zod' | ||
|
|
||
| const LOG_PREFIX = '[chat:auth/lti]' | ||
|
|
||
| const querySchema = z.object({ | ||
| jwt: z.string().min(1), | ||
| courseId: z.string().uuid(), | ||
| chatbotId: z.string().uuid(), | ||
| }) | ||
|
|
||
| async function getParticipantTokenSub( | ||
| req: NextRequest | ||
| ): Promise<string | null> { | ||
| const token = req.cookies.get('participant_token')?.value | ||
| if (!token) return null | ||
| const appSecret = process.env.APP_SECRET | ||
| if (!appSecret) return null | ||
| try { | ||
| const result = await jwtVerify(token, new TextEncoder().encode(appSecret)) | ||
| return typeof result.payload.sub === 'string' && | ||
| result.payload.sub.length > 0 | ||
| ? result.payload.sub | ||
| : null | ||
| } catch { | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| function noLoginRedirect(req: NextRequest, chatbotId: string | null) { | ||
| const noLoginUrl = req.nextUrl.clone() | ||
| noLoginUrl.pathname = '/noLogin' | ||
| noLoginUrl.search = '' | ||
| noLoginUrl.searchParams.set('lti', '1') | ||
| if (chatbotId) { | ||
| noLoginUrl.searchParams.set('redirectTo', `/${chatbotId}`) | ||
| } | ||
| return NextResponse.redirect(noLoginUrl) | ||
| } | ||
|
|
||
| export async function GET(req: NextRequest) { | ||
| const { searchParams } = req.nextUrl | ||
|
|
||
| const queryResult = querySchema.safeParse({ | ||
| jwt: searchParams.get('jwt'), | ||
| courseId: searchParams.get('courseId'), | ||
| chatbotId: searchParams.get('chatbotId'), | ||
| }) | ||
|
|
||
| if (!queryResult.success) { | ||
| console.error( | ||
| LOG_PREFIX, | ||
| 'Invalid query params:', | ||
| queryResult.error.flatten() | ||
| ) | ||
| return NextResponse.json( | ||
| { | ||
| error: 'Missing or invalid query parameters (jwt, courseId, chatbotId)', | ||
| }, | ||
| { status: 400 } | ||
| ) | ||
| } | ||
|
|
||
| const { jwt, courseId, chatbotId } = queryResult.data | ||
|
|
||
| let ltiPayload | ||
| try { | ||
| ltiPayload = await verifyLtiToken(jwt) | ||
| } catch (error) { | ||
| console.error(LOG_PREFIX, 'LTI JWT verification failed:', error) | ||
| return noLoginRedirect(req, chatbotId) | ||
| } | ||
|
|
||
| const [course, chatbot] = await Promise.all([ | ||
| prisma.course.findUnique({ where: { id: courseId }, select: { id: true } }), | ||
| prisma.chatbot.findUnique({ | ||
| where: { id: chatbotId }, | ||
| select: { id: true, courseId: true }, | ||
| }), | ||
| ]) | ||
|
|
||
| if (!course) { | ||
| return NextResponse.json({ error: 'Course not found' }, { status: 404 }) | ||
| } | ||
| if (!chatbot) { | ||
| return NextResponse.json({ error: 'Chatbot not found' }, { status: 404 }) | ||
| } | ||
| if (chatbot.courseId !== courseId) { | ||
| console.error(LOG_PREFIX, 'Cross-course access blocked', { | ||
| chatbotCourseId: chatbot.courseId, | ||
| requestedCourseId: courseId, | ||
| chatbotId, | ||
| }) | ||
| return NextResponse.json( | ||
| { error: 'Chatbot not found in this course' }, | ||
| { status: 403 } | ||
| ) | ||
| } | ||
|
|
||
| const participantTokenSub = await getParticipantTokenSub(req) | ||
|
|
||
| let decision | ||
| try { | ||
| decision = await resolveLtiAuthDecision({ | ||
| ltiSub: ltiPayload.sub, | ||
| ltiScope: ltiPayload.scope, | ||
| courseId, | ||
| participantTokenSub, | ||
| }) | ||
| } catch (error) { | ||
| console.error(LOG_PREFIX, 'resolveLtiAuthDecision failed:', error) | ||
| return NextResponse.json( | ||
| { error: 'Failed to resolve auth decision' }, | ||
| { status: 500 } | ||
| ) | ||
| } | ||
|
|
||
| console.info(LOG_PREFIX, 'auth resolved', { | ||
| mode: decision.mode, | ||
| chatbotId, | ||
| courseId, | ||
| }) | ||
|
|
||
| // Probe whether third-party cookies survived the LMS iframe context. | ||
| // `apps/lti` sets `lti-token` with `secure; sameSite=none; domain=COOKIE_DOMAIN`; | ||
| // browsers blocking 3p cookies (Safari ITP, Brave, Firefox total cookie protection | ||
| // pre-141, Chrome with Tracking Protection) strip it before this request lands. | ||
| // Mirrors the PWA pattern in `getParticipantToken.ts`. | ||
| const cookiesAvailable = !!req.cookies.get('lti-token')?.value | ||
|
|
||
| const chatbotUrl = req.nextUrl.clone() | ||
| chatbotUrl.pathname = `/${chatbotId}` | ||
| chatbotUrl.search = '' | ||
|
|
||
| const isProduction = | ||
| process.env.NODE_ENV === 'production' && | ||
| process.env.COOKIE_DOMAIN !== '127.0.0.1' | ||
|
|
||
| if (decision.mode === 'account') { | ||
| // Account branch: clear any stale `chat_participant_token` so the | ||
| // guest-first middleware order (verify chat-guest before participant) does | ||
| // not keep forcing `authMode='anonymous'` after this redirect. | ||
| const accountResponse = NextResponse.redirect(chatbotUrl) | ||
| accountResponse.cookies.delete({ | ||
| name: 'chat_participant_token', | ||
| path: '/', | ||
| }) | ||
| return accountResponse | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
greptile-apps[bot] marked this conversation as resolved.
|
||
|
|
||
| // Guest path. Issue chat_participant_token; never override participant_token. | ||
| let chatGuestToken | ||
| try { | ||
| chatGuestToken = await signChatGuestToken(decision.participantId) | ||
| } catch (error) { | ||
| console.error(LOG_PREFIX, 'Failed to sign chat guest token:', error) | ||
| return NextResponse.json( | ||
| { error: 'Failed to create guest session' }, | ||
| { status: 500 } | ||
| ) | ||
| } | ||
|
|
||
| // sessionStorage fallback for browsers where CHIPS is not yet supported | ||
| // (pre-Safari 26.2, Firefox <141). Hand the token off via `?_t=` query so | ||
| // the client bootstrap (`useChatGuestTokenBootstrap`) can stuff it into | ||
| // sessionStorage and strip the URL parameter via `router.replace`. | ||
| if (!cookiesAvailable) { | ||
| chatbotUrl.searchParams.set('_t', chatGuestToken) | ||
| } | ||
|
|
||
| const response = NextResponse.redirect(chatbotUrl) | ||
|
|
||
| // Host-only cookie: no `domain` set → cookie never leaves the chat subdomain. | ||
| // Backend GraphQL on api.<domain> never sees this token even if leaked. | ||
| // `Partitioned` (CHIPS) lets modern browsers keep the cookie in third-party | ||
| // iframe contexts (Chrome 114+, Edge 114+, Firefox 141+, Safari 26.2+). | ||
| response.cookies.set('chat_participant_token', chatGuestToken, { | ||
| httpOnly: true, | ||
| secure: isProduction, | ||
| sameSite: isProduction ? 'none' : 'lax', | ||
| partitioned: isProduction, | ||
| path: '/', | ||
| maxAge: 60 * 60 * 24 * 14, | ||
| }) | ||
|
|
||
| return response | ||
| } | ||
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.
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.