//----------------------------------------------------------------------- // // Copyright (c) Outercurve Foundation. All rights reserved. // //----------------------------------------------------------------------- namespace DotNetOpenAuth.InfoCard { using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; using System.IdentityModel.Tokens; using System.Security.Cryptography.X509Certificates; /// /// Arguments for the event. /// public class ReceivingTokenEventArgs : EventArgs { /// /// Initializes a new instance of the class. /// /// The raw token XML, prior to any decryption. internal ReceivingTokenEventArgs(string tokenXml) { Requires.NotNull(tokenXml, "tokenXml"); this.TokenXml = tokenXml; this.IsEncrypted = Token.IsEncrypted(this.TokenXml); this.DecryptingTokens = new List(); } /// /// Gets a value indicating whether the token is encrypted. /// /// /// true if the token is encrypted; otherwise, false. /// public bool IsEncrypted { get; private set; } /// /// Gets the raw token XML, prior to any decryption. /// public string TokenXml { get; private set; } /// /// Gets or sets a value indicating whether processing /// this token should be canceled. /// /// true if cancel; otherwise, false. /// /// If set the true, the /// event will never be fired. /// public bool Cancel { get; set; } /// /// Gets a list where security tokens such as X.509 certificates may be /// added to be used for token decryption. /// internal IList DecryptingTokens { get; private set; } /// /// Adds a security token that may be used to decrypt the incoming token. /// /// The security token. public void AddDecryptingToken(SecurityToken securityToken) { Requires.NotNull(securityToken, "securityToken"); this.DecryptingTokens.Add(securityToken); } /// /// Adds an X.509 certificate with a private key that may be used to decrypt the incoming token. /// /// The certificate. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "False positive")] public void AddDecryptingToken(X509Certificate2 certificate) { Requires.NotNull(certificate, "certificate"); Requires.True(certificate.HasPrivateKey, "certificate"); var cert = new X509SecurityToken(certificate); try { this.AddDecryptingToken(cert); } catch { cert.Dispose(); throw; } } #if CONTRACTS_FULL /// /// Verifies conditions that should be true for any valid state of this object. /// [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Called by code contracts.")] [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Called by code contracts.")] [ContractInvariantMethod] private void ObjectInvariant() { Contract.Invariant(this.TokenXml != null); Contract.Invariant(this.DecryptingTokens != null); } #endif } }