< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.Tokens.BootstrapContext
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/BootstrapContext.cs
Line coverage
8%
Covered lines: 4
Uncovered lines: 44
Coverable lines: 48
Total lines: 182
Line coverage: 8.3%
Branch coverage
6%
Covered branches: 2
Total branches: 30
Branch coverage: 6.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)0%14140%
.ctor(...)50%44100%
.ctor(...)0%220%
.ctor(...)0%220%
GetObjectData(...)0%880%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/BootstrapContext.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.IO;
 6using System.Runtime.Serialization;
 7using System.Xml;
 8
 9namespace CoreWCF.IdentityModel.Tokens
 10{
 11    /// <summary>
 12    /// Represents a serializable version of a token that can be attached to a <see cref="System.Security.Claims.ClaimsI
 13    /// original token that was used to create <see cref="System.Security.Claims.ClaimsIdentity"/>
 14    /// </summary>
 15    [Serializable]
 16    public class BootstrapContext : ISerializable
 17    {
 18        private readonly SecurityToken _token;
 19        private readonly string _tokenString;
 20        private readonly byte[] _tokenBytes;
 21        private readonly SecurityTokenHandler _tokenHandler;
 22        private const string TokenTypeKey = "K";
 23        private const string TokenKey = "T";
 24        private const char SecurityTokenType = 'T';
 25        private const char StringTokenType = 'S';
 26        private const char ByteTokenType = 'B';
 27
 028        protected BootstrapContext(SerializationInfo info, StreamingContext context)
 29        {
 030            if (info == null)
 031                return;
 32
 033            switch (info.GetChar(TokenTypeKey))
 34            {
 35                case SecurityTokenType:
 36                    {
 037                        if (context.Context is SecurityTokenHandler sth)
 38                        {
 039                            using (XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(Convert.FromBase64S
 40                            {
 041                                reader.MoveToContent();
 042                                if (sth.CanReadToken(reader))
 43                                {
 044                                    SecurityToken token = sth.ReadToken(reader);
 045                                    if (token == null)
 46                                    {
 047                                        _tokenString = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(info
 48                                    }
 49                                    else
 50                                    {
 051                                        _token = token;
 52                                    }
 53                                }
 054                            }
 55                        }
 56                        else
 57                        {
 058                            _tokenString = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(info.GetString(T
 59                        }
 60                    }
 61
 062                    break;
 63
 64                case StringTokenType:
 65                    {
 066                        _tokenString = info.GetString(TokenKey);
 67                    }
 068                    break;
 69
 70                case ByteTokenType:
 71                    {
 072                        _tokenBytes = (byte[])info.GetValue(TokenKey, typeof(byte[]));
 73                    }
 74                    break;
 75
 76                default:
 77                    break;
 78            }
 079        }
 80
 81        /// <summary>
 82        /// A SecurityToken and a SecurityTokenHandler that can serialize the token.
 83        /// </summary>
 84        /// <param name=nameof(token)><see cref="SecurityToken"/> that can be serialized. Cannot be null.</param>
 85        /// <param name="tokenHandler"><see cref="SecurityTokenHandler"/> that is responsible for serializing the token.
 86        /// <exception cref="ArgumentNullException"> thrown if 'token' or 'tokenHandler' is null.</exception>
 87        /// <remarks>The <see cref="SecurityTokenHandler"/> is used not used to deserialize the token as it cannot be as
 2888        public BootstrapContext(SecurityToken token, SecurityTokenHandler tokenHandler)
 89        {
 2890            _token = token ?? throw new ArgumentNullException(nameof(token));
 2891            _tokenHandler = tokenHandler ?? throw new ArgumentNullException(nameof(tokenHandler));
 2892        }
 93
 94        /// <summary>
 95        /// String that represents a SecurityToken.
 96        /// </summary>
 97        /// <param name=nameof(token)>string that represents a token.  Can not be null.</param>
 98        /// <exception cref="ArgumentNullException"> thrown if 'token' is null.</exception>
 099        public BootstrapContext(string token)
 100        {
 0101            _tokenString = token ?? throw new ArgumentNullException(nameof(token));
 0102        }
 103
 104        /// <summary>
 105        /// String that represents a SecurityToken.
 106        /// </summary>
 107        /// <param name=nameof(token)>string that represents a token.  Can not be null.</param>
 108        /// <exception cref="ArgumentNullException"> thrown if 'token' is null.</exception>
 0109        public BootstrapContext(byte[] token)
 110        {
 0111            _tokenBytes = token ?? throw new ArgumentNullException(nameof(token));
 0112        }
 113
 114        #region ISerializable Members
 115        /// <summary>
 116        /// Called to serialize this context.
 117        /// </summary>
 118        /// <param name="info"><see cref="SerializationInfo"/> container for storing data. Cannot be null.</param>
 119        /// <param name="context"><see cref="StreamingContext"/> contains the context for streaming and optionally addit
 120        /// <exception cref="ArgumentNullException"> thrown if 'info' is null.</exception>
 121        public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
 122        {
 0123            if (_tokenBytes != null)
 124            {
 0125                info.AddValue(TokenTypeKey, ByteTokenType);
 0126                info.AddValue(TokenKey, _tokenBytes);
 127            }
 0128            else if (_tokenString != null)
 129            {
 0130                info.AddValue(TokenTypeKey, StringTokenType);
 0131                info.AddValue(TokenKey, _tokenString);
 132            }
 0133            else if (_token != null && _tokenHandler != null)
 134            {
 0135                using (MemoryStream ms = new MemoryStream())
 136                {
 0137                    info.AddValue(TokenTypeKey, SecurityTokenType);
 0138                    using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(ms, System.Text.Encoding.UT
 139                    {
 0140                        _tokenHandler.WriteToken(writer, _token);
 0141                        writer.Flush();
 0142                        info.AddValue(TokenKey, Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length));
 0143                    }
 144                }
 145            }
 0146        }
 147
 148        #endregion
 149
 150        /// <summary>
 151        /// Gets the string that was passed in constructor. If a different constructor was used, will be null.
 152        /// </summary>
 153        public byte[] TokenBytes
 154        {
 0155            get { return _tokenBytes; }
 156        }
 157
 158        /// <summary>
 159        /// Gets the string that was passed in constructor. If a different constructor was used, will be null.
 160        /// </summary>
 161        public string Token
 162        {
 0163            get { return _tokenString; }
 164        }
 165
 166        /// <summary>
 167        /// Gets the SecurityToken that was passed in constructor. If a different constructor was used, will be null.
 168        /// </summary>
 169        public SecurityToken SecurityToken
 170        {
 0171            get { return _token; }
 172        }
 173
 174        /// <summary>
 175        /// Gets the SecurityTokenHandler that was passed in constructor. If a different constructor was used, will be n
 176        /// </summary>
 177        public SecurityTokenHandler SecurityTokenHandler
 178        {
 0179            get { return _tokenHandler; }
 180        }
 181    }
 182}