Skip to content

Commit

Permalink
Refactor AuthenticationOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
raman-m committed Apr 7, 2024
1 parent 0e8f6df commit dcd2ed3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 37 deletions.
57 changes: 36 additions & 21 deletions src/Ocelot/Configuration/AuthenticationOptions.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,59 @@
using Ocelot.Configuration.File;
using System.Collections.Generic;

namespace Ocelot.Configuration
{
public sealed class AuthenticationOptions
{
public AuthenticationOptions(List<string> allowedScopes, List<string> requiredRole, string authenticationProviderKey, string scopeKey, string roleKey, string policyName)
{
PolicyName = policyName;
AllowedScopes = allowedScopes;
RequiredRole = requiredRole;
AuthenticationProviderKey = authenticationProviderKey;
AuthenticationProviderKeys = [];
}

public AuthenticationOptions(FileAuthenticationOptions from)
{
AllowedScopes = from.AllowedScopes ?? [];
AuthenticationProviderKey = from.AuthenticationProviderKey ?? string.Empty;
AuthenticationProviderKeys = from.AuthenticationProviderKeys ?? [];
BuildAuthenticationProviderKeys(from.AuthenticationProviderKey, from.AuthenticationProviderKeys);
PolicyName = from.PolicyName;
RequiredRole = from.RequiredRole;
ScopeKey = from.ScopeKey;
RoleKey = from.RoleKey;
}

public AuthenticationOptions(List<string> allowedScopes, string authenticationProviderKey,
string[] authenticationProviderKeys)
public AuthenticationOptions(List<string> allowedScopes, string authenticationProviderKey, string[] authenticationProviderKeys)
{
AllowedScopes = allowedScopes ?? [];
AuthenticationProviderKey = authenticationProviderKey ?? string.Empty;
AuthenticationProviderKeys = authenticationProviderKeys ?? [];
BuildAuthenticationProviderKeys(authenticationProviderKey, authenticationProviderKeys);
}

public AuthenticationOptions(List<string> allowedScopes, List<string> requiredRole, string authenticationProviderKey, string scopeKey, string roleKey, string policyName)
public AuthenticationOptions(List<string> allowedScopes, string[] authenticationProviderKeys, List<string> requiredRole, string scopeKey, string roleKey, string policyName)
{
PolicyName = policyName;
AllowedScopes = allowedScopes;
AuthenticationProviderKey = string.Empty;
AuthenticationProviderKeys = authenticationProviderKeys ?? [];
PolicyName = policyName;
RequiredRole = requiredRole;
AuthenticationProviderKey = authenticationProviderKey;
ScopeKey = scopeKey;
RoleKey = roleKey;
}

/// <summary>
/// Builds auth keys migrating legacy key to new ones.
/// </summary>
/// <param name="legacyKey">The legacy <see cref="AuthenticationProviderKey"/>.</param>
/// <param name="keys">New <see cref="AuthenticationProviderKeys"/> to build.</param>
private void BuildAuthenticationProviderKeys(string legacyKey, string[] keys)
{
keys ??= [];
if (string.IsNullOrEmpty(legacyKey))
{
return;
}

// Add legacy Key to new Keys array as the first element
var arr = new string[keys.Length + 1];
arr[0] = legacyKey;
Array.Copy(keys, 0, arr, 1, keys.Length);

// Update the object
AuthenticationProviderKeys = arr;
AuthenticationProviderKey = string.Empty;
}

public List<string> AllowedScopes { get; }

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

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

public List<string> RequiredRole { get; }
public string ScopeKey { get; }
Expand Down
18 changes: 6 additions & 12 deletions src/Ocelot/Configuration/Builder/AuthenticationOptionsBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System;

namespace Ocelot.Configuration.Builder
{
public class AuthenticationOptionsBuilder
{
private List<string> _allowedScopes = new();
private List<string> _requiredRole = new();
private string _authenticationProviderKey;
private string[] _authenticationProviderKeys =[];
private string[] _authenticationProviderKeys = [];
private string _roleKey;
private string _scopeKey;
private string _policyName;
Expand All @@ -25,14 +22,11 @@ public AuthenticationOptionsBuilder WithRequiredRole(List<string> requiredRole)
}

[Obsolete("Use the " + nameof(WithAuthenticationProviderKeys) + " property!")]
public AuthenticationOptionsBuilder WithAuthenticationProviderKey(string authenticationProviderKey)
{
_authenticationProviderKey = authenticationProviderKey;
return this;
}
public AuthenticationOptionsBuilder WithAuthenticationProviderKey(string authenticationProviderKey)
=> WithAuthenticationProviderKeys([authenticationProviderKey]);

public AuthenticationOptionsBuilder WithAuthenticationProviderKeys(string[] authenticationProviderKeys)
{
{
_authenticationProviderKeys = authenticationProviderKeys;
return this;
}
Expand All @@ -56,8 +50,8 @@ public AuthenticationOptionsBuilder WithPolicyName(string policyName)
}

public AuthenticationOptions Build()
{
return new AuthenticationOptions(_allowedScopes, _requiredRole, _authenticationProviderKey, _authenticationProviderKeys, _scopeKey, _roleKey, _policyName);
{
return new AuthenticationOptions(_allowedScopes, _authenticationProviderKeys, _requiredRole, _scopeKey, _roleKey, _policyName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class AuthenticationOptionsCreator : IAuthenticationOptionsCreator
{
public AuthenticationOptions Create(FileRoute route)
{
return new AuthenticationOptions(route.AuthenticationOptions.AllowedScopes, route.AuthenticationOptions.RequiredRole, route.AuthenticationOptions.AuthenticationProviderKey, route.AuthenticationOptions.ScopeKey, route.AuthenticationOptions.RoleKey, route.AuthenticationOptions.PolicyName);
return new AuthenticationOptions(route.AuthenticationOptions);
}
}
}
5 changes: 2 additions & 3 deletions src/Ocelot/Configuration/File/FileAuthenticationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public FileAuthenticationOptions()

public FileAuthenticationOptions(FileAuthenticationOptions from)
{
AllowedScopes = [..from.AllowedScopes];
AllowedScopes = new(from.AllowedScopes);
AuthenticationProviderKey = from.AuthenticationProviderKey;
AuthenticationProviderKeys = from.AuthenticationProviderKeys;
}
Expand All @@ -22,7 +22,6 @@ public FileAuthenticationOptions(FileAuthenticationOptions from)
public string AuthenticationProviderKey { get; set; }
public string[] AuthenticationProviderKeys { get; set; }

public List<string> AllowedScopes { get; set; }
public List<string> RequiredRole { get; set; }
public string ScopeKey { get; set; }
public string RoleKey { get; set; }
Expand All @@ -34,7 +33,7 @@ public override string ToString() => new StringBuilder()
.Append($"{nameof(AllowedScopes)}:[{string.Join(',', AllowedScopes.Select(x => $"'{x}'"))}]")
.ToString();

public override string ToString2()
public string ToString2()
{
var sb = new StringBuilder();
sb.Append($"{nameof(AuthenticationProviderKey)}:{AuthenticationProviderKey},{nameof(AllowedScopes)}:[");
Expand Down

0 comments on commit dcd2ed3

Please sign in to comment.