Skip to content

Commit 1e295dc

Browse files
committed
Refactor AuthenticationOptions
1 parent 44569a5 commit 1e295dc

File tree

4 files changed

+41
-29
lines changed

4 files changed

+41
-29
lines changed

src/Ocelot/Configuration/AuthenticationOptions.cs

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,60 @@
11
using Ocelot.Configuration.File;
2-
using System.Collections.Generic;
2+
using System;
33

44
namespace Ocelot.Configuration
55
{
66
public sealed class AuthenticationOptions
77
{
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 = Array.Empty<string>();
15-
}
16-
178
public AuthenticationOptions(FileAuthenticationOptions from)
189
{
1910
AllowedScopes = from.AllowedScopes ?? new();
20-
AuthenticationProviderKey = from.AuthenticationProviderKey ?? string.Empty;
21-
AuthenticationProviderKeys = from.AuthenticationProviderKeys ?? Array.Empty<string>();
11+
BuildAuthenticationProviderKeys(from.AuthenticationProviderKey, from.AuthenticationProviderKeys);
12+
PolicyName = from.PolicyName;
13+
RequiredRole = from.RequiredRole;
14+
ScopeKey = from.ScopeKey;
15+
RoleKey = from.RoleKey;
2216
}
2317

2418
public AuthenticationOptions(List<string> allowedScopes, string authenticationProviderKey, string[] authenticationProviderKeys)
2519
{
2620
AllowedScopes = allowedScopes ?? new();
27-
AuthenticationProviderKey = authenticationProviderKey ?? string.Empty;
28-
AuthenticationProviderKeys = authenticationProviderKeys ?? Array.Empty<string>();
21+
BuildAuthenticationProviderKeys(authenticationProviderKey, authenticationProviderKeys);
2922
}
3023

31-
public AuthenticationOptions(List<string> allowedScopes, List<string> requiredRole, string authenticationProviderKey, string scopeKey, string roleKey, string policyName)
24+
public AuthenticationOptions(List<string> allowedScopes, string[] authenticationProviderKeys, List<string> requiredRole, string scopeKey, string roleKey, string policyName)
3225
{
33-
PolicyName = policyName;
3426
AllowedScopes = allowedScopes;
27+
AuthenticationProviderKey = string.Empty;
28+
AuthenticationProviderKeys = authenticationProviderKeys ?? Array.Empty<string>();
29+
PolicyName = policyName;
3530
RequiredRole = requiredRole;
36-
AuthenticationProviderKey = authenticationProviderKey;
3731
ScopeKey = scopeKey;
3832
RoleKey = roleKey;
3933
}
4034

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

4360
/// <summary>
@@ -47,7 +64,7 @@ public AuthenticationOptions(List<string> allowedScopes, List<string> requiredRo
4764
/// A <see langword="string"/> value of the scheme name.
4865
/// </value>
4966
[Obsolete("Use the " + nameof(AuthenticationProviderKeys) + " property!")]
50-
public string AuthenticationProviderKey { get; }
67+
public string AuthenticationProviderKey { get; private set; }
5168

5269
/// <summary>
5370
/// Multiple authentication schemes registered in DI services with appropriate authentication providers.
@@ -58,7 +75,7 @@ public AuthenticationOptions(List<string> allowedScopes, List<string> requiredRo
5875
/// <value>
5976
/// An array of <see langword="string"/> values of the scheme names.
6077
/// </value>
61-
public string[] AuthenticationProviderKeys { get; }
78+
public string[] AuthenticationProviderKeys { get; private set; }
6279

6380
public List<string> RequiredRole { get; }
6481
public string ScopeKey { get; }

src/Ocelot/Configuration/Builder/AuthenticationOptionsBuilder.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ public class AuthenticationOptionsBuilder
66
{
77
private List<string> _allowedScopes = new();
88
private List<string> _requiredRole = new();
9-
private string _authenticationProviderKey;
109
private string[] _authenticationProviderKeys = Array.Empty<string>();
1110
private string _roleKey;
1211
private string _scopeKey;
@@ -17,7 +16,7 @@ public AuthenticationOptionsBuilder WithAllowedScopes(List<string> allowedScopes
1716
_allowedScopes = allowedScopes;
1817
return this;
1918
}
20-
19+
2120
public AuthenticationOptionsBuilder WithRequiredRole(List<string> requiredRole)
2221
{
2322
_requiredRole = requiredRole;
@@ -26,10 +25,7 @@ public AuthenticationOptionsBuilder WithRequiredRole(List<string> requiredRole)
2625

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

3430
public AuthenticationOptionsBuilder WithAuthenticationProviderKeys(string[] authenticationProviderKeys)
3531
{
@@ -57,7 +53,7 @@ public AuthenticationOptionsBuilder WithPolicyName(string policyName)
5753

5854
public AuthenticationOptions Build()
5955
{
60-
return new AuthenticationOptions(_allowedScopes, _requiredRole, _authenticationProviderKey, _authenticationProviderKeys, _scopeKey, _roleKey, _policyName);
56+
return new AuthenticationOptions(_allowedScopes, _authenticationProviderKeys, _requiredRole, _scopeKey, _roleKey, _policyName);
6157
}
6258
}
6359
}

src/Ocelot/Configuration/Creator/AuthenticationOptionsCreator.cs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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)