Skip to content

Commit dcd2ed3

Browse files
committed
Refactor AuthenticationOptions
1 parent 0e8f6df commit dcd2ed3

File tree

4 files changed

+45
-37
lines changed

4 files changed

+45
-37
lines changed

src/Ocelot/Configuration/AuthenticationOptions.cs

+36-21
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,59 @@
11
using Ocelot.Configuration.File;
2-
using System.Collections.Generic;
32

43
namespace Ocelot.Configuration
54
{
65
public sealed class AuthenticationOptions
76
{
8-
public AuthenticationOptions(List<string> allowedScopes, List<string> requiredRole, string authenticationProviderKey, string scopeKey, string roleKey, string policyName)
9-
{
10-
PolicyName = policyName;
11-
AllowedScopes = allowedScopes;
12-
RequiredRole = requiredRole;
13-
AuthenticationProviderKey = authenticationProviderKey;
14-
AuthenticationProviderKeys = [];
15-
}
16-
177
public AuthenticationOptions(FileAuthenticationOptions from)
188
{
199
AllowedScopes = from.AllowedScopes ?? [];
20-
AuthenticationProviderKey = from.AuthenticationProviderKey ?? string.Empty;
21-
AuthenticationProviderKeys = from.AuthenticationProviderKeys ?? [];
10+
BuildAuthenticationProviderKeys(from.AuthenticationProviderKey, from.AuthenticationProviderKeys);
11+
PolicyName = from.PolicyName;
12+
RequiredRole = from.RequiredRole;
13+
ScopeKey = from.ScopeKey;
14+
RoleKey = from.RoleKey;
2215
}
2316

24-
public AuthenticationOptions(List<string> allowedScopes, string authenticationProviderKey,
25-
string[] authenticationProviderKeys)
17+
public AuthenticationOptions(List<string> allowedScopes, string authenticationProviderKey, string[] authenticationProviderKeys)
2618
{
2719
AllowedScopes = allowedScopes ?? [];
28-
AuthenticationProviderKey = authenticationProviderKey ?? string.Empty;
29-
AuthenticationProviderKeys = authenticationProviderKeys ?? [];
20+
BuildAuthenticationProviderKeys(authenticationProviderKey, authenticationProviderKeys);
3021
}
3122

32-
public AuthenticationOptions(List<string> allowedScopes, List<string> requiredRole, string authenticationProviderKey, string scopeKey, string roleKey, string policyName)
23+
public AuthenticationOptions(List<string> allowedScopes, string[] authenticationProviderKeys, List<string> requiredRole, string scopeKey, string roleKey, string policyName)
3324
{
34-
PolicyName = policyName;
3525
AllowedScopes = allowedScopes;
26+
AuthenticationProviderKey = string.Empty;
27+
AuthenticationProviderKeys = authenticationProviderKeys ?? [];
28+
PolicyName = policyName;
3629
RequiredRole = requiredRole;
37-
AuthenticationProviderKey = authenticationProviderKey;
3830
ScopeKey = scopeKey;
3931
RoleKey = roleKey;
4032
}
4133

34+
/// <summary>
35+
/// Builds auth keys migrating legacy key to new ones.
36+
/// </summary>
37+
/// <param name="legacyKey">The legacy <see cref="AuthenticationProviderKey"/>.</param>
38+
/// <param name="keys">New <see cref="AuthenticationProviderKeys"/> to build.</param>
39+
private void BuildAuthenticationProviderKeys(string legacyKey, string[] keys)
40+
{
41+
keys ??= [];
42+
if (string.IsNullOrEmpty(legacyKey))
43+
{
44+
return;
45+
}
46+
47+
// Add legacy Key to new Keys array as the first element
48+
var arr = new string[keys.Length + 1];
49+
arr[0] = legacyKey;
50+
Array.Copy(keys, 0, arr, 1, keys.Length);
51+
52+
// Update the object
53+
AuthenticationProviderKeys = arr;
54+
AuthenticationProviderKey = string.Empty;
55+
}
56+
4257
public List<string> AllowedScopes { get; }
4358

4459
/// <summary>
@@ -48,7 +63,7 @@ public AuthenticationOptions(List<string> allowedScopes, List<string> requiredRo
4863
/// A <see langword="string"/> value of the scheme name.
4964
/// </value>
5065
[Obsolete("Use the " + nameof(AuthenticationProviderKeys) + " property!")]
51-
public string AuthenticationProviderKey { get; }
66+
public string AuthenticationProviderKey { get; private set; }
5267

5368
/// <summary>
5469
/// Multiple authentication schemes registered in DI services with appropriate authentication providers.
@@ -59,7 +74,7 @@ public AuthenticationOptions(List<string> allowedScopes, List<string> requiredRo
5974
/// <value>
6075
/// An array of <see langword="string"/> values of the scheme names.
6176
/// </value>
62-
public string[] AuthenticationProviderKeys { get; }
77+
public string[] AuthenticationProviderKeys { get; private set; }
6378

6479
public List<string> RequiredRole { get; }
6580
public string ScopeKey { get; }

src/Ocelot/Configuration/Builder/AuthenticationOptionsBuilder.cs

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
using System;
2-
31
namespace Ocelot.Configuration.Builder
42
{
53
public class AuthenticationOptionsBuilder
64
{
75
private List<string> _allowedScopes = new();
86
private List<string> _requiredRole = new();
9-
private string _authenticationProviderKey;
10-
private string[] _authenticationProviderKeys =[];
7+
private string[] _authenticationProviderKeys = [];
118
private string _roleKey;
129
private string _scopeKey;
1310
private string _policyName;
@@ -25,14 +22,11 @@ public AuthenticationOptionsBuilder WithRequiredRole(List<string> requiredRole)
2522
}
2623

2724
[Obsolete("Use the " + nameof(WithAuthenticationProviderKeys) + " property!")]
28-
public AuthenticationOptionsBuilder WithAuthenticationProviderKey(string authenticationProviderKey)
29-
{
30-
_authenticationProviderKey = authenticationProviderKey;
31-
return this;
32-
}
25+
public AuthenticationOptionsBuilder WithAuthenticationProviderKey(string authenticationProviderKey)
26+
=> WithAuthenticationProviderKeys([authenticationProviderKey]);
3327

3428
public AuthenticationOptionsBuilder WithAuthenticationProviderKeys(string[] authenticationProviderKeys)
35-
{
29+
{
3630
_authenticationProviderKeys = authenticationProviderKeys;
3731
return this;
3832
}
@@ -56,8 +50,8 @@ public AuthenticationOptionsBuilder WithPolicyName(string policyName)
5650
}
5751

5852
public AuthenticationOptions Build()
59-
{
60-
return new AuthenticationOptions(_allowedScopes, _requiredRole, _authenticationProviderKey, _authenticationProviderKeys, _scopeKey, _roleKey, _policyName);
53+
{
54+
return new AuthenticationOptions(_allowedScopes, _authenticationProviderKeys, _requiredRole, _scopeKey, _roleKey, _policyName);
6155
}
6256
}
6357
}

src/Ocelot/Configuration/Creator/AuthenticationOptionsCreator.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class AuthenticationOptionsCreator : IAuthenticationOptionsCreator
66
{
77
public AuthenticationOptions Create(FileRoute route)
88
{
9-
return new AuthenticationOptions(route.AuthenticationOptions.AllowedScopes, route.AuthenticationOptions.RequiredRole, route.AuthenticationOptions.AuthenticationProviderKey, route.AuthenticationOptions.ScopeKey, route.AuthenticationOptions.RoleKey, route.AuthenticationOptions.PolicyName);
9+
return new AuthenticationOptions(route.AuthenticationOptions);
1010
}
1111
}
1212
}

src/Ocelot/Configuration/File/FileAuthenticationOptions.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public FileAuthenticationOptions()
1111

1212
public FileAuthenticationOptions(FileAuthenticationOptions from)
1313
{
14-
AllowedScopes = [..from.AllowedScopes];
14+
AllowedScopes = new(from.AllowedScopes);
1515
AuthenticationProviderKey = from.AuthenticationProviderKey;
1616
AuthenticationProviderKeys = from.AuthenticationProviderKeys;
1717
}
@@ -22,7 +22,6 @@ public FileAuthenticationOptions(FileAuthenticationOptions from)
2222
public string AuthenticationProviderKey { get; set; }
2323
public string[] AuthenticationProviderKeys { get; set; }
2424

25-
public List<string> AllowedScopes { get; set; }
2625
public List<string> RequiredRole { get; set; }
2726
public string ScopeKey { get; set; }
2827
public string RoleKey { get; set; }
@@ -34,7 +33,7 @@ public FileAuthenticationOptions(FileAuthenticationOptions from)
3433
.Append($"{nameof(AllowedScopes)}:[{string.Join(',', AllowedScopes.Select(x => $"'{x}'"))}]")
3534
.ToString();
3635

37-
public override string ToString2()
36+
public string ToString2()
3837
{
3938
var sb = new StringBuilder();
4039
sb.Append($"{nameof(AuthenticationProviderKey)}:{AuthenticationProviderKey},{nameof(AllowedScopes)}:[");

0 commit comments

Comments
 (0)