microsoftEntraProvider function

Microsoft Entra ID (Azure AD) OAuth provider.

Based on Microsoft's OAuth 2.0 and OpenID Connect documentation.

Resources

Example

final provider = microsoftEntraProvider(
  MicrosoftEntraProviderOptions(
    clientId: 'client-id',
    clientSecret: 'client-secret',
    tenantId: 'your-tenant-id',
    redirectUri: 'https://example.com/auth/callback/microsoft-entra-id',
  ),
);

Implementation

OAuthProvider<MicrosoftEntraProfile> microsoftEntraProvider(
  MicrosoftEntraProviderOptions options,
) {
  final issuerPath = options._issuerPath;
  final baseUrl = 'https://login.microsoftonline.com/$issuerPath/v2.0';

  return OAuthProvider<MicrosoftEntraProfile>(
    id: 'microsoft-entra-id',
    name: 'Microsoft Entra ID',
    type: AuthProviderType.oidc,
    clientId: options.clientId,
    clientSecret: options.clientSecret,
    authorizationEndpoint: Uri.parse('$baseUrl/authorize'),
    tokenEndpoint: Uri.parse('$baseUrl/token'),
    userInfoEndpoint: Uri.parse('https://graph.microsoft.com/oidc/userinfo'),
    redirectUri: options.redirectUri,
    scopes: options.scopes,
    profileParser: MicrosoftEntraProfile.fromJson,
    profileSerializer: (profile) => profile.toJson(),
    profile: (profile) {
      return AuthUser(
        id: profile.sub,
        name: profile.name,
        email: profile.email ?? profile.preferredUsername,
        image: profile.picture,
        attributes: profile.toJson(),
      );
    },
  );
}