Skip to content

Commit

Permalink
Update consent component to use http requests directly and support cu…
Browse files Browse the repository at this point in the history
…stom base path
  • Loading branch information
mpgxvii committed Oct 8, 2024
1 parent 3548a82 commit 6546a73
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
49 changes: 25 additions & 24 deletions pages/api/consent.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import { Configuration, OAuth2Api } from "@ory/client"
import { NextApiRequest, NextApiResponse } from "next"
import axios from "axios" // Using axios for HTTP requests

const hydra = new OAuth2Api(
new Configuration({
basePath: process.env.HYDRA_ADMIN_URL,
baseOptions: {
"X-Forwarded-Proto": "https",
withCredentials: true,
},
}),
)
const baseURL = process.env.HYDRA_ADMIN_URL

// Helper function to extract session data
const extractSession = (identity: any, grantScope: string[]) => {
Expand All @@ -33,40 +25,49 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
try {
if (req.method === "GET") {
const { consent_challenge } = req.query
const response = await hydra.getOAuth2ConsentRequest({
consentChallenge: String(consent_challenge),
})
const response = await axios.get(
`${baseURL}/oauth2/auth/requests/consent`,
{
params: {
consent_challenge: String(consent_challenge),
},
},
)
return res.status(200).json(response.data)
} else {
if (!consentChallenge || !consentAction) {
return res.status(400).json({ error: "Missing required parameters" })
}
if (consentAction === "accept") {
const { data: body } = await hydra.getOAuth2ConsentRequest({
consentChallenge,
})
const { data: body } = await axios.get(
`${baseURL}/oauth2/auth/requests/consent`,
{
params: { consent_challenge: consentChallenge },
},
)

const session = extractSession(identity, grantScope)
const acceptResponse = await hydra.acceptOAuth2ConsentRequest({
consentChallenge,
acceptOAuth2ConsentRequest: {
const acceptResponse = await axios.put(
`${baseURL}/oauth2/auth/requests/consent/accept?consent_challenge=${consentChallenge}`,
{
grant_scope: session.access_token.scope,
grant_access_token_audience: body.requested_access_token_audience,
session,
remember: Boolean(remember),
remember_for: 3600,
},
})
)
return res
.status(200)
.json({ redirect_to: acceptResponse.data.redirect_to })
} else {
const rejectResponse = await hydra.rejectOAuth2ConsentRequest({
consentChallenge,
rejectOAuth2Request: {
const rejectResponse = await axios.put(
`${baseURL}/oauth2/auth/requests/consent/${consentChallenge}/reject`,
{
error: "access_denied",
error_description: "The resource owner denied the request",
},
})
)

return res
.status(200)
Expand Down
13 changes: 7 additions & 6 deletions pages/consent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const Consent = () => {
const [csrfToken, setCsrfToken] = useState<string>("")
const [isLoading, setIsLoading] = useState<boolean>(false)

const basePath = process.env.BASE_PATH || ""

useEffect(() => {
const { consent_challenge } = router.query

Expand All @@ -27,7 +29,7 @@ const Consent = () => {
}

const consentResponse = await fetch(
`/api/consent?consent_challenge=${consent_challenge}`,
`${basePath}/api/consent?consent_challenge=${consent_challenge}`,
)
const consentData = await consentResponse.json()

Expand All @@ -40,7 +42,7 @@ const Consent = () => {
// Automatically handle skipping consent if enabled
if (consentData.client?.skip_consent) {
console.log("Skipping consent, automatically submitting.")
const skipResponse = await fetch("/api/consent", {
const skipResponse = await fetch(`${basePath}/api/consent`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand All @@ -58,7 +60,7 @@ const Consent = () => {
if (skipData.error) {
throw new Error(skipData.error)
}
router.push(skipData.redirect_to)
window.location.href = skipData.redirect_to
return
}
} catch (error) {
Expand Down Expand Up @@ -90,7 +92,7 @@ const Consent = () => {
}

try {
const response = await fetch("/api/consent", {
const response = await fetch(`${basePath}/api/consent`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand All @@ -109,8 +111,7 @@ const Consent = () => {
console.error("Error submitting consent:", data.error)
return
}

router.push(data.redirect_to)
window.location.href = data.redirect_to
} catch (error) {
console.error("Error during consent submission:", error)
}
Expand Down

0 comments on commit 6546a73

Please sign in to comment.