Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions epicshop/epic-me/app/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export default [
route('/healthcheck', 'routes/healthcheck.tsx'),
route('/db-api', 'routes/db-api.tsx'),
route('/introspect', 'routes/introspect.tsx'),
route('/test-auth', 'routes/test-auth.tsx'),
] satisfies RouteConfig
88 changes: 88 additions & 0 deletions epicshop/epic-me/app/routes/test-auth.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { z } from 'zod'
import { type Route } from './+types/test-auth'

const requestParamsSchema = z
.object({
response_type: z.string().default('code'),
client_id: z.string(),
code_challenge: z.string(),
code_challenge_method: z.string(),
redirect_uri: z.string(),
scope: z
.string()
.optional()
.default('')
.transform((s) => (s ? s.split(' ') : [])),
state: z.string().optional().default(''),
user_id: z.string().optional(), // For programmatic testing
Copy link

Copilot AI Aug 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test-auth endpoint allows arbitrary user_id specification without authentication, which could enable privilege escalation in testing environments. Consider adding proper access controls or restricting this endpoint to development/test environments only.

Copilot uses AI. Check for mistakes.

})
.passthrough()
.transform(
({
response_type: responseType,
client_id: clientId,
code_challenge: codeChallenge,
code_challenge_method: codeChallengeMethod,
redirect_uri: redirectUri,
user_id: userId,
...val
}) => ({
responseType,
clientId,
codeChallenge,
codeChallengeMethod,
redirectUri,
userId,
...val,
}),
)

export async function loader({ request, context }: Route.LoaderArgs) {
const url = new URL(request.url)

try {
const requestParams = requestParamsSchema.parse(
Object.fromEntries(url.searchParams),
)

// Default to first user for testing if no user_id specified
let userId = requestParams.userId
if (!userId) {
const users = await context.db.getAllUsers()
if (users.length === 0) {
return Response.json({ error: 'No users available' }, { status: 400 })
}
userId = String(users[0].id)
}

const user = await context.db.getUserById(Number(userId))
if (!user) {
return Response.json({ error: 'User not found' }, { status: 404 })
}

const { redirectTo } =
await context.cloudflare.env.OAUTH_PROVIDER.completeAuthorization({
request: requestParams,
userId: String(user.id),
metadata: {
label: user.email,
},
scope: requestParams.scope || [],
props: {
userId: String(user.id),
userEmail: user.email,
},
})

return Response.json({ redirectTo, userId: user.id })
} catch (error) {
console.error('Error in test-auth:', error)
return Response.json(
{
error: 'Failed to complete authorization',
details: error instanceof Error ? error.message : String(error),
},
{ status: 500 },
)
}
}
Loading
Loading