< Summary

Information
Class: Elsa.ExternalAuthentication.Endpoints.Sessions.ExternalAuthenticationSessionListResponse
Assembly: Elsa.ExternalAuthentication
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.ExternalAuthentication/Endpoints/Sessions/ExternalAuthenticationSessionEndpoints.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 1
Coverable lines: 1
Total lines: 120
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Items()100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.ExternalAuthentication/Endpoints/Sessions/ExternalAuthenticationSessionEndpoints.cs

#LineLine coverage
 1using System.Security.Claims;
 2using System.Text;
 3using System.Text.Json;
 4using Elsa.Abstractions;
 5using Elsa.Common.Multitenancy;
 6using Elsa.ExternalAuthentication.Contracts;
 7using Elsa.ExternalAuthentication.Notifications;
 8using Elsa.ExternalAuthentication.Options;
 9using Elsa.ExternalAuthentication.Permissions;
 10using Elsa.ExternalAuthentication.Services;
 11using Microsoft.AspNetCore.Http;
 12using Microsoft.Extensions.Options;
 13
 14namespace Elsa.ExternalAuthentication.Endpoints.Sessions;
 15
 16internal sealed class ListExternalAuthenticationSessions(
 17    IExternalAuthenticationSessionStore sessions,
 18    ITenantAccessor tenantAccessor,
 19    IOptions<ExternalAuthenticationOptions> options) : ElsaEndpoint<ExternalAuthenticationSessionListRequest, ExternalAu
 20{
 21    public override void Configure()
 22    {
 23        Get("/external-authentication/sessions");
 24        ConfigurePermissions(ExternalAuthenticationPermissions.SessionsRead);
 25    }
 26
 27    public override async Task<ExternalAuthenticationSessionListResponse> ExecuteAsync(ExternalAuthenticationSessionList
 28    {
 29        if (!options.Value.Operations.EnableSessionAdministration)
 30        {
 31            HttpContext.Response.StatusCode = StatusCodes.Status404NotFound;
 32            return new ExternalAuthenticationSessionListResponse([], null);
 33        }
 34        if (request.PageSize is < 1 or > 100 || !IsKnownStatus(request.Status) || !TryDecode(request.Cursor, out var cur
 35        {
 36            HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
 37            return new ExternalAuthenticationSessionListResponse([], null);
 38        }
 39        var pageSize = request.PageSize ?? 100;
 40        var rows = (await sessions.FindAsync(new Models.ExternalAuthenticationSessionFilter { TenantId = tenantAccessor.
 41            .OrderBy(x => x.StartedAt).ThenBy(x => x.Id, StringComparer.Ordinal)
 42            .Where(x => cursor is null || Compare(new SessionCursor(x.StartedAt, x.Id), cursor) > 0)
 43            .Take(pageSize + 1).ToArray();
 44        var hasMore = rows.Length > pageSize;
 45        var page = hasMore ? rows[..^1] : rows;
 46        return new ExternalAuthenticationSessionListResponse(page.Select(ExternalAuthenticationSessionDocument.From).ToA
 47    }
 48
 49    private static bool IsKnownStatus(string? status) => string.IsNullOrWhiteSpace(status) || status.Equals("active", St
 50    private static int Compare(SessionCursor left, SessionCursor right) => left.StartedAt != right.StartedAt ? left.Star
 51    internal static string Encode(SessionCursor cursor) => Convert.ToBase64String(Encoding.UTF8.GetBytes(JsonSerializer.
 52    internal static bool TryDecode(string? value, out SessionCursor? cursor)
 53    {
 54        cursor = null;
 55        if (string.IsNullOrWhiteSpace(value)) return true;
 56        try
 57        {
 58            var padded = value.Replace('-', '+').Replace('_', '/');
 59            padded = padded.PadRight(padded.Length + (4 - padded.Length % 4) % 4, '=');
 60            cursor = JsonSerializer.Deserialize<SessionCursor>(Encoding.UTF8.GetString(Convert.FromBase64String(padded))
 61            return cursor is { Id.Length: > 0 and <= 256 };
 62        }
 63        catch (Exception exception) when (exception is FormatException or JsonException) { return false; }
 64    }
 65}
 66
 67internal sealed class RevokeExternalAuthenticationSession(
 68    IExternalAuthenticationSessionStore sessions,
 69    ITenantAccessor tenantAccessor,
 70    IOptions<ExternalAuthenticationOptions> options,
 71    ExternalAuthenticationSecurityNotifier notifier) : ElsaEndpoint<RevokeExternalAuthenticationSessionRequest>
 72{
 73    public override void Configure()
 74    {
 75        Delete("/external-authentication/sessions/{sessionId}");
 76        ConfigurePermissions(ExternalAuthenticationPermissions.SessionsRevoke);
 77    }
 78
 79    public override async Task HandleAsync(RevokeExternalAuthenticationSessionRequest request, CancellationToken cancell
 80    {
 81        if (!options.Value.Operations.EnableSessionAdministration)
 82        {
 83            HttpContext.Response.StatusCode = StatusCodes.Status404NotFound;
 84            return;
 85        }
 86        var session = await sessions.FindByIdAsync(Route<string>("sessionId")!, cancellationToken);
 87        if (session is null || !string.Equals(session.TenantId, tenantAccessor.TenantId, StringComparison.Ordinal))
 88        {
 89            HttpContext.Response.StatusCode = StatusCodes.Status404NotFound;
 90            return;
 91        }
 92        var revoked = await sessions.RevokeAsync(session.Id, string.IsNullOrWhiteSpace(request.Reason) ? "administrator_
 93        if (!revoked)
 94        {
 95            HttpContext.Response.StatusCode = StatusCodes.Status409Conflict;
 96            return;
 97        }
 98        await notifier.PublishAsync(new ExternalAuthenticationSessionRevoked(
 99            ExternalAuthenticationSecurityNotifier.Context(User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? User.Find
 100            session.Id,
 101            "administrator_revoked"), cancellationToken);
 102        HttpContext.Response.StatusCode = StatusCodes.Status204NoContent;
 103    }
 104}
 105
 106internal sealed class ExternalAuthenticationSessionListRequest
 107{
 108    public string? UserId { get; set; }
 109    public string? ConnectionKey { get; set; }
 110    public string? Status { get; set; }
 111    public string? Cursor { get; set; }
 112    public int? PageSize { get; set; }
 113}
 114internal sealed class RevokeExternalAuthenticationSessionRequest { public string? Reason { get; set; } }
 115internal sealed record SessionCursor(DateTimeOffset StartedAt, string Id);
 0116internal sealed record ExternalAuthenticationSessionListResponse(IReadOnlyCollection<ExternalAuthenticationSessionDocume
 117internal sealed record ExternalAuthenticationSessionDocument(string Id, string UserId, string TenantId, string Connectio
 118{
 119    public static ExternalAuthenticationSessionDocument From(Models.ExternalAuthenticationSession value) => new(value.Id
 120}

Methods/Properties

get_Items()