< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Security.RequestSecurityToken
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/RequestSecurityToken.cs
Line coverage
13%
Covered lines: 27
Uncovered lines: 166
Coverable lines: 193
Total lines: 571
Line coverage: 13.9%
Branch coverage
5%
Covered branches: 5
Total branches: 88
Branch coverage: 5.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%110%
.ctor(...)100%110%
.ctor(...)100%110%
.ctor(...)100%110%
.ctor(...)50%44100%
.ctor(...)100%110%
.ctor(...)0%220%
GetChannelBinding()0%440%
SetBinaryNegotiation(...)0%440%
GetBinaryNegotiation()0%660%
GetRequestorEntropy()100%110%
GetRequestorEntropy(...)50%2266.66%
SetRequestorEntropy(...)0%220%
SetAppliesTo(...)0%660%
GetAppliesToQName(...)50%2275%
GetAppliesTo()100%110%
GetAppliesTo(...)0%440%
OnWriteTo(...)0%220%
WriteTo(...)0%660%
CreateFrom(...)100%110%
CreateFrom(...)100%110%
CreateFrom(...)100%110%
MakeReadOnly()0%440%
OnWriteCustomAttributes(...)100%110%
OnWriteCustomElements(...)100%110%
OnMakeReadOnly()100%110%
OnWriteBodyContents(...)100%110%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/RequestSecurityToken.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.Globalization;
 8using System.IO;
 9using System.Runtime.Serialization;
 10using System.Security.Authentication.ExtendedProtection;
 11using System.Xml;
 12using CoreWCF.Channels;
 13using CoreWCF.Dispatcher;
 14using CoreWCF.IdentityModel;
 15using CoreWCF.IdentityModel.Selectors;
 16using CoreWCF.IdentityModel.Tokens;
 17using CoreWCF.Security.Tokens;
 18
 19namespace CoreWCF.Security
 20{
 21    internal class RequestSecurityToken : BodyWriter
 22    {
 23        private string _context;
 24        private string _tokenType;
 25        private string _requestType;
 26        private SecurityToken _entropyToken;
 27        private BinaryNegotiation _negotiationData;
 28        private readonly XmlElement _rstXml;
 29        private IList<XmlElement> _requestProperties;
 30        private byte[] _cachedWriteBuffer;
 31        private int _cachedWriteBufferLength;
 32        private int _keySize;
 33        private SecurityKeyIdentifierClause _renewTarget;
 34        private SecurityKeyIdentifierClause _closeTarget;
 35        private OnGetBinaryNegotiationCallback _onGetBinaryNegotiation;
 36        private SecurityStandardsManager _standardsManager;
 37        private object _appliesTo;
 38        private DataContractSerializer _appliesToSerializer;
 39        private Type _appliesToType;
 40
 41        public RequestSecurityToken()
 042            : this(SecurityStandardsManager.DefaultInstance)
 43        {
 044        }
 45
 46        public RequestSecurityToken(MessageSecurityVersion messageSecurityVersion, SecurityTokenSerializer securityToken
 047            : this(SecurityUtils.CreateSecurityStandardsManager(messageSecurityVersion, securityTokenSerializer))
 48        {
 049        }
 50
 51        public RequestSecurityToken(MessageSecurityVersion messageSecurityVersion,
 52                                    SecurityTokenSerializer securityTokenSerializer,
 53                                    XmlElement requestSecurityTokenXml,
 54                                    string context,
 55                                    string tokenType,
 56                                    string requestType,
 57                                    int keySize,
 58                                    SecurityKeyIdentifierClause renewTarget,
 59                                    SecurityKeyIdentifierClause closeTarget)
 060            : this(SecurityUtils.CreateSecurityStandardsManager(messageSecurityVersion, securityTokenSerializer),
 061                   requestSecurityTokenXml,
 062                   context,
 063                   tokenType,
 064                   requestType,
 065                   keySize,
 066                   renewTarget,
 067                   closeTarget)
 68        {
 069        }
 70
 71        public RequestSecurityToken(XmlElement requestSecurityTokenXml,
 72                                    string context,
 73                                    string tokenType,
 74                                    string requestType,
 75                                    int keySize,
 76                                    SecurityKeyIdentifierClause renewTarget,
 77                                    SecurityKeyIdentifierClause closeTarget)
 078            : this(SecurityStandardsManager.DefaultInstance,
 079                   requestSecurityTokenXml,
 080                   context,
 081                   tokenType,
 082                   requestType,
 083                   keySize,
 084                   renewTarget,
 085                   closeTarget)
 86        {
 087        }
 88
 89        internal RequestSecurityToken(SecurityStandardsManager standardsManager,
 90                                      XmlElement rstXml,
 91                                      string context,
 92                                      string tokenType,
 93                                      string requestType,
 94                                      int keySize,
 95                                      SecurityKeyIdentifierClause renewTarget,
 96                                      SecurityKeyIdentifierClause closeTarget)
 2097            : base(true)
 98        {
 2099            _standardsManager = standardsManager ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Argume
 20100            _rstXml = rstXml ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(rstXml));
 20101            _context = context;
 20102            _tokenType = tokenType;
 20103            _keySize = keySize;
 20104            _requestType = requestType;
 20105            _renewTarget = renewTarget;
 20106            _closeTarget = closeTarget;
 20107            IsReceiver = true;
 20108            IsReadOnly = true;
 20109        }
 110
 111        internal RequestSecurityToken(SecurityStandardsManager standardsManager)
 0112            : this(standardsManager, true)
 113        {
 114            // no op
 0115        }
 116
 117        internal RequestSecurityToken(SecurityStandardsManager standardsManager, bool isBuffered)
 0118            : base(isBuffered)
 119        {
 0120            _standardsManager = standardsManager ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Argume
 0121            _requestType = _standardsManager.TrustDriver.RequestTypeIssue;
 0122            _requestProperties = null;
 0123            IsReceiver = false;
 0124            IsReadOnly = false;
 0125        }
 126
 127        public ChannelBinding GetChannelBinding()
 128        {
 0129            if (Message == null)
 130            {
 0131                return null;
 132            }
 133
 0134            ChannelBindingMessageProperty.TryGet(Message, out ChannelBindingMessageProperty channelBindingMessagePropert
 0135            ChannelBinding channelBinding = null;
 136
 0137            if (channelBindingMessageProperty != null)
 138            {
 0139                channelBinding = channelBindingMessageProperty.ChannelBinding;
 140            }
 141
 0142            return channelBinding;
 143        }
 144
 145        /// <summary>
 146        /// Will hold a reference to the outbound message from which we will fish the ChannelBinding out of.
 147        /// </summary>
 0148        public Message Message { get; set; }
 149
 150        public string Context
 151        {
 152            get
 153            {
 20154                return _context;
 155            }
 156            set
 157            {
 0158                if (IsReadOnly)
 159                {
 0160                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR
 161                }
 162
 0163                _context = value;
 0164            }
 165        }
 166
 167        public string TokenType
 168        {
 169            get
 170            {
 20171                return _tokenType;
 172            }
 173            set
 174            {
 0175                if (IsReadOnly)
 176                {
 0177                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR
 178                }
 179
 0180                _tokenType = value;
 0181            }
 182        }
 183
 184        public int KeySize
 185        {
 186            get
 187            {
 30188                return _keySize;
 189            }
 190            set
 191            {
 0192                if (IsReadOnly)
 193                {
 0194                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR
 195                }
 196
 0197                if (value < 0)
 198                {
 0199                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 200                }
 201
 0202                _keySize = value;
 0203            }
 204        }
 205
 20206        public bool IsReadOnly { get; private set; }
 207
 208        public delegate void OnGetBinaryNegotiationCallback(ChannelBinding channelBinding);
 209        public OnGetBinaryNegotiationCallback OnGetBinaryNegotiation
 210        {
 211            get
 212            {
 0213                return _onGetBinaryNegotiation;
 214            }
 215            set
 216            {
 0217                if (IsReadOnly)
 218                {
 0219                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR
 220                }
 0221                _onGetBinaryNegotiation = value;
 0222            }
 223        }
 224
 225        public IEnumerable<XmlElement> RequestProperties
 226        {
 227            get
 228            {
 0229                if (IsReceiver)
 230                {
 0231                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR
 232                }
 0233                return _requestProperties;
 234            }
 235            set
 236            {
 0237                if (IsReadOnly)
 238                {
 0239                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR
 240                }
 241
 0242                if (value != null)
 243                {
 0244                    int index = 0;
 0245                    Collection<XmlElement> coll = new Collection<XmlElement>();
 0246                    foreach (XmlElement property in value)
 247                    {
 0248                        if (property == null)
 249                        {
 0250                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(string.F
 251                        }
 252
 0253                        coll.Add(property);
 0254                        ++index;
 255                    }
 0256                    _requestProperties = coll;
 257                }
 258                else
 259                {
 0260                    _requestProperties = null;
 261                }
 0262            }
 263        }
 264
 265        public string RequestType
 266        {
 267            get
 268            {
 40269                return _requestType;
 270            }
 271            set
 272            {
 0273                if (IsReadOnly)
 274                {
 0275                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR
 276                }
 277
 0278                _requestType = value ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value));
 0279            }
 280        }
 281
 282        public SecurityKeyIdentifierClause RenewTarget
 283        {
 284            get
 285            {
 0286                return _renewTarget;
 287            }
 288            set
 289            {
 0290                if (IsReadOnly)
 291                {
 0292                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR
 293                }
 294
 0295                _renewTarget = value;
 0296            }
 297        }
 298
 299        public SecurityKeyIdentifierClause CloseTarget
 300        {
 301            get
 302            {
 20303                return _closeTarget;
 304            }
 305            set
 306            {
 0307                if (IsReadOnly)
 308                {
 0309                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR
 310                }
 311
 0312                _closeTarget = value;
 0313            }
 314        }
 315
 316        public XmlElement RequestSecurityTokenXml
 317        {
 318            get
 319            {
 20320                if (!IsReceiver)
 321                {
 0322                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR
 323                }
 20324                return _rstXml;
 325            }
 326        }
 327
 328        internal SecurityStandardsManager StandardsManager
 329        {
 330            get
 331            {
 0332                return _standardsManager;
 333            }
 334            set
 335            {
 0336                if (IsReadOnly)
 337                {
 0338                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR
 339                }
 340
 0341                _standardsManager = value ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullE
 0342            }
 343        }
 344
 40345        internal bool IsReceiver { get; }
 346
 347        internal object AppliesTo
 348        {
 349            get
 350            {
 0351                if (IsReceiver)
 352                {
 0353                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR
 354                }
 0355                return _appliesTo;
 356            }
 357        }
 358
 359        internal DataContractSerializer AppliesToSerializer
 360        {
 361            get
 362            {
 0363                if (IsReceiver)
 364                {
 0365                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR
 366                }
 0367                return _appliesToSerializer;
 368            }
 369        }
 370
 371        internal Type AppliesToType
 372        {
 373            get
 374            {
 0375                if (IsReceiver)
 376                {
 0377                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR
 378                }
 0379                return _appliesToType;
 380            }
 381        }
 382
 20383        protected object ThisLock { get; } = new object();
 384
 385        internal void SetBinaryNegotiation(BinaryNegotiation negotiation)
 386        {
 0387            if (IsReadOnly)
 388            {
 0389                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Obj
 390            }
 391
 0392            _negotiationData = negotiation ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(ne
 0393        }
 394
 395        internal BinaryNegotiation GetBinaryNegotiation()
 396        {
 0397            if (IsReceiver)
 398            {
 0399                return _standardsManager.TrustDriver.GetBinaryNegotiation(this);
 400            }
 0401            else if (_negotiationData == null && _onGetBinaryNegotiation != null)
 402            {
 0403                _onGetBinaryNegotiation(GetChannelBinding());
 404            }
 0405            return _negotiationData;
 406        }
 407
 408        public SecurityToken GetRequestorEntropy()
 409        {
 0410            return GetRequestorEntropy(null);
 411        }
 412
 413        internal SecurityToken GetRequestorEntropy(SecurityTokenResolver resolver)
 414        {
 10415            if (IsReceiver)
 416            {
 10417                return _standardsManager.TrustDriver.GetEntropy(this, resolver);
 418            }
 419            else
 420            {
 0421                return _entropyToken;
 422            }
 423        }
 424
 425        //public void SetRequestorEntropy(byte[] entropy)
 426        //{
 427        //    if (this.IsReadOnly)
 428        //        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.O
 429        //    this.entropyToken = (entropy != null) ? new NonceToken(entropy) : null;
 430        //}
 431
 432        internal void SetRequestorEntropy(WrappedKeySecurityToken entropyToken)
 433        {
 0434            if (IsReadOnly)
 435            {
 0436                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Obj
 437            }
 438
 0439            _entropyToken = entropyToken;
 0440        }
 441
 442        public void SetAppliesTo<T>(T appliesTo, DataContractSerializer serializer)
 443        {
 0444            if (IsReadOnly)
 445            {
 0446                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Obj
 447            }
 448
 0449            if (appliesTo != null && serializer == null)
 450            {
 0451                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(serializer));
 452            }
 0453            _appliesTo = appliesTo;
 0454            _appliesToSerializer = serializer;
 0455            _appliesToType = typeof(T);
 0456        }
 457
 458        public void GetAppliesToQName(out string localName, out string namespaceUri)
 459        {
 10460            if (!IsReceiver)
 461            {
 0462                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Ite
 463            }
 464
 10465            _standardsManager.TrustDriver.GetAppliesToQName(this, out localName, out namespaceUri);
 10466        }
 467
 468        public T GetAppliesTo<T>()
 469        {
 0470            return GetAppliesTo<T>(DataContractSerializerDefaults.CreateSerializer(typeof(T), DataContractSerializerDefa
 471        }
 472
 473        public T GetAppliesTo<T>(XmlObjectSerializer serializer)
 474        {
 0475            if (IsReceiver)
 476            {
 0477                if (serializer == null)
 478                {
 0479                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(serializer));
 480                }
 0481                return _standardsManager.TrustDriver.GetAppliesTo<T>(this, serializer);
 482            }
 483            else
 484            {
 0485                return (T)_appliesTo;
 486            }
 487        }
 488
 489        private void OnWriteTo(XmlWriter writer)
 490        {
 0491            if (IsReceiver)
 492            {
 0493                _rstXml.WriteTo(writer);
 494            }
 495            else
 496            {
 0497                _standardsManager.TrustDriver.WriteRequestSecurityToken(this, writer);
 498            }
 0499        }
 500
 501        public void WriteTo(XmlWriter writer)
 502        {
 0503            if (writer == null)
 504            {
 0505                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer));
 506            }
 507
 0508            if (IsReadOnly)
 509            {
 510                // cache the serialized bytes to ensure repeatability
 0511                if (_cachedWriteBuffer == null)
 512                {
 0513                    MemoryStream stream = new MemoryStream();
 0514                    using (XmlDictionaryWriter binaryWriter = XmlDictionaryWriter.CreateBinaryWriter(stream, XD.Dictiona
 515                    {
 0516                        OnWriteTo(binaryWriter);
 0517                        binaryWriter.Flush();
 0518                        stream.Flush();
 0519                        stream.Seek(0, SeekOrigin.Begin);
 0520                        _cachedWriteBuffer = stream.GetBuffer();
 0521                        _cachedWriteBufferLength = (int)stream.Length;
 0522                    }
 523                }
 0524                writer.WriteNode(XmlDictionaryReader.CreateBinaryReader(_cachedWriteBuffer, 0, _cachedWriteBufferLength,
 525            }
 526            else
 527            {
 0528                OnWriteTo(writer);
 529            }
 0530        }
 531
 532        public static RequestSecurityToken CreateFrom(XmlReader reader)
 533        {
 0534            return CreateFrom(SecurityStandardsManager.DefaultInstance, reader);
 535        }
 536
 537        public static RequestSecurityToken CreateFrom(XmlReader reader, MessageSecurityVersion messageSecurityVersion, S
 538        {
 0539            return CreateFrom(SecurityUtils.CreateSecurityStandardsManager(messageSecurityVersion, securityTokenSerializ
 540        }
 541
 542        internal static RequestSecurityToken CreateFrom(SecurityStandardsManager standardsManager, XmlReader reader)
 543        {
 0544            return standardsManager.TrustDriver.CreateRequestSecurityToken(reader);
 545        }
 546
 547        public void MakeReadOnly()
 548        {
 0549            if (!IsReadOnly)
 550            {
 0551                IsReadOnly = true;
 0552                if (_requestProperties != null)
 553                {
 0554                    _requestProperties = new ReadOnlyCollection<XmlElement>(_requestProperties);
 555                }
 0556                OnMakeReadOnly();
 557            }
 0558        }
 559
 0560        protected internal virtual void OnWriteCustomAttributes(XmlWriter writer) { }
 561
 0562        protected internal virtual void OnWriteCustomElements(XmlWriter writer) { }
 563
 0564        protected internal virtual void OnMakeReadOnly() { }
 565
 566        protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
 567        {
 0568            WriteTo(writer);
 0569        }
 570    }
 571}

Methods/Properties

.ctor()
.ctor(CoreWCF.MessageSecurityVersion,CoreWCF.IdentityModel.Selectors.SecurityTokenSerializer)
.ctor(CoreWCF.MessageSecurityVersion,CoreWCF.IdentityModel.Selectors.SecurityTokenSerializer,System.Xml.XmlElement,System.String,System.String,System.String,System.Int32,CoreWCF.IdentityModel.SecurityKeyIdentifierClause,CoreWCF.IdentityModel.SecurityKeyIdentifierClause)
.ctor(System.Xml.XmlElement,System.String,System.String,System.String,System.Int32,CoreWCF.IdentityModel.SecurityKeyIdentifierClause,CoreWCF.IdentityModel.SecurityKeyIdentifierClause)
.ctor(CoreWCF.Security.SecurityStandardsManager,System.Xml.XmlElement,System.String,System.String,System.String,System.Int32,CoreWCF.IdentityModel.SecurityKeyIdentifierClause,CoreWCF.IdentityModel.SecurityKeyIdentifierClause)
.ctor(CoreWCF.Security.SecurityStandardsManager)
.ctor(CoreWCF.Security.SecurityStandardsManager,System.Boolean)
GetChannelBinding()
Message()
Context()
Context(System.String)
TokenType()
TokenType(System.String)
KeySize()
KeySize(System.Int32)
IsReadOnly()
OnGetBinaryNegotiation()
OnGetBinaryNegotiation(CoreWCF.Security.RequestSecurityToken/OnGetBinaryNegotiationCallback)
RequestProperties()
RequestProperties(System.Collections.Generic.IEnumerable`1<System.Xml.XmlElement>)
RequestType()
RequestType(System.String)
RenewTarget()
RenewTarget(CoreWCF.IdentityModel.SecurityKeyIdentifierClause)
CloseTarget()
CloseTarget(CoreWCF.IdentityModel.SecurityKeyIdentifierClause)
RequestSecurityTokenXml()
StandardsManager()
StandardsManager(CoreWCF.Security.SecurityStandardsManager)
IsReceiver()
AppliesTo()
AppliesToSerializer()
AppliesToType()
ThisLock()
SetBinaryNegotiation(CoreWCF.Security.BinaryNegotiation)
GetBinaryNegotiation()
GetRequestorEntropy()
GetRequestorEntropy(CoreWCF.IdentityModel.Selectors.SecurityTokenResolver)
SetRequestorEntropy(CoreWCF.Security.Tokens.WrappedKeySecurityToken)
SetAppliesTo(T,System.Runtime.Serialization.DataContractSerializer)
GetAppliesToQName(System.String&,System.String&)
GetAppliesTo()
GetAppliesTo(System.Runtime.Serialization.XmlObjectSerializer)
OnWriteTo(System.Xml.XmlWriter)
WriteTo(System.Xml.XmlWriter)
CreateFrom(System.Xml.XmlReader)
CreateFrom(System.Xml.XmlReader,CoreWCF.MessageSecurityVersion,CoreWCF.IdentityModel.Selectors.SecurityTokenSerializer)
CreateFrom(CoreWCF.Security.SecurityStandardsManager,System.Xml.XmlReader)
MakeReadOnly()
OnWriteCustomAttributes(System.Xml.XmlWriter)
OnWriteCustomElements(System.Xml.XmlWriter)
OnMakeReadOnly()
OnWriteBodyContents(System.Xml.XmlDictionaryWriter)