< Summary - Syki

Information
Class: Syki.Back.Auth.Managers.SsoSchemeManager
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Auth/Managers/SsoSchemeManager.cs
Tag: 56_26538939494
Line coverage
0%
Covered lines: 0
Uncovered lines: 26
Coverable lines: 26
Total lines: 59
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

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

File(s)

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

#LineLine coverage
 1using Syki.Back.Auth.Schemes;
 2using Syki.Back.Domain.Identity;
 3using Microsoft.Extensions.Options;
 4using System.Collections.Concurrent;
 5using Microsoft.AspNetCore.Authentication;
 6using Microsoft.AspNetCore.Authentication.OpenIdConnect;
 7
 8namespace Syki.Back.Auth.Managers;
 9
 010public class SsoSchemeManager(
 011    SsoEncryptionManager encryption,
 012    IAuthenticationSchemeProvider schemeProvider,
 013    IOptionsMonitorCache<OpenIdConnectOptions> optionsCache,
 014    IEnumerable<IPostConfigureOptions<OpenIdConnectOptions>> postConfigureOptions)
 15{
 016    private readonly ConcurrentDictionary<string, DateTime> _schemeTimestamps = new();
 17
 18    public void RegisterScheme(SsoConfiguration config)
 19    {
 020        RemoveScheme(config.PublicId);
 021        var schemeName = $"{SsoOidcScheme.Prefix}{config.PublicId}";
 22
 023        config.ClientSecret = encryption.Decrypt(config.ClientSecret);
 24
 025        var options = new OpenIdConnectOptions();
 026        SsoOidcScheme.ConfigureSsoSchemeOptions(options, config);
 27
 028        foreach (var postConfigure in postConfigureOptions)
 29        {
 030            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.
 036        optionsCache.TryAdd(schemeName, options);
 037        schemeProvider.AddScheme(new AuthenticationScheme(schemeName, schemeName, typeof(OpenIdConnectHandler)));
 038        _schemeTimestamps[schemeName] = config.UpdatedAt;
 039    }
 40
 41    public void RemoveScheme(Guid configExternalId)
 42    {
 043        var schemeName = $"{SsoOidcScheme.Prefix}{configExternalId}";
 044        schemeProvider.RemoveScheme(schemeName);
 045        optionsCache.TryRemove(schemeName);
 046        _schemeTimestamps.TryRemove(schemeName, out _);
 047    }
 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}