Replies: 2 comments
-
I've had a go at this using the Credentials provider and have it sort of working with SvelteKit.
import Credentials from "@auth/core/providers/credentials"
import { jwtVerify, createRemoteJWKSet } from 'jose'
export default function CloudflareAccess(event) {
const getIdentity = async (host, auth) =>
await fetch(`https://${host}/cdn-cgi/access/get-identity`, {
headers: {
Cookie: `CF_Authorization=${auth}`,
},
}).then((data) => data.json());
async function isValidJwt(host, token) {
const JWKS = createRemoteJWKSet(new URL("https://${host}/cdn-cgi/access/certs"))
const { payload, protectedHeader } = await jwtVerify(token, JWKS, {
audience: event.platform?.env.ACCESS_AUD,
})
return (payload && protectedHeader)
}
return Credentials({
id: "cloudflare",
name: "Cloudflare Access",
credentials: {},
async authorize(_, request) {
const { cookies } = event;
const auth = cookies.get('CF_Authorization')
if (!auth) {
console.log('CF_Authorization header not found!')
throw Error('CF_Authorization header not found')
}
const access = await isValidJwt(event.url.host, auth)
if (!access) {
console.log('Invalid JWT')
throw Error('Invalid JWT')
}
const identity = await getIdentity(event.url.host, auth);
const user = (({ id, name, email, }) => ({ id, name, email }))(identity)
console.log('cloudflare - user', user)
if (!user) return null
return user
}
})
} You'll also need Jose in Add this as a provider in import { sequence } from '@sveltejs/kit/hooks';
import { SvelteKitAuth } from '@auth/sveltekit'
import CloudflareAccess from '$lib/server/@auth/providers/cloudflareaccess'
const CFAccess = (/** @type {import("@sveltejs/kit").RequestEvent<Partial<Record<string, string>>, string | null>} */ event) => {
return CloudflareAccess(event)
}
export const handle = sequence(
SvelteKitAuth(async (event) => {
const authOptions = {
providers: [ CFAccess(event) ],
// other AuthJS options
};
return authOptions;
})
) Set a protected route in the Cloudflare Access dashboard. I used If you go to the AuthJS signing page and pick What doesn't work is setting the AuthJS/NextJS session cookies and JWT. I think this is because the Credential provider with |
Beta Was this translation helpful? Give feedback.
-
Just discovered someone giving it a red hot go: https://github.com/seiry/next-cloudflare-zerotrust-jwt-auth-middleware/blob/main/src/edge.ts |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Description 📓
I'm wanting to have Cloudflare Access sitting in front of our website.
We current use NextAuth with AuzreAD, but now want Cloudflare Access deal with AD for us.
My understanding that to get it working, we'd need a new Provider modeled off the Credentials provider?
There is already some prior art.
How to reproduce ☕️
NA, think it's self explanatory?
Contributing 🙌🏽
Yes, I am willing to help implement this feature in a PR
Happy to pitch in (or donate if someone can implement it quick), just need guidance :)
Beta Was this translation helpful? Give feedback.
All reactions