< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.ProtectedDataCookieTransform
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/ProtectedDataCookieTransform.cs
Line coverage
14%
Covered lines: 3
Uncovered lines: 18
Coverable lines: 21
Total lines: 98
Line coverage: 14.2%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
Decode(...)0%440%
Encode(...)0%440%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/ProtectedDataCookieTransform.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.Security.Cryptography;
 5using System.Text;
 6using System;
 7using Microsoft.AspNetCore.DataProtection;
 8
 9namespace CoreWCF.IdentityModel
 10{
 11    /// <summary>
 12    /// Provides cookie integrity and confidentiality using <see cref="ProtectedData"/>.
 13    /// </summary>
 14    /// <remarks>
 15    /// Due to the nature of <see cref="ProtectedData"/>, cookies
 16    /// which use this tranform can only be read by the same machine
 17    /// which wrote them. As such, this transform is not appropriate
 18    /// for use in applications that run on a web server farm.
 19    /// </remarks>
 20    public sealed class ProtectedDataCookieTransform : CookieTransform
 21    {
 22        private const string EntropyString = "CoreWCF.IdentityModel.ProtectedDataCookieTransform";
 23        private IDataProtector _protector;
 24
 225        public ProtectedDataCookieTransform(IDataProtectionProvider provider)
 26        {
 227            _protector = provider.CreateProtector(EntropyString);
 228        }
 29        /// <summary>
 30        /// Verifies data protection.
 31        /// </summary>
 32        /// <param name=nameof(encoded)>Data previously returned from <see cref="Encode"/></param>
 33        /// <returns>The originally protected data.</returns>
 34        /// <exception cref="ArgumentNullException">The argument 'encoded' is null.</exception>
 35        /// <exception cref="ArgumentException">The argument 'encoded' contains zero bytes.</exception>
 36        public override byte[] Decode(byte[] encoded)
 37        {
 038            if (null == encoded)
 39            {
 040                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(encoded));
 41            }
 42
 043            if (0 == encoded.Length)
 44            {
 045                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(encoded), SR.Format(SR.ID6045));
 46            }
 47
 48            // CurrentUser is used here, and this has been tested as
 49            // NetworkService. Using CurrentMachine allows anyone on
 50            // the machine to decrypt the data, which isn't what we
 51            // want.
 52            byte[] decoded;
 53            try
 54            {
 055                decoded = _protector.Unprotect(encoded);
 056            }
 057            catch (CryptographicException e)
 58            {
 059                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.ID1
 60            }
 61
 062            return decoded;
 63        }
 64
 65        /// <summary>
 66        /// Protects data.
 67        /// </summary>
 68        /// <param name=nameof(value)>Data to be protected.</param>
 69        /// <returns>Protected data.</returns>
 70        /// <exception cref="ArgumentNullException">The argument 'value' is null.</exception>
 71        /// <exception cref="ArgumentException">The argument 'value' contains zero bytes.</exception>
 72        public override byte[] Encode(byte[] value)
 73        {
 074            if (null == value)
 75            {
 076                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value));
 77            }
 78
 079            if (0 == value.Length)
 80            {
 081                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(value), SR.Format(SR.ID6044));
 82            }
 83
 84            // See note in Decode about the DataProtectionScope.
 85            byte[] encoded;
 86            try
 87            {
 088                encoded = _protector.Protect(value);
 089            }
 090            catch (CryptographicException e)
 91            {
 092                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.ID1
 93            }
 94
 095            return encoded;
 96        }
 97    }
 98}