Allows role linking on Discord with Azure Active Directory. Made for deployment to Cloudflare Workers.
The setup guide below assumes you are mildly familiar with Cloudflare Workers, Discord and Azure Active Directory as development platforms.
- Azure Active Directory side:
- Create an Azure Active Directory OAuth application
- From the Overview tab, copy the client ID to your
AAD_CLIENT_ID
secret, and the tenant ID to yourAAD_TENANT_ID
secret - Add a redirect URI under the Authentication tab of
https://yourworker.yourusername.workers.dev/aad
and set the same redirect URI in theAAD_REDIRECT_URI
secret - Generate a client secret under the Certificates & Secrets tab and set your secret
AAD_CLIENT_SECRET
to it
- Discord side:
- Create a Discord OAuth application
- Under General Information, set the Linked Roles verification URL to
https://yourworker.yourusername.workers.dev/
- From the OAuth2 tab, copy the client ID to your
DISCORD_CLIENT_ID
secret, and the client secret to yourDISCORD_CLIENT_SECRET
secret - Add a redirect URI of
https://yourworker.yourusername.workers.dev/discord
and set the same redirect URI in theDISCORD_REDIRECT_URI
secret - Assuming you have a Discord server created, copy the ID of that server to the
DISCORD_GUILD_ID
secret - Generate a bot user for your application and copy its bot token to the
DISCORD_BOT_TOKEN
secret - Invite the bot user to your server and allow it to manage nicknames
- Run a script to register your linked roles
- Set the
DISCORD_LOG_CHANNEL_ID
to the ID of a channel on your server which will receive authorization logs
- Cloudflare Workers:
- Clone this repository and switch to this directory
- Run
npm install
to install all necessary dependencies - Run
wrangler kv:namespace create DISCORD_AAD
and do the required modifications towrangler.toml
it tells you to do - Run
npm run deploy
to deploy your application - After your application is deployed, you can move your secrets from
.dev.vars
to the environment variables of your Worker through the Cloudflare dashboard, or by usingwrangler secret put <NAME>
- Optional:
- Modify
AAD_EMAIL_REGEX
for your AAD - Modify
DISCORD_PLATFORM_NAME
to make sense for your server
- Modify
- Testing:
- Create a role with your bot's connection set in Links
- Select the Linked Roles menu from the server menu
- Select your role
- Select your application
- Go with the flow
- You got it!
You can read more about configuring your worker under the Workers environment variables documentation.
These environment variables are available for configuration from wrangler.toml
:
AAD_EMAIL_REGEX
: Regular expression that emails received from AAD authentication must match to passDISCORD_PLATFORM_NAME
: "Platform name" shown on user profiles after linking their role
These secrets should be added to a .dev.vars
file or as secrets through the Cloudflare Workers dashboard:
AAD_TENANT_ID
: Tenant ID of the Azure Active Directory you are authenticating your users toAAD_CLIENT_ID
: Client ID of your Azure Active Directory OAuth applicationAAD_CLIENT_SECRET
: Client secret of your Azure Active Directory OAuth applicationAAD_DENYLIST
: Comma-separated list of email addresses that should not succeed in linking the roleAAD_REDIRECT_URI
: Redirect URI of your Azure Active Directory OAuth applicationDISCORD_CLIENT_ID
: Client ID of your Discord OAuth applicationDISCORD_CLIENT_SECRET
: Client secret of your Discord OAuth applicationDISCORD_REDIRECT_URI
: Redirect URI of your Discord OAuth applicationDISCORD_GUILD_ID
: ID of the Discord server in which you are implementing role linking with AADDISCORD_BOT_TOKEN
: Bot token of the bot associated with the OAuth applicationDISCORD_LOG_CHANNEL_ID
: Channel to log successful authentications in (denylisted users will be logged as well)