| | | 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; |
| | | 5 | | using System.Collections.ObjectModel; |
| | | 6 | | using CoreWCF.IdentityModel.Tokens; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.IdentityModel.Selectors |
| | | 9 | | { |
| | | 10 | | // TODO: Evaluate removing this class and all usages as the full framework implementation has a lot more functionali |
| | | 11 | | public abstract class SecurityTokenResolver |
| | | 12 | | { |
| | | 13 | | public SecurityToken ResolveToken(SecurityKeyIdentifier keyIdentifier) |
| | | 14 | | { |
| | 0 | 15 | | if (keyIdentifier == null) |
| | | 16 | | { |
| | 0 | 17 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifier)); |
| | | 18 | | } |
| | 0 | 19 | | if (!TryResolveTokenCore(keyIdentifier, out SecurityToken token)) |
| | | 20 | | { |
| | 0 | 21 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format("Una |
| | | 22 | | } |
| | 0 | 23 | | return token; |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | public bool TryResolveToken(SecurityKeyIdentifier keyIdentifier, out SecurityToken token) |
| | | 27 | | { |
| | 77 | 28 | | if (keyIdentifier == null) |
| | | 29 | | { |
| | 0 | 30 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifier)); |
| | | 31 | | } |
| | 77 | 32 | | return TryResolveTokenCore(keyIdentifier, out token); |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | public SecurityToken ResolveToken(SecurityKeyIdentifierClause keyIdentifierClause) |
| | | 36 | | { |
| | 0 | 37 | | if (keyIdentifierClause == null) |
| | | 38 | | { |
| | 0 | 39 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause)); |
| | | 40 | | } |
| | 0 | 41 | | if (!TryResolveTokenCore(keyIdentifierClause, out SecurityToken token)) |
| | | 42 | | { |
| | 0 | 43 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format("Una |
| | | 44 | | } |
| | 0 | 45 | | return token; |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | public bool TryResolveToken(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityToken token) |
| | | 49 | | { |
| | 0 | 50 | | if (keyIdentifierClause == null) |
| | | 51 | | { |
| | 0 | 52 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause)); |
| | | 53 | | } |
| | 0 | 54 | | return TryResolveTokenCore(keyIdentifierClause, out token); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | public SecurityKey ResolveSecurityKey(SecurityKeyIdentifierClause keyIdentifierClause) |
| | | 58 | | { |
| | 0 | 59 | | if (keyIdentifierClause == null) |
| | | 60 | | { |
| | 0 | 61 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause)); |
| | | 62 | | } |
| | 0 | 63 | | if (!TryResolveSecurityKeyCore(keyIdentifierClause, out SecurityKey key)) |
| | | 64 | | { |
| | 0 | 65 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format("Una |
| | | 66 | | } |
| | 0 | 67 | | return key; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | public bool TryResolveSecurityKey(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityKey key) |
| | | 71 | | { |
| | 3 | 72 | | if (keyIdentifierClause == null) |
| | | 73 | | { |
| | 0 | 74 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause)); |
| | | 75 | | } |
| | 3 | 76 | | return TryResolveSecurityKeyCore(keyIdentifierClause, out key); |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | // protected methods |
| | | 80 | | protected abstract bool TryResolveTokenCore(SecurityKeyIdentifier keyIdentifier, out SecurityToken token); |
| | | 81 | | protected abstract bool TryResolveTokenCore(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityToken t |
| | | 82 | | protected abstract bool TryResolveSecurityKeyCore(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityK |
| | | 83 | | |
| | | 84 | | public static SecurityTokenResolver CreateDefaultSecurityTokenResolver(ReadOnlyCollection<SecurityToken> tokens, |
| | | 85 | | { |
| | 7 | 86 | | return new SimpleTokenResolver(tokens, canMatchLocalId); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | private class SimpleTokenResolver : SecurityTokenResolver |
| | | 90 | | { |
| | | 91 | | private readonly ReadOnlyCollection<SecurityToken> _tokens; |
| | | 92 | | private readonly bool _canMatchLocalId; |
| | | 93 | | |
| | 7 | 94 | | public SimpleTokenResolver(ReadOnlyCollection<SecurityToken> tokens, bool canMatchLocalId) |
| | | 95 | | { |
| | 7 | 96 | | _tokens = tokens ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(tokens)); |
| | 7 | 97 | | _canMatchLocalId = canMatchLocalId; |
| | 7 | 98 | | } |
| | | 99 | | |
| | | 100 | | protected override bool TryResolveSecurityKeyCore(SecurityKeyIdentifierClause keyIdentifierClause, out Secur |
| | | 101 | | { |
| | 1 | 102 | | if (keyIdentifierClause == null) |
| | | 103 | | { |
| | 0 | 104 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause)); |
| | | 105 | | } |
| | | 106 | | |
| | 1 | 107 | | key = null; |
| | 2 | 108 | | for (int i = 0; i < _tokens.Count; ++i) |
| | | 109 | | { |
| | 1 | 110 | | SecurityKey securityKey = _tokens[i].ResolveKeyIdentifierClause(keyIdentifierClause); |
| | 1 | 111 | | if (securityKey != null) |
| | | 112 | | { |
| | 1 | 113 | | key = securityKey; |
| | 1 | 114 | | return true; |
| | | 115 | | } |
| | | 116 | | } |
| | | 117 | | |
| | 0 | 118 | | if (keyIdentifierClause is EncryptedKeyIdentifierClause) |
| | | 119 | | { |
| | 0 | 120 | | EncryptedKeyIdentifierClause keyClause = (EncryptedKeyIdentifierClause)keyIdentifierClause; |
| | 0 | 121 | | SecurityKeyIdentifier keyIdentifier = keyClause.EncryptingKeyIdentifier; |
| | 0 | 122 | | if (keyIdentifier != null && keyIdentifier.Count > 0) |
| | | 123 | | { |
| | 0 | 124 | | for (int i = 0; i < keyIdentifier.Count; i++) |
| | | 125 | | { |
| | 0 | 126 | | if (TryResolveSecurityKey(keyIdentifier[i], out SecurityKey unwrappingSecurityKey)) |
| | | 127 | | { |
| | 0 | 128 | | byte[] wrappedKey = keyClause.GetEncryptedKey(); |
| | 0 | 129 | | string wrappingAlgorithm = keyClause.EncryptionMethod; |
| | 0 | 130 | | byte[] unwrappedKey = unwrappingSecurityKey.DecryptKey(wrappingAlgorithm, wrappedKey); |
| | 0 | 131 | | key = new InMemorySymmetricSecurityKey(unwrappedKey, false); |
| | 0 | 132 | | return true; |
| | | 133 | | } |
| | | 134 | | } |
| | | 135 | | } |
| | | 136 | | } |
| | | 137 | | |
| | 0 | 138 | | return key != null; |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | protected override bool TryResolveTokenCore(SecurityKeyIdentifier keyIdentifier, out SecurityToken token) |
| | | 142 | | { |
| | 52 | 143 | | if (keyIdentifier == null) |
| | | 144 | | { |
| | 0 | 145 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifier)); |
| | | 146 | | } |
| | | 147 | | |
| | 52 | 148 | | token = null; |
| | 108 | 149 | | for (int i = 0; i < keyIdentifier.Count; ++i) |
| | | 150 | | { |
| | 52 | 151 | | SecurityToken securityToken = ResolveSecurityToken(keyIdentifier[i]); |
| | 52 | 152 | | if (securityToken != null) |
| | | 153 | | { |
| | 50 | 154 | | token = securityToken; |
| | 50 | 155 | | break; |
| | | 156 | | } |
| | | 157 | | } |
| | | 158 | | |
| | 52 | 159 | | return (token != null); |
| | | 160 | | } |
| | | 161 | | |
| | | 162 | | protected override bool TryResolveTokenCore(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityTok |
| | | 163 | | { |
| | 0 | 164 | | if (keyIdentifierClause == null) |
| | | 165 | | { |
| | 0 | 166 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause)); |
| | | 167 | | } |
| | | 168 | | |
| | 0 | 169 | | token = null; |
| | | 170 | | |
| | 0 | 171 | | SecurityToken securityToken = ResolveSecurityToken(keyIdentifierClause); |
| | 0 | 172 | | if (securityToken != null) |
| | | 173 | | { |
| | 0 | 174 | | token = securityToken; |
| | | 175 | | } |
| | | 176 | | |
| | 0 | 177 | | return (token != null); |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | private SecurityToken ResolveSecurityToken(SecurityKeyIdentifierClause keyIdentifierClause) |
| | | 181 | | { |
| | 52 | 182 | | if (keyIdentifierClause == null) |
| | | 183 | | { |
| | 0 | 184 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause)); |
| | | 185 | | } |
| | | 186 | | |
| | 52 | 187 | | if (!_canMatchLocalId && keyIdentifierClause is LocalIdKeyIdentifierClause) |
| | | 188 | | { |
| | 0 | 189 | | return null; |
| | | 190 | | } |
| | | 191 | | |
| | 108 | 192 | | for (int i = 0; i < _tokens.Count; ++i) |
| | | 193 | | { |
| | 52 | 194 | | if (_tokens[i].MatchesKeyIdentifierClause(keyIdentifierClause)) |
| | | 195 | | { |
| | 50 | 196 | | return _tokens[i]; |
| | | 197 | | } |
| | | 198 | | } |
| | | 199 | | |
| | 2 | 200 | | return null; |
| | | 201 | | } |
| | | 202 | | } |
| | | 203 | | } |
| | | 204 | | } |