< Summary

Information
Class: Elsa.ExternalAuthentication.Services.BrokerErrorFactory
Assembly: Elsa.ExternalAuthentication
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.ExternalAuthentication/Services/BrokerErrorFactory.cs
Line coverage
95%
Covered lines: 21
Uncovered lines: 1
Coverable lines: 22
Total lines: 46
Line coverage: 95.4%
Branch coverage
96%
Covered branches: 28
Total branches: 29
Branch coverage: 96.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Create(...)92.3%131393.33%
CreateCorrelationId()100%66100%
IsSafeCorrelationId(...)100%1010100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.ExternalAuthentication/Services/BrokerErrorFactory.cs

#LineLine coverage
 1using System.Diagnostics;
 2using Elsa.ExternalAuthentication.Models;
 3
 4namespace Elsa.ExternalAuthentication.Services;
 5
 6/// <summary>
 7/// Produces the deliberately small, non-sensitive error contract exposed by the authentication broker.
 8/// </summary>
 9public static class BrokerErrorFactory
 10{
 11    public static PublicBrokerError Create(BrokerErrorCategory category, string? correlationId = null)
 12    {
 3713        var (error, message) = category switch
 3714        {
 1415            BrokerErrorCategory.InvalidRequest => ("invalid_request", "The sign-in request is invalid. Start again."),
 516            BrokerErrorCategory.MethodUnavailable => ("method_unavailable", "This sign-in method is unavailable."),
 617            BrokerErrorCategory.AuthenticationFailed => ("authentication_failed", "Authentication could not be completed
 118            BrokerErrorCategory.IdentityUnlinked => ("identity_unlinked", "This identity is not permitted to sign in."),
 119            BrokerErrorCategory.FlowExpired => ("flow_expired", "The sign-in attempt expired. Start again."),
 320            BrokerErrorCategory.FlowChanged => ("flow_changed", "The sign-in method changed. Start again."),
 121            BrokerErrorCategory.AccessDenied => ("access_denied", "Access was denied."),
 122            BrokerErrorCategory.RateLimited => ("rate_limited", "Too many sign-in attempts. Try again later."),
 323            BrokerErrorCategory.TemporarilyUnavailable => ("temporarily_unavailable", "Sign-in is temporarily unavailabl
 224            BrokerErrorCategory.ServerError => ("server_error", "Sign-in could not be completed. Try again later."),
 025            _ => throw new ArgumentOutOfRangeException(nameof(category), category, null)
 3726        };
 27
 3728        return new PublicBrokerError(error, message, IsSafeCorrelationId(correlationId) ? correlationId! : CreateCorrela
 29    }
 30
 31    public static string CreateCorrelationId()
 32    {
 5033        var traceId = Activity.Current?.TraceId.ToString();
 5034        return !string.IsNullOrWhiteSpace(traceId) && traceId != "00000000000000000000000000000000"
 5035            ? traceId
 5036            : ActivityTraceId.CreateRandom().ToString();
 37    }
 38
 39    private static bool IsSafeCorrelationId(string? correlationId)
 40    {
 3741        if (string.IsNullOrWhiteSpace(correlationId) || correlationId.Length > 128)
 2542            return false;
 43
 24744        return correlationId.All(character => char.IsAsciiLetterOrDigit(character) || character is '-' or '_');
 45    }
 46}