< Summary - Estud

Information
Class: Estud.Back.Auth.Managers.SsoSchemeManager
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Auth/Managers/SsoSchemeManager.cs
Tag: 114_29044117136
Line coverage
84%
Covered lines: 22
Uncovered lines: 4
Coverable lines: 26
Total lines: 59
Line coverage: 84.6%
Branch coverage
50%
Covered branches: 2
Total branches: 4
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
RegisterScheme(...)100%22100%
RemoveScheme(...)100%11100%
UpdateScheme(...)100%210%
IsStale(...)0%620%

File(s)

/home/runner/work/syki/syki/Back/Auth/Managers/SsoSchemeManager.cs

#LineLine coverage
 1using Estud.Back.Auth.Schemes;
 2using Estud.Back.Domain.Identity;
 3using Microsoft.Extensions.Options;
 4using System.Collections.Concurrent;
 5using Microsoft.AspNetCore.Authentication;
 6using Microsoft.AspNetCore.Authentication.OpenIdConnect;
 7
 8namespace Estud.Back.Auth.Managers;
 9
 210public class SsoSchemeManager(
 211    SsoEncryptionManager encryption,
 212    IAuthenticationSchemeProvider schemeProvider,
 213    IOptionsMonitorCache<OpenIdConnectOptions> optionsCache,
 214    IEnumerable<IPostConfigureOptions<OpenIdConnectOptions>> postConfigureOptions)
 15{
 216    private readonly ConcurrentDictionary<string, DateTime> _schemeTimestamps = new();
 17
 18    public void RegisterScheme(SsoConfiguration config)
 19    {
 1220        RemoveScheme(config.PublicId);
 1221        var schemeName = $"{SsoOidcScheme.Prefix}{config.PublicId}";
 22
 1223        config.ClientSecret = encryption.Decrypt(config.ClientSecret);
 24
 1225        var options = new OpenIdConnectOptions();
 1226        SsoOidcScheme.ConfigureSsoSchemeOptions(options, config);
 27
 4828        foreach (var postConfigure in postConfigureOptions)
 29        {
 1230            postConfigure.PostConfigure(schemeName, options);
 31        }
 32
 33        // Options must be cached BEFORE the scheme is registered.
 34        // Otherwise, concurrent requests can trigger the auth middleware to resolve
 35        // default (empty) options via the factory, which TryAdd won't overwrite.
 1236        optionsCache.TryAdd(schemeName, options);
 1237        schemeProvider.AddScheme(new AuthenticationScheme(schemeName, schemeName, typeof(OpenIdConnectHandler)));
 1238        _schemeTimestamps[schemeName] = config.UpdatedAt;
 1239    }
 40
 41    public void RemoveScheme(Guid configExternalId)
 42    {
 1243        var schemeName = $"{SsoOidcScheme.Prefix}{configExternalId}";
 1244        schemeProvider.RemoveScheme(schemeName);
 1245        optionsCache.TryRemove(schemeName);
 1246        _schemeTimestamps.TryRemove(schemeName, out _);
 1247    }
 48
 49    public void UpdateScheme(SsoConfiguration config)
 50    {
 051        RemoveScheme(config.PublicId);
 052        RegisterScheme(config);
 053    }
 54
 55    public bool IsStale(string schemeName, DateTime dbUpdatedAt)
 56    {
 057        return !_schemeTimestamps.TryGetValue(schemeName, out var cached) || cached < dbUpdatedAt;
 58    }
 59}