| | | 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.Xml; |
| | | 6 | | using CoreWCF.IdentityModel.Selectors; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// This class derives from System.IdentityModel.Selectors.SecurityTokenSerializer and wraps a collection of Securit |
| | | 12 | | /// Any call to this serilaizer is delegated to the token handler and delegated to the base class if no token handle |
| | | 13 | | /// is registered to handle this particular token or KeyIdentifier. |
| | | 14 | | /// </summary> |
| | | 15 | | internal class SecurityTokenSerializerAdapter : SecurityTokenSerializer |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Initializes an instance of <see cref="SecurityTokenSerializerAdapter"/> |
| | | 19 | | /// </summary> |
| | | 20 | | /// <param name="securityTokenHandlerCollection"> |
| | | 21 | | /// The <see cref="SecurityTokenHandlerCollection" /> containing the set of <see cref="SecurityTokenHandler" /> |
| | | 22 | | /// </param> |
| | 0 | 23 | | public SecurityTokenSerializerAdapter(SecurityTokenHandlerCollection securityTokenHandlerCollection) |
| | | 24 | | { |
| | 0 | 25 | | SecurityTokenHandlers = securityTokenHandlerCollection ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelp |
| | 0 | 26 | | if (securityTokenHandlerCollection.KeyInfoSerializer is KeyInfoSerializer serializer) |
| | | 27 | | { |
| | 0 | 28 | | serializer.InnerSecurityTokenSerializer = this; |
| | | 29 | | } |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Gets the SecurityTokenHandlerCollection. |
| | | 34 | | /// </summary> |
| | 0 | 35 | | public SecurityTokenHandlerCollection SecurityTokenHandlers { get; } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Checks if one of the wrapped SecurityTokenHandlers or the base WSSecurityTokenSerializer |
| | | 39 | | /// can read the security token. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="reader">Reader to a Security token.</param> |
| | | 42 | | /// <returns>'True' if the serializer can read the given Security Token.</returns> |
| | 0 | 43 | | protected override bool CanReadTokenCore(XmlReader reader) => SecurityTokenHandlers.CanReadToken(reader); |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Checks if one of the wrapped SecurityTokenHandlers or the base WSSecurityTokenSerializer |
| | | 47 | | /// can write the given security token. |
| | | 48 | | /// </summary> |
| | | 49 | | /// <param name="token">SecurityToken instance.</param> |
| | | 50 | | /// <returns>'True' if the serializer can write the given security token.</returns> |
| | 0 | 51 | | protected override bool CanWriteTokenCore(SecurityToken token) => SecurityTokenHandlers.CanWriteToken(token); |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Deserializes the SecurityToken from the given XmlReader. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <param name="reader">Reader to a Security token.</param> |
| | | 57 | | /// <param name="tokenResolver">Instance of SecurityTokenResolver.</param> |
| | | 58 | | /// <returns>'True' if the serializer can read the given Security Token.</returns> |
| | 0 | 59 | | protected override SecurityToken ReadTokenCore(XmlReader reader, SecurityTokenResolver tokenResolver) => Securit |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Serializes the SecurityToken to the XmlWriter. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="writer">XmlWriter to write to.</param> |
| | | 65 | | /// <param name="token">The SecurityToken to serializer.</param> |
| | | 66 | | /// <exception cref="ArgumentNullException">The input parameter 'writer' or 'token' is null.</exception> |
| | 0 | 67 | | protected override void WriteTokenCore(XmlWriter writer, SecurityToken token) => SecurityTokenHandlers.WriteToke |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Checks if one of the wrapped SecurityTokenHandlers or the base WSSecurityTokenSerializer |
| | | 71 | | /// can read the security key identifier. |
| | | 72 | | /// </summary> |
| | | 73 | | /// <param name="reader">Reader pointing at a Security Key Identifier {ds:Keyinfo}.</param> |
| | | 74 | | /// <returns>'True' if the serializer can read the given Security Key Identifier.</returns> |
| | | 75 | | /// <exception cref="ArgumentNullException">The <paramref name="reader"/> is null.</exception> |
| | 0 | 76 | | protected override bool CanReadKeyIdentifierCore(XmlReader reader) => (reader.IsStartElement(XmlSignatureConstan |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Reads an SecurityKeyIdentifier from a XML stream. |
| | | 80 | | /// </summary> |
| | | 81 | | /// <param name="reader">An XML reader positioned at an SecurityKeyIdentifier (ds: KeyInfo) as defined in 'http: |
| | | 82 | | /// <returns>SecurityKeyIdentifier.</returns> |
| | | 83 | | /// <exception cref="ArgumentNullException">The <paramref name="reader"/> is null.</exception> |
| | | 84 | | /// <exception cref="InvalidOperationException">If the <paramref name="reader"/> is not positioned at KeyInfo el |
| | | 85 | | protected override SecurityKeyIdentifier ReadKeyIdentifierCore(XmlReader reader) |
| | | 86 | | { |
| | 0 | 87 | | if (reader == null) |
| | | 88 | | { |
| | 0 | 89 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 90 | | } |
| | | 91 | | |
| | 0 | 92 | | if (reader.IsStartElement(XmlSignatureConstants.Elements.KeyInfo, XmlSignatureConstants.Namespace)) |
| | | 93 | | { |
| | 0 | 94 | | KeyInfo keyInfo = new KeyInfo(this); |
| | 0 | 95 | | keyInfo.ReadXml(XmlDictionaryReader.CreateDictionaryReader(reader)); |
| | 0 | 96 | | return keyInfo.KeyIdentifier; |
| | | 97 | | } |
| | | 98 | | else |
| | | 99 | | { |
| | 0 | 100 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperXml(reader, SR.Format(SR.ID4192)); |
| | | 101 | | } |
| | | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | protected override bool CanWriteKeyIdentifierCore(SecurityKeyIdentifier keyIdentifier) => SecurityTokenHandlers. |
| | | 105 | | |
| | 0 | 106 | | protected override void WriteKeyIdentifierCore(XmlWriter writer, SecurityKeyIdentifier keyIdentifier) => Securit |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Checks if the wrapped SecurityTokenHandler can read the |
| | | 110 | | /// SecurityKeyIdentifierClause. |
| | | 111 | | /// </summary> |
| | | 112 | | /// <param name="reader">Reader to a SecurityKeyIdentifierClause.</param> |
| | | 113 | | /// <returns>'True' if the SecurityKeyIdentifierCause can be read.</returns> |
| | | 114 | | /// <exception cref="ArgumentNullException">The input parameter 'reader' is null.</exception> |
| | | 115 | | protected override bool CanReadKeyIdentifierClauseCore(XmlReader reader) |
| | | 116 | | { |
| | 0 | 117 | | foreach (SecurityTokenHandler securityTokenHandler in SecurityTokenHandlers) |
| | | 118 | | { |
| | 0 | 119 | | if (securityTokenHandler.CanReadKeyIdentifierClause(reader)) |
| | | 120 | | { |
| | 0 | 121 | | return true; |
| | | 122 | | } |
| | | 123 | | } |
| | | 124 | | |
| | 0 | 125 | | return SecurityTokenHandlers.KeyInfoSerializer != null && SecurityTokenHandlers.KeyInfoSerializer.CanReadKey |
| | 0 | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Checks if the wrapped SecurityTokenHandler or the base WSSecurityTokenSerializer can write the |
| | | 130 | | /// given SecurityKeyIdentifierClause. |
| | | 131 | | /// </summary> |
| | | 132 | | /// <param name="keyIdentifierClause">SecurityKeyIdentifierClause to be checked.</param> |
| | | 133 | | /// <returns>'True' if the SecurityTokenKeyIdentifierClause can be written.</returns> |
| | | 134 | | /// <exception cref="ArgumentNullException">The input parameter 'keyIdentifierClause' is null.</exception> |
| | | 135 | | protected override bool CanWriteKeyIdentifierClauseCore(SecurityKeyIdentifierClause keyIdentifierClause) |
| | | 136 | | { |
| | 0 | 137 | | foreach (SecurityTokenHandler securityTokenHandler in SecurityTokenHandlers) |
| | | 138 | | { |
| | 0 | 139 | | if (securityTokenHandler.CanWriteKeyIdentifierClause(keyIdentifierClause)) |
| | | 140 | | { |
| | 0 | 141 | | return true; |
| | | 142 | | } |
| | | 143 | | } |
| | | 144 | | |
| | 0 | 145 | | return SecurityTokenHandlers.KeyInfoSerializer != null && SecurityTokenHandlers.KeyInfoSerializer.CanWriteKe |
| | 0 | 146 | | } |
| | | 147 | | |
| | | 148 | | /// <summary> |
| | | 149 | | /// Deserializes a SecurityKeyIdentifierClause from the given reader. |
| | | 150 | | /// </summary> |
| | | 151 | | /// <param name="reader">XmlReader to a SecurityKeyIdentifierClause.</param> |
| | | 152 | | /// <returns>The deserialized SecurityKeyIdentifierClause.</returns> |
| | 0 | 153 | | protected override SecurityKeyIdentifierClause ReadKeyIdentifierClauseCore(XmlReader reader) => SecurityTokenHan |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Serializes the given SecurityKeyIdentifierClause in a XmlWriter. |
| | | 157 | | /// </summary> |
| | | 158 | | /// <param name="writer">XmlWriter to write into.</param> |
| | | 159 | | /// <param name="keyIdentifierClause">SecurityKeyIdentifierClause to be written.</param> |
| | 0 | 160 | | protected override void WriteKeyIdentifierClauseCore(XmlWriter writer, SecurityKeyIdentifierClause keyIdentifier |
| | | 161 | | } |
| | | 162 | | } |