< Summary - Combined Code Coverage

Information
Class: NLightning.Daemon.Services.Ipc.CookieFileAuthenticator
Assembly: NLightning.Daemon
File(s): /home/runner/work/NLightning/NLightning/src/NLightning.Daemon/Services/Ipc/CookieFileAuthenticator.cs
Tag: 57_24045730253
Line coverage
0%
Covered lines: 0
Uncovered lines: 15
Coverable lines: 15
Total lines: 44
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%
ValidateAsync()0%2040%
FixedTimeEquals(...)100%210%

File(s)

/home/runner/work/NLightning/NLightning/src/NLightning.Daemon/Services/Ipc/CookieFileAuthenticator.cs

#LineLine coverage
 1using System.Security.Cryptography;
 2using Microsoft.Extensions.Logging;
 3
 4namespace NLightning.Daemon.Services.Ipc;
 5
 6using Daemon.Ipc.Interfaces;
 7
 8/// <summary>
 9/// Cookie-file-based authenticator (Bitcoin Core style). Uses constant-time comparison.
 10/// </summary>
 11public sealed class CookieFileAuthenticator : IIpcAuthenticator
 12{
 13    private readonly string _cookieFilePath;
 14    private readonly ILogger<CookieFileAuthenticator> _logger;
 15
 016    public CookieFileAuthenticator(string cookieFilePath, ILogger<CookieFileAuthenticator> logger)
 17    {
 018        _cookieFilePath = cookieFilePath;
 019        _logger = logger;
 020    }
 21
 22    public async Task<bool> ValidateAsync(string? token, CancellationToken ct = default)
 23    {
 24        try
 25        {
 026            if (string.IsNullOrEmpty(token)) return false;
 027            if (!File.Exists(_cookieFilePath)) return false;
 028            var expected = (await File.ReadAllTextAsync(_cookieFilePath, ct)).Trim();
 029            return FixedTimeEquals(expected, token);
 30        }
 031        catch (Exception ex)
 32        {
 033            _logger.LogWarning(ex, "Auth validation failed");
 034            return false;
 35        }
 036    }
 37
 38    private static bool FixedTimeEquals(string a, string b)
 39    {
 040        var aBytes = System.Text.Encoding.UTF8.GetBytes(a);
 041        var bBytes = System.Text.Encoding.UTF8.GetBytes(b);
 042        return CryptographicOperations.FixedTimeEquals(aBytes, bBytes);
 43    }
 44}