• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Sholtee / warehouse / 57

05 Feb 2025 04:57AM UTC coverage: 93.36% (-0.1%) from 93.461%
57

push

appveyor

Sholtee
profiling & API explorer can be disabled from configuration

1181 of 1265 relevant lines covered (93.36%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.56
/SRC/App/Warehouse.Host/Infrastructure/Registrations/Registrations.Profiler.cs
1
/********************************************************************************
2
* Registrations.Profiler.cs                                                     *
3
*                                                                               *
4
* Author: Denes Solti                                                           *
5
* Project: Warehouse API (boilerplate)                                          *
6
* License: MIT                                                                  *
7
********************************************************************************/
8
using System;
9
using System.Security.Principal;
10

11
using Microsoft.AspNetCore.Authentication;
12
using Microsoft.AspNetCore.Builder;
13
using Microsoft.Extensions.Configuration;
14
using Microsoft.Extensions.DependencyInjection;
15
using Microsoft.Extensions.DependencyInjection.Extensions;
16
using StackExchange.Profiling;
17
using StackExchange.Profiling.Storage;
18

19
namespace Warehouse.Host.Infrastructure.Registrations
20
{
21
    using Core.Auth;
22
    using Core.Extensions;
23

24
    internal static partial class Registrations
25
    {
26
        public static IServiceCollection AddProfiler(this IServiceCollection services, IConfiguration configuration)
27
        {
1✔
28
            if (configuration.GetSection("MiniProfiler") is null)
1✔
29
                return services;
×
30

31
            services.TryAddScoped(_ => MiniProfiler.Current!);
×
32
            services.AddMiniProfiler(options =>
1✔
33
            {
1✔
34
                options.RouteBasePath = configuration.GetRequiredValue<string>("MiniProfiler:RouteBasePath");
1✔
35

1✔
36
                string? endpoint = configuration.GetValue<string>("WAREHOUSE_REDIS_ENDPOINT");
1✔
37
                if (!string.IsNullOrWhiteSpace(endpoint))
1✔
38
                    options.Storage = new RedisStorage(endpoint);
×
39

1✔
40
                //
1✔
41
                // Only the allowed user can see the profiling results
1✔
42
                //
1✔
43

1✔
44
                options.ResultsAuthorizeAsync = options.ResultsListAuthorizeAsync = async static req =>
1✔
45
                {
1✔
46
                    IConfigurationSection profilerConfig = req
1✔
47
                        .HttpContext
1✔
48
                        .RequestServices
1✔
49
                        .GetRequiredService<IConfiguration>()
1✔
50
                        .GetRequiredSection("MiniProfiler");
1✔
51

1✔
52
                    if (!req.Path.StartsWithSegments(profilerConfig.GetRequiredValue<string>("RouteBasePath"), StringComparison.OrdinalIgnoreCase))
1✔
53
                        return false;
1✔
54

1✔
55
                    IIdentity? identity = req.HttpContext.User.Identity;
×
56
                    if (identity?.IsAuthenticated is not true)
×
57
                    {
×
58
                        AuthenticateResult result = await req.HttpContext.AuthenticateAsync(WarehouseAuthentication.SCHEME);
×
59
                        if (result.Succeeded)
×
60
                            identity = result.Principal.Identity;
×
61
                    }
×
62

1✔
63
                    return identity?.IsAuthenticated is true && identity.Name == profilerConfig.GetRequiredValue<string>("AllowedUser");
×
64
                };
1✔
65
            });
1✔
66

67
            return services;
1✔
68
        }
1✔
69

70
        public static IApplicationBuilder UseProfiling(this IApplicationBuilder builder)
71
        {
1✔
72
            if (builder.ApplicationServices.GetRequiredService<IConfiguration>().GetSection("MiniProfiler") is not null)
1✔
73
                builder.UseMiniProfiler();
1✔
74
            return builder;
1✔
75
        }
1✔
76
    }
77
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc