88using DebugProbe . AspNetCore . Models ;
99using DebugProbe . AspNetCore . Options ;
1010using DebugProbe . AspNetCore . Storage ;
11+ using Microsoft . AspNetCore . Authorization ;
1112using Microsoft . AspNetCore . Builder ;
1213using Microsoft . AspNetCore . Http ;
1314using Microsoft . Extensions . DependencyInjection ;
@@ -63,10 +64,20 @@ public static IServiceCollection AddDebugProbe(this IServiceCollection services,
6364 /// Registers DebugProbe services.
6465 /// </summary>
6566 public static IApplicationBuilder UseDebugProbe ( this IApplicationBuilder app )
67+ {
68+ return app . UseDebugProbe ( configure : null ) ;
69+ }
70+
71+ /// <summary>
72+ /// Registers DebugProbe services and configures runtime endpoint options.
73+ /// </summary>
74+ public static IApplicationBuilder UseDebugProbe ( this IApplicationBuilder app , Action < DebugProbeOptions > ? configure )
6675 {
6776 var options = app . ApplicationServices . GetRequiredService < DebugProbeOptions > ( ) ;
6877 var environment = app . ApplicationServices . GetRequiredService < IHostEnvironment > ( ) ;
6978
79+ configure ? . Invoke ( options ) ;
80+
7081 options . AllowLocalCompareTargets ??= environment . IsDevelopment ( ) ;
7182
7283 app . UseMiddleware < DebugProbeMiddleware > ( ) ;
@@ -76,7 +87,7 @@ public static IApplicationBuilder UseDebugProbe(this IApplicationBuilder app)
7687 {
7788 if ( ShouldMapUiEndpoints ( environment , options ) )
7889 {
79- webApp . MapGet ( "/debug" , async ( HttpContext ctx , DebugEntryStore store ) =>
90+ RequireDebugAuthorization ( webApp . MapGet ( "/debug" , async ( HttpContext ctx , DebugEntryStore store ) =>
8091 {
8192 var items = store . GetAll ( )
8293 . OrderByDescending ( x => x . Timestamp )
@@ -87,9 +98,9 @@ public static IApplicationBuilder UseDebugProbe(this IApplicationBuilder app)
8798
8899 await ctx . Response . WriteAsync ( html ) ;
89100
90- } ) . ExcludeFromDescription ( ) ;
101+ } ) . ExcludeFromDescription ( ) , options ) ;
91102
92- webApp . MapGet ( "/debug/{id}" , async ( HttpContext ctx , string id , DebugEntryStore store ) =>
103+ RequireDebugAuthorization ( webApp . MapGet ( "/debug/{id}" , async ( HttpContext ctx , string id , DebugEntryStore store ) =>
93104 {
94105 var item = store . Get ( id ) ;
95106
@@ -108,9 +119,9 @@ public static IApplicationBuilder UseDebugProbe(this IApplicationBuilder app)
108119
109120 await ctx . Response . WriteAsync ( html ) ;
110121
111- } ) . ExcludeFromDescription ( ) ;
122+ } ) . ExcludeFromDescription ( ) , options ) ;
112123
113- webApp . MapGet ( "/compare" , ( string ? baseUrl , string ? traceId , string ? localTraceId ) =>
124+ RequireDebugAuthorization ( webApp . MapGet ( "/compare" , ( string ? baseUrl , string ? traceId , string ? localTraceId ) =>
114125 {
115126 if ( string . IsNullOrWhiteSpace ( localTraceId ) )
116127 {
@@ -121,9 +132,9 @@ public static IApplicationBuilder UseDebugProbe(this IApplicationBuilder app)
121132
122133 return Results . Content ( html , "text/html" ) ;
123134
124- } ) . ExcludeFromDescription ( ) ;
135+ } ) . ExcludeFromDescription ( ) , options ) ;
125136
126- webApp . MapGet ( "/debug/js/{file}" , ( string file ) =>
137+ RequireDebugAuthorization ( webApp . MapGet ( "/debug/js/{file}" , ( string file ) =>
127138 {
128139 if ( ! EmbeddedResources . JavaScript . TryGetValue ( file , out var content ) )
129140 {
@@ -132,26 +143,26 @@ public static IApplicationBuilder UseDebugProbe(this IApplicationBuilder app)
132143
133144 return Results . Text ( content , "application/javascript" ) ;
134145
135- } ) . ExcludeFromDescription ( ) ;
146+ } ) . ExcludeFromDescription ( ) , options ) ;
136147
137- webApp . MapPost ( "/debug/clear" , ( DebugEntryStore store ) =>
148+ RequireDebugAuthorization ( webApp . MapPost ( "/debug/clear" , ( DebugEntryStore store ) =>
138149 {
139150 store . Clear ( ) ;
140151
141152 return Results . Ok ( ) ;
142153
143- } ) . ExcludeFromDescription ( ) ;
154+ } ) . ExcludeFromDescription ( ) , options ) ;
144155
145- webApp . Map ( "/debug/logo.png" , ctx =>
156+ RequireDebugAuthorization ( webApp . Map ( "/debug/logo.png" , ctx =>
146157 EmbeddedAssetWriter . WriteEmbeddedAsset ( ctx , "DebugProbe.AspNetCore.Assets.images.debugprobe_logo_white_transparent.png" , "image/png" )
147- ) . ExcludeFromDescription ( ) ;
158+ ) . ExcludeFromDescription ( ) , options ) ;
148159
149- webApp . Map ( "/debug/favicon.ico" , ctx =>
160+ RequireDebugAuthorization ( webApp . Map ( "/debug/favicon.ico" , ctx =>
150161 EmbeddedAssetWriter . WriteEmbeddedAsset ( ctx , "DebugProbe.AspNetCore.Assets.images.debugprobe_favicon.ico" , "image/x-icon" )
151- ) . ExcludeFromDescription ( ) ;
162+ ) . ExcludeFromDescription ( ) , options ) ;
152163 }
153164
154- webApp . MapGet ( "/debug/compare/{id}" , async ( string id , string baseUrl , string remoteTraceId ,
165+ RequireDebugAuthorization ( webApp . MapGet ( "/debug/compare/{id}" , async ( string id , string baseUrl , string remoteTraceId ,
155166 DebugEntryStore store ,
156167 DebugProbeOptions options ) =>
157168 {
@@ -228,21 +239,21 @@ public static IApplicationBuilder UseDebugProbe(this IApplicationBuilder app)
228239 diffs = diff
229240 } ) ;
230241
231- } ) . ExcludeFromDescription ( ) ;
242+ } ) . ExcludeFromDescription ( ) , options ) ;
232243
233- webApp . MapGet ( "/debug/environment" , ( DebugEntryStore store ) =>
244+ RequireDebugAuthorization ( webApp . MapGet ( "/debug/environment" , ( DebugEntryStore store ) =>
234245 {
235246 return Results . Ok ( store . Environment ) ;
236247
237- } ) . ExcludeFromDescription ( ) ;
248+ } ) . ExcludeFromDescription ( ) , options ) ;
238249
239- webApp . MapGet ( "/debug/json/{id}" , ( string id , DebugEntryStore store ) =>
250+ RequireDebugAuthorization ( webApp . MapGet ( "/debug/json/{id}" , ( string id , DebugEntryStore store ) =>
240251 {
241252 var item = store . Get ( id ) ;
242253
243254 return item is null ? Results . NotFound ( ) : Results . Json ( item ) ;
244255
245- } ) . ExcludeFromDescription ( ) ;
256+ } ) . ExcludeFromDescription ( ) , options ) ;
246257
247258
248259 }
@@ -254,4 +265,12 @@ private static bool ShouldMapUiEndpoints(IHostEnvironment environment, DebugProb
254265 {
255266 return ! environment . IsProduction ( ) || options . AllowUiInProduction ;
256267 }
268+
269+ private static void RequireDebugAuthorization ( IEndpointConventionBuilder endpoint , DebugProbeOptions options )
270+ {
271+ if ( ! string . IsNullOrWhiteSpace ( options . AuthorizationPolicy ) )
272+ {
273+ endpoint . RequireAuthorization ( options . AuthorizationPolicy ) ;
274+ }
275+ }
257276}
0 commit comments