| | | 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.Collections.Generic; |
| | | 5 | | using System.Globalization; |
| | | 6 | | using System; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 9 | | { |
| | | 10 | | public class ConfigurationBasedIssuerNameRegistry : IssuerNameRegistry |
| | | 11 | | { |
| | 6 | 12 | | private readonly Dictionary<string, string> _configuredTrustedIssuers = new Dictionary<string, string>(new Thumb |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Creates an instance of <see cref="ConfigurationBasedIssuerNameRegistry"/> |
| | | 16 | | /// </summary> |
| | 6 | 17 | | public ConfigurationBasedIssuerNameRegistry() |
| | | 18 | | { |
| | 6 | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Returns the issuer name of the given X509SecurityToken mapping the Certificate Thumbprint to |
| | | 23 | | /// a name in the configured map. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="securityToken">SecurityToken for which the issuer name is requested.</param> |
| | | 26 | | /// <returns>Issuer name if the token was registered, null otherwise.</returns> |
| | | 27 | | /// <exception cref="ArgumentNullException">The input parameter 'securityToken' is null.</exception> |
| | | 28 | | public override string GetIssuerName(SecurityToken securityToken) |
| | | 29 | | { |
| | 32 | 30 | | if (securityToken == null) |
| | | 31 | | { |
| | 2 | 32 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(securityToken)); |
| | | 33 | | } |
| | | 34 | | |
| | 30 | 35 | | if (securityToken is X509SecurityToken x509SecurityToken) |
| | | 36 | | { |
| | 30 | 37 | | string thumbprint = x509SecurityToken.Certificate.Thumbprint; |
| | 30 | 38 | | if (_configuredTrustedIssuers.ContainsKey(thumbprint)) |
| | | 39 | | { |
| | 30 | 40 | | string issuerName = _configuredTrustedIssuers[thumbprint]; |
| | 30 | 41 | | issuerName = string.IsNullOrEmpty(issuerName) ? x509SecurityToken.Certificate.Subject : issuerName; |
| | | 42 | | |
| | | 43 | | //if (TD.GetIssuerNameSuccessIsEnabled()) |
| | | 44 | | //{ |
| | | 45 | | // TD.GetIssuerNameSuccess(EventTraceActivity.GetFromThreadOrCreate(), issuerName, securityToken. |
| | | 46 | | //} |
| | | 47 | | |
| | 30 | 48 | | return issuerName; |
| | | 49 | | } |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | //if (TD.GetIssuerNameFailureIsEnabled()) |
| | | 53 | | //{ |
| | | 54 | | // TD.GetIssuerNameFailure(EventTraceActivity.GetFromThreadOrCreate(), securityToken.Id); |
| | | 55 | | //} |
| | | 56 | | |
| | 0 | 57 | | return null; |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets the Dictionary of Configured Trusted Issuers. The key |
| | | 62 | | /// to the dictionary is the ASN.1 encoded form of the Thumbprint |
| | | 63 | | /// of the trusted issuer's certificate and the value is the issuer name. |
| | | 64 | | /// </summary> |
| | | 65 | | public IDictionary<string, string> ConfiguredTrustedIssuers |
| | | 66 | | { |
| | 0 | 67 | | get { return _configuredTrustedIssuers; } |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Adds a trusted issuer to the collection. |
| | | 72 | | /// </summary> |
| | | 73 | | /// <param name="certificateThumbprint">ASN.1 encoded form of the trusted issuer's certificate Thumbprint.</para |
| | | 74 | | /// <param name="name">Name of the trusted issuer.</param> |
| | | 75 | | /// <exception cref="ArgumentException">The argument 'certificateThumbprint' or 'name' is either null or Empty.< |
| | | 76 | | /// <exception cref="InvalidOperationException">The issuer specified by 'certificateThumbprint' argument has alr |
| | | 77 | | public void AddTrustedIssuer(string certificateThumbprint, string name) |
| | | 78 | | { |
| | 4 | 79 | | if (string.IsNullOrEmpty(certificateThumbprint)) |
| | | 80 | | { |
| | 0 | 81 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(certificateThumbprint)); |
| | | 82 | | } |
| | | 83 | | |
| | 4 | 84 | | if (string.IsNullOrEmpty(name)) |
| | | 85 | | { |
| | 0 | 86 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(name)); |
| | | 87 | | } |
| | | 88 | | |
| | 4 | 89 | | if (_configuredTrustedIssuers.ContainsKey(certificateThumbprint)) |
| | | 90 | | { |
| | 0 | 91 | | throw new InvalidOperationException(SR.Format(SR.ID4265, certificateThumbprint)); |
| | | 92 | | } |
| | | 93 | | |
| | 4 | 94 | | certificateThumbprint = certificateThumbprint.Replace(" ", ""); |
| | | 95 | | |
| | 4 | 96 | | _configuredTrustedIssuers.Add(certificateThumbprint, name); |
| | 4 | 97 | | } |
| | | 98 | | |
| | | 99 | | private class ThumbprintKeyComparer : IEqualityComparer<string> |
| | | 100 | | { |
| | | 101 | | #region IEqualityComparer<string> Members |
| | | 102 | | |
| | | 103 | | public bool Equals(string x, string y) |
| | | 104 | | { |
| | 60 | 105 | | return StringComparer.OrdinalIgnoreCase.Equals(x, y); |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | public int GetHashCode(string obj) |
| | | 109 | | { |
| | 64 | 110 | | return obj.ToUpper(CultureInfo.InvariantCulture).GetHashCode(); |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | #endregion |
| | | 114 | | } |
| | | 115 | | } |
| | | 116 | | } |