| | | 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 | | using CoreWCF.IdentityModel.Selectors; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Defines the interface for a Security Token Handler. |
| | | 14 | | /// </summary> |
| | | 15 | | public abstract class SecurityTokenHandler //: ICustomIdentityConfiguration |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Creates an instance of <see cref="SecurityTokenHandler"/> |
| | | 19 | | /// </summary> |
| | 30 | 20 | | protected SecurityTokenHandler() |
| | | 21 | | { |
| | 30 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Gets a value indicating whether this handler supports validation of tokens |
| | | 26 | | /// handled by this instance. |
| | | 27 | | /// </summary>v |
| | | 28 | | /// <returns>'True' if the instance is capable of SecurityToken |
| | | 29 | | /// validation.</returns> |
| | 0 | 30 | | public virtual bool CanValidateToken => false; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Gets a value indicating whether the class provides serialization functionality to serialize token handled |
| | | 34 | | /// by this instance. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <returns>true if the WriteToken method can serialize this token.</returns> |
| | 0 | 37 | | public virtual bool CanWriteToken => false; |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Gets or sets the <see cref="SecurityTokenHandlerConfiguration" /> |
| | | 41 | | /// </summary> |
| | 248 | 42 | | public SecurityTokenHandlerConfiguration Configuration { get; set; } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Gets or sets the SecurityTokenHandlerCollection that this SecurityTokenHandler |
| | | 46 | | /// is part of. This property should never be set directly. When the SecurityTokenHandler |
| | | 47 | | /// is added to a collection this property is automatically set. |
| | | 48 | | /// </summary> |
| | 30 | 49 | | public SecurityTokenHandlerCollection ContainingCollection { get; internal set; } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets the System.Type of the SecurityToken this instance handles. |
| | | 53 | | /// </summary> |
| | | 54 | | public abstract Type TokenType |
| | | 55 | | { |
| | | 56 | | get; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Indicates whether the current XML element can be read as a token |
| | | 61 | | /// of the type handled by this instance. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <param name="reader">An XML reader positioned at a start |
| | | 64 | | /// element. The reader should not be advanced.</param> |
| | | 65 | | /// <returns>'True' if the ReadToken method can the element.</returns> |
| | 0 | 66 | | public virtual bool CanReadToken(XmlReader reader) => false; |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Indicates whether the current token string can be read as a token |
| | | 70 | | /// of the type handled by this instance. |
| | | 71 | | /// </summary> |
| | | 72 | | /// <param name="tokenString">The token string thats needs to be read.</param> |
| | | 73 | | /// <returns>'True' if the ReadToken method can parse the token string.</returns> |
| | 0 | 74 | | public virtual bool CanReadToken(string tokenString) => false; |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Deserializes from XML a token of the type handled by this instance. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <param name="reader">An XML reader positioned at the token's start |
| | | 80 | | /// element.</param> |
| | | 81 | | /// <returns>SecurityToken instance.</returns> |
| | 0 | 82 | | public virtual SecurityToken ReadToken(XmlReader reader) => throw DiagnosticUtility.ExceptionUtility.ThrowHelper |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Deserializes from XML a token of the type handled by this instance. |
| | | 86 | | /// </summary> |
| | | 87 | | /// <param name="reader">An XML reader positioned at the token's start |
| | | 88 | | /// element.</param> |
| | | 89 | | /// <param name="tokenResolver">The SecrityTokenResolver that contains out-of-band and cached tokens.</param> |
| | | 90 | | /// <returns>SecurityToken instance.</returns> |
| | | 91 | | public virtual SecurityToken ReadToken(XmlReader reader, SecurityTokenResolver tokenResolver) => |
| | | 92 | | // The default implementation ignores the SecurityTokenResolver and delegates the call to the |
| | | 93 | | // ReadToken method that takes a XmlReader. |
| | 53 | 94 | | ReadToken(reader); |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Deserializes from string a token of the type handled by this instance. |
| | | 98 | | /// </summary> |
| | | 99 | | /// <param name="tokenString">The string to be deserialized.</param> |
| | | 100 | | /// <returns>SecurityToken instance which represents the serialized token.</returns> |
| | 0 | 101 | | public virtual SecurityToken ReadToken(string tokenString) => throw DiagnosticUtility.ExceptionUtility.ThrowHelp |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Serializes to XML a token of the type handled by this instance. |
| | | 105 | | /// </summary> |
| | | 106 | | /// <param name="writer">The XML writer.</param> |
| | | 107 | | /// <param name="token">A token of type TokenType.</param> |
| | 0 | 108 | | public virtual void WriteToken(XmlWriter writer, SecurityToken token) => throw DiagnosticUtility.ExceptionUtilit |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Serializes to string a token of the type handled by this instance. |
| | | 112 | | /// </summary> |
| | | 113 | | /// <param name="token">A token of type TokenType.</param> |
| | | 114 | | /// <returns>The serialized token.</returns> |
| | 0 | 115 | | public virtual string WriteToken(SecurityToken token) => throw DiagnosticUtility.ExceptionUtility.ThrowHelperErr |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Indicates if the current XML element is pointing to a KeyIdentifierClause that |
| | | 119 | | /// can be serialized by this instance. |
| | | 120 | | /// </summary> |
| | | 121 | | /// <param name="reader">An XML reader positioned at the start element. |
| | | 122 | | /// The reader should not be advanced.</param> |
| | | 123 | | /// <returns>true if the ReadKeyIdentifierClause can read the element.</returns> |
| | 0 | 124 | | public virtual bool CanReadKeyIdentifierClause(XmlReader reader) => false; |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Deserializes the XML to a KeyIdentifierClause that references a token |
| | | 128 | | /// handled by this instance. |
| | | 129 | | /// </summary> |
| | | 130 | | /// <param name="reader">An XML reader positioned at the KeyIdentifierClause start element.</param> |
| | | 131 | | /// <returns>SecurityKeyIdentifierClause instance.</returns> |
| | 0 | 132 | | public virtual SecurityKeyIdentifierClause ReadKeyIdentifierClause(XmlReader reader) => throw DiagnosticUtility. |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Indicates if the given SecurityKeyIdentifierClause can be serialized by this |
| | | 136 | | /// instance. |
| | | 137 | | /// </summary> |
| | | 138 | | /// <param name="securityKeyIdentifierClause">SecurityKeyIdentifierClause to be serialized.</param> |
| | | 139 | | /// <returns>true if the given SecurityKeyIdentifierClause can be serialized.</returns> |
| | 0 | 140 | | public virtual bool CanWriteKeyIdentifierClause(SecurityKeyIdentifierClause securityKeyIdentifierClause) => fals |
| | | 141 | | |
| | | 142 | | /// <summary> |
| | | 143 | | /// Serializes to XML a SecurityKeyIdentifierClause that this instance supports. |
| | | 144 | | /// </summary> |
| | | 145 | | /// <param name="writer">The XML writer.</param> |
| | | 146 | | /// <param name="securityKeyIdentifierClause">The SecurityKeyIdentifierClause to be used to serialize the token. |
| | 0 | 147 | | public virtual void WriteKeyIdentifierClause(XmlWriter writer, SecurityKeyIdentifierClause securityKeyIdentifier |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// Called by the STS to create a token given a token descriptor. |
| | | 151 | | /// </summary> |
| | | 152 | | /// <param name="tokenDescriptor">Describes the token; properties such |
| | | 153 | | /// as ValidFrom, AppliesTo, EncryptingCredentials, Claims, etc., are filled in |
| | | 154 | | /// before the call to create token. </param> |
| | | 155 | | /// <returns>A SecurityToken that matches the properties of the token descriptor.</returns> |
| | 0 | 156 | | public virtual SecurityToken CreateToken(SecurityTokenDescriptor tokenDescriptor) => throw DiagnosticUtility.Exc |
| | | 157 | | |
| | | 158 | | /// <summary> |
| | | 159 | | /// Creates the security token reference for tokens handled by this instance. |
| | | 160 | | /// </summary> |
| | | 161 | | /// <param name="token">The SecurityToken instance for which the references needs to be |
| | | 162 | | /// created.</param> |
| | | 163 | | /// <param name="attached">Boolean that indicates if a attached or unattached |
| | | 164 | | /// reference needs to be created.</param> |
| | | 165 | | /// <returns>A SecurityKeyIdentifierClause that identifies the given token.</returns> |
| | 0 | 166 | | public virtual SecurityKeyIdentifierClause CreateSecurityTokenReference(SecurityToken token, bool attached) => t |
| | | 167 | | |
| | | 168 | | /// <summary> |
| | | 169 | | /// The URI used in requests to identify a token of the type handled |
| | | 170 | | /// by this instance. |
| | | 171 | | /// </summary> |
| | | 172 | | /// <remarks> |
| | | 173 | | /// For example, this should be the URI value used |
| | | 174 | | /// in the RequestSecurityToken's TokenType element to request this |
| | | 175 | | /// sort of token. |
| | | 176 | | /// </remarks> |
| | | 177 | | /// <returns>The set of URIs that identify the token this handler supports.</returns> |
| | | 178 | | public abstract string[] GetTokenTypeIdentifiers(); |
| | | 179 | | |
| | | 180 | | /// <summary> |
| | | 181 | | /// Validates a <see cref="SecurityToken"/>. |
| | | 182 | | /// </summary> |
| | | 183 | | /// <param name="token">The <see cref="SecurityToken"/> to validate.</param> |
| | | 184 | | /// <returns>The <see cref="ReadOnlyCollection{T}"/> of <see cref="ClaimsIdentity"/> representing the identities |
| | | 185 | | /// <remarks>Derived types will validate specific tokens.</remarks> |
| | 0 | 186 | | public virtual ReadOnlyCollection<ClaimsIdentity> ValidateToken(SecurityToken token) => throw DiagnosticUtility. |
| | | 187 | | |
| | | 188 | | /// <summary> |
| | | 189 | | /// Throws if a token is detected as being replayed. |
| | | 190 | | /// Override this method in your derived class to detect replays. |
| | | 191 | | /// </summary> |
| | | 192 | | /// <param name="token">The token to check for replay.</param> |
| | | 193 | | protected virtual void DetectReplayedToken(SecurityToken token) |
| | | 194 | | { |
| | 0 | 195 | | } |
| | | 196 | | } |
| | | 197 | | } |