@@ -11,81 +11,69 @@ namespace Microsoft.Azure.SignalR
11
11
internal static class ConnectionStringParser
12
12
{
13
13
private const string AccessKeyProperty = "accesskey" ;
14
-
15
14
private const string AuthTypeProperty = "authtype" ;
16
-
17
15
private const string ClientCertProperty = "clientCert" ;
18
-
19
- private const string ClientEndpointProperty = "clientEndpoint" ;
20
-
16
+ private const string ClientEndpointProperty = "ClientEndpoint" ;
21
17
private const string ClientIdProperty = "clientId" ;
22
-
23
18
private const string ClientSecretProperty = "clientSecret" ;
24
-
25
19
private const string EndpointProperty = "endpoint" ;
26
-
20
+ private const string ServerEndpoint = "ServerEndpoint" ;
27
21
private const string InvalidVersionValueFormat = "Version {0} is not supported." ;
28
-
29
22
private const string PortProperty = "port" ;
30
-
31
- private const string ServerEndpoint = "ServerEndpoint" ;
32
-
33
23
// For SDK 1.x, only support Azure SignalR Service 1.x
34
24
private const string SupportedVersion = "1" ;
35
25
36
26
private const string TenantIdProperty = "tenantId" ;
37
-
38
- private const string TypeAzure = "azure" ;
39
-
40
- private const string TypeAzureAD = "aad" ;
41
-
42
- private const string TypeAzureApp = "azure.app" ;
43
-
44
- private const string TypeAzureMsi = "azure.msi" ;
45
-
46
27
private const string ValidVersionRegex = "^" + SupportedVersion + @"\.\d+(?:[\w-.]+)?$" ;
47
-
48
28
private const string VersionProperty = "version" ;
49
-
50
- private static readonly string InvalidClientEndpointProperty = $ "Invalid value for { ClientEndpointProperty } property, it must be a valid URI.";
51
-
52
- private static readonly string InvalidEndpointProperty = $ "Invalid value for { EndpointProperty } property, it must be a valid URI.";
53
-
54
- private static readonly string InvalidPortValue = $ "Invalid value for { PortProperty } property, it must be an positive integer between (0, 65536)";
29
+ private static readonly string InvalidPortValue = $ "Invalid value for { PortProperty } property.";
55
30
56
31
private static readonly char [ ] KeyValueSeparator = { '=' } ;
57
32
58
33
private static readonly string MissingAccessKeyProperty =
59
34
$ "{ AccessKeyProperty } is required.";
60
35
61
- private static readonly string MissingClientIdProperty =
62
- $ "Connection string missing required properties { ClientIdProperty } .";
63
-
64
36
private static readonly string MissingClientSecretProperty =
65
37
$ "Connection string missing required properties { ClientSecretProperty } or { ClientCertProperty } .";
66
38
67
39
private static readonly string MissingEndpointProperty =
68
40
$ "Connection string missing required properties { EndpointProperty } .";
69
41
70
- private static readonly string MissingTenantIdProperty =
71
- $ "Connection string missing required properties { TenantIdProperty } .";
72
-
73
42
private static readonly char [ ] PropertySeparator = { ';' } ;
74
43
75
44
internal static ParsedConnectionString Parse ( string connectionString )
76
45
{
77
- var dict = ToDictionary ( connectionString ) ;
46
+ var properties = connectionString . Split ( PropertySeparator , StringSplitOptions . RemoveEmptyEntries ) ;
47
+ if ( properties . Length < 2 )
48
+ {
49
+ throw new ArgumentException ( MissingEndpointProperty , nameof ( connectionString ) ) ;
50
+ }
51
+
52
+ var dict = new Dictionary < string , string > ( StringComparer . OrdinalIgnoreCase ) ;
53
+ foreach ( var property in properties )
54
+ {
55
+ var kvp = property . Split ( KeyValueSeparator , 2 ) ;
56
+ if ( kvp . Length != 2 ) continue ;
57
+
58
+ var key = kvp [ 0 ] . Trim ( ) ;
59
+ if ( dict . ContainsKey ( key ) )
60
+ {
61
+ throw new ArgumentException ( $ "Duplicate properties found in connection string: { key } .") ;
62
+ }
63
+
64
+ dict . Add ( key , kvp [ 1 ] . Trim ( ) ) ;
65
+ }
78
66
79
67
// parse and validate endpoint.
80
68
if ( ! dict . TryGetValue ( EndpointProperty , out var endpoint ) )
81
69
{
82
- throw new ArgumentException ( MissingEndpointProperty , nameof ( endpoint ) ) ;
70
+ throw new ArgumentException ( MissingEndpointProperty , nameof ( connectionString ) ) ;
83
71
}
84
72
endpoint = endpoint . TrimEnd ( '/' ) ;
85
73
86
74
if ( ! TryGetEndpointUri ( endpoint , out var endpointUri ) )
87
75
{
88
- throw new ArgumentException ( InvalidEndpointProperty , nameof ( endpoint ) ) ;
76
+ throw new ArgumentException ( $ "Endpoint property in connection string is not a valid URI: { dict [ EndpointProperty ] } ." ) ;
89
77
}
90
78
var builder = new UriBuilder ( endpointUri ) ;
91
79
@@ -95,43 +83,38 @@ internal static ParsedConnectionString Parse(string connectionString)
95
83
{
96
84
if ( ! Regex . IsMatch ( v , ValidVersionRegex ) )
97
85
{
98
- throw new ArgumentException ( string . Format ( InvalidVersionValueFormat , v ) , nameof ( version ) ) ;
86
+ throw new ArgumentException ( string . Format ( InvalidVersionValueFormat , v ) , nameof ( connectionString ) ) ;
99
87
}
100
88
version = v ;
101
89
}
102
90
103
91
// parse and validate port.
104
92
if ( dict . TryGetValue ( PortProperty , out var s ) )
105
93
{
106
- if ( int . TryParse ( s , out var port ) && port > 0 && port <= 0xFFFF )
94
+ if ( int . TryParse ( s , out var p ) && p > 0 && p <= 0xFFFF )
107
95
{
108
- builder . Port = port ;
96
+ builder . Port = p ;
109
97
}
110
98
else
111
99
{
112
- throw new ArgumentException ( InvalidPortValue , nameof ( port ) ) ;
100
+ throw new ArgumentException ( InvalidPortValue , nameof ( connectionString ) ) ;
113
101
}
114
102
}
115
103
116
104
Uri clientEndpointUri = null ;
117
-
118
105
// parse and validate clientEndpoint.
119
106
if ( dict . TryGetValue ( ClientEndpointProperty , out var clientEndpoint ) )
120
107
{
121
108
if ( ! TryGetEndpointUri ( clientEndpoint , out clientEndpointUri ) )
122
109
{
123
- throw new ArgumentException ( InvalidClientEndpointProperty , nameof ( clientEndpoint ) ) ;
110
+ throw new ArgumentException ( $ " { ClientEndpointProperty } property in connection string is not a valid URI: { clientEndpoint } ." ) ;
124
111
}
125
112
}
126
113
127
- // try building accesskey.
128
114
dict . TryGetValue ( AuthTypeProperty , out var type ) ;
129
115
var accessKey = type ? . ToLower ( ) switch
130
116
{
131
- TypeAzureAD => BuildAadAccessKey ( builder . Uri , dict ) ,
132
- TypeAzure => BuildAzureAccessKey ( builder . Uri , dict ) ,
133
- TypeAzureApp => BuildAzureAppAccessKey ( builder . Uri , dict ) ,
134
- TypeAzureMsi => BuildAzureMsiAccessKey ( builder . Uri , dict ) ,
117
+ "aad" => BuildAadAccessKey ( builder . Uri , dict ) ,
135
118
_ => BuildAccessKey ( builder . Uri , dict ) ,
136
119
} ;
137
120
@@ -197,70 +180,5 @@ private static AccessKey BuildAccessKey(Uri uri, Dictionary<string, string> dict
197
180
}
198
181
throw new ArgumentException ( MissingAccessKeyProperty , AccessKeyProperty ) ;
199
182
}
200
-
201
- private static AccessKey BuildAzureAccessKey ( Uri uri , Dictionary < string , string > dict )
202
- {
203
- return new AadAccessKey ( uri , new DefaultAzureCredential ( ) ) ;
204
- }
205
-
206
- private static AccessKey BuildAzureAppAccessKey ( Uri uri , Dictionary < string , string > dict )
207
- {
208
- if ( ! dict . TryGetValue ( ClientIdProperty , out var clientId ) )
209
- {
210
- throw new ArgumentException ( MissingClientIdProperty , ClientIdProperty ) ;
211
- }
212
-
213
- if ( ! dict . TryGetValue ( TenantIdProperty , out var tenantId ) )
214
- {
215
- throw new ArgumentException ( MissingTenantIdProperty , TenantIdProperty ) ;
216
- }
217
-
218
- if ( dict . TryGetValue ( ClientSecretProperty , out var clientSecret ) )
219
- {
220
- return new AadAccessKey ( uri , new ClientSecretCredential ( tenantId , clientId , clientSecret ) ) ;
221
- }
222
- else if ( dict . TryGetValue ( ClientCertProperty , out var clientCertPath ) )
223
- {
224
- return new AadAccessKey ( uri , new ClientCertificateCredential ( tenantId , clientId , clientCertPath ) ) ;
225
- }
226
- throw new ArgumentException ( MissingClientSecretProperty , ClientSecretProperty ) ;
227
- }
228
-
229
- private static AccessKey BuildAzureMsiAccessKey ( Uri uri , Dictionary < string , string > dict )
230
- {
231
- if ( dict . TryGetValue ( ClientIdProperty , out var clientId ) )
232
- {
233
- return new AadAccessKey ( uri , new ManagedIdentityCredential ( clientId ) ) ;
234
- }
235
- return new AadAccessKey ( uri , new ManagedIdentityCredential ( ) ) ;
236
- }
237
-
238
- private static Dictionary < string , string > ToDictionary ( string connectionString )
239
- {
240
- var properties = connectionString . Split ( PropertySeparator , StringSplitOptions . RemoveEmptyEntries ) ;
241
- if ( properties . Length < 2 )
242
- {
243
- throw new ArgumentException ( MissingEndpointProperty , nameof ( connectionString ) ) ;
244
- }
245
-
246
- var dict = new Dictionary < string , string > ( StringComparer . OrdinalIgnoreCase ) ;
247
- foreach ( var property in properties )
248
- {
249
- var kvp = property . Split ( KeyValueSeparator , 2 ) ;
250
- if ( kvp . Length != 2 )
251
- {
252
- continue ;
253
- }
254
-
255
- var key = kvp [ 0 ] . Trim ( ) ;
256
- if ( dict . ContainsKey ( key ) )
257
- {
258
- throw new ArgumentException ( $ "Duplicate properties found in connection string: { key } .") ;
259
- }
260
-
261
- dict . Add ( key , kvp [ 1 ] . Trim ( ) ) ;
262
- }
263
- return dict ;
264
- }
265
183
}
266
184
}
0 commit comments