Skip to content

Commit

Permalink
Added logging to diagnose group matches (#30)
Browse files Browse the repository at this point in the history
* Added logging for failures on entra id mapping

* Fix for compilation issue

Never rename properties on a Saturday morning! :-)

* Update README.md

---------

Co-authored-by: Steve Temple <[email protected]>
  • Loading branch information
RachBreeze and stevetemple authored Oct 24, 2024
1 parent c7b1f77 commit 3019bf5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ You'll need to configure the package by adding the following section to the root
],
"Icon": "fa fa-lock",
"ButtonStyle": "btn-microsoft",
"LogUnmappedRolesAsWarning": false
},
```
On Umbraco v13+ change the `Icon` to `"icon-microsoft-fill"`, i.e. `"Icon": "icon-microsoft-fill",`
Expand Down Expand Up @@ -166,5 +167,10 @@ i.e.
}
```

## LogUnmappedRolesAsWarning

When `SetGroupsOnLogin` is set to true, if `LogUnmappedRolesAsWarning` is also set to true this will log as warning for unmapped Entra ID groups, where the Entra ID name has a slash `\` in it. Be design it does not log everything to prevent logging of email addresses and so on.




2 changes: 2 additions & 0 deletions src/Umbraco.Community.AzureSSO/AzureSSOConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class AzureSSOConfiguration
public Dictionary<string, string> GroupBindings { get; set; } = new();

public bool? SetGroupsOnLogin { get; set; }

public bool? LogUnmappedRolesAsWarning { get; set; }

public string[]? DefaultGroups { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Security.Claims;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core.Security;
using Umbraco.Cms.Web.BackOffice.Security;
Expand All @@ -14,6 +15,16 @@ public class MicrosoftAccountBackOfficeExternalLoginProviderOptions(AzureSsoSett
{
public const string SchemeName = "MicrosoftAccount";

private readonly AzureSsoSettings _settings;
private readonly ILogger<MicrosoftAccountBackOfficeExternalLoginProviderOptions> _logger;

public MicrosoftAccountBackOfficeExternalLoginProviderOptions(AzureSsoSettings settings,
ILogger<MicrosoftAccountBackOfficeExternalLoginProviderOptions> logger)
{
_settings = settings;
_logger = logger;
}

public void Configure(string? name, BackOfficeExternalLoginProviderOptions options)
{
var profile = settings.Profiles
Expand Down Expand Up @@ -90,7 +101,8 @@ private void SetGroups(BackOfficeIdentityUser user, ExternalLoginInfo loginInfo,
{
user.Roles.Clear();

var groups = loginInfo.Principal.Claims.Where(c => settings.GroupLookup.ContainsKey(c.Value));
var groups = loginInfo.Principal.Claims.Where(c => _settings.GroupLookup.ContainsKey(c.Value)).ToList();

foreach (var group in groups)
{
var umbracoGroups = settings.GroupLookup[group.Value].Split(',');
Expand All @@ -104,6 +116,16 @@ private void SetGroups(BackOfficeIdentityUser user, ExternalLoginInfo loginInfo,
{
user.AddRole(group);
}

if (_settings.LogUnmappedRolesAsWarning)
{
var unmappedGroups = loginInfo.Principal.Claims.Where(c => !_settings.GroupLookup.ContainsKey(c.Value) && c.Value.Contains("\\")).Select(c => c.Value).ToList();
if (unmappedGroups.Any())
{
_logger.LogWarning("The following groups were not mapped to Umbraco roles: {Groups}", string.Join(", ", unmappedGroups));
}
}

}

private void SetName(BackOfficeIdentityUser user, ExternalLoginInfo loginInfo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public AzureSsoSettings(AzureSSOConfiguration configuration)

Profiles = configuration.Profiles.Select(x => new AzureSsoProfileSettings(x)).ToArray();
}

public AzureSsoProfileSettings[] Profiles { get; }
}

Expand All @@ -28,6 +27,7 @@ public class AzureSsoProfileSettings(AzureSSOConfiguration configuration)
public string Icon => configuration.Icon ?? "fa fa-lock";
public Dictionary<string, string> GroupLookup => configuration.GroupBindings;
public bool SetGroupsOnLogin => configuration.SetGroupsOnLogin ?? true;
public bool LogUnmappedRolesAsWarning => _configuration.LogUnmappedRolesAsWarning ?? false;
public string[] DefaultGroups => configuration.DefaultGroups ?? System.Array.Empty<string>();
public bool DenyLocalLogin => configuration.DenyLocalLogin ?? false;
public TokenCacheType TokenCacheType => configuration.TokenCacheType;
Expand Down

0 comments on commit 3019bf5

Please sign in to comment.