Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Concurrent;
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 +51,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 = new AuthenticationScheme[0];
private IEnumerable<AuthenticationScheme> _requestHandlersCopy = new AuthenticationScheme[0];

private Task<AuthenticationScheme> GetDefaultSchemeAsync()
=> _options.DefaultScheme != null
Expand Down Expand Up @@ -123,7 +128,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 +149,10 @@ public virtual void AddScheme(AuthenticationScheme scheme)
if (typeof(IAuthenticationRequestHandler).IsAssignableFrom(scheme.HandlerType))
{
_requestHandlers.Add(scheme);
_requestHandlersCopy = _requestHandlers.ToList();
}
_schemes[scheme.Name] = scheme;
_schemesCopy = _schemes.Values.ToList();
}
}

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

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