Skip to content
This repository has been archived by the owner on Nov 20, 2018. It is now read-only.

Commit

Permalink
Add safe copy for enumeration (#1052)
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoK committed Oct 24, 2018
1 parent 19908d9 commit 91db78c
Showing 1 changed file with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -49,6 +50,9 @@ protected AuthenticationSchemeProvider(IOptions<AuthenticationOptions> options,

private readonly IDictionary<string, AuthenticationScheme> _schemes;
private readonly List<AuthenticationScheme> _requestHandlers;
// Used as a safe return value for enumeration apis
private IEnumerable<AuthenticationScheme> _schemesCopy = Array.Empty<AuthenticationScheme>();
private IEnumerable<AuthenticationScheme> _requestHandlersCopy = Array.Empty<AuthenticationScheme>();

private Task<AuthenticationScheme> GetDefaultSchemeAsync()
=> _options.DefaultScheme != null
Expand Down Expand Up @@ -123,7 +127,7 @@ public virtual Task<AuthenticationScheme> GetSchemeAsync(string name)
/// </summary>
/// <returns>The schemes in priority order for request handling</returns>
public virtual Task<IEnumerable<AuthenticationScheme>> GetRequestHandlerSchemesAsync()
=> Task.FromResult<IEnumerable<AuthenticationScheme>>(_requestHandlers);
=> Task.FromResult(_requestHandlersCopy);

/// <summary>
/// Registers a scheme for use by <see cref="IAuthenticationService"/>.
Expand All @@ -144,8 +148,10 @@ public virtual void AddScheme(AuthenticationScheme scheme)
if (typeof(IAuthenticationRequestHandler).IsAssignableFrom(scheme.HandlerType))
{
_requestHandlers.Add(scheme);
_requestHandlersCopy = _requestHandlers.ToArray();
}
_schemes[scheme.Name] = scheme;
_schemesCopy = _schemes.Values.ToArray();
}
}

Expand All @@ -164,13 +170,17 @@ public virtual void RemoveScheme(string name)
if (_schemes.ContainsKey(name))
{
var scheme = _schemes[name];
_requestHandlers.Remove(scheme);
if (_requestHandlers.Remove(scheme))
{
_requestHandlersCopy = _requestHandlers.ToArray();
}
_schemes.Remove(name);
_schemesCopy = _schemes.Values.ToArray();
}
}
}

public virtual Task<IEnumerable<AuthenticationScheme>> GetAllSchemesAsync()
=> Task.FromResult<IEnumerable<AuthenticationScheme>>(_schemes.Values);
=> Task.FromResult(_schemesCopy);
}
}

0 comments on commit 91db78c

Please sign in to comment.