| | | 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.Generic; |
| | | 6 | | using System.Collections.ObjectModel; |
| | | 7 | | using System.Security.Claims; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | using CoreWCF.Dispatcher; |
| | | 11 | | using CoreWCF.IdentityModel.Policy; |
| | | 12 | | using CoreWCF.IdentityModel.Selectors; |
| | | 13 | | using CoreWCF.IdentityModel.Tokens; |
| | | 14 | | using CoreWCF.Security.Tokens; |
| | | 15 | | |
| | | 16 | | namespace CoreWCF.Security |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Wraps a SessionSecurityTokenHandler. Delegates the token authentication call to |
| | | 20 | | /// this wrapped tokenAuthenticator. Wraps the returned ClaimsIdentities into |
| | | 21 | | /// an IAuthorizationPolicy. This class is wired into WCF and actually receives |
| | | 22 | | /// SecurityContextSecurityTokens which are then wrapped into SessionSecurityTokens for |
| | | 23 | | /// validation. |
| | | 24 | | /// </summary> |
| | | 25 | | internal class WrappedSessionSecurityTokenAuthenticator : SecurityTokenAuthenticator, IIssuanceSecurityTokenAuthenti |
| | | 26 | | { |
| | | 27 | | private readonly SessionSecurityTokenHandler _sessionTokenHandler; |
| | | 28 | | private readonly IIssuanceSecurityTokenAuthenticator _issuanceSecurityTokenAuthenticator; |
| | | 29 | | private readonly ICommunicationObject _communicationObject; |
| | | 30 | | private readonly SctClaimsHandler _sctClaimsHandler; |
| | | 31 | | private readonly ExceptionMapper _exceptionMapper; |
| | | 32 | | |
| | | 33 | | #pragma warning disable CS0067 // The event is never used |
| | | 34 | | public event EventHandler Closed; |
| | | 35 | | public event EventHandler Faulted; |
| | | 36 | | #pragma warning restore CS0067 // The event is never used |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Initializes an instance of <see cref="WrappedRsaSecurityTokenAuthenticator"/> |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="sessionTokenHandler">The sessionTokenHandler to wrap</param> |
| | | 42 | | /// <param name="wcfSessionAuthenticator">The wcf SessionTokenAuthenticator.</param> |
| | | 43 | | /// <param name="sctClaimsHandler">Handler that converts WCF generated IAuthorizationPolicy to <see cref="Author |
| | | 44 | | /// <param name="exceptionMapper">Converts token validation exception to SOAP faults.</param> |
| | | 45 | | public WrappedSessionSecurityTokenAuthenticator(SessionSecurityTokenHandler sessionTokenHandler, |
| | | 46 | | SecurityTokenAuthenticator wcfSessionAuthenticator, |
| | | 47 | | SctClaimsHandler sctClaimsHandler, |
| | | 48 | | ExceptionMapper exceptionMapper) |
| | | 49 | | : base() |
| | | 50 | | { |
| | | 51 | | if (wcfSessionAuthenticator == null) |
| | | 52 | | { |
| | | 53 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(wcfSessionAuthenticator)); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | _issuanceSecurityTokenAuthenticator = wcfSessionAuthenticator as IIssuanceSecurityTokenAuthenticator; |
| | | 57 | | if (_issuanceSecurityTokenAuthenticator == null) |
| | | 58 | | { |
| | | 59 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperInvalidOperation(SR.Format(SR.ID4244)); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | _communicationObject = wcfSessionAuthenticator as ICommunicationObject; |
| | | 63 | | if (_communicationObject == null) |
| | | 64 | | { |
| | | 65 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperInvalidOperation(SR.Format(SR.ID4245)); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | _sessionTokenHandler = sessionTokenHandler ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNu |
| | | 69 | | _sctClaimsHandler = sctClaimsHandler ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nam |
| | | 70 | | |
| | | 71 | | _exceptionMapper = exceptionMapper ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameo |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Validates the token using the wrapped token handler and generates IAuthorizationPolicy |
| | | 76 | | /// wrapping the returned ClaimsIdentities. |
| | | 77 | | /// </summary> |
| | | 78 | | /// <param name="token">Token to be validated. This is always a SecurityContextSecurityToken.</param> |
| | | 79 | | /// <returns>Read-only collection of IAuthorizationPolicy</returns> |
| | | 80 | | protected override ValueTask<ReadOnlyCollection<IAuthorizationPolicy>> ValidateTokenCoreAsync(SecurityToken toke |
| | | 81 | | { |
| | | 82 | | SecurityContextSecurityToken sct = token as SecurityContextSecurityToken; |
| | | 83 | | SessionSecurityToken sessionToken = SecurityContextSecurityTokenHelper.ConvertSctToSessionToken(sct); |
| | | 84 | | IEnumerable<ClaimsIdentity> identities = null; |
| | | 85 | | |
| | | 86 | | try |
| | | 87 | | { |
| | | 88 | | identities = _sessionTokenHandler.ValidateToken(sessionToken, _sctClaimsHandler.EndpointId); |
| | | 89 | | } |
| | | 90 | | catch (Exception ex) |
| | | 91 | | { |
| | | 92 | | if (!_exceptionMapper.HandleSecurityTokenProcessingException(ex)) |
| | | 93 | | { |
| | | 94 | | throw; |
| | | 95 | | } |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | return new ValueTask<ReadOnlyCollection<IAuthorizationPolicy>>(new List<IAuthorizationPolicy>(new Authorizat |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | protected override bool CanValidateTokenCore(SecurityToken token) |
| | | 102 | | { |
| | | 103 | | return (token is SecurityContextSecurityToken); |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// The SecurityServiceDispatcher is updated to communication obj which is SecuritySessionSecurityTokenAuthentic |
| | | 108 | | /// </summary> |
| | | 109 | | /// <param name="securitySeviceDispatcher"></param> |
| | | 110 | | public void SetSecureServiceDispatcher(SecurityServiceDispatcher securitySeviceDispatcher) |
| | | 111 | | { |
| | | 112 | | if (_communicationObject == null || !(_communicationObject is SecuritySessionSecurityTokenAuthenticator)) |
| | | 113 | | { |
| | | 114 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperInvalidOperation(SR.Format(SR.ID4245)); |
| | | 115 | | } |
| | | 116 | | var authenticator = (SecuritySessionSecurityTokenAuthenticator)_communicationObject; |
| | | 117 | | authenticator.SecurityServiceDispatcher = securitySeviceDispatcher; |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | #region IIssuanceSecurityTokenAuthenticator Members |
| | | 121 | | |
| | | 122 | | public IssuedSecurityTokenHandler IssuedSecurityTokenHandler |
| | | 123 | | { |
| | | 124 | | get |
| | | 125 | | { |
| | | 126 | | return _issuanceSecurityTokenAuthenticator.IssuedSecurityTokenHandler; |
| | | 127 | | } |
| | | 128 | | set |
| | | 129 | | { |
| | | 130 | | _issuanceSecurityTokenAuthenticator.IssuedSecurityTokenHandler = value; |
| | | 131 | | } |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | public RenewedSecurityTokenHandler RenewedSecurityTokenHandler |
| | | 135 | | { |
| | | 136 | | get |
| | | 137 | | { |
| | | 138 | | return _issuanceSecurityTokenAuthenticator.RenewedSecurityTokenHandler; |
| | | 139 | | } |
| | | 140 | | set |
| | | 141 | | { |
| | | 142 | | _issuanceSecurityTokenAuthenticator.RenewedSecurityTokenHandler = value; |
| | | 143 | | } |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | #endregion |
| | | 147 | | |
| | | 148 | | |
| | | 149 | | #region ICommunicationObject Members |
| | | 150 | | |
| | | 151 | | // all these methods are passthroughs |
| | | 152 | | |
| | | 153 | | public void Abort() |
| | | 154 | | { |
| | | 155 | | _communicationObject.Abort(); |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | public event System.EventHandler Closing |
| | | 159 | | { |
| | | 160 | | add { _communicationObject.Closing += value; } |
| | | 161 | | remove { _communicationObject.Closing -= value; } |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | public Task CloseAsync() |
| | | 165 | | { |
| | | 166 | | return _communicationObject.CloseAsync(); |
| | | 167 | | } |
| | | 168 | | public Task CloseAsync(CancellationToken token) |
| | | 169 | | { |
| | | 170 | | return _communicationObject.CloseAsync(token); |
| | | 171 | | } |
| | | 172 | | public Task OpenAsync() |
| | | 173 | | { |
| | | 174 | | return _communicationObject.OpenAsync(); |
| | | 175 | | } |
| | | 176 | | public Task OpenAsync(CancellationToken token) |
| | | 177 | | { |
| | | 178 | | return _communicationObject.OpenAsync(token); |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | public event System.EventHandler Opened |
| | | 182 | | { |
| | | 183 | | add { _communicationObject.Opened += value; } |
| | | 184 | | remove { _communicationObject.Opened -= value; } |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | public event System.EventHandler Opening |
| | | 188 | | { |
| | | 189 | | add { _communicationObject.Opening += value; } |
| | | 190 | | remove { _communicationObject.Opening -= value; } |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | public CommunicationState State |
| | | 194 | | { |
| | | 195 | | |
| | | 196 | | get { return _communicationObject.State; } |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | #endregion |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | /// <summary> |
| | | 203 | | /// Defines a SecurityStateEncoder whose Encode and Decode operations are |
| | | 204 | | /// a no-op. This class is used to null WCF SecurityContextToken creation |
| | | 205 | | /// code to skip any encryption and decryption cost. When SessionSecurityTokenHandler |
| | | 206 | | /// is being used we will use our own EncryptionTransform and ignore the WCF |
| | | 207 | | /// generated cookie. |
| | | 208 | | /// </summary> |
| | | 209 | | internal class NoOpSecurityStateEncoder : SecurityStateEncoder |
| | | 210 | | { |
| | | 211 | | protected internal override byte[] EncodeSecurityState(byte[] data) |
| | | 212 | | { |
| | 0 | 213 | | return data; |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | protected internal override byte[] DecodeSecurityState(byte[] data) |
| | | 217 | | { |
| | 0 | 218 | | return data; |
| | | 219 | | } |
| | | 220 | | } |
| | | 221 | | } |