| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 3 | | // license information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | |
|
| | 8 | | namespace Microsoft.Azure.KeyVault |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Singleton class for handling caching of the http bearer challenge |
| | 12 | | /// </summary> |
| | 13 | | public sealed class HttpBearerChallengeCache |
| | 14 | | { |
| 2 | 15 | | private static HttpBearerChallengeCache _instance = new HttpBearerChallengeCache(); |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// Gets the singleton instance of <see cref="HttpBearerChallengeCache"/> |
| | 19 | | /// </summary> |
| | 20 | | /// <returns>Instance of this class</returns> |
| | 21 | | public static HttpBearerChallengeCache GetInstance() |
| | 22 | | { |
| 20 | 23 | | return _instance; |
| | 24 | | } |
| | 25 | |
|
| | 26 | | private Dictionary<string, HttpBearerChallenge> _cache = null; |
| | 27 | | private object _cacheLock = null; |
| | 28 | |
|
| 2 | 29 | | private HttpBearerChallengeCache() |
| | 30 | | { |
| 2 | 31 | | _cache = new Dictionary<string, HttpBearerChallenge>(); |
| 2 | 32 | | _cacheLock = new object(); |
| 2 | 33 | | } |
| | 34 | |
|
| | 35 | | #if WINDOWS_PHONE |
| | 36 | |
|
| | 37 | | public HttpBearerChallenge this[Uri url] |
| | 38 | | { |
| | 39 | | get |
| | 40 | | { |
| | 41 | | if ( url == null ) |
| | 42 | | throw new ArgumentNullException( "url" ); |
| | 43 | |
|
| | 44 | | HttpBearerChallenge value = null; |
| | 45 | |
|
| | 46 | | lock ( _cacheLock ) |
| | 47 | | { |
| | 48 | | _cache.TryGetValue( url.FullAuthority(), out value ); |
| | 49 | | } |
| | 50 | |
|
| | 51 | | return value; |
| | 52 | | } |
| | 53 | | set |
| | 54 | | { |
| | 55 | | if ( url == null ) |
| | 56 | | throw new ArgumentNullException( "url" ); |
| | 57 | |
|
| | 58 | | if ( value != null && string.Compare( url.FullAuthority(), value.SourceAuthority, StringComparison.Ordin |
| | 59 | | throw new ArgumentException( "Source URL and Challenge URL do not match" ); |
| | 60 | |
|
| | 61 | | lock ( _cacheLock ) |
| | 62 | | { |
| | 63 | | if ( value == null ) |
| | 64 | | _cache.Remove( url.FullAuthority() ); |
| | 65 | | else |
| | 66 | | _cache[url.FullAuthority()] = value; |
| | 67 | | } |
| | 68 | | } |
| | 69 | | } |
| | 70 | |
|
| | 71 | | #else |
| | 72 | | /// <summary> |
| | 73 | | /// Gets the challenge for the cached URL. |
| | 74 | | /// </summary> |
| | 75 | | /// <param name="url"> the URL that the challenge is cached for.</param> |
| | 76 | | /// <returns>the cached challenge or null otherwise.</returns> |
| | 77 | | public HttpBearerChallenge GetChallengeForURL(Uri url) |
| | 78 | | { |
| 14 | 79 | | if (url == null) |
| 0 | 80 | | throw new ArgumentNullException("url"); |
| | 81 | |
|
| 14 | 82 | | HttpBearerChallenge value = null; |
| | 83 | |
|
| 14 | 84 | | lock (_cacheLock) |
| | 85 | | { |
| 14 | 86 | | _cache.TryGetValue(url.FullAuthority(), out value); |
| 14 | 87 | | } |
| | 88 | |
|
| 14 | 89 | | return value; |
| | 90 | | } |
| | 91 | |
|
| | 92 | | /// <summary> |
| | 93 | | /// Removes the cached challenge for the specified URL |
| | 94 | | /// </summary> |
| | 95 | | /// <param name="url"> the URL to remove its cached challenge </param> |
| | 96 | | public void RemoveChallengeForURL(Uri url) |
| | 97 | | { |
| 0 | 98 | | if (url == null) |
| 0 | 99 | | throw new ArgumentNullException("url"); |
| | 100 | |
|
| 0 | 101 | | lock (_cacheLock) |
| | 102 | | { |
| 0 | 103 | | _cache.Remove(url.FullAuthority()); |
| 0 | 104 | | } |
| 0 | 105 | | } |
| | 106 | |
|
| | 107 | | /// <summary> |
| | 108 | | /// Caches the challenge for the specified URL |
| | 109 | | /// </summary> |
| | 110 | | /// <param name="url"> URL corresponding to challenge as cache key </param> |
| | 111 | | /// <param name="value"> the challenge </param> |
| | 112 | | public void SetChallengeForURL(Uri url, HttpBearerChallenge value) |
| | 113 | | { |
| 6 | 114 | | if (url == null) |
| 0 | 115 | | throw new ArgumentNullException("url"); |
| | 116 | |
|
| 6 | 117 | | if (value == null) |
| 0 | 118 | | throw new ArgumentNullException("value"); |
| | 119 | |
|
| 6 | 120 | | if (string.Compare(url.FullAuthority(), value.SourceAuthority, StringComparison.OrdinalIgnoreCase) != 0) |
| 0 | 121 | | throw new ArgumentException("Source URL and Challenge URL do not match"); |
| | 122 | |
|
| 6 | 123 | | lock (_cacheLock) |
| | 124 | | { |
| 6 | 125 | | _cache[url.FullAuthority()] = value; |
| 6 | 126 | | } |
| 6 | 127 | | } |
| | 128 | | #endif |
| | 129 | | /// <summary> |
| | 130 | | /// Clears the cache |
| | 131 | | /// </summary> |
| | 132 | | public void Clear() |
| | 133 | | { |
| 0 | 134 | | lock (_cacheLock) |
| | 135 | | { |
| 0 | 136 | | _cache.Clear(); |
| 0 | 137 | | } |
| 0 | 138 | | } |
| | 139 | | } |
| | 140 | | } |