| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Collections.ObjectModel; |
| | | 6 | | using CoreWCF.IdentityModel.Selectors; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// This class defines a TokenResolver that can wrap multiple Token Resolvers |
| | | 12 | | /// and resolve tokens across all the wrapped token resolvers. |
| | | 13 | | /// </summary> |
| | | 14 | | internal class AggregateTokenResolver : SecurityTokenResolver |
| | | 15 | | { |
| | 148 | 16 | | private readonly List<SecurityTokenResolver> _tokenResolvers = new List<SecurityTokenResolver>(); |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Initializes an instance of <see cref="AggregateTokenResolver"/> |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="tokenResolvers">IEnumerable list of TokenResolvers to be wrapped.</param> |
| | | 22 | | /// <exception cref="ArgumentNullException">The input argument 'tokenResolvers' is null.</exception> |
| | | 23 | | /// <exception cref="ArgumentException">The input 'tokenResolver' list does not contain a valid |
| | | 24 | | /// SecurityTokenResolver. At least one SecurityTokenResolver should be specified.</exception> |
| | 148 | 25 | | public AggregateTokenResolver(IEnumerable<SecurityTokenResolver> tokenResolvers) |
| | | 26 | | { |
| | 148 | 27 | | if (tokenResolvers == null) |
| | | 28 | | { |
| | 0 | 29 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(tokenResolvers)); |
| | | 30 | | } |
| | | 31 | | |
| | 148 | 32 | | AddNonEmptyResolvers(tokenResolvers); |
| | 148 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets a read-only collection of TokenResolvers. |
| | | 37 | | /// </summary> |
| | | 38 | | public ReadOnlyCollection<SecurityTokenResolver> TokenResolvers |
| | | 39 | | { |
| | | 40 | | get |
| | | 41 | | { |
| | 40 | 42 | | return _tokenResolvers.AsReadOnly(); |
| | | 43 | | } |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Override of the base class. Resolves the given SecurityKeyIdentifierClause to a |
| | | 48 | | /// SecurityKey. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="keyIdentifierClause">The Clause to be resolved.</param> |
| | | 51 | | /// <param name="key">The resolved SecurityKey</param> |
| | | 52 | | /// <returns>True if successfully resolved.</returns> |
| | | 53 | | /// <exception cref="ArgumentNullException">Input argument 'keyIdentifierClause' is null.</exception> |
| | | 54 | | protected override bool TryResolveSecurityKeyCore(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityK |
| | | 55 | | { |
| | 0 | 56 | | if (keyIdentifierClause == null) |
| | | 57 | | { |
| | 0 | 58 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause)); |
| | | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | key = null; |
| | 0 | 62 | | foreach (SecurityTokenResolver tokenResolver in _tokenResolvers) |
| | | 63 | | { |
| | 0 | 64 | | if (tokenResolver.TryResolveSecurityKey(keyIdentifierClause, out key)) |
| | | 65 | | { |
| | 0 | 66 | | return true; |
| | | 67 | | } |
| | | 68 | | } |
| | | 69 | | |
| | 0 | 70 | | return false; |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Override of the base class. Resolves the given SecurityKeyIdentifier to a |
| | | 75 | | /// SecurityToken. |
| | | 76 | | /// </summary> |
| | | 77 | | /// <param name="keyIdentifier">The KeyIdentifier to be resolved.</param> |
| | | 78 | | /// <param name="token">The resolved SecurityToken</param> |
| | | 79 | | /// <returns>True if successfully resolved.</returns> |
| | | 80 | | /// <exception cref="ArgumentNullException">Input argument 'keyIdentifier' is null.</exception> |
| | | 81 | | protected override bool TryResolveTokenCore(SecurityKeyIdentifier keyIdentifier, out SecurityToken token) |
| | | 82 | | { |
| | 0 | 83 | | if (keyIdentifier == null) |
| | | 84 | | { |
| | 0 | 85 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifier)); |
| | | 86 | | } |
| | | 87 | | |
| | 0 | 88 | | token = null; |
| | 0 | 89 | | foreach (SecurityTokenResolver tokenResolver in _tokenResolvers) |
| | | 90 | | { |
| | 0 | 91 | | if (tokenResolver.TryResolveToken(keyIdentifier, out token)) |
| | | 92 | | { |
| | 0 | 93 | | return true; |
| | | 94 | | } |
| | | 95 | | } |
| | | 96 | | |
| | 0 | 97 | | return false; |
| | 0 | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Override of the base class. Resolves the given SecurityKeyIdentifierClause to a |
| | | 102 | | /// SecurityToken. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <param name="keyIdentifierClause">The KeyIdentifier to be resolved.</param> |
| | | 105 | | /// <param name="token">The resolved SecurityToken</param> |
| | | 106 | | /// <returns>True if successfully resolved.</returns> |
| | | 107 | | /// <exception cref="ArgumentNullException">Input argument 'keyIdentifierClause' is null.</exception> |
| | | 108 | | protected override bool TryResolveTokenCore(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityToken t |
| | | 109 | | { |
| | 0 | 110 | | if (keyIdentifierClause == null) |
| | | 111 | | { |
| | 0 | 112 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause)); |
| | | 113 | | } |
| | | 114 | | |
| | 0 | 115 | | token = null; |
| | 0 | 116 | | foreach (SecurityTokenResolver tokenResolver in _tokenResolvers) |
| | | 117 | | { |
| | 0 | 118 | | if (tokenResolver.TryResolveToken(keyIdentifierClause, out token)) |
| | | 119 | | { |
| | 0 | 120 | | return true; |
| | | 121 | | } |
| | | 122 | | } |
| | | 123 | | |
| | 0 | 124 | | return false; |
| | 0 | 125 | | } |
| | | 126 | | |
| | | 127 | | private void AddNonEmptyResolvers(IEnumerable<SecurityTokenResolver> resolvers) |
| | | 128 | | { |
| | 596 | 129 | | foreach (SecurityTokenResolver resolver in resolvers) |
| | | 130 | | { |
| | 150 | 131 | | if (resolver != null) // && resolver != EmptySecurityTokenResolver.Instance) |
| | | 132 | | { |
| | 150 | 133 | | _tokenResolvers.Add(resolver); |
| | | 134 | | } |
| | | 135 | | } |
| | 148 | 136 | | } |
| | | 137 | | } |
| | | 138 | | } |