Repository navigation
Closed
Labels
Description
Summary
Implement automatic provisioning of teams and roles based on Entra ID attributes during SSO authentication. When users log in via Microsoft Entra, the system will automatically sync their roles (from App Roles) and team memberships (from Groups) to the local database.
Goals
- Use Better Auth official SSO plugin for OIDC/SAML authentication
- Auto-assign roles based on Entra App Roles
- Auto-create teams and sync memberships based on Entra Groups
- Add SSO configuration UI in Settings → Integrations
- Maintain compatibility with existing Email and OneDrive integrations
Login Flow Change
Before (Social Provider):
User → Click "Sign in with Microsoft" → Microsoft auth → Login success
After (SSO Plugin):
User → Enter email → Domain detection → Auto-redirect to Entra → Login success
→ organizationProvisioning (join org + set role)
→ provisionUser callback (sync teams)
Data Sources
- Roles: App Roles (via
getRolecallback fromuserInfo.roles) - Teams: Groups (via
provisionUsercallback calling Graph API)
Files to Create/Modify
New Files
| File | Purpose |
|---|---|
convex/betterAuth/sso/team_sync.ts |
Team sync logic |
convex/sso_providers/queries.ts |
SSO provider queries |
convex/sso_providers/mutations.ts |
SSO provider CRUD |
app/features/settings/integrations/components/sso-card.tsx |
SSO config UI card |
app/features/settings/integrations/components/sso-config-dialog.tsx |
SSO config dialog |
Modified Files
| File | Changes |
|---|---|
convex/auth.ts |
Add SSO plugin config |
app/features/settings/integrations/components/integrations-settings.tsx |
Add SSO card |
docker-entrypoint.sh |
Add new env vars |
Azure Configuration Required
API Permissions
GroupMember.Read.All(Delegated)User.Read(Delegated)
App Roles to Define
| Display Name | Value | Description |
|---|---|---|
| Administrator | admin |
Full access |
| Developer | developer |
Development access |
| Editor | editor |
Content editing access |
| Member | member |
Read-only access |
Configuration
Minimal (2 env vars):
SSO_AUTO_PROVISION_ENABLED=true
SSO_DOMAIN=yourcompany.comOptional:
SSO_EXCLUDE_GROUPS=All-Employees,Domain-Users
SSO_TEAM_MEMBERSHIP_MODE=sync # or 'additive'
Activity
larryro commentedon Jan 31, 2026
Teams and Roles Auto-Provisioning via Entra ID SSO
Summary
Implement automatic provisioning using Better Auth's official SSO plugin. When users log in via Entra ID, automatically sync roles and teams to the local database.
Better Auth SSO Plugin Features
Official plugin support (docs):
organizationProvisioning)getRolecallback)provisionUsercallback)To implement: Team sync (in
provisionUsercallback)Current Architecture
convex/auth.tsteamandteamMembertablesdisabled,member,editor,developer,admin)Login Flow Change
Before (Social Provider):
After (SSO Plugin):
UI Change: Login page needs email input field for domain-based SSO routing.
High-Level Flow
Data Sources:
getRolecallback fromuserInfo.roles)provisionUsercallback calling Graph API)Files to Modify/Create
New Files
convex/betterAuth/sso/team_sync.tsconvex/sso_providers/queries.tsconvex/sso_providers/mutations.tsapp/features/settings/integrations/components/sso-card.tsxapp/features/settings/integrations/components/sso-config-dialog.tsxModified Files
convex/auth.tsapp/features/settings/integrations/components/integrations-settings.tsxdocker-entrypoint.shImplementation Steps
Step 1: Install SSO Plugin Dependency
Step 2: Configure SSO Plugin
File:
convex/auth.tsStep 3: Create Team Sync Logic
File:
convex/betterAuth/sso/team_sync.tsStep 4: Register Entra SSO Provider
SSO providers are stored in the
ssoProvidertable. Choose one registration method:Option A: Direct Database Insert (recommended for automated deployment)
Create a seed script or migration to insert into the
ssoProvidertable:Option B: Via API
ssoProviderTable Schema:Step 5: Add SSO Configuration UI
File:
app/features/settings/integrations/components/sso-card.tsxFile:
app/features/settings/integrations/components/sso-config-dialog.tsxForm fields:
https://login.microsoftonline.com/{tenant}/v2.0)Step 6: SSO Provider CRUD API
File:
convex/sso_providers/queries.tsFile:
convex/sso_providers/mutations.tsStep 7: Update Docker Configuration
File:
docker-entrypoint.shNote:
SSO_DOMAINno longer needed as it's configured via UIEntra ID App Registration Requirements
In Azure Portal, the app registration needs:
1. API Permissions
GroupMember.Read.All(Delegated) - Read user group membershipsUser.Read(Delegated) - Already present2. App Roles Definition
Create the following roles in "App roles" page (values must match Tale role names):
admindevelopereditormember3. Assign Users to App Roles
In "Enterprise applications" → Your app → "Users and groups", assign users/groups to corresponding roles
Design Decisions
provisionUserandgetRolecallbacksgetRoleuserInfo.rolesprovisionUsercallbackError Handling
memberroleAudit Log Events
New actions for traceability:
sso_team_provisioned- Team created from Entra groupsso_team_membership_added- User added to teamsso_team_membership_removed- User removed from teamsso_role_synced- Role updated from Entra groupsAll with
metadata: { source: 'entra_oauth', entraGroupId: '...' }for filtering.Verification
Organization Handling
Single-company deployment: Auto-detect the single organization in the system.
organizationtable and uses the only existing orgCompatibility with Existing Integrations
emailProviderstableFiles.ReadscopeOneDrive Compatibility: Include necessary scopes in SSO provider config:
Configuration (Simplified)
Minimal - 2 env vars:
SSO_AUTO_PROVISION_ENABLED=true SSO_DOMAIN=yourcompany.com # For domain associationAutomatic Logic:
getRolecallback gets fromuserInfo.roles(App Roles)provisionUsercallback fetches Groups from Graph API and syncsOptional Configuration:
Critical Files Summary
Sources
feat(platform): add Entra ID SSO team auto-provisioning
feat(sso): implement per-organization Microsoft Entra ID SSO