| | 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.Concurrent; |
| | 7 | | using System.Threading; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | | using Microsoft.Azure.KeyVault.Core; |
| | 10 | |
|
| | 11 | | namespace Microsoft.Azure.KeyVault |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// The collection of key resolvers that would iterate on a key id to resolve to <see cref="IKey"/>. |
| | 15 | | /// </summary> |
| | 16 | | public class AggregateKeyResolver : IKeyResolver |
| | 17 | | { |
| 0 | 18 | | private readonly ConcurrentBag<IKeyResolver> _resolvers = new ConcurrentBag<IKeyResolver>(); |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Adds a key resolver to the collection of key resolvers. |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="resolver">The key resolver to add to the collection</param> |
| | 24 | | /// <returns></returns> |
| | 25 | | public AggregateKeyResolver Add( IKeyResolver resolver ) |
| | 26 | | { |
| 0 | 27 | | if ( resolver == null ) |
| 0 | 28 | | throw new ArgumentNullException( "resolver" ); |
| | 29 | |
|
| 0 | 30 | | _resolvers.Add( resolver ); |
| | 31 | |
|
| 0 | 32 | | return this; |
| | 33 | | } |
| | 34 | |
|
| | 35 | | #region IKeyResolver |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Resolve a key indicated by its ID to the corresponding <see cref="IKey"/> |
| | 39 | | /// </summary> |
| | 40 | | /// <param name="kid"> the key identifier </param> |
| | 41 | | /// <param name="token"> the cancellation token </param> |
| | 42 | | /// <returns> task result of the <see cref="IKey"/></returns> |
| | 43 | | public async Task<IKey> ResolveKeyAsync( string kid, CancellationToken token ) |
| | 44 | | { |
| 0 | 45 | | foreach ( var resolver in _resolvers ) |
| | 46 | | { |
| 0 | 47 | | IKey resolved = await resolver.ResolveKeyAsync( kid, token ).ConfigureAwait( false ); |
| | 48 | |
|
| 0 | 49 | | if ( resolved != null ) |
| | 50 | | { |
| 0 | 51 | | return resolved; |
| | 52 | | } |
| | 53 | | } |
| | 54 | |
|
| 0 | 55 | | return null; |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | #endregion |
| | 59 | | } |
| | 60 | | } |