< Summary

Information
Class: Elsa.Identity.Providers.DefaultApiKeyProvider
Assembly: Elsa.Identity
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Providers/DefaultApiKeyProvider.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 14
Coverable lines: 14
Total lines: 46
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 2
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%
ProvideAsync()0%620%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Providers/DefaultApiKeyProvider.cs

#LineLine coverage
 1using System.Security.Claims;
 2using AspNetCore.Authentication.ApiKey;
 3using Elsa.Identity.Contracts;
 4using Elsa.Identity.Models;
 5
 6namespace Elsa.Identity.Providers;
 7
 8/// <summary>
 9/// Validates a given API key and returns an instance of <see cref="IApiKey"/> if the key is valid.
 10/// </summary>
 11public class DefaultApiKeyProvider : IApiKeyProvider
 12{
 13    private readonly IApplicationCredentialsValidator _applicationCredentialsValidator;
 14    private readonly IRoleProvider _roleProvider;
 15
 16    /// <summary>
 17    /// Initializes a new instance of the <see cref="DefaultApiKeyProvider"/> class.
 18    /// </summary>
 019    public DefaultApiKeyProvider(IApplicationCredentialsValidator applicationCredentialsValidator, IRoleProvider rolePro
 20    {
 021        _applicationCredentialsValidator = applicationCredentialsValidator;
 022        _roleProvider = roleProvider;
 023    }
 24
 25    /// <summary>
 26    /// Gets an instance of <see cref="IApiKey"/>.
 27    /// </summary>
 28    /// <param name="key">The API key to validate.</param>
 29    /// <returns>The API key if a valid key was provided.</returns>
 30    public async Task<IApiKey?> ProvideAsync(string key)
 31    {
 032        var application = await _applicationCredentialsValidator.ValidateAsync(key);
 33
 034        if (application == null)
 035            return null;
 36
 037        var filter = new RoleFilter { Ids = application.Roles.Distinct().ToList() };
 038        var roles = (await _roleProvider.FindManyAsync(filter)).ToList();
 039        var permissions = roles.SelectMany(x => x.Permissions).Distinct().ToList();
 040        var claims = new List<Claim>();
 41
 042        claims.AddRange(permissions.Select(p => new Claim("permissions", p)));
 43
 044        return new ApiKey(key, application.ClientId, claims);
 045    }
 46}