6
6
7
7
using System ;
8
8
using System . Collections . Generic ;
9
- using System . Linq ;
9
+ using System . Collections . ObjectModel ;
10
10
using JetBrains . Annotations ;
11
11
using Microsoft . Extensions . Primitives ;
12
12
@@ -20,10 +20,7 @@ public class OpenIdAuthenticationMessage
20
20
/// <summary>
21
21
/// Initializes a new OpenID message.
22
22
/// </summary>
23
- public OpenIdAuthenticationMessage ( )
24
- : this ( new Dictionary < string , string > ( ) )
25
- {
26
- }
23
+ public OpenIdAuthenticationMessage ( ) { }
27
24
28
25
/// <summary>
29
26
/// Initializes a new OpenID message.
@@ -36,7 +33,15 @@ public OpenIdAuthenticationMessage([NotNull] IDictionary<string, string> paramet
36
33
throw new ArgumentNullException ( nameof ( parameters ) ) ;
37
34
}
38
35
39
- Parameters = parameters ;
36
+ foreach ( var parameter in parameters )
37
+ {
38
+ if ( string . IsNullOrEmpty ( parameter . Key ) )
39
+ {
40
+ continue ;
41
+ }
42
+
43
+ Parameters . Add ( parameter . Key , parameter . Value ) ;
44
+ }
40
45
}
41
46
42
47
/// <summary>
@@ -50,92 +55,134 @@ public OpenIdAuthenticationMessage([NotNull] IEnumerable<KeyValuePair<string, St
50
55
throw new ArgumentNullException ( nameof ( parameters ) ) ;
51
56
}
52
57
53
- Parameters = parameters . ToDictionary (
54
- parameter => parameter . Key ,
55
- parameter => ( string ) parameter . Value ) ;
58
+ foreach ( var parameter in parameters )
59
+ {
60
+ if ( string . IsNullOrEmpty ( parameter . Key ) )
61
+ {
62
+ continue ;
63
+ }
64
+
65
+ Parameters . Add ( parameter . Key , parameter . Value ) ;
66
+ }
56
67
}
57
68
58
69
/// <summary>
59
70
/// Gets or sets the openid.claimed_id property.
60
71
/// </summary>
61
72
public string ClaimedIdentifier
62
73
{
63
- get { return GetParameter ( OpenIdAuthenticationConstants . Parameters . ClaimedId ) ; }
64
- set { SetParameter ( OpenIdAuthenticationConstants . Parameters . ClaimedId , value ) ; }
74
+ get => GetParameter ( OpenIdAuthenticationConstants . Parameters . ClaimedId ) ;
75
+ set => SetParameter ( OpenIdAuthenticationConstants . Parameters . ClaimedId , value ) ;
65
76
}
66
77
67
78
/// <summary>
68
79
/// Gets or sets the openid.identity property.
69
80
/// </summary>
70
81
public string Identity
71
82
{
72
- get { return GetParameter ( OpenIdAuthenticationConstants . Parameters . Identity ) ; }
73
- set { SetParameter ( OpenIdAuthenticationConstants . Parameters . Identity , value ) ; }
83
+ get => GetParameter ( OpenIdAuthenticationConstants . Parameters . Identity ) ;
84
+ set => SetParameter ( OpenIdAuthenticationConstants . Parameters . Identity , value ) ;
74
85
}
75
86
76
87
/// <summary>
77
88
/// Gets or sets the openid.error property.
78
89
/// </summary>
79
90
public string Error
80
91
{
81
- get { return GetParameter ( OpenIdAuthenticationConstants . Parameters . Error ) ; }
82
- set { SetParameter ( OpenIdAuthenticationConstants . Parameters . Error , value ) ; }
92
+ get => GetParameter ( OpenIdAuthenticationConstants . Parameters . Error ) ;
93
+ set => SetParameter ( OpenIdAuthenticationConstants . Parameters . Error , value ) ;
83
94
}
84
95
85
96
/// <summary>
86
97
/// Gets or sets the openid.mode property.
87
98
/// </summary>
88
99
public string Mode
89
100
{
90
- get { return GetParameter ( OpenIdAuthenticationConstants . Parameters . Mode ) ; }
91
- set { SetParameter ( OpenIdAuthenticationConstants . Parameters . Mode , value ) ; }
101
+ get => GetParameter ( OpenIdAuthenticationConstants . Parameters . Mode ) ;
102
+ set => SetParameter ( OpenIdAuthenticationConstants . Parameters . Mode , value ) ;
92
103
}
93
104
94
105
/// <summary>
95
106
/// Gets or sets the openid.ns property.
96
107
/// </summary>
97
108
public string Namespace
98
109
{
99
- get { return GetParameter ( OpenIdAuthenticationConstants . Parameters . Namespace ) ; }
100
- set { SetParameter ( OpenIdAuthenticationConstants . Parameters . Namespace , value ) ; }
110
+ get => GetParameter ( OpenIdAuthenticationConstants . Parameters . Namespace ) ;
111
+ set => SetParameter ( OpenIdAuthenticationConstants . Parameters . Namespace , value ) ;
101
112
}
102
113
103
114
/// <summary>
104
115
/// Gets or sets the openid.realm property.
105
116
/// </summary>
106
117
public string Realm
107
118
{
108
- get { return GetParameter ( OpenIdAuthenticationConstants . Parameters . Realm ) ; }
109
- set { SetParameter ( OpenIdAuthenticationConstants . Parameters . Realm , value ) ; }
119
+ get => GetParameter ( OpenIdAuthenticationConstants . Parameters . Realm ) ;
120
+ set => SetParameter ( OpenIdAuthenticationConstants . Parameters . Realm , value ) ;
110
121
}
111
122
112
123
/// <summary>
113
124
/// Gets or sets the openid.return_to property.
114
125
/// </summary>
115
126
public string ReturnTo
116
127
{
117
- get { return GetParameter ( OpenIdAuthenticationConstants . Parameters . ReturnTo ) ; }
118
- set { SetParameter ( OpenIdAuthenticationConstants . Parameters . ReturnTo , value ) ; }
128
+ get => GetParameter ( OpenIdAuthenticationConstants . Parameters . ReturnTo ) ;
129
+ set => SetParameter ( OpenIdAuthenticationConstants . Parameters . ReturnTo , value ) ;
119
130
}
120
131
121
132
/// <summary>
122
133
/// Gets the parameters associated with this OpenID message.
123
134
/// </summary>
124
- public IDictionary < string , string > Parameters { get ; }
135
+ protected IDictionary < string , string > Parameters { get ; } =
136
+ new Dictionary < string , string > ( StringComparer . Ordinal ) ;
137
+
138
+ /// <summary>
139
+ /// Adds a parameter using the default prefix.
140
+ /// </summary>
141
+ /// <param name="name">The parameter name.</param>
142
+ /// <param name="value">The parameter value.</param>
143
+ /// <returns>The current instance, which allows chaining calls.</returns>
144
+ public OpenIdAuthenticationMessage AddParameter ( [ NotNull ] string name , [ CanBeNull ] string value )
145
+ => AddParameter ( OpenIdAuthenticationConstants . Prefixes . OpenId , name , value ) ;
146
+
147
+ /// <summary>
148
+ /// Adds a parameter using the specified prefix.
149
+ /// </summary>
150
+ /// <param name="prefix">The prefix used to discriminate the parameter.</param>
151
+ /// <param name="name">The parameter to store.</param>
152
+ /// <param name="value">The value associated with the parameter.</param>
153
+ /// <returns>The current instance, which allows chaining calls.</returns>
154
+ public OpenIdAuthenticationMessage AddParameter ( [ NotNull ] string prefix , [ NotNull ] string name , [ CanBeNull ] string value )
155
+ {
156
+ if ( string . IsNullOrEmpty ( prefix ) )
157
+ {
158
+ throw new ArgumentException ( "The prefix cannot be null or empty." , nameof ( prefix ) ) ;
159
+ }
160
+
161
+ if ( string . IsNullOrEmpty ( name ) )
162
+ {
163
+ throw new ArgumentException ( "The parameter name cannot be null or empty." , nameof ( name ) ) ;
164
+ }
165
+
166
+ if ( ! Parameters . ContainsKey ( $ "{ prefix } .{ name } ") )
167
+ {
168
+ Parameters . Add ( $ "{ prefix } .{ name } ", value ) ;
169
+ }
170
+
171
+ return this ;
172
+ }
125
173
126
174
/// <summary>
127
175
/// Gets the attributes returned by the identity provider, or an empty
128
176
/// dictionary if the message doesn't expose an attribute exchange alias.
129
177
/// </summary>
130
178
/// <returns>The attributes contained in this message.</returns>
131
- public IDictionary < string , string > GetAttributes ( )
179
+ public IReadOnlyDictionary < string , string > GetAttributes ( )
132
180
{
133
181
var attributes = new Dictionary < string , string > ( StringComparer . Ordinal ) ;
134
182
135
- string alias ;
136
- var extensions = GetExtensions ( ) ;
137
183
// If the ax alias cannot be found, return an empty dictionary.
138
- if ( ! extensions . TryGetValue ( OpenIdAuthenticationConstants . Namespaces . Ax , out alias ) )
184
+ var extensions = GetExtensions ( ) ;
185
+ if ( ! extensions . TryGetValue ( OpenIdAuthenticationConstants . Namespaces . Ax , out string alias ) )
139
186
{
140
187
return attributes ;
141
188
}
@@ -165,9 +212,8 @@ public IDictionary<string, string> GetAttributes()
165
212
}
166
213
167
214
// Exclude attributes whose value is missing.
168
- string value ;
169
215
if ( ! Parameters . TryGetValue ( $ "{ OpenIdAuthenticationConstants . Prefixes . OpenId } .{ alias } ." +
170
- $ "{ OpenIdAuthenticationConstants . Suffixes . Value } .{ name } ", out value ) )
216
+ $ "{ OpenIdAuthenticationConstants . Suffixes . Value } .{ name } ", out string value ) )
171
217
{
172
218
continue ;
173
219
}
@@ -188,7 +234,7 @@ public IDictionary<string, string> GetAttributes()
188
234
/// Gets the extensions and their corresponding alias.
189
235
/// </summary>
190
236
/// <returns>The extensions contained in this message.</returns>
191
- public IDictionary < string , string > GetExtensions ( )
237
+ public IReadOnlyDictionary < string , string > GetExtensions ( )
192
238
{
193
239
var extensions = new Dictionary < string , string > ( StringComparer . Ordinal ) ;
194
240
@@ -212,9 +258,7 @@ public IDictionary<string, string> GetExtensions()
212
258
/// <param name="name">The parameter to retrieve.</param>
213
259
/// <returns>The value extracted from the parameter.</returns>
214
260
public string GetParameter ( [ NotNull ] string name )
215
- {
216
- return GetParameter ( OpenIdAuthenticationConstants . Prefixes . OpenId , name ) ;
217
- }
261
+ => GetParameter ( OpenIdAuthenticationConstants . Prefixes . OpenId , name ) ;
218
262
219
263
/// <summary>
220
264
/// Gets the parameter corresponding to the requested name and the given
@@ -235,25 +279,30 @@ public string GetParameter([NotNull] string prefix, [NotNull] string name)
235
279
throw new ArgumentNullException ( nameof ( name ) ) ;
236
280
}
237
281
238
- string value ;
239
- if ( Parameters . TryGetValue ( $ "{ prefix } .{ name } ", out value ) )
282
+ if ( Parameters . TryGetValue ( $ "{ prefix } .{ name } ", out string value ) )
240
283
{
241
284
return value ;
242
285
}
243
286
244
287
return null ;
245
288
}
246
289
290
+ /// <summary>
291
+ /// Gets all the parameters associated with this instance.
292
+ /// </summary>
293
+ /// <returns>The parameters associated with this instance.</returns>
294
+ public IReadOnlyDictionary < string , string > GetParameters ( )
295
+ => new ReadOnlyDictionary < string , string > ( Parameters ) ;
296
+
247
297
/// <summary>
248
298
/// Adds, replaces or removes the parameter corresponding
249
299
/// to the requested name and the default prefix.
250
300
/// </summary>
251
301
/// <param name="name">The parameter to store.</param>
252
302
/// <param name="value">The value associated with the parameter.</param>
253
- public void SetParameter ( [ NotNull ] string name , [ CanBeNull ] string value )
254
- {
255
- SetParameter ( OpenIdAuthenticationConstants . Prefixes . OpenId , name , value ) ;
256
- }
303
+ /// <returns>The current instance, which allows chaining calls.</returns>
304
+ public OpenIdAuthenticationMessage SetParameter ( [ NotNull ] string name , [ CanBeNull ] string value )
305
+ => SetParameter ( OpenIdAuthenticationConstants . Prefixes . OpenId , name , value ) ;
257
306
258
307
/// <summary>
259
308
/// Adds, replaces or removes the parameter corresponding
@@ -262,28 +311,32 @@ public void SetParameter([NotNull] string name, [CanBeNull] string value)
262
311
/// <param name="prefix">The prefix used to discriminate the parameter.</param>
263
312
/// <param name="name">The parameter to store.</param>
264
313
/// <param name="value">The value associated with the parameter.</param>
265
- public void SetParameter ( [ NotNull ] string prefix , [ NotNull ] string name , [ CanBeNull ] string value )
314
+ /// <returns>The current instance, which allows chaining calls.</returns>
315
+ public OpenIdAuthenticationMessage SetParameter ( [ NotNull ] string prefix , [ NotNull ] string name , [ CanBeNull ] string value )
266
316
{
267
317
if ( string . IsNullOrEmpty ( prefix ) )
268
318
{
269
- throw new ArgumentNullException ( nameof ( prefix ) ) ;
319
+ throw new ArgumentException ( "The prefix cannot be null or empty." , nameof ( prefix ) ) ;
270
320
}
271
321
272
322
if ( string . IsNullOrEmpty ( name ) )
273
323
{
274
- throw new ArgumentNullException ( nameof ( name ) ) ;
324
+ throw new ArgumentException ( "The parameter name cannot be null or empty." , nameof ( name ) ) ;
275
325
}
276
326
277
327
// If the parameter value is null, remove
278
328
// it from the parameters dictionary.
279
329
if ( string . IsNullOrEmpty ( value ) )
280
330
{
281
331
Parameters . Remove ( $ "{ prefix } .{ name } ") ;
332
+ }
282
333
283
- return ;
334
+ else
335
+ {
336
+ Parameters [ $ "{ prefix } .{ name } "] = value ;
284
337
}
285
338
286
- Parameters [ $ " { prefix } . { name } " ] = value ;
339
+ return this ;
287
340
}
288
341
}
289
342
}
0 commit comments