//-----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------
namespace System.IdentityModel.Tokens
{
using System.Collections.ObjectModel;
using System.IdentityModel.Configuration;
using System.IdentityModel.Diagnostics.Application;
using System.IdentityModel.Selectors;
using System.Runtime.Diagnostics;
using System.Security.Claims;
using System.Xml;
///
/// Defines the interface for a Security Token Handler.
///
public abstract class SecurityTokenHandler : ICustomIdentityConfiguration
{
private SecurityTokenHandlerCollection collection;
private SecurityTokenHandlerConfiguration configuration;
private EventTraceActivity eventTraceActivity;
///
/// Creates an instance of
///
protected SecurityTokenHandler()
{
}
private EventTraceActivity EventTraceActivity
{
get
{
if (this.eventTraceActivity == null)
{
this.eventTraceActivity = EventTraceActivity.GetFromThreadOrCreate();
}
return this.eventTraceActivity;
}
}
///
/// Gets a value indicating whether this handler supports validation of tokens
/// handled by this instance.
/// v
/// 'True' if the instance is capable of SecurityToken
/// validation.
public virtual bool CanValidateToken
{
get { return false; }
}
///
/// Gets a value indicating whether the class provides serialization functionality to serialize token handled
/// by this instance.
///
/// true if the WriteToken method can serialize this token.
public virtual bool CanWriteToken
{
get { return false; }
}
///
/// Gets or sets the
///
public SecurityTokenHandlerConfiguration Configuration
{
get { return this.configuration; }
set { this.configuration = value; }
}
///
/// Gets or sets the SecurityTokenHandlerCollection that this SecurityTokenHandler
/// is part of. This property should never be set directly. When the SecurityTokenHandler
/// is added to a collection this property is automatically set.
///
public SecurityTokenHandlerCollection ContainingCollection
{
get
{
return this.collection;
}
internal set
{
this.collection = value;
}
}
///
/// Gets the System.Type of the SecurityToken this instance handles.
///
public abstract Type TokenType
{
get;
}
///
/// Indicates whether the current XML element can be read as a token
/// of the type handled by this instance.
///
/// An XML reader positioned at a start
/// element. The reader should not be advanced.
/// 'True' if the ReadToken method can the element.
public virtual bool CanReadToken(XmlReader reader)
{
return false;
}
///
/// Indicates whether the current token string can be read as a token
/// of the type handled by this instance.
///
/// The token string thats needs to be read.
/// 'True' if the ReadToken method can parse the token string.
public virtual bool CanReadToken(string tokenString)
{
return false;
}
///
/// Deserializes from XML a token of the type handled by this instance.
///
/// An XML reader positioned at the token's start
/// element.
/// SecurityToken instance.
public virtual SecurityToken ReadToken(XmlReader reader)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException(SR.GetString(SR.ID4008, "SecurityTokenHandler", "ReadToken")));
}
///
/// Deserializes from XML a token of the type handled by this instance.
///
/// An XML reader positioned at the token's start
/// element.
/// The SecrityTokenResolver that contains out-of-band and cached tokens.
/// SecurityToken instance.
public virtual SecurityToken ReadToken(XmlReader reader, SecurityTokenResolver tokenResolver)
{
// The default implementation ignores the SecurityTokenResolver and delegates the call to the
// ReadToken method that takes a XmlReader.
return this.ReadToken(reader);
}
///
/// Deserializes from string a token of the type handled by this instance.
///
/// The string to be deserialized.
/// SecurityToken instance which represents the serialized token.
public virtual SecurityToken ReadToken(string tokenString)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException(SR.GetString(SR.ID4008, "SecurityTokenHandler", "ReadToken")));
}
///
/// Serializes to XML a token of the type handled by this instance.
///
/// The XML writer.
/// A token of type TokenType.
public virtual void WriteToken(XmlWriter writer, SecurityToken token)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException(SR.GetString(SR.ID4008, "SecurityTokenHandler", "WriteToken")));
}
///
/// Serializes to string a token of the type handled by this instance.
///
/// A token of type TokenType.
/// The serialized token.
public virtual string WriteToken(SecurityToken token)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException(SR.GetString(SR.ID4008, "SecurityTokenHandler", "WriteToken")));
}
///
/// Indicates if the current XML element is pointing to a KeyIdentifierClause that
/// can be serialized by this instance.
///
/// An XML reader positioned at the start element.
/// The reader should not be advanced.
/// true if the ReadKeyIdentifierClause can read the element.
public virtual bool CanReadKeyIdentifierClause(XmlReader reader)
{
return false;
}
///
/// Deserializes the XML to a KeyIdentifierClause that references a token
/// handled by this instance.
///
/// An XML reader positioned at the KeyIdentifierClause start element.
/// SecurityKeyIdentifierClause instance.
public virtual SecurityKeyIdentifierClause ReadKeyIdentifierClause(XmlReader reader)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException(SR.GetString(SR.ID4008, "SecurityTokenHandler", "ReadKeyIdentifierClause")));
}
///
/// Indicates if the given SecurityKeyIdentifierClause can be serialized by this
/// instance.
///
/// SecurityKeyIdentifierClause to be serialized.
/// true if the given SecurityKeyIdentifierClause can be serialized.
public virtual bool CanWriteKeyIdentifierClause(SecurityKeyIdentifierClause securityKeyIdentifierClause)
{
return false;
}
///
/// Serializes to XML a SecurityKeyIdentifierClause that this instance supports.
///
/// The XML writer.
/// The SecurityKeyIdentifierClause to be used to serialize the token.
public virtual void WriteKeyIdentifierClause(XmlWriter writer, SecurityKeyIdentifierClause securityKeyIdentifierClause)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException(SR.GetString(SR.ID4008, "SecurityTokenHandler", "WriteKeyIdentifierClause")));
}
///
/// Called by the STS to create a token given a token descriptor.
///
/// Describes the token; properties such
/// as ValidFrom, AppliesTo, EncryptingCredentials, Claims, etc., are filled in
/// before the call to create token.
/// A SecurityToken that matches the properties of the token descriptor.
public virtual SecurityToken CreateToken(SecurityTokenDescriptor tokenDescriptor)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException(SR.GetString(SR.ID4008, "SecurityTokenHandler", "CreateToken")));
}
///
/// Creates the security token reference for tokens handled by this instance.
///
/// The SecurityToken instance for which the references needs to be
/// created.
/// Boolean that indicates if a attached or unattached
/// reference needs to be created.
/// A SecurityKeyIdentifierClause that identifies the given token.
public virtual SecurityKeyIdentifierClause CreateSecurityTokenReference(SecurityToken token, bool attached)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException(SR.GetString(SR.ID4008, "SecurityTokenHandler", "CreateSecurityTokenReference")));
}
///
/// The URI used in requests to identify a token of the type handled
/// by this instance.
///
///
/// For example, this should be the URI value used
/// in the RequestSecurityToken's TokenType element to request this
/// sort of token.
///
/// The set of URIs that identify the token this handler supports.
public abstract string[] GetTokenTypeIdentifiers();
///
/// Validates a .
///
/// The to validate.
/// The of representing the identities contained in the token.
/// Derived types will validate specific tokens.
public virtual ReadOnlyCollection ValidateToken(SecurityToken token)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException(SR.GetString(SR.ID4008, "SecurityTokenHandler", "ValidateToken")));
}
///
/// Throws if a token is detected as being replayed.
/// Override this method in your derived class to detect replays.
///
/// The token to check for replay.
protected virtual void DetectReplayedToken(SecurityToken token)
{
}
///
/// Load custom configuration from Xml
///
/// Custom configuration elements
public virtual void LoadCustomConfiguration(XmlNodeList nodelist)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException(SR.GetString(SR.ID0023, this.GetType().AssemblyQualifiedName)));
}
protected void TraceTokenValidationSuccess(SecurityToken token)
{
if (TD.TokenValidationSuccessIsEnabled())
{
TD.TokenValidationSuccess(this.EventTraceActivity, token.GetType().ToString(), token.Id);
}
}
protected void TraceTokenValidationFailure(SecurityToken token, string errorMessage)
{
if (TD.TokenValidationFailureIsEnabled())
{
TD.TokenValidationFailure(this.EventTraceActivity, token.GetType().ToString(), token.Id, errorMessage);
}
}
}
}