< Summary

Information
Class: Api.Auth.Handlers.AuthHandler
Assembly: Api
File(s): /home/runner/work/ProjectRead.ing/ProjectRead.ing/Api/Auth/Handlers/AuthHandler.cs
Line coverage
33%
Covered lines: 9
Uncovered lines: 18
Coverable lines: 27
Total lines: 58
Line coverage: 33.3%
Branch coverage
75%
Covered branches: 6
Total branches: 8
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
HandleAuthenticateAsync()83.33%18630.43%

File(s)

/home/runner/work/ProjectRead.ing/ProjectRead.ing/Api/Auth/Handlers/AuthHandler.cs

#LineLine coverage
 1// SPDX-FileCopyrightText: 2026 Alper Çelik <[email protected]>
 2//
 3// SPDX-License-Identifier: AGPL-3.0-or-later
 4
 5using System.Buffers.Text;
 6using System.Security.Claims;
 7using System.Text.Encodings.Web;
 8
 9using Api.Auth.Models;
 10using Api.Auth.Utils;
 11using Api.Database;
 12
 13using Microsoft.AspNetCore.Authentication;
 14using Microsoft.EntityFrameworkCore;
 15using Microsoft.Extensions.Options;
 16
 17namespace Api.Auth.Handlers;
 18
 19class AuthHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, PGCon
 120    : AuthenticationHandler<AuthenticationSchemeOptions>(options, logger, encoder)
 21{
 22    protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
 123    {
 124        string?[] tokenHashes = [Context.Request.Headers.Authorization.LastOrDefault(), Context.Request.Cookies[LoginUti
 125        List<byte[]> userTokenHashes = [.. tokenHashes
 126            .Where(s => s != null && s.StartsWith(LoginUtils.UserTokenPrefix))
 027        .Select(s => Base64Url.DecodeFromChars(
 028                    s.AsSpan()[(LoginUtils.UserTokenPrefix.Length - 1)..]))];
 29
 130        var userToken = await db.UserTokens.Where(ut => userTokenHashes.Contains(ut.TokenHash)).FirstOrDefaultAsync();
 31
 132        if (userToken is not null)
 033        {
 034            await LoginUtils.UpdateLastUsedForUserToken(userToken, db);
 35
 36
 037            Claim[] claims = [
 038                new Claim(ClaimTypes.NameIdentifier,userToken.UserId.ToString()),
 039                new Claim(UserToken.PermissionBitsType,((long)userToken.Permissions).ToString()),
 040            ];
 41
 042            return AuthenticateResult.Success(
 043                    new AuthenticationTicket(
 044                        new ClaimsPrincipal(
 045                                new ClaimsPrincipal(new ClaimsIdentity(
 046                                        claims
 047                                        )
 048                                    )
 049                            ),
 050                        Scheme.Name)
 051                    );
 52        }
 53
 54
 155        return AuthenticateResult.NoResult();
 156    }
 57
 58}