| | | 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 | | |
| | | 7 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Defines a SecurityTokenHandler for Username Password Tokens. |
| | | 11 | | /// </summary> |
| | | 12 | | public abstract class UserNameSecurityTokenHandler : SecurityTokenHandler |
| | | 13 | | { |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Initializes an instance of <see cref="UserNameSecurityTokenHandler"/> |
| | | 17 | | /// </summary> |
| | 0 | 18 | | protected UserNameSecurityTokenHandler() |
| | | 19 | | { |
| | 0 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Controls if the password will be retained in the bootstrap token that is |
| | | 24 | | /// attached to the ClaimsIdentity in ValidateToken. The default is false. |
| | | 25 | | /// </summary> |
| | 0 | 26 | | public virtual bool RetainPassword { get; set; } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Checks the given XmlReader to verify that it is pointing to a Username |
| | | 30 | | /// token. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name=nameof(reader)>XmlReader pointing to SecurityToken.</param> |
| | | 33 | | /// <returns>True if the reader is pointing to a Username SecurityToken.</returns> |
| | | 34 | | /// <exception cref="ArgumentNullException">The given reader is null.</exception> |
| | | 35 | | public override bool CanReadToken(XmlReader reader) |
| | | 36 | | { |
| | 0 | 37 | | if (reader == null) |
| | | 38 | | { |
| | 0 | 39 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 40 | | } |
| | | 41 | | |
| | 0 | 42 | | return reader.IsStartElement(WSSecurity10Constants.Elements.UsernameToken, WSSecurity10Constants.Namespace); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Returns true to indicate that the handler can write UsernameSecurityToken. |
| | | 47 | | /// </summary> |
| | 0 | 48 | | public override bool CanWriteToken => true; |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Get the System.Type of the SecurityToken that this handler can handle. |
| | | 52 | | /// </summary> |
| | 0 | 53 | | public override Type TokenType => typeof(UserNameSecurityToken); |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Get the TokenTypeIdentifier of the token that this handler can work with. |
| | | 57 | | /// </summary> |
| | | 58 | | public override string[] GetTokenTypeIdentifiers() |
| | | 59 | | { |
| | 0 | 60 | | return new string[] { SecurityTokenTypes.UserName }; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Reads the UsernameSecurityToken from the given XmlReader. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="reader">XmlReader pointing to the SecurityToken.</param> |
| | | 67 | | /// <returns>An instance of <see cref="UserNameSecurityToken"/>.</returns> |
| | | 68 | | /// <exception cref="ArgumentNullException">The parameter 'reader' is null.</exception> |
| | | 69 | | /// <exception cref="XmlException">The token cannot be read.</exception> |
| | | 70 | | /// <exception cref="NotSupportedException">The Password was not in plain text format.</exception> |
| | | 71 | | /// <exception cref="InvalidOperationException">An unknown element was found in the SecurityToken or |
| | | 72 | | /// the username was not specified.</exception> |
| | | 73 | | public override SecurityToken ReadToken(XmlReader reader) |
| | | 74 | | { |
| | 0 | 75 | | if (reader == null) |
| | | 76 | | { |
| | 0 | 77 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 78 | | } |
| | | 79 | | |
| | 0 | 80 | | if (!CanReadToken(reader)) |
| | | 81 | | { |
| | 0 | 82 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 83 | | new XmlException( |
| | 0 | 84 | | SR.Format( |
| | 0 | 85 | | SR.ID4065, |
| | 0 | 86 | | WSSecurity10Constants.Elements.Username, |
| | 0 | 87 | | WSSecurity10Constants.Namespace, |
| | 0 | 88 | | reader.LocalName, |
| | 0 | 89 | | reader.NamespaceURI))); |
| | | 90 | | } |
| | | 91 | | |
| | 0 | 92 | | string id = null; |
| | 0 | 93 | | string userName = null; |
| | 0 | 94 | | string password = null; |
| | | 95 | | |
| | 0 | 96 | | reader.MoveToContent(); |
| | 0 | 97 | | id = reader.GetAttribute(WSUtilityConstants.Attributes.IdAttribute, WSUtilityConstants.NamespaceURI); |
| | | 98 | | |
| | 0 | 99 | | reader.ReadStartElement(WSSecurity10Constants.Elements.UsernameToken, WSSecurity10Constants.Namespace); |
| | 0 | 100 | | while (reader.IsStartElement()) |
| | | 101 | | { |
| | 0 | 102 | | if (reader.IsStartElement(WSSecurity10Constants.Elements.Username, WSSecurity10Constants.Namespace)) |
| | | 103 | | { |
| | 0 | 104 | | userName = reader.ReadElementString(); |
| | | 105 | | } |
| | 0 | 106 | | else if (reader.IsStartElement(WSSecurity10Constants.Elements.Password, WSSecurity10Constants.Namespace) |
| | | 107 | | { |
| | 0 | 108 | | string type = reader.GetAttribute(WSSecurity10Constants.Attributes.Type, null); |
| | 0 | 109 | | if (!string.IsNullOrEmpty(type) && !StringComparer.Ordinal.Equals(type, WSSecurity10Constants.UPToke |
| | | 110 | | { |
| | 0 | 111 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Format(SR |
| | | 112 | | } |
| | | 113 | | |
| | 0 | 114 | | password = reader.ReadElementString(); |
| | | 115 | | } |
| | 0 | 116 | | else if (reader.IsStartElement(WSSecurity10Constants.Elements.Nonce, WSSecurity10Constants.Namespace)) |
| | | 117 | | { |
| | | 118 | | // Nonce can be safely ignored |
| | 0 | 119 | | reader.Skip(); |
| | | 120 | | } |
| | 0 | 121 | | else if (reader.IsStartElement(WSUtilityConstants.ElementNames.Created, WSUtilityConstants.NamespaceURI) |
| | | 122 | | { |
| | | 123 | | // wsu:Created can be safely ignored |
| | 0 | 124 | | reader.Skip(); |
| | | 125 | | } |
| | | 126 | | else |
| | | 127 | | { |
| | 0 | 128 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.ID4060, read |
| | | 129 | | } |
| | | 130 | | } |
| | 0 | 131 | | reader.ReadEndElement(); |
| | | 132 | | |
| | 0 | 133 | | if (string.IsNullOrEmpty(userName)) |
| | | 134 | | { |
| | 0 | 135 | | throw new InvalidOperationException(SR.Format(SR.ID4061)); |
| | | 136 | | } |
| | | 137 | | |
| | 0 | 138 | | return string.IsNullOrEmpty(id) ? |
| | 0 | 139 | | new UserNameSecurityToken(userName, password) : |
| | 0 | 140 | | new UserNameSecurityToken(userName, password, id); |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// Writes the given UsernameSecurityToken to the XmlWriter. |
| | | 146 | | /// </summary> |
| | | 147 | | /// <param name=nameof(writer)>XmlWriter to write the token to.</param> |
| | | 148 | | /// <param name=nameof(token)>SecurityToken to be written.</param> |
| | | 149 | | /// <exception cref="InvalidOperationException">The given token is not a UsernameSecurityToken.</exception> |
| | | 150 | | /// <exception cref="ArgumentNullException">The parameter 'writer' or 'token' is null.</exception> |
| | | 151 | | public override void WriteToken(XmlWriter writer, SecurityToken token) |
| | | 152 | | { |
| | 0 | 153 | | if (writer == null) |
| | | 154 | | { |
| | 0 | 155 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer)); |
| | | 156 | | } |
| | | 157 | | |
| | 0 | 158 | | if (token == null) |
| | | 159 | | { |
| | 0 | 160 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token)); |
| | | 161 | | } |
| | | 162 | | |
| | | 163 | | |
| | 0 | 164 | | if (!(token is UserNameSecurityToken usernameSecurityToken)) |
| | | 165 | | { |
| | 0 | 166 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(token), SR.Format(SR.ID0018, typeof( |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | // <wsse:UsernameToken |
| | 0 | 170 | | writer.WriteStartElement( |
| | 0 | 171 | | WSSecurity10Constants.Elements.UsernameToken, |
| | 0 | 172 | | WSSecurity10Constants.Namespace |
| | 0 | 173 | | ); |
| | 0 | 174 | | if (!string.IsNullOrEmpty(token.Id)) |
| | | 175 | | { |
| | | 176 | | // wsu:Id="..." |
| | 0 | 177 | | writer.WriteAttributeString( |
| | 0 | 178 | | WSUtilityConstants.Attributes.IdAttribute, |
| | 0 | 179 | | WSUtilityConstants.NamespaceURI, |
| | 0 | 180 | | token.Id |
| | 0 | 181 | | ); |
| | | 182 | | } |
| | | 183 | | // <wsse:Username>...</wsse:Username> |
| | 0 | 184 | | writer.WriteElementString( |
| | 0 | 185 | | WSSecurity10Constants.Elements.Username, |
| | 0 | 186 | | WSSecurity10Constants.Namespace, |
| | 0 | 187 | | usernameSecurityToken.UserName |
| | 0 | 188 | | ); |
| | | 189 | | |
| | | 190 | | // <wsse:Password>...</wsse:Password> |
| | 0 | 191 | | if (usernameSecurityToken.Password != null) |
| | | 192 | | { |
| | 0 | 193 | | writer.WriteStartElement( |
| | 0 | 194 | | WSSecurity10Constants.Elements.Password, |
| | 0 | 195 | | WSSecurity10Constants.Namespace |
| | 0 | 196 | | ); |
| | | 197 | | |
| | 0 | 198 | | writer.WriteAttributeString( |
| | 0 | 199 | | WSSecurity10Constants.Attributes.Type, |
| | 0 | 200 | | null, |
| | 0 | 201 | | WSSecurity10Constants.UPTokenPasswordTextValue |
| | 0 | 202 | | ); |
| | | 203 | | |
| | 0 | 204 | | writer.WriteString(usernameSecurityToken.Password); |
| | 0 | 205 | | writer.WriteEndElement(); |
| | | 206 | | } |
| | | 207 | | |
| | 0 | 208 | | writer.WriteEndElement(); |
| | 0 | 209 | | writer.Flush(); |
| | 0 | 210 | | } |
| | | 211 | | } |
| | | 212 | | } |