Simply put: Allows you to authorise with Cloudflare Access using your Discord account via a Cloudflare Worker. Wraps OIDC around the Discord OAuth2 API to achieve this, storing signing keys in KV.
Process flow was inspired by kimcore/discord-oidc but rewritten entirely for Cloudflare Workers and Hono.
Some ideas were also taken from eidam/cf-access-workers-oidc.
Show them some love!
Requirements:
NAME.cloudflareaccess.com
subddomain.https://YOURNAME.cloudflareaccess.com/cdn-cgi/access/callback
to the Discord application.Steps:
cd
into it: git clone https://github.com/Erisa/discord-oidc-worker.git && cd discord-oidc-worker
npm install
wrangler.toml
to use your new KV namespace ID.config.sample.json
to config.json
.config.json
.config.json
under redirectURL
. This should be the same URL you added to Discord.npx wrangler publish
!Discord
https://discord-oidc.YOURNAME.workers.dev/authorize/email
or swap out /email
for /guilds
to include the Guilds scope.https://discord-oidc.YOURNAME.workers.dev/token
https://discord-oidc.YOURNAME.workers.dev/jwks.json
id
here, as the users unique Discord user ID.preferred_username
will map to the users username and discrim if they have one e.g. Erisa#9999
or erisachu
name
will map to the non-unique Display Name of the user, or username if there is none. E.g. Erisa
. Basically a safer form of global_name
, which might sometimes be null./guilds
then the guilds
claim can be used to provide a list of guild IDs./guilds
auth URL.bot
scope and use it to invite the bot to your server.
npx wrangler secret put DISCORD_TOKEN
.config.json
with a list of server IDs that you wish to check user roles for. Make sure the bot is a member of all servers in this list.roles:
, e.g. roles:438781053675634713
roles:
claims as the name, and use the role ID as the claim value. This will match users in that server who have that role.Example config for a roles setup:
{
"clientId": "1056005449054429204",
"clientSecret": "aaaaaaaaaaaaa",
"redirectURL": "https://erisa.cloudflareaccess.com/cdn-cgi/access/callback",
"serversToCheckRolesFor": [
"438781053675634713"
]
}
My setup, as an example:
To use this in a policy, simply enable it as an Identity provider in your Access application and then create a rule using OIDC Claims
and the relevant claim above. Make sure the claim has been added to your provider in the steps above.
With roles:
This example would allow me to access the application if I was myself on Discord or if I was a member of a specific server:
If you find a security vulnerability in this repository, do NOT create an Issue or Pull Request. Please contact me through email or message (There are links on my GitHub profile). If you create an issue for an active security vulnerability I will save the information and delete the issue.
Alternatively, you can try out a new GitHub feature for Security Advisories: https://github.com/Erisa/discord-oidc-worker/security