//-----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------
using System;
using System.IdentityModel.Selectors;
using System.IdentityModel.Tokens;
using System.IO;
using System.Xml;
using System.Security.Claims;
using System.Collections.ObjectModel;
namespace System.IdentityModel.Tokens
{
///
/// This class represents a number elements found in a which represent security tokens.
///
///
/// This class is not thread-safe.
///
public class SecurityTokenElement
{
SecurityToken _securityToken;
XmlElement _securityTokenXml;
SecurityTokenHandlerCollection _securityTokenHandlers;
ReadOnlyCollection _subject;
///
/// Creates an instance of this object using a object.
///
/// The security token this object represents.
///
/// is not supported by this object if this constructor is used unless
/// is overriden.
/// If the securityToken passed in is a then SecurityTokenXml will
/// be set to the value found in
///
public SecurityTokenElement(SecurityToken securityToken)
{
if (securityToken == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("securityToken");
}
GenericXmlSecurityToken xmlToken = securityToken as GenericXmlSecurityToken;
if (xmlToken != null)
{
_securityTokenXml = xmlToken.TokenXml;
}
_securityToken = securityToken;
}
///
/// Creates an instance of this object using XML representation of the security token.
///
/// The representation of the security token.
/// The collection of objects that may
/// be used to read and validate the security token this object represents.
public SecurityTokenElement(XmlElement securityTokenXml, SecurityTokenHandlerCollection securityTokenHandlers)
{
if (securityTokenXml == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("securityTokenXml");
}
if (securityTokenHandlers == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("securityTokenHandlers");
}
_securityTokenXml = securityTokenXml;
_securityTokenHandlers = securityTokenHandlers;
}
///
/// Gets the XML representation of the token.
///
/// This property will be null unless this object was constructed using
/// .
///
public XmlElement SecurityTokenXml
{
get
{
return _securityTokenXml;
}
}
///
/// Gets the security token this object represents.
///
///
/// If this object was not constructed directly with a using
/// ,
/// will be called for this value.
///
/// The this object represents
public SecurityToken GetSecurityToken()
{
if (_securityToken == null)
{
_securityToken = ReadSecurityToken(_securityTokenXml, _securityTokenHandlers);
}
return _securityToken;
}
///
/// Gets the collection of contained in the token.
///
///
/// A of representing the identities contained in the token.
public ReadOnlyCollection GetIdentities()
{
if (_subject == null)
{
_subject = ValidateToken(_securityTokenXml, _securityTokenHandlers);
}
return _subject;
}
///
/// Creates the identities for the represented by the .
///
/// The representation of the security token.
/// The collection of objects that may
/// be used to read and validate the security token this object represents.
/// A of representing the identities contained in the token.
/// If either parameter 'securityTokenXml' or 'securityTokenHandlers' are null.
protected virtual ReadOnlyCollection ValidateToken(XmlElement securityTokenXml, SecurityTokenHandlerCollection securityTokenHandlers)
{
if (securityTokenXml == null || securityTokenHandlers == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ID4052)));
}
SecurityToken securityToken = GetSecurityToken();
return securityTokenHandlers.ValidateToken(securityToken);
}
///
/// Reads a from the provided XML representation.
///
/// The XML representation of the security token.
/// The used to
/// read the token.
/// A .
protected virtual SecurityToken ReadSecurityToken(XmlElement securityTokenXml,
SecurityTokenHandlerCollection securityTokenHandlers)
{
SecurityToken securityToken = null;
XmlReader reader = new XmlNodeReader(securityTokenXml);
reader.MoveToContent();
securityToken = securityTokenHandlers.ReadToken(reader);
if (securityToken == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ID4051, securityTokenXml, reader.LocalName, reader.NamespaceURI)));
}
return securityToken;
}
}
}