< Summary

Information
Class: Elsa.ExternalAuthentication.Services.HmacExternalAuthenticationHandleHasher
Assembly: Elsa.ExternalAuthentication
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.ExternalAuthentication/Services/HmacExternalAuthenticationHandleHasher.cs
Line coverage
72%
Covered lines: 16
Uncovered lines: 6
Coverable lines: 22
Total lines: 64
Line coverage: 72.7%
Branch coverage
50%
Covered branches: 3
Total branches: 6
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%
.ctor(...)100%11100%
.ctor(...)100%11100%
Hash(...)100%11100%
Dispose()100%11100%
GetKey(...)50%12646.15%

File(s)

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

#LineLine coverage
 1using System.Security.Cryptography;
 2using System.Text;
 3using Elsa.ExternalAuthentication.Contracts;
 4using Elsa.ExternalAuthentication.Options;
 5using Microsoft.Extensions.Options;
 6
 7namespace Elsa.ExternalAuthentication.Services;
 8
 9/// <summary>
 10/// HMAC-SHA-256 handle hasher. It uses a configured shared key when present and a process-local
 11/// key for single-node development otherwise.
 12/// </summary>
 13public sealed class HmacExternalAuthenticationHandleHasher : IExternalAuthenticationHandleHasher, IDisposable
 14{
 15    private readonly byte[] _key;
 16
 17    /// <summary>
 18    /// Creates a process-local hasher for tests and single-node development.
 19    /// </summary>
 5620    public HmacExternalAuthenticationHandleHasher() : this(RandomNumberGenerator.GetBytes(32))
 21    {
 5622    }
 23
 24    /// <summary>
 25    /// Creates a hasher from deployment-owned External Authentication options.
 26    /// </summary>
 27    public HmacExternalAuthenticationHandleHasher(IOptions<ExternalAuthenticationOptions> options)
 1528        : this(GetKey(options.Value.HandleHashing))
 29    {
 1530    }
 31
 7132    private HmacExternalAuthenticationHandleHasher(byte[] key)
 33    {
 7134        _key = key;
 7135    }
 36
 17637    public string Hash(string value) => Convert.ToHexString(HMACSHA256.HashData(_key, Encoding.UTF8.GetBytes(value)));
 38
 3639    public void Dispose() => CryptographicOperations.ZeroMemory(_key);
 40
 41    private static byte[] GetKey(ExternalAuthenticationHandleHashingOptions? options)
 42    {
 1543        if (options is null)
 044            throw new InvalidOperationException("External Authentication handle-hashing settings are required.");
 45
 1546        if (string.IsNullOrWhiteSpace(options.SharedKeyBase64))
 1347            return RandomNumberGenerator.GetBytes(32);
 48
 49        try
 50        {
 251            var key = Convert.FromBase64String(options.SharedKeyBase64);
 252            if (key.Length >= 32)
 253                return key;
 54
 055            CryptographicOperations.ZeroMemory(key);
 056        }
 057        catch (FormatException)
 58        {
 59            // The options validator reports the actionable configuration error at startup.
 060        }
 61
 062        throw new InvalidOperationException("The External Authentication shared handle-hashing key must be valid base64 
 263    }
 64}