You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was looking for a way to add access control using Cloudflare Zero Trust to restrict who can see my note via its Cloudflare Access Gateway.
I found a solution, but unfortunately, I am not very proficient in writing TypeScript or JavaScript. So I asked AI for help and developed my own implementation. The purpose of this issue is to see if someone can make a PR or to leave it here in case anyone else needs this solution.
However, this solution is not perfect for a workspace with many pages. If you don't include all the pages in the slugToPage section, there is a chance that your original Notion domain prefix might be revealed in the URL path. You could potentially use random strings or a UUID-like string to obscure the original Notion site domain, as it is still available to everyone by design.
Step 1
Follow the Readme instructions, using npx to generate your own code from the template.
Step 2
Continue to follow the instructions in the Readme to edit wrangler.toml and site-config.ts.
Step 3
Add the jose package to package.json to decode Cloudflare Access JWT tokens.
After adding this package to package.json, run npm install to install it.
Step 4
Modify src/index.ts and adapt the code. Here is an example. Replace YOUR_TEAM_DOMAIN_PREFIX with your own Cloudflare team domain prefix.
import{initializeReverseProxy,reverseProxy}from'notehost'import{jwtVerify}from'jose';import{SITE_CONFIG}from'./site-config'initializeReverseProxy(SITE_CONFIG)asyncfunctiongetPublicKey(kid: string): Promise<Uint8Array|null>{constresponse=awaitfetch('http://YOUR_TEAM_DOMAIN_PREFIX.cloudflareaccess.com/cdn-cgi/access/certs');const{ keys }=awaitresponse.json();// Find the key that matches the "kid" (Key ID) in the JWT headerconstkey=keys.find((k: any)=>k.kid===kid);if(!key)returnnull;returnnewUint8Array(Buffer.from(key.x5c[0],'base64'));}asyncfunctionverifyJWT(jwt: string): Promise<boolean>{try{const{ header }=jwtVerify(jwt,async(header)=>{constpublicKey=awaitgetPublicKey(header.kid);if(!publicKey)thrownewError('Public key not found');returnpublicKey;});// If no error is thrown, the JWT is validreturntrue;}catch(error){console.error('JWT verification failed:',error);returnfalse;}}exportdefault{asyncfetch(request: Request): Promise<Response>{constjwt=request.headers.get('Cf-Access-Jwt-Assertion');if(!jwt){returnnewResponse('Unauthorized',{status: 401});}constisValid=awaitverifyJWT(jwt);if(!isValid){returnnewResponse('Unauthorized',{status: 401});}returnawaitreverseProxy(request)},}
Step 5
Deploy it to Cloudflare Workers, setup an self hosted application in zero trust using on the custom domain you deployed and verify if it works.
The text was updated successfully, but these errors were encountered:
I was looking for a way to add access control using Cloudflare Zero Trust to restrict who can see my note via its Cloudflare Access Gateway.
I found a solution, but unfortunately, I am not very proficient in writing TypeScript or JavaScript. So I asked AI for help and developed my own implementation. The purpose of this issue is to see if someone can make a PR or to leave it here in case anyone else needs this solution.
However, this solution is not perfect for a workspace with many pages. If you don't include all the pages in the
slugToPage
section,there is a chance that your original Notion domain prefix might be revealed in the URL path. You could potentially use random strings or a UUID-like string to obscure the original Notion site domain, as it is still available to everyone by design.Step 1
Follow the Readme instructions, using
npx
to generate your own code from the template.Step 2
Continue to follow the instructions in the Readme to edit
wrangler.toml
andsite-config.ts
.Step 3
Add the jose package to
package.json
to decode Cloudflare Access JWT tokens.After adding this package to
package.json
, runnpm install
to install it.Step 4
Modify
src/index.ts
and adapt the code. Here is an example. ReplaceYOUR_TEAM_DOMAIN_PREFIX
with your own Cloudflare team domain prefix.Step 5
Deploy it to Cloudflare Workers, setup an self hosted application in zero trust using on the custom domain you deployed and verify if it works.
The text was updated successfully, but these errors were encountered: