/
IdentityServerLicenseValidatorTests.cs
433 lines (407 loc) · 17.9 KB
/
IdentityServerLicenseValidatorTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.
using System;
using System.Security.Claims;
using Duende;
using Duende.IdentityServer;
using FluentAssertions;
using Xunit;
using static Duende.License;
namespace UnitTests.Licensing;
public class IdentityServerLicenseValidatorTests
{
private const string Category = "License validator tests";
[Fact]
[Trait("Category", Category)]
public void license_should_parse_company_data()
{
var subject = new IdentityServerLicense(
new Claim("edition", "enterprise"),
new Claim("company_name", "foo"),
new Claim("contact_info", "bar"));
subject.CompanyName.Should().Be("foo");
subject.ContactInfo.Should().Be("bar");
}
[Fact]
[Trait("Category", Category)]
public void license_should_parse_expiration()
{
{
var subject = new IdentityServerLicense(new Claim("edition", "enterprise"));
subject.Expiration.Should().BeNull();
}
{
var exp = new DateTimeOffset(2020, 1, 12, 13, 5, 0, TimeSpan.Zero).ToUnixTimeSeconds();
var subject = new IdentityServerLicense(
new Claim("edition", "enterprise"),
new Claim("exp", exp.ToString()));
subject.Expiration.Should().Be(new DateTime(2020, 1, 12, 13, 5, 0, DateTimeKind.Utc));
}
}
[Fact]
[Trait("Category", Category)]
public void license_should_parse_edition_and_use_default_values()
{
// non-ISV
{
var subject = new IdentityServerLicense(new Claim("edition", "enterprise"));
subject.Edition.Should().Be(LicenseEdition.Enterprise);
subject.IsEnterpriseEdition.Should().BeTrue();
subject.ClientLimit.Should().BeNull();
subject.IssuerLimit.Should().BeNull();
subject.KeyManagementFeature.Should().BeTrue();
subject.ResourceIsolationFeature.Should().BeTrue();
subject.DynamicProvidersFeature.Should().BeTrue();
subject.ServerSideSessionsFeature.Should().BeTrue();
//subject.ConfigApiFeature.Should().BeTrue();
subject.DPoPFeature.Should().BeTrue();
//subject.BffFeature.Should().BeTrue();
subject.RedistributionFeature.Should().BeFalse();
subject.CibaFeature.Should().BeTrue();
subject.ParFeature.Should().BeTrue();
}
{
var subject = new IdentityServerLicense(new Claim("edition", "business"));
subject.Edition.Should().Be(LicenseEdition.Business);
subject.IsBusinessEdition.Should().BeTrue();
subject.ClientLimit.Should().Be(15);
subject.IssuerLimit.Should().Be(1);
subject.KeyManagementFeature.Should().BeTrue();
subject.ResourceIsolationFeature.Should().BeFalse();
subject.DynamicProvidersFeature.Should().BeFalse();
subject.ServerSideSessionsFeature.Should().BeTrue();
//subject.ConfigApiFeature.Should().BeTrue();
subject.DPoPFeature.Should().BeFalse();
//subject.BffFeature.Should().BeTrue();
subject.RedistributionFeature.Should().BeFalse();
subject.CibaFeature.Should().BeFalse();
subject.ParFeature.Should().BeTrue();
}
{
var subject = new IdentityServerLicense(new Claim("edition", "starter"));
subject.Edition.Should().Be(LicenseEdition.Starter);
subject.IsStarterEdition.Should().BeTrue();
subject.ClientLimit.Should().Be(5);
subject.IssuerLimit.Should().Be(1);
subject.KeyManagementFeature.Should().BeFalse();
subject.ResourceIsolationFeature.Should().BeFalse();
subject.DynamicProvidersFeature.Should().BeFalse();
subject.ServerSideSessionsFeature.Should().BeFalse();
//subject.ConfigApiFeature.Should().BeFalse();
subject.DPoPFeature.Should().BeFalse();
//subject.BffFeature.Should().BeFalse();
subject.RedistributionFeature.Should().BeFalse();
subject.CibaFeature.Should().BeFalse();
subject.ParFeature.Should().BeFalse();
}
{
var subject = new IdentityServerLicense(new Claim("edition", "community"));
subject.Edition.Should().Be(LicenseEdition.Community);
subject.IsCommunityEdition.Should().BeTrue();
subject.ClientLimit.Should().BeNull();
subject.IssuerLimit.Should().BeNull();
subject.KeyManagementFeature.Should().BeTrue();
subject.ResourceIsolationFeature.Should().BeTrue();
subject.DynamicProvidersFeature.Should().BeTrue();
subject.ServerSideSessionsFeature.Should().BeTrue();
//subject.ConfigApiFeature.Should().BeTrue();
subject.DPoPFeature.Should().BeTrue();
//subject.BffFeature.Should().BeTrue();
subject.RedistributionFeature.Should().BeFalse();
subject.CibaFeature.Should().BeTrue();
subject.ParFeature.Should().BeTrue();
}
// BFF
// TODO
//{
// var subject = new IdentityServerLicense(new Claim("edition", "bff"));
// subject.Edition.Should().Be(LicenseEdition.Bff);
// subject.IsBffEdition.Should().BeTrue();
// subject.ServerSideSessionsFeature.Should().BeFalse();
// //subject.ConfigApiFeature.Should().BeFalse();
// subject.DPoPFeature.Should().BeFalse();
// //subject.BffFeature.Should().BeTrue();
// subject.ClientLimit.Should().Be(0);
// subject.IssuerLimit.Should().Be(0);
// subject.KeyManagementFeature.Should().BeFalse();
// subject.ResourceIsolationFeature.Should().BeFalse();
// subject.DynamicProvidersFeature.Should().BeFalse();
// subject.RedistributionFeature.Should().BeFalse();
// subject.CibaFeature.Should().BeFalse();
//}
// ISV
{
var subject = new IdentityServerLicense(new Claim("edition", "enterprise"), new Claim("feature", "isv"));
subject.Edition.Should().Be(LicenseEdition.Enterprise);
subject.IsEnterpriseEdition.Should().BeTrue();
subject.ClientLimit.Should().Be(5);
subject.IssuerLimit.Should().BeNull();
subject.KeyManagementFeature.Should().BeTrue();
subject.ResourceIsolationFeature.Should().BeTrue();
subject.DynamicProvidersFeature.Should().BeTrue();
subject.ServerSideSessionsFeature.Should().BeTrue();
//subject.ConfigApiFeature.Should().BeTrue();
subject.DPoPFeature.Should().BeTrue();
//subject.BffFeature.Should().BeTrue();
subject.RedistributionFeature.Should().BeTrue();
subject.CibaFeature.Should().BeTrue();
}
{
var subject = new IdentityServerLicense(new Claim("edition", "business"), new Claim("feature", "isv"));
subject.Edition.Should().Be(LicenseEdition.Business);
subject.IsBusinessEdition.Should().BeTrue();
subject.ClientLimit.Should().Be(5);
subject.IssuerLimit.Should().Be(1);
subject.KeyManagementFeature.Should().BeTrue();
subject.ResourceIsolationFeature.Should().BeFalse();
subject.DynamicProvidersFeature.Should().BeFalse();
subject.ServerSideSessionsFeature.Should().BeTrue();
//subject.ConfigApiFeature.Should().BeTrue();
subject.DPoPFeature.Should().BeFalse();
//subject.BffFeature.Should().BeTrue();
subject.RedistributionFeature.Should().BeTrue();
subject.CibaFeature.Should().BeFalse();
}
{
var subject = new IdentityServerLicense(new Claim("edition", "starter"), new Claim("feature", "isv"));
subject.Edition.Should().Be(LicenseEdition.Starter);
subject.IsStarterEdition.Should().BeTrue();
subject.ClientLimit.Should().Be(5);
subject.IssuerLimit.Should().Be(1);
subject.KeyManagementFeature.Should().BeFalse();
subject.ResourceIsolationFeature.Should().BeFalse();
subject.DynamicProvidersFeature.Should().BeFalse();
subject.ServerSideSessionsFeature.Should().BeFalse();
//subject.ConfigApiFeature.Should().BeFalse();
subject.DPoPFeature.Should().BeFalse();
//subject.BffFeature.Should().BeFalse();
subject.RedistributionFeature.Should().BeTrue();
subject.CibaFeature.Should().BeFalse();
}
// TODO: these exceptions were moved to the validator
//{
// Action a = () => new IdentityServerLicense(new Claim("edition", "community"), new Claim("feature", "isv"));
// a.Should().Throw<Exception>();
//}
//{
// Action a = () => new IdentityServerLicense(new Claim("edition", "bff"), new Claim("feature", "isv"));
// a.Should().Throw<Exception>();
//}
}
[Fact]
[Trait("Category", Category)]
public void license_should_handle_overrides_for_default_edition_values()
{
{
var subject = new IdentityServerLicense(
new Claim("edition", "enterprise"),
new Claim("client_limit", "20"),
new Claim("issuer_limit", "5"));
subject.ClientLimit.Should().BeNull();
subject.IssuerLimit.Should().BeNull();
}
{
var subject = new IdentityServerLicense(
new Claim("edition", "business"),
new Claim("client_limit", "20"),
new Claim("issuer_limit", "5"),
new Claim("feature", "resource_isolation"),
new Claim("feature", "ciba"),
new Claim("feature", "dynamic_providers"));
subject.ClientLimit.Should().Be(20);
subject.IssuerLimit.Should().Be(5);
subject.ResourceIsolationFeature.Should().BeTrue();
subject.DynamicProvidersFeature.Should().BeTrue();
subject.CibaFeature.Should().BeTrue();
}
{
var subject = new IdentityServerLicense(
new Claim("edition", "business"),
new Claim("client_limit", "20"),
new Claim("feature", "unlimited_issuers"),
new Claim("issuer_limit", "5"),
new Claim("feature", "unlimited_clients"));
subject.ClientLimit.Should().BeNull();
subject.IssuerLimit.Should().BeNull();
}
{
var subject = new IdentityServerLicense(
new Claim("edition", "starter"),
new Claim("client_limit", "20"),
new Claim("issuer_limit", "5"),
new Claim("feature", "key_management"),
new Claim("feature", "isv"),
new Claim("feature", "resource_isolation"),
new Claim("feature", "server_side_sessions"),
new Claim("feature", "config_api"),
new Claim("feature", "dpop"),
new Claim("feature", "bff"),
new Claim("feature", "ciba"),
new Claim("feature", "dynamic_providers"),
new Claim("feature", "par"));
subject.ClientLimit.Should().Be(20);
subject.IssuerLimit.Should().Be(5);
subject.KeyManagementFeature.Should().BeTrue();
subject.ResourceIsolationFeature.Should().BeTrue();
subject.ServerSideSessionsFeature.Should().BeTrue();
//subject.ConfigApiFeature.Should().BeTrue();
subject.DPoPFeature.Should().BeTrue();
//subject.BffFeature.Should().BeTrue();
subject.DynamicProvidersFeature.Should().BeTrue();
subject.RedistributionFeature.Should().BeTrue();
subject.CibaFeature.Should().BeTrue();
subject.ParFeature.Should().BeTrue();
}
{
var subject = new IdentityServerLicense(
new Claim("edition", "starter"),
new Claim("client_limit", "20"),
new Claim("feature", "unlimited_issuers"),
new Claim("issuer_limit", "5"),
new Claim("feature", "unlimited_clients"));
subject.ClientLimit.Should().BeNull();
subject.IssuerLimit.Should().BeNull();
}
{
var subject = new IdentityServerLicense(
new Claim("edition", "community"),
new Claim("client_limit", "20"),
new Claim("issuer_limit", "5"));
subject.ClientLimit.Should().BeNull();
subject.IssuerLimit.Should().BeNull();
}
// ISV
{
var subject = new IdentityServerLicense(
new Claim("edition", "enterprise"),
new Claim("feature", "isv"),
new Claim("client_limit", "20"));
subject.ClientLimit.Should().Be(20);
}
{
var subject = new IdentityServerLicense(
new Claim("edition", "business"),
new Claim("feature", "isv"),
new Claim("feature", "ciba"),
new Claim("client_limit", "20"));
subject.ClientLimit.Should().Be(20);
subject.CibaFeature.Should().BeTrue();
}
{
var subject = new IdentityServerLicense(
new Claim("edition", "starter"),
new Claim("feature", "isv"),
new Claim("feature", "server_side_sessions"),
new Claim("feature", "config_api"),
new Claim("feature", "dpop"),
new Claim("feature", "bff"),
new Claim("feature", "ciba"),
new Claim("client_limit", "20"));
subject.ClientLimit.Should().Be(20);
subject.ServerSideSessionsFeature.Should().BeTrue();
//subject.ConfigApiFeature.Should().BeTrue();
subject.DPoPFeature.Should().BeTrue();
//subject.BffFeature.Should().BeTrue();
subject.CibaFeature.Should().BeTrue();
}
{
var subject = new IdentityServerLicense(
new Claim("edition", "enterprise"),
new Claim("feature", "isv"),
new Claim("feature", "unlimited_clients"),
new Claim("client_limit", "20"));
subject.ClientLimit.Should().BeNull();
}
{
var subject = new IdentityServerLicense(
new Claim("edition", "business"),
new Claim("feature", "isv"),
new Claim("feature", "unlimited_clients"),
new Claim("client_limit", "20"));
subject.ClientLimit.Should().BeNull();
}
{
var subject = new IdentityServerLicense(
new Claim("edition", "starter"),
new Claim("feature", "isv"),
new Claim("feature", "unlimited_clients"),
new Claim("client_limit", "20"));
subject.ClientLimit.Should().BeNull();
}
// BFF
// TODO: validate BFF initialize
//{
// var subject = new IdentityServerLicense(
// new Claim("edition", "bff"),
// new Claim("client_limit", "20"),
// new Claim("issuer_limit", "10"),
// new Claim("feature", "resource_isolation"),
// new Claim("feature", "dynamic_providers"),
// new Claim("feature", "ciba"),
// new Claim("feature", "key_management")
// );
// //subject.BffFeature.Should().BeTrue();
// subject.ClientLimit.Should().Be(0);
// subject.IssuerLimit.Should().Be(0);
// subject.KeyManagementFeature.Should().BeFalse();
// subject.ResourceIsolationFeature.Should().BeFalse();
// subject.DynamicProvidersFeature.Should().BeFalse();
// subject.CibaFeature.Should().BeFalse();
//}
}
[Fact]
[Trait("Category", Category)]
public void invalid_edition_should_fail()
{
{
Action func = () => new IdentityServerLicense(new Claim("edition", "invalid"));
func.Should().Throw<Exception>();
}
{
Action func = () => new IdentityServerLicense(new Claim("edition", ""));
func.Should().Throw<Exception>();
}
}
private class MockLicenseValidator : IdentityServerLicenseValidator
{
public MockLicenseValidator()
{
ErrorLog = (str, obj) => { ErrorLogCount++; };
WarningLog = (str, obj) => { WarningLogCount++; };
}
public int ErrorLogCount { get; set; }
public int WarningLogCount { get; set; }
}
[Theory]
[Trait("Category", Category)]
[InlineData(false, 5)]
[InlineData(true, 15)]
public void client_count_exceeded_should_warn(bool hasLicense, int allowedClients)
{
var license = hasLicense ? new IdentityServerLicense(new Claim("edition", "business")) : null;
var subject = new MockLicenseValidator();
for (int i = 0; i < allowedClients; i++)
{
subject.ValidateClient("client" + i, license);
}
// Adding the allowed number of clients shouldn't log.
subject.ErrorLogCount.Should().Be(0);
subject.WarningLogCount.Should().Be(0);
// Validating same client again shouldn't log.
subject.ValidateClient("client3", license);
subject.ErrorLogCount.Should().Be(0);
subject.WarningLogCount.Should().Be(0);
subject.ValidateClient("extra1", license);
subject.ValidateClient("extra2", license);
if (hasLicense)
{
subject.ErrorLogCount.Should().Be(2);
subject.WarningLogCount.Should().Be(0);
}
else
{
subject.ErrorLogCount.Should().Be(0);
subject.WarningLogCount.Should().Be(1);
}
}
}