This commit is contained in:
2025-10-30 02:54:47 -03:00
parent b0ae990c7d
commit a9433d5a57
8 changed files with 31 additions and 22 deletions

View File

@@ -1,6 +0,0 @@
export const OK = 200
export const FOUND = 302
export const BAD_REQUEST = 400
export const UNAUTHORIZED = 401
export const FORBIDDEN = 403
export const INTERNAL_SERVER = 500

View File

@@ -1,8 +1,11 @@
import type { Route } from './+types'
import * as httpStatus from '@/lib/http-status'
import { parse } from 'cookie'
export const OK = 200
export const FOUND = 302
export const INTERNAL_SERVER_ERROR = 500
export async function loader({ request, context }: Route.LoaderArgs) {
const cookies = parse(request.headers.get('Cookie') || '')
const url = new URL(request.url)
@@ -13,7 +16,7 @@ export async function loader({ request, context }: Route.LoaderArgs) {
if (!cookies?.__session) {
return new Response(null, {
status: httpStatus.FOUND,
status: FOUND,
headers: {
Location: loginUrl.toString()
}
@@ -30,23 +33,23 @@ export async function loader({ request, context }: Route.LoaderArgs) {
redirect: 'manual'
})
if (r.status === httpStatus.FOUND) {
if (r.status === FOUND) {
return new Response(await r.text(), {
status: r.status,
headers: r.headers
})
}
console.log('Issuer response', {
console.log('Authorize response', {
json: await r.json(),
headers: r.headers,
status: r.status
})
// Deny authorization if user lacks scopes requested by client
if (r.status === httpStatus.FORBIDDEN) {
if (r.status === FOUND) {
return new Response(null, {
status: httpStatus.FOUND,
status: r.status,
headers: {
Location: new URL('/deny', url.origin).toString()
}
@@ -54,13 +57,13 @@ export async function loader({ request, context }: Route.LoaderArgs) {
}
return new Response(null, {
status: httpStatus.FOUND,
status: FOUND,
headers: {
Location: loginUrl.toString()
}
})
} catch (error) {
console.error(error)
return new Response(null, { status: httpStatus.INTERNAL_SERVER })
return new Response(null, { status: INTERNAL_SERVER_ERROR })
}
}

View File

@@ -21,7 +21,7 @@ import { Button } from '@/components/ui/button'
import { Checkbox } from '@/components/ui/checkbox'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import * as httpStatus from '@/lib/http-status'
import { FOUND, INTERNAL_SERVER_ERROR, OK } from './authorize'
const schema = z.object({
username: z
@@ -60,7 +60,7 @@ export async function action({ request, context }: Route.ActionArgs) {
body: JSON.stringify(formData)
})
if (r.status !== httpStatus.OK) {
if (r.status !== OK) {
return Response.json(await r.json(), {
status: r.status,
headers: r.headers
@@ -74,12 +74,12 @@ export async function action({ request, context }: Route.ActionArgs) {
headers.set('Location', url.toString())
return new Response(await r.text(), {
status: httpStatus.FOUND,
status: FOUND,
headers
})
} catch (error) {
console.error(error)
return Response.json({}, { status: httpStatus.INTERNAL_SERVER })
return Response.json({}, { status: INTERNAL_SERVER_ERROR })
}
}

View File

@@ -7,8 +7,6 @@ async function proxy({
request,
context
}: Route.ActionArgs): Promise<Response> {
console.debug('Request headers', request.headers)
const url = new URL(request.url)
const issuerUrl = new URL(url.pathname, context.cloudflare.env.ISSUER_URL)
const response = await fetch(issuerUrl.toString(), {
@@ -16,8 +14,6 @@ async function proxy({
headers: request.headers
})
console.debug('Response headers', response.headers)
return new Response(await response.text(), {
status: response.status,
headers: response.headers