1
1
using System ;
2
2
using System . Security . Claims ;
3
- using System . Text ;
4
- using Microsoft . AspNetCore . Authorization . Infrastructure ;
5
3
using Microsoft . Extensions . Primitives ;
6
4
using NpgsqlRest ;
7
5
@@ -22,43 +20,51 @@ public class DefaultResponseParser(
22
20
private readonly Dictionary < string , StringValues > ? customClaims = customClaims ;
23
21
private readonly Dictionary < string , string ? > ? customParameters = customParameters ;
24
22
23
+ private const string @null = "null" ;
24
+ private const char @open = '{' ;
25
+ private const char @close = '}' ;
26
+ private const char @quote = '"' ;
27
+ private const char @arrOpen = '[' ;
28
+ private const char @arrClose = ']' ;
29
+ private const char @comma = ',' ;
30
+
25
31
public ReadOnlySpan < char > Parse ( ReadOnlySpan < char > input , RoutineEndpoint endpoint , HttpContext context )
26
32
{
27
33
Dictionary < string , string > replacements = [ ] ;
28
34
29
35
if ( userIdParameterName is not null )
30
36
{
31
37
var value = context . User . FindFirst ( ClaimTypes . NameIdentifier ) ? . Value ;
32
- replacements . Add ( userIdParameterName , value is null ? "NULL" : string . Concat ( '"' , value , '"' ) ) ;
38
+ replacements . Add ( userIdParameterName , value is null ? @null : string . Concat ( @quote , value , @quote ) ) ;
33
39
}
34
40
if ( userNameParameterName is not null )
35
41
{
36
42
var value = context . User . Identity ? . Name ;
37
- replacements . Add ( userNameParameterName , value is null ? "NULL" : string . Concat ( '"' , value , '"' ) ) ;
43
+ replacements . Add ( userNameParameterName , value is null ? @null : string . Concat ( @quote , value , @quote ) ) ;
38
44
}
39
45
if ( userRolesParameterName is not null )
40
46
{
41
- var value = context . User . FindAll ( c => string . Equals ( c . Type , ClaimTypes . Role , StringComparison . Ordinal ) ) ? . Select ( r => string . Concat ( '"' , r . Value , '"' ) ) ;
42
- replacements . Add ( userRolesParameterName , value is null ? "NULL" : string . Concat ( '[' , string . Join ( ',' , value ) , ']' ) ) ;
47
+ var value = context . User . FindAll ( c => string . Equals ( c . Type , ClaimTypes . Role , StringComparison . Ordinal ) ) ? . Select ( r => string . Concat ( @quote , r . Value , @quote ) ) ;
48
+ replacements . Add ( userRolesParameterName , value is null ? @null : string . Concat ( @arrOpen , string . Join ( @comma , value ) , @arrClose ) ) ;
43
49
}
44
50
if ( ipAddressParameterName is not null )
45
51
{
46
52
var value = App . GetClientIpAddress ( context . Request ) ;
47
- replacements . Add ( ipAddressParameterName , value is null ? "NULL" : string . Concat ( '"' , value , '"' ) ) ;
53
+ replacements . Add ( ipAddressParameterName , value is null ? @null : string . Concat ( @quote , value , @quote ) ) ;
48
54
}
49
55
if ( customClaims is not null )
50
56
{
51
57
foreach ( var ( key , value ) in customClaims )
52
58
{
53
59
var claim = context . User . FindFirst ( key ) ;
54
- replacements . Add ( key , claim is null ? "NULL" : string . Concat ( '"' , claim . Value , '"' ) ) ;
60
+ replacements . Add ( key , claim is null ? @null : string . Concat ( @quote , claim . Value , @quote ) ) ;
55
61
}
56
62
}
57
63
if ( customParameters is not null )
58
64
{
59
65
foreach ( var ( key , value ) in customParameters )
60
66
{
61
- replacements . Add ( key , value is null ? "NULL" : string . Concat ( '"' , value , '"' ) ) ;
67
+ replacements . Add ( key , value is null ? @null : string . Concat ( @quote , value , @quote ) ) ;
62
68
}
63
69
}
64
70
return FormatString ( input , replacements ) ;
@@ -86,7 +92,7 @@ public static ReadOnlySpan<char> FormatString(ReadOnlySpan<char> input, Dictiona
86
92
for ( int i = 0 ; i < inputLength ; i ++ )
87
93
{
88
94
var ch = input [ i ] ;
89
- if ( ch == '{' )
95
+ if ( ch == @open )
90
96
{
91
97
if ( inside is true )
92
98
{
@@ -97,7 +103,7 @@ public static ReadOnlySpan<char> FormatString(ReadOnlySpan<char> input, Dictiona
97
103
continue ;
98
104
}
99
105
100
- if ( ch == '}' && inside is true )
106
+ if ( ch == @close && inside is true )
101
107
{
102
108
inside = false ;
103
109
if ( lookup . TryGetValue ( input [ ( startIndex + 1 ) ..i ] , out var value ) )
@@ -130,7 +136,7 @@ public static ReadOnlySpan<char> FormatString(ReadOnlySpan<char> input, Dictiona
130
136
for ( int i = 0 ; i < inputLength ; i ++ )
131
137
{
132
138
var ch = input [ i ] ;
133
- if ( ch == '{' )
139
+ if ( ch == @open )
134
140
{
135
141
if ( inside is true )
136
142
{
@@ -142,7 +148,7 @@ public static ReadOnlySpan<char> FormatString(ReadOnlySpan<char> input, Dictiona
142
148
continue ;
143
149
}
144
150
145
- if ( ch == '}' && inside is true )
151
+ if ( ch == @close && inside is true )
146
152
{
147
153
inside = false ;
148
154
if ( lookup . TryGetValue ( input [ ( startIndex + 1 ) ..i ] , out var value ) )
0 commit comments