| | | 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; |
| | | 5 | | using System.Collections.ObjectModel; |
| | | 6 | | using System.Security.Claims; |
| | | 7 | | using System.Xml; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// This class represents a number elements found in a <see cref="System.IdentityModel.Protocols.WSTrust.RequestSecu |
| | | 13 | | /// </summary> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// This class is not thread-safe. |
| | | 16 | | /// </remarks> |
| | | 17 | | public class SecurityTokenElement |
| | | 18 | | { |
| | | 19 | | private SecurityToken _securityToken; |
| | | 20 | | private readonly SecurityTokenHandlerCollection _securityTokenHandlers; |
| | | 21 | | private ReadOnlyCollection<ClaimsIdentity> _subject; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Creates an instance of this object using a <see cref="SecurityToken"/> object. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="securityToken">The security token this object represents.</param> |
| | | 27 | | /// <remarks> |
| | | 28 | | /// <see cref="GetIdentities"/> is not supported by this object if this constructor is used unless |
| | | 29 | | /// <see cref="ValidateToken"/> is overriden. |
| | | 30 | | /// If the securityToken passed in is a <see cref="GenericXmlSecurityToken"/> then SecurityTokenXml will |
| | | 31 | | /// be set to the value found in <see cref="GenericXmlSecurityToken"/> |
| | | 32 | | /// </remarks> |
| | 0 | 33 | | public SecurityTokenElement(SecurityToken securityToken) |
| | | 34 | | { |
| | 0 | 35 | | if (securityToken == null) |
| | | 36 | | { |
| | 0 | 37 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(securityToken)); |
| | | 38 | | } |
| | | 39 | | |
| | 0 | 40 | | if (securityToken is GenericXmlSecurityToken xmlToken) |
| | | 41 | | { |
| | 0 | 42 | | SecurityTokenXml = xmlToken.TokenXml; |
| | | 43 | | } |
| | | 44 | | |
| | 0 | 45 | | _securityToken = securityToken; |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Creates an instance of this object using XML representation of the security token. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="securityTokenXml">The <see cref="XmlElement"/> representation of the security token.</param> |
| | | 52 | | /// <param name="securityTokenHandlers">The collection of <see cref="SecurityTokenHandler"/> objects that may |
| | | 53 | | /// be used to read and validate the security token this object represents.</param> |
| | 0 | 54 | | public SecurityTokenElement(XmlElement securityTokenXml, SecurityTokenHandlerCollection securityTokenHandlers) |
| | | 55 | | { |
| | 0 | 56 | | if (securityTokenXml == null) |
| | | 57 | | { |
| | 0 | 58 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(securityTokenXml)); |
| | | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | if (securityTokenHandlers == null) |
| | | 62 | | { |
| | 0 | 63 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(securityTokenHandlers)); |
| | | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | SecurityTokenXml = securityTokenXml; |
| | 0 | 67 | | _securityTokenHandlers = securityTokenHandlers; |
| | 0 | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Gets the XML representation of the token. |
| | | 72 | | /// </summary> |
| | | 73 | | /// <remarks>This property will be null unless this object was constructed using |
| | | 74 | | /// <see cref="SecurityTokenElement(XmlElement, SecurityTokenHandlerCollection)"/>. |
| | | 75 | | /// </remarks> |
| | 0 | 76 | | public XmlElement SecurityTokenXml { get; } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Gets the security token this object represents. |
| | | 80 | | /// </summary> |
| | | 81 | | /// <remarks> |
| | | 82 | | /// If this object was not constructed directly with a <see cref="SecurityToken"/> using |
| | | 83 | | /// <see cref="SecurityTokenElement(SecurityToken)"/>, <see cref="ReadSecurityToken"/> |
| | | 84 | | /// will be called for this value. |
| | | 85 | | /// </remarks> |
| | | 86 | | /// <returns>The <see cref="SecurityToken"/> this object represents</returns> |
| | | 87 | | public SecurityToken GetSecurityToken() |
| | | 88 | | { |
| | 0 | 89 | | if (_securityToken == null) |
| | | 90 | | { |
| | 0 | 91 | | _securityToken = ReadSecurityToken(SecurityTokenXml, _securityTokenHandlers); |
| | | 92 | | } |
| | | 93 | | |
| | 0 | 94 | | return _securityToken; |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Gets the collection of <see cref="ClaimsIdentity"/> contained in the token. |
| | | 99 | | /// <seealso cref="ValidateToken"/> |
| | | 100 | | /// </summary> |
| | | 101 | | /// <returns>A <see cref="ReadOnlyCollection{T}"/> of <see cref="ClaimsIdentity"/> representing the identities c |
| | | 102 | | public ReadOnlyCollection<ClaimsIdentity> GetIdentities() |
| | | 103 | | { |
| | 0 | 104 | | if (_subject == null) |
| | | 105 | | { |
| | 0 | 106 | | _subject = ValidateToken(SecurityTokenXml, _securityTokenHandlers); |
| | | 107 | | } |
| | | 108 | | |
| | 0 | 109 | | return _subject; |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Creates the identities for the represented by the <see cref="SecurityToken"/>. |
| | | 114 | | /// </summary> |
| | | 115 | | /// <param name="securityTokenXml">The <see cref="XmlElement"/> representation of the security token.</param> |
| | | 116 | | /// <param name="securityTokenHandlers">The collection of <see cref="SecurityTokenHandler"/> objects that may |
| | | 117 | | /// be used to read and validate the security token this object represents.</param> |
| | | 118 | | /// <returns>A <see cref="ReadOnlyCollection{T}"/> of <see cref="ClaimsIdentity"/> representing the identities c |
| | | 119 | | /// <exception cref="InvalidOperationException">If either parameter 'securityTokenXml' or 'securityTokenHandlers |
| | | 120 | | protected virtual ReadOnlyCollection<ClaimsIdentity> ValidateToken(XmlElement securityTokenXml, SecurityTokenHan |
| | | 121 | | { |
| | 0 | 122 | | if (securityTokenXml == null || securityTokenHandlers == null) |
| | | 123 | | { |
| | 0 | 124 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.ID4 |
| | | 125 | | } |
| | | 126 | | |
| | 0 | 127 | | SecurityToken securityToken = GetSecurityToken(); |
| | 0 | 128 | | return securityTokenHandlers.ValidateToken(securityToken); |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Reads a <see cref="SecurityToken"/> from the provided XML representation. |
| | | 133 | | /// </summary> |
| | | 134 | | /// <param name="securityTokenXml">The XML representation of the security token.</param> |
| | | 135 | | /// <param name="securityTokenHandlers">The <see cref="SecurityTokenHandlerCollection"/> used to |
| | | 136 | | /// read the token.</param> |
| | | 137 | | /// <returns>A <see cref="SecurityToken"/>.</returns> |
| | | 138 | | protected virtual SecurityToken ReadSecurityToken(XmlElement securityTokenXml, |
| | | 139 | | SecurityTokenHandlerCollection securityTokenHandlers) |
| | | 140 | | { |
| | 0 | 141 | | XmlReader reader = new XmlNodeReader(securityTokenXml); |
| | 0 | 142 | | reader.MoveToContent(); |
| | 0 | 143 | | SecurityToken securityToken = securityTokenHandlers.ReadToken(reader); |
| | 0 | 144 | | if (securityToken == null) |
| | | 145 | | { |
| | 0 | 146 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.ID4 |
| | | 147 | | } |
| | | 148 | | |
| | 0 | 149 | | return securityToken; |
| | | 150 | | } |
| | | 151 | | } |
| | | 152 | | } |