//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.IdentityModel.Selectors;
using System.IdentityModel.Tokens;
using System.Collections.ObjectModel;
namespace System.IdentityModel.Tokens
{
///
/// This class defines a TokenResolver that can wrap multiple Token Resolvers
/// and resolve tokens across all the wrapped token resolvers.
///
public class AggregateTokenResolver : SecurityTokenResolver
{
List _tokenResolvers = new List();
///
/// Initializes an instance of
///
/// IEnumerable list of TokenResolvers to be wrapped.
/// The input argument 'tokenResolvers' is null.
/// The input 'tokenResolver' list does not contain a valid
/// SecurityTokenResolver. At least one SecurityTokenResolver should be specified.
public AggregateTokenResolver( IEnumerable tokenResolvers )
{
if ( tokenResolvers == null )
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( "tokenResolvers" );
}
AddNonEmptyResolvers( tokenResolvers );
}
///
/// Gets a read-only collection of TokenResolvers.
///
public ReadOnlyCollection TokenResolvers
{
get
{
return _tokenResolvers.AsReadOnly();
}
}
///
/// Override of the base class. Resolves the given SecurityKeyIdentifierClause to a
/// SecurityKey.
///
/// The Clause to be resolved.
/// The resolved SecurityKey
/// True if successfully resolved.
/// Input argument 'keyIdentifierClause' is null.
protected override bool TryResolveSecurityKeyCore( SecurityKeyIdentifierClause keyIdentifierClause, out SecurityKey key )
{
if ( keyIdentifierClause == null )
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( "keyIdentifierClause" );
}
key = null;
foreach ( SecurityTokenResolver tokenResolver in _tokenResolvers )
{
if ( tokenResolver.TryResolveSecurityKey( keyIdentifierClause, out key ) )
{
return true;
}
}
return false;
}
///
/// Override of the base class. Resolves the given SecurityKeyIdentifier to a
/// SecurityToken.
///
/// The KeyIdentifier to be resolved.
/// The resolved SecurityToken
/// True if successfully resolved.
/// Input argument 'keyIdentifier' is null.
protected override bool TryResolveTokenCore( SecurityKeyIdentifier keyIdentifier, out SecurityToken token )
{
if ( keyIdentifier == null )
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( "keyIdentifer" );
}
token = null;
foreach ( SecurityTokenResolver tokenResolver in _tokenResolvers )
{
if ( tokenResolver.TryResolveToken( keyIdentifier, out token ) )
{
return true;
}
}
return false;
}
///
/// Override of the base class. Resolves the given SecurityKeyIdentifierClause to a
/// SecurityToken.
///
/// The KeyIdentifier to be resolved.
/// The resolved SecurityToken
/// True if successfully resolved.
/// Input argument 'keyIdentifierClause' is null.
protected override bool TryResolveTokenCore( SecurityKeyIdentifierClause keyIdentifierClause, out SecurityToken token )
{
if ( keyIdentifierClause == null )
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( "keyIdentifierClause" );
}
token = null;
foreach ( SecurityTokenResolver tokenResolver in _tokenResolvers )
{
if ( tokenResolver.TryResolveToken( keyIdentifierClause, out token ) )
{
return true;
}
}
return false;
}
private void AddNonEmptyResolvers( IEnumerable resolvers )
{
foreach ( SecurityTokenResolver resolver in resolvers )
{
if ( resolver != null && resolver != EmptySecurityTokenResolver.Instance )
{
_tokenResolvers.Add( resolver );
}
}
}
}
}