< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Security.SecurityProtocol
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/SecurityProtocol.cs
Line coverage
25%
Covered lines: 71
Uncovered lines: 204
Coverable lines: 275
Total lines: 640
Line coverage: 25.8%
Branch coverage
17%
Covered branches: 29
Total branches: 169
Branch coverage: 17.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/SecurityProtocol.cs

#LineLine coverage
 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
 4using System;
 5using System.Collections.Generic;
 6using System.Collections.ObjectModel;
 7using System.Threading;
 8using System.Threading.Tasks;
 9using CoreWCF.Channels;
 10using CoreWCF.Description;
 11using CoreWCF.IdentityModel.Policy;
 12using CoreWCF.IdentityModel.Selectors;
 13using CoreWCF.IdentityModel.Tokens;
 14using CoreWCF.Runtime;
 15using CoreWCF.Security.Tokens;
 16
 17namespace CoreWCF.Security
 18{
 19    // See SecurityProtocolFactory for contracts on subclasses etc
 20    internal abstract class SecurityProtocol : ISecurityCommunicationObject
 21    {
 22        private static ReadOnlyCollection<SupportingTokenProviderSpecification> s_emptyTokenProviders;
 23        private Dictionary<string, Collection<SupportingTokenProviderSpecification>> _mergedSupportingTokenProvidersMap;
 24
 3425        protected SecurityProtocol(SecurityProtocolFactory factory, EndpointAddress target, Uri via)
 26        {
 3427            SecurityProtocolFactory = factory;
 3428            Target = target;
 3429            Via = via;
 3430            CommunicationObject = new WrapperSecurityCommunicationObject(this);
 3431        }
 32
 21133        protected WrapperSecurityCommunicationObject CommunicationObject { get; }
 34
 62535        public SecurityProtocolFactory SecurityProtocolFactory { get; }
 36
 037        public EndpointAddress Target { get; }
 38
 039        public Uri Via { get; }
 40
 041        public ICollection<SupportingTokenProviderSpecification> ChannelSupportingTokenProviderSpecification { get; priv
 42
 043        public Dictionary<string, ICollection<SupportingTokenProviderSpecification>> ScopedSupportingTokenProviderSpecif
 44
 45        private static ReadOnlyCollection<SupportingTokenProviderSpecification> EmptyTokenProviders
 46        {
 47            get
 48            {
 049                if (s_emptyTokenProviders == null)
 50                {
 051                    s_emptyTokenProviders = new ReadOnlyCollection<SupportingTokenProviderSpecification>(new List<Suppor
 52                }
 053                return s_emptyTokenProviders;
 54            }
 55        }
 56
 57        // ISecurityCommunicationObject members
 58        public TimeSpan DefaultOpenTimeout
 59        {
 6860            get { return ServiceDefaults.OpenTimeout; }
 61        }
 62
 63        public TimeSpan DefaultCloseTimeout
 64        {
 2065            get { return ServiceDefaults.CloseTimeout; }
 66        }
 67
 68        public Task OpenAsync(TimeSpan timeout)
 69        {
 3470            return CommunicationObject.OpenAsync();
 71        }
 72
 1073        public void OnClosed() { }
 74
 075        public void OnClosing() { }
 76
 077        public void OnFaulted() { }
 78
 079        public void OnOpened() { }
 80
 081        public void OnOpening() { }
 82
 83        internal IList<SupportingTokenProviderSpecification> GetSupportingTokenProviders(string action)
 84        {
 085            if (_mergedSupportingTokenProvidersMap != null && _mergedSupportingTokenProvidersMap.Count > 0)
 86            {
 087                if (action != null && _mergedSupportingTokenProvidersMap.ContainsKey(action))
 88                {
 089                    return _mergedSupportingTokenProvidersMap[action];
 90                }
 091                else if (_mergedSupportingTokenProvidersMap.ContainsKey(MessageHeaders.WildcardAction))
 92                {
 093                    return _mergedSupportingTokenProvidersMap[MessageHeaders.WildcardAction];
 94                }
 95            }
 96            // return null if the token providers list is empty - this gets a perf benefit since calling Count is expens
 97            // ReadOnlyCollection
 098            return (ChannelSupportingTokenProviderSpecification == EmptyTokenProviders) ? null : (IList<SupportingTokenP
 99        }
 100
 101        protected InitiatorServiceModelSecurityTokenRequirement CreateInitiatorSecurityTokenRequirement()
 102        {
 0103            InitiatorServiceModelSecurityTokenRequirement requirement = new InitiatorServiceModelSecurityTokenRequiremen
 0104            {
 0105                TargetAddress = Target,
 0106                Via = Via,
 0107                SecurityBindingElement = SecurityProtocolFactory.SecurityBindingElement,
 0108                SecurityAlgorithmSuite = SecurityProtocolFactory.OutgoingAlgorithmSuite,
 0109                MessageSecurityVersion = SecurityProtocolFactory.MessageSecurityVersion.SecurityTokenVersion
 0110            };
 0111            return requirement;
 112        }
 113
 114        private InitiatorServiceModelSecurityTokenRequirement CreateInitiatorSecurityTokenRequirement(SecurityTokenParam
 115        {
 0116            InitiatorServiceModelSecurityTokenRequirement requirement = CreateInitiatorSecurityTokenRequirement();
 0117            parameters.InitializeSecurityTokenRequirement(requirement);
 0118            requirement.KeyUsage = SecurityKeyUsage.Signature;
 0119            requirement.Properties[ServiceModelSecurityTokenRequirement.MessageDirectionProperty] = MessageDirection.Out
 0120            requirement.Properties[ServiceModelSecurityTokenRequirement.SupportingTokenAttachmentModeProperty] = attachm
 0121            return requirement;
 122        }
 123
 124        private void AddSupportingTokenProviders(SupportingTokenParameters supportingTokenParameters, bool isOptional, I
 125        {
 0126            for (int i = 0; i < supportingTokenParameters.Endorsing.Count; ++i)
 127            {
 0128                SecurityTokenRequirement requirement = CreateInitiatorSecurityTokenRequirement(supportingTokenParameters
 129                try
 130                {
 0131                    if (isOptional)
 132                    {
 0133                        requirement.IsOptionalToken = true;
 134                    }
 0135                    SecurityTokenProvider provider = SecurityProtocolFactory.SecurityTokenManager.CreateSecurityTokenPro
 0136                    if (provider == null)
 137                    {
 0138                        continue;
 139                    }
 0140                    SupportingTokenProviderSpecification providerSpec = new SupportingTokenProviderSpecification(provide
 0141                    providerSpecList.Add(providerSpec);
 0142                }
 0143                catch (Exception e)
 144                {
 0145                    if (!isOptional || Fx.IsFatal(e))
 146                    {
 0147                        throw;
 148                    }
 0149                }
 150            }
 151
 0152            for (int i = 0; i < supportingTokenParameters.SignedEndorsing.Count; ++i)
 153            {
 0154                SecurityTokenRequirement requirement = CreateInitiatorSecurityTokenRequirement(supportingTokenParameters
 155                try
 156                {
 0157                    if (isOptional)
 158                    {
 0159                        requirement.IsOptionalToken = true;
 160                    }
 0161                    SecurityTokenProvider provider = SecurityProtocolFactory.SecurityTokenManager.CreateSecurityTokenPro
 0162                    if (provider == null)
 163                    {
 0164                        continue;
 165                    }
 0166                    SupportingTokenProviderSpecification providerSpec = new SupportingTokenProviderSpecification(provide
 0167                    providerSpecList.Add(providerSpec);
 0168                }
 0169                catch (Exception e)
 170                {
 0171                    if (!isOptional || Fx.IsFatal(e))
 172                    {
 0173                        throw;
 174                    }
 0175                }
 176            }
 177
 0178            for (int i = 0; i < supportingTokenParameters.SignedEncrypted.Count; ++i)
 179            {
 0180                SecurityTokenRequirement requirement = CreateInitiatorSecurityTokenRequirement(supportingTokenParameters
 181                try
 182                {
 0183                    if (isOptional)
 184                    {
 0185                        requirement.IsOptionalToken = true;
 186                    }
 0187                    SecurityTokenProvider provider = SecurityProtocolFactory.SecurityTokenManager.CreateSecurityTokenPro
 0188                    if (provider == null)
 189                    {
 0190                        continue;
 191                    }
 0192                    SupportingTokenProviderSpecification providerSpec = new SupportingTokenProviderSpecification(provide
 0193                    providerSpecList.Add(providerSpec);
 0194                }
 0195                catch (Exception e)
 196                {
 0197                    if (!isOptional || Fx.IsFatal(e))
 198                    {
 0199                        throw;
 200                    }
 0201                }
 202            }
 203
 0204            for (int i = 0; i < supportingTokenParameters.Signed.Count; ++i)
 205            {
 0206                SecurityTokenRequirement requirement = CreateInitiatorSecurityTokenRequirement(supportingTokenParameters
 207                try
 208                {
 0209                    if (isOptional)
 210                    {
 0211                        requirement.IsOptionalToken = true;
 212                    }
 0213                    SecurityTokenProvider provider = SecurityProtocolFactory.SecurityTokenManager.CreateSecurityTokenPro
 0214                    if (provider == null)
 215                    {
 0216                        continue;
 217                    }
 0218                    SupportingTokenProviderSpecification providerSpec = new SupportingTokenProviderSpecification(provide
 0219                    providerSpecList.Add(providerSpec);
 0220                }
 0221                catch (Exception e)
 222                {
 0223                    if (!isOptional || Fx.IsFatal(e))
 224                    {
 0225                        throw;
 226                    }
 0227                }
 228            }
 0229        }
 230
 231        private async Task MergeSupportingTokenProvidersAsync(TimeSpan timeout)
 232        {
 0233            if (ScopedSupportingTokenProviderSpecification.Count == 0)
 234            {
 0235                _mergedSupportingTokenProvidersMap = null;
 236            }
 237            else
 238            {
 0239                TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
 0240                SecurityProtocolFactory.ExpectSupportingTokens = true;
 0241                _mergedSupportingTokenProvidersMap = new Dictionary<string, Collection<SupportingTokenProviderSpecificat
 0242                foreach (string action in ScopedSupportingTokenProviderSpecification.Keys)
 243                {
 0244                    ICollection<SupportingTokenProviderSpecification> scopedProviders = ScopedSupportingTokenProviderSpe
 0245                    if (scopedProviders == null || scopedProviders.Count == 0)
 246                    {
 247                        continue;
 248                    }
 0249                    Collection<SupportingTokenProviderSpecification> mergedProviders = new Collection<SupportingTokenPro
 0250                    foreach (SupportingTokenProviderSpecification spec in ChannelSupportingTokenProviderSpecification)
 251                    {
 0252                        mergedProviders.Add(spec);
 253                    }
 0254                    foreach (SupportingTokenProviderSpecification spec in scopedProviders)
 255                    {
 0256                        await SecurityUtils.OpenTokenProviderIfRequiredAsync(spec.TokenProvider, timeoutHelper.GetCancel
 0257                        if (spec.SecurityTokenAttachmentMode == SecurityTokenAttachmentMode.Endorsing || spec.SecurityTo
 258                        {
 0259                            if (spec.TokenParameters.RequireDerivedKeys && !spec.TokenParameters.HasAsymmetricKey)
 260                            {
 0261                                SecurityProtocolFactory.ExpectKeyDerivation = true;
 262                            }
 263                        }
 0264                        mergedProviders.Add(spec);
 0265                    }
 0266                    _mergedSupportingTokenProvidersMap.Add(action, mergedProviders);
 0267                }
 268            }
 0269        }
 270
 271        public virtual async Task OnOpenAsync(TimeSpan timeout)
 272        {
 34273            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
 34274            if (SecurityProtocolFactory.ActAsInitiator)
 275            {
 0276                ChannelSupportingTokenProviderSpecification = new Collection<SupportingTokenProviderSpecification>();
 0277                ScopedSupportingTokenProviderSpecification = new Dictionary<string, ICollection<SupportingTokenProviderS
 278
 0279                AddSupportingTokenProviders(SecurityProtocolFactory.SecurityBindingElement.EndpointSupportingTokenParame
 0280                AddSupportingTokenProviders(SecurityProtocolFactory.SecurityBindingElement.OptionalEndpointSupportingTok
 0281                foreach (string action in SecurityProtocolFactory.SecurityBindingElement.OperationSupportingTokenParamet
 282                {
 0283                    Collection<SupportingTokenProviderSpecification> providerSpecList = new Collection<SupportingTokenPr
 0284                    AddSupportingTokenProviders(SecurityProtocolFactory.SecurityBindingElement.OperationSupportingTokenP
 0285                    ScopedSupportingTokenProviderSpecification.Add(action, providerSpecList);
 286                }
 287
 0288                foreach (string action in SecurityProtocolFactory.SecurityBindingElement.OptionalOperationSupportingToke
 289                {
 290                    Collection<SupportingTokenProviderSpecification> providerSpecList;
 0291                    if (ScopedSupportingTokenProviderSpecification.TryGetValue(action, out ICollection<SupportingTokenPr
 292                    {
 0293                        providerSpecList = ((Collection<SupportingTokenProviderSpecification>)existingList);
 294                    }
 295                    else
 296                    {
 0297                        providerSpecList = new Collection<SupportingTokenProviderSpecification>();
 0298                        ScopedSupportingTokenProviderSpecification.Add(action, providerSpecList);
 299                    }
 300
 0301                    AddSupportingTokenProviders(SecurityProtocolFactory.SecurityBindingElement.OptionalOperationSupporti
 302                }
 303
 0304                if (!ChannelSupportingTokenProviderSpecification.IsReadOnly)
 305                {
 0306                    if (ChannelSupportingTokenProviderSpecification.Count == 0)
 307                    {
 0308                        ChannelSupportingTokenProviderSpecification = EmptyTokenProviders;
 309                    }
 310                    else
 311                    {
 0312                        SecurityProtocolFactory.ExpectSupportingTokens = true;
 0313                        CancellationToken cancellationToken = timeoutHelper.GetCancellationToken();
 0314                        foreach (SupportingTokenProviderSpecification tokenProviderSpec in ChannelSupportingTokenProvide
 315                        {
 0316                            await SecurityUtils.OpenTokenProviderIfRequiredAsync(tokenProviderSpec.TokenProvider, cancel
 0317                            if (tokenProviderSpec.SecurityTokenAttachmentMode == SecurityTokenAttachmentMode.Endorsing |
 318                            {
 0319                                if (tokenProviderSpec.TokenParameters.RequireDerivedKeys && !tokenProviderSpec.TokenPara
 320                                {
 0321                                    SecurityProtocolFactory.ExpectKeyDerivation = true;
 322                                }
 323                            }
 0324                        }
 0325                        ChannelSupportingTokenProviderSpecification =
 0326                            new ReadOnlyCollection<SupportingTokenProviderSpecification>((Collection<SupportingTokenProv
 327                    }
 328                }
 329                // create a merged map of the per operation supporting tokens
 0330                await MergeSupportingTokenProvidersAsync(timeoutHelper.RemainingTime());
 331            }
 34332        }
 333
 334        public virtual void OnAbort()
 335        {
 0336            if (SecurityProtocolFactory.ActAsInitiator)
 337            {
 0338                foreach (SupportingTokenProviderSpecification spec in ChannelSupportingTokenProviderSpecification)
 339                {
 0340                    SecurityUtils.AbortTokenProviderIfRequired(spec.TokenProvider);
 341                }
 0342                foreach (string action in ScopedSupportingTokenProviderSpecification.Keys)
 343                {
 0344                    ICollection<SupportingTokenProviderSpecification> supportingProviders = ScopedSupportingTokenProvide
 0345                    foreach (SupportingTokenProviderSpecification spec in supportingProviders)
 346                    {
 0347                        SecurityUtils.AbortTokenProviderIfRequired(spec.TokenProvider);
 348                    }
 349                }
 350            }
 0351        }
 352
 353        //public virtual async Task OnCloseAsync(CancellationToken token)
 354        //{
 355        //    if (SecurityProtocolFactory.ActAsInitiator)
 356        //    {
 357        //        /*
 358        //        TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
 359        //        foreach (SupportingTokenProviderSpecification spec in ChannelSupportingTokenProviderSpecification)
 360        //        {
 361        //            await SecurityUtils.CloseTokenProviderIfRequiredAsync(spec.TokenProvider, timeoutHelper.RemainingT
 362        //        }
 363
 364        //        foreach (string action in ScopedSupportingTokenProviderSpecification.Keys)
 365        //        {
 366        //            ICollection<SupportingTokenProviderSpecification> supportingProviders = ScopedSupportingTokenProvi
 367        //            foreach (SupportingTokenProviderSpecification spec in supportingProviders)
 368        //            {
 369        //                await SecurityUtils.CloseTokenProviderIfRequiredAsync(spec.TokenProvider, timeoutHelper.Remain
 370        //            }
 371        //        }*/
 372        //    }
 373        //}
 374
 375        private static void SetSecurityHeaderId(SendSecurityHeader securityHeader, Message message)
 376        {
 63377            SecurityMessageProperty messageProperty = message.Properties.Security;
 63378            if (messageProperty != null)
 379            {
 0380                securityHeader.IdPrefix = messageProperty.SenderIdPrefix;
 381            }
 63382        }
 383
 384        private void AddSupportingTokenSpecification(SecurityMessageProperty security, IList<SecurityToken> tokens, Secu
 385        {
 252386            if (tokens == null || tokens.Count == 0)
 387            {
 189388                return;
 389            }
 390
 252391            for (int i = 0; i < tokens.Count; ++i)
 392            {
 63393                security.IncomingSupportingTokens.Add(new SupportingTokenSpecification(tokens[i], tokenPoliciesMapping[t
 394            }
 63395        }
 396
 397        protected void AddSupportingTokenSpecification(SecurityMessageProperty security, IList<SecurityToken> basicToken
 398        {
 63399            AddSupportingTokenSpecification(security, basicTokens, SecurityTokenAttachmentMode.SignedEncrypted, tokenPol
 63400            AddSupportingTokenSpecification(security, endorsingTokens, SecurityTokenAttachmentMode.Endorsing, tokenPolic
 63401            AddSupportingTokenSpecification(security, signedEndorsingTokens, SecurityTokenAttachmentMode.SignedEndorsing
 63402            AddSupportingTokenSpecification(security, signedTokens, SecurityTokenAttachmentMode.Signed, tokenPoliciesMap
 63403        }
 404
 405        protected SendSecurityHeader CreateSendSecurityHeader(Message message, string actor, SecurityProtocolFactory fac
 406        {
 0407            return CreateSendSecurityHeader(message, actor, factory, true);
 408        }
 409
 410        protected SendSecurityHeader CreateSendSecurityHeaderForTransportProtocol(Message message, string actor, Securit
 411        {
 63412            return CreateSendSecurityHeader(message, actor, factory, false);
 413        }
 414
 415        private SendSecurityHeader CreateSendSecurityHeader(Message message, string actor, SecurityProtocolFactory facto
 416        {
 63417            MessageDirection transferDirection = factory.ActAsInitiator ? MessageDirection.Input : MessageDirection.Outp
 63418            SendSecurityHeader sendSecurityHeader = factory.StandardsManager.CreateSendSecurityHeader(
 63419                message,
 63420                actor, true, false,
 63421                factory.OutgoingAlgorithmSuite, transferDirection);
 63422            sendSecurityHeader.Layout = factory.SecurityHeaderLayout;
 63423            sendSecurityHeader.RequireMessageProtection = requireMessageProtection;
 63424            SetSecurityHeaderId(sendSecurityHeader, message);
 63425            if (factory.AddTimestamp)
 426            {
 63427                sendSecurityHeader.AddTimestamp(factory.TimestampValidityDuration);
 428            }
 429
 63430            sendSecurityHeader.StreamBufferManager = factory.StreamBufferManager;
 63431            return sendSecurityHeader;
 432        }
 433
 434        internal void AddMessageSupportingTokens(Message message, ref IList<SupportingTokenSpecification> supportingToke
 435        {
 0436            SecurityMessageProperty supportingTokensProperty = message.Properties.Security;
 0437            if (supportingTokensProperty != null && supportingTokensProperty.HasOutgoingSupportingTokens)
 438            {
 0439                if (supportingTokens == null)
 440                {
 0441                    supportingTokens = new Collection<SupportingTokenSpecification>();
 442                }
 443
 0444                for (int i = 0; i < supportingTokensProperty.OutgoingSupportingTokens.Count; ++i)
 445                {
 0446                    SupportingTokenSpecification spec = supportingTokensProperty.OutgoingSupportingTokens[i];
 0447                    if (spec.SecurityTokenParameters == null)
 448                    {
 0449                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.Sender
 450                    }
 0451                    supportingTokens.Add(spec);
 452                }
 453            }
 0454        }
 455
 456        internal async Task<IList<SupportingTokenSpecification>> TryGetSupportingTokensAsync(SecurityProtocolFactory fac
 457        {
 0458            IList<SupportingTokenSpecification> supportingTokens = null;
 0459            if (!factory.ActAsInitiator)
 460            {
 0461                return null;
 462            }
 463
 0464            if (message == null)
 465            {
 0466                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(message));
 467            }
 468
 0469            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
 0470            IList<SupportingTokenProviderSpecification> supportingTokenProviders = GetSupportingTokenProviders(message.H
 0471            if (supportingTokenProviders != null && supportingTokenProviders.Count > 0)
 472            {
 0473                supportingTokens = new Collection<SupportingTokenSpecification>();
 0474                for (int i = 0; i < supportingTokenProviders.Count; ++i)
 475                {
 0476                    SupportingTokenProviderSpecification spec = supportingTokenProviders[i];
 477                    SecurityToken supportingToken;
 0478                    supportingToken = await spec.TokenProvider.GetTokenAsync(timeoutHelper.GetCancellationToken());
 479
 0480                    supportingTokens.Add(new SupportingTokenSpecification(supportingToken, EmptyReadOnlyCollection<IAuth
 0481                }
 482            }
 483
 484            // add any runtime supporting tokens
 0485            AddMessageSupportingTokens(message, ref supportingTokens);
 0486            return supportingTokens;
 0487        }
 488
 489        protected ReadOnlyCollection<SecurityTokenResolver> MergeOutOfBandResolvers(IList<SupportingTokenAuthenticatorSp
 490        {
 94491            Collection<SecurityTokenResolver> outOfBandResolvers = null;
 94492            if (supportingAuthenticators != null && supportingAuthenticators.Count > 0)
 493            {
 296494                for (int i = 0; i < supportingAuthenticators.Count; ++i)
 495                {
 74496                    if (supportingAuthenticators[i].TokenResolver != null)
 497                    {
 53498                        outOfBandResolvers = outOfBandResolvers ?? new Collection<SecurityTokenResolver>();
 53499                        outOfBandResolvers.Add(supportingAuthenticators[i].TokenResolver);
 500                    }
 501                }
 502            }
 503
 94504            if (outOfBandResolvers != null)
 505            {
 53506                if (primaryResolvers != null)
 507                {
 106508                    for (int i = 0; i < primaryResolvers.Count; ++i)
 509                    {
 0510                        outOfBandResolvers.Insert(0, primaryResolvers[i]);
 511                    }
 512                }
 53513                return new ReadOnlyCollection<SecurityTokenResolver>(outOfBandResolvers);
 514            }
 515            else
 516            {
 41517                return primaryResolvers ?? EmptyReadOnlyCollection<SecurityTokenResolver>.Instance;
 518            }
 519        }
 520
 521        protected void AddSupportingTokens(SendSecurityHeader securityHeader, IList<SupportingTokenSpecification> suppor
 522        {
 0523            if (supportingTokens != null)
 524            {
 0525                for (int i = 0; i < supportingTokens.Count; ++i)
 526                {
 0527                    SecurityToken token = supportingTokens[i].SecurityToken;
 0528                    SecurityTokenParameters tokenParameters = supportingTokens[i].SecurityTokenParameters;
 0529                    switch (supportingTokens[i].SecurityTokenAttachmentMode)
 530                    {
 531                        case SecurityTokenAttachmentMode.Signed:
 0532                            securityHeader.AddSignedSupportingToken(token, tokenParameters);
 0533                            break;
 534                        case SecurityTokenAttachmentMode.Endorsing:
 0535                            securityHeader.AddEndorsingSupportingToken(token, tokenParameters);
 0536                            break;
 537                        case SecurityTokenAttachmentMode.SignedEncrypted:
 0538                            securityHeader.AddBasicSupportingToken(token, tokenParameters);
 0539                            break;
 540                        case SecurityTokenAttachmentMode.SignedEndorsing:
 0541                            securityHeader.AddSignedEndorsingSupportingToken(token, tokenParameters);
 0542                            break;
 543                        default:
 544                            Fx.Assert("Unknown token attachment mode " + supportingTokens[i].SecurityTokenAttachmentMode
 0545                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Forma
 546                    }
 547                }
 548            }
 0549        }
 550
 551        internal static async Task<SecurityToken> GetTokenAsync(SecurityTokenProvider provider, EndpointAddress target, 
 552        {
 0553            if (provider == null)
 554            {
 0555                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.Format(SR.Toke
 556            }
 557
 558            SecurityToken token;
 559            try
 560            {
 0561                token = await provider.GetTokenAsync(new TimeoutHelper(timeout).GetCancellationToken());
 0562            }
 0563            catch (SecurityTokenException exception)
 564            {
 0565                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.Format(SR.Toke
 566            }
 0567            catch (SecurityNegotiationException sne)
 568            {
 0569                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(SR.Format(SR.
 570            }
 571
 0572            return token;
 0573        }
 574
 575        public abstract Message SecureOutgoingMessage(Message message, CancellationToken token);
 576
 577        // subclasses that offer correlation should override this version
 578        public virtual (SecurityProtocolCorrelationState, Message) SecureOutgoingMessage(Message message, SecurityProtoc
 579        {
 41580            return (null, SecureOutgoingMessage(message, token));
 581        }
 582
 583        protected virtual void OnOutgoingMessageSecured(Message securedMessage)
 584        {
 63585        }
 586
 587        protected virtual void OnSecureOutgoingMessageFailure(Message message)
 588        {
 0589        }
 590
 591        public abstract ValueTask<Message> VerifyIncomingMessageAsync(Message message, TimeSpan timeout);
 592
 593        // subclasses that offer correlation should override this version
 594        public virtual async ValueTask<(Message, SecurityProtocolCorrelationState)> VerifyIncomingMessageAsync(Message m
 595        {
 91596            var verifiedMessage = await VerifyIncomingMessageAsync(message, timeout);
 61597            return (verifiedMessage, null);
 61598        }
 599
 600        protected virtual void OnIncomingMessageVerified(Message verifiedMessage)
 601        {
 63602        }
 603
 604        protected virtual void OnVerifyIncomingMessageFailure(Message message, Exception exception)
 605        {
 31606        }
 607        protected IList<SupportingTokenAuthenticatorSpecification> GetSupportingTokenAuthenticatorsAndSetExpectationFlag
 608    ReceiveSecurityHeader securityHeader)
 609        {
 20610            if (message == null)
 611            {
 0612                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(message));
 613            }
 20614            IList<SupportingTokenAuthenticatorSpecification> authenticators = factory.GetSupportingTokenAuthenticators(m
 20615                out bool expectSignedTokens, out bool expectBasicTokens, out bool expectEndorsingTokens);
 20616            securityHeader.ExpectBasicTokens = expectBasicTokens;
 20617            securityHeader.ExpectEndorsingTokens = expectEndorsingTokens;
 20618            securityHeader.ExpectSignedTokens = expectSignedTokens;
 20619            return authenticators;
 620        }
 621
 622        public Task CloseAsync(bool aborted, TimeSpan timeout)
 623        {
 10624            if (aborted)
 625            {
 0626                CommunicationObject.Abort();
 627            }
 628            else
 629            {
 10630                CommunicationObject.CloseAsync();
 631            }
 10632            return Task.CompletedTask;
 633        }
 634        public Task OnCloseAsync(TimeSpan timeout)
 635        {
 10636            OnClosed();
 10637            return Task.CompletedTask;
 638        }
 639    }
 640}

Methods/Properties

.ctor(CoreWCF.Security.SecurityProtocolFactory,CoreWCF.EndpointAddress,System.Uri)
CommunicationObject()
SecurityProtocolFactory()
Target()
Via()
ChannelSupportingTokenProviderSpecification()
ScopedSupportingTokenProviderSpecification()
EmptyTokenProviders()
DefaultOpenTimeout()
DefaultCloseTimeout()
OpenAsync(System.TimeSpan)
OnClosed()
OnClosing()
OnFaulted()
OnOpened()
OnOpening()
GetSupportingTokenProviders(System.String)
CreateInitiatorSecurityTokenRequirement()
CreateInitiatorSecurityTokenRequirement(CoreWCF.Security.Tokens.SecurityTokenParameters,CoreWCF.Security.SecurityTokenAttachmentMode)
AddSupportingTokenProviders(CoreWCF.Security.Tokens.SupportingTokenParameters,System.Boolean,System.Collections.Generic.IList`1<CoreWCF.Security.SupportingTokenProviderSpecification>)
MergeSupportingTokenProvidersAsync()
OnOpenAsync()
OnAbort()
SetSecurityHeaderId(CoreWCF.Security.SendSecurityHeader,CoreWCF.Channels.Message)
AddSupportingTokenSpecification(CoreWCF.Security.SecurityMessageProperty,System.Collections.Generic.IList`1<CoreWCF.IdentityModel.Tokens.SecurityToken>,CoreWCF.Security.SecurityTokenAttachmentMode,System.Collections.Generic.IDictionary`2<CoreWCF.IdentityModel.Tokens.SecurityToken,System.Collections.ObjectModel.ReadOnlyCollection`1<CoreWCF.IdentityModel.Policy.IAuthorizationPolicy>>)
AddSupportingTokenSpecification(CoreWCF.Security.SecurityMessageProperty,System.Collections.Generic.IList`1<CoreWCF.IdentityModel.Tokens.SecurityToken>,System.Collections.Generic.IList`1<CoreWCF.IdentityModel.Tokens.SecurityToken>,System.Collections.Generic.IList`1<CoreWCF.IdentityModel.Tokens.SecurityToken>,System.Collections.Generic.IList`1<CoreWCF.IdentityModel.Tokens.SecurityToken>,System.Collections.Generic.IDictionary`2<CoreWCF.IdentityModel.Tokens.SecurityToken,System.Collections.ObjectModel.ReadOnlyCollection`1<CoreWCF.IdentityModel.Policy.IAuthorizationPolicy>>)
CreateSendSecurityHeader(CoreWCF.Channels.Message,System.String,CoreWCF.Security.SecurityProtocolFactory)
CreateSendSecurityHeaderForTransportProtocol(CoreWCF.Channels.Message,System.String,CoreWCF.Security.SecurityProtocolFactory)
CreateSendSecurityHeader(CoreWCF.Channels.Message,System.String,CoreWCF.Security.SecurityProtocolFactory,System.Boolean)
AddMessageSupportingTokens(CoreWCF.Channels.Message,System.Collections.Generic.IList`1<CoreWCF.Security.SupportingTokenSpecification>&)
TryGetSupportingTokensAsync()
MergeOutOfBandResolvers(System.Collections.Generic.IList`1<CoreWCF.Security.SupportingTokenAuthenticatorSpecification>,System.Collections.ObjectModel.ReadOnlyCollection`1<CoreWCF.IdentityModel.Selectors.SecurityTokenResolver>)
AddSupportingTokens(CoreWCF.Security.SendSecurityHeader,System.Collections.Generic.IList`1<CoreWCF.Security.SupportingTokenSpecification>)
GetTokenAsync()
SecureOutgoingMessage(CoreWCF.Channels.Message,CoreWCF.Security.SecurityProtocolCorrelationState,System.Threading.CancellationToken)
OnOutgoingMessageSecured(CoreWCF.Channels.Message)
OnSecureOutgoingMessageFailure(CoreWCF.Channels.Message)
VerifyIncomingMessageAsync()
OnIncomingMessageVerified(CoreWCF.Channels.Message)
OnVerifyIncomingMessageFailure(CoreWCF.Channels.Message,System.Exception)
GetSupportingTokenAuthenticatorsAndSetExpectationFlags(CoreWCF.Security.SecurityProtocolFactory,CoreWCF.Channels.Message,CoreWCF.Security.ReceiveSecurityHeader)
CloseAsync(System.Boolean,System.TimeSpan)
OnCloseAsync(System.TimeSpan)