< Summary - Heimdall API

Information
Class: ArturRios.Heimdall.Command.Services.TotpSecretProtector
Assembly: ArturRios.Heimdall.Command
File(s): /home/runner/work/heimdall-api/heimdall-api/src/Application/ArturRios.Heimdall.Command/Services/TotpSecretProtector.cs
Line coverage
100%
Covered lines: 5
Uncovered lines: 0
Coverable lines: 5
Total lines: 33
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage
100%
Covered methods: 3
Fully covered methods: 3
Total methods: 3
Method coverage: 100%
Full method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Protect(...)100%11100%
Unprotect(...)100%11100%

File(s)

/home/runner/work/heimdall-api/heimdall-api/src/Application/ArturRios.Heimdall.Command/Services/TotpSecretProtector.cs

#LineLine coverage
 1using System.Text;
 2using Microsoft.AspNetCore.DataProtection;
 3
 4namespace ArturRios.Heimdall.Command.Services;
 5
 6/// <summary>
 7///     Default <see cref="ITotpSecretProtector" />, wrapping ASP.NET Core's Data Protection API
 8///     (UC-36 step 3, FR-2F-02). Registered from <c>Startup.AddDependencies</c>, which also adds
 9///     <c>AddDataProtection()</c> to the container this protector's <see cref="IDataProtector" /> is
 10///     resolved from.
 11/// </summary>
 12/// <remarks>
 13///     A single, fixed purpose string scopes the protector to this one use — Data Protection derives
 14///     a different key for every distinct purpose, so a secret protected here cannot be unprotected
 15///     by any other component that also happens to call <c>CreateProtector</c>.
 16/// </remarks>
 17public class TotpSecretProtector : ITotpSecretProtector
 18{
 19    private const string Purpose = "ArturRios.Heimdall.TwoFactorAuth.TotpSecret.v1";
 20
 21    private readonly IDataProtector _protector;
 22
 18823    public TotpSecretProtector(IDataProtectionProvider provider)
 24    {
 18825        _protector = provider.CreateProtector(Purpose);
 18826    }
 27
 28    public byte[] Protect(string base32Secret) =>
 1929        _protector.Protect(Encoding.UTF8.GetBytes(base32Secret));
 30
 31    public string Unprotect(byte[] protectedSecret) =>
 3332        Encoding.UTF8.GetString(_protector.Unprotect(protectedSecret));
 33}