Created
January 23, 2026 14:11
-
-
Save Chaoskjell44/c580bc56bced5cc5dc0cc93e228b8be6 to your computer and use it in GitHub Desktop.
Authentik fixed discord login property mapping
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import base64 | |
| import requests | |
| from authentik.core.models import Group | |
| # To get the guild ID number for the parameters, open Discord, go to Settings > Advanced and enable developer mode. | |
| # Right-click on the server/guild title and select "Copy ID" to get the guild ID. | |
| # Set these values | |
| ALLOWED_GUILDS = "123456789101112" # Discord server id to fetch roles from | |
| AVATAR_SIZE = "64" # Valid avatar size values: 16,32,64,128,256,512,1024. | |
| # Larger values than 64 may cause HTTP error 431 on applications/providers | |
| # due to headers being too large. | |
| # Generate avatar URL and base64 avatar image | |
| avatar_url = None | |
| avatar_base64 = None | |
| if info.get("avatar"): | |
| avatar_url = (f"https://cdn.discordapp.com/avatars/{info.get('id')}/" | |
| f"{info.get('avatar')}.png?size={AVATAR_SIZE}") | |
| try: | |
| response = client.do_request("GET", avatar_url) | |
| encoded_image = base64.b64encode(response.content).decode('utf-8') | |
| avatar_base64 = f"data:image/png;base64,{encoded_image}" | |
| except: | |
| avatar_base64 = None | |
| # Initialize user_groups as empty list | |
| user_groups = [] | |
| # Check user's guild membership | |
| guilds_url = "https://discord.com/api/v10/users/@me/guilds" | |
| guilds_response = client.do_request("GET", guilds_url, token=token) | |
| guilds_data = guilds_response.json() | |
| # Check if user is member of the allowed guild | |
| is_member = any(guild.get("id") == ALLOWED_GUILDS for guild in guilds_data) | |
| if is_member: | |
| # Get guild membership details | |
| guild_url = f"https://discord.com/api/v10/users/@me/guilds/{ALLOWED_GUILDS}/member" | |
| guild_response = client.do_request("GET", guild_url, token=token) | |
| guild_data = guild_response.json() | |
| # Get matching groups | |
| user_groups = Group.objects.filter(attributes__discord_role_id__in=guild_data["roles"]) | |
| # Return user data | |
| return { | |
| "name": info.get("global_name"), | |
| "attributes.discord": { | |
| "id": info.get("id"), | |
| "username": info.get("username"), | |
| "discriminator": info.get("discriminator"), | |
| "email": info.get("email"), | |
| "avatar": info.get("avatar"), | |
| "avatar_url": avatar_url | |
| }, | |
| "groups": [group.name for group in user_groups], | |
| "attributes.avatar": avatar_base64 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment