< Summary

Information
Class: Syki.Daemon.HangfireAuthFilter
Assembly: Daemon
File(s): /home/runner/work/syki/syki/Daemon/AuthorizationFilter.cs
Tag: 22_11348620282
Line coverage
92%
Covered lines: 13
Uncovered lines: 1
Coverable lines: 14
Total lines: 39
Line coverage: 92.8%
Branch coverage
80%
Covered branches: 8
Total branches: 10
Branch coverage: 80%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Authorize(...)100%22100%
Challenge(...)100%11100%
CheckAuth(...)75%8.13887.5%

File(s)

/home/runner/work/syki/syki/Daemon/AuthorizationFilter.cs

#LineLine coverage
 1using System.Text;
 2using Hangfire.Dashboard;
 3using Hangfire.Annotations;
 4using System.Net.Http.Headers;
 5
 6namespace Syki.Daemon;
 7
 18public class HangfireAuthFilter(string user, string password) : IDashboardAuthorizationFilter
 9{
 10    public bool Authorize([NotNull] DashboardContext context)
 11  {
 312    var authHeader = context.GetHttpContext().Request.Headers.Authorization;
 313    return CheckAuth(authHeader) || Challenge(context);
 14  }
 15
 16  private static bool Challenge([NotNull] DashboardContext context)
 17  {
 218    context.GetHttpContext().Response.StatusCode = 401;
 219    context.GetHttpContext().Response.Headers.Append("WWW-Authenticate", "Basic realm=\"Hangfire Dashboard\"");
 220    return false;
 21  }
 22
 23  private bool CheckAuth(string authHeader)
 24  {
 425    if (authHeader.IsEmpty()) return false;
 26
 227    var authValues = AuthenticationHeaderValue.Parse(authHeader);
 228    if ("Basic".Equals(authValues.Scheme, StringComparison.InvariantCultureIgnoreCase))
 29    {
 230      var parameter = Encoding.UTF8.GetString(Convert.FromBase64String(authValues.Parameter));
 231      var parts = parameter.Split(':');
 232      if (parts.Length > 1)
 33      {
 234        return parts[0] == user && parts[1] == password;
 35      }
 36    }
 037    return false;
 38  }
 39}