using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace Best.HTTP.Request.Authentication
{
///
/// Stores and manages already received digest infos.
///
public static class DigestStore
{
private static ConcurrentDictionary Digests = new ConcurrentDictionary();
///
/// Array of algorithms that the plugin supports. It's in the order of priority(first has the highest priority).
///
private static string[] SupportedAlgorithms = new string[] { "digest", "basic" };
public static Digest Get(Uri uri)
{
if (Digests.TryGetValue(uri.Host, out var digest))
if (!digest.IsUriProtected(uri))
return null;
return digest;
}
///
/// It will retrieve or create a new Digest for the given Uri.
///
///
///
public static Digest GetOrCreate(Uri uri) => Digests.GetOrAdd(uri.Host, new Digest(uri));
public static void Remove(Uri uri) => Digests.TryRemove(uri.Host, out var _);
public static void Clear() => Digests.Clear();
public static string FindBest(List authHeaders)
{
if (authHeaders == null || authHeaders.Count == 0)
return string.Empty;
List headers = new List(authHeaders.Count);
for (int i = 0; i < authHeaders.Count; ++i)
headers.Add(authHeaders[i].ToLowerInvariant());
for (int i = 0; i < SupportedAlgorithms.Length; ++i)
{
int idx = headers.FindIndex((header) => header.StartsWith(SupportedAlgorithms[i]));
if (idx != -1)
return authHeaders[idx];
}
return string.Empty;
}
}
}