//----------------------------------------------------------------------- // // Copyright (c) Andrew Arnott. All rights reserved. // //----------------------------------------------------------------------- namespace DotNetOAuth.ChannelElements { using System; using System.Collections.Generic; using System.Linq; using System.Text; using DotNetOAuth.Messaging; using DotNetOAuth.Messaging.Bindings; /// /// A binding element that signs outgoing messages and verifies the signature on incoming messages. /// internal abstract class SigningBindingElementBase : IChannelBindingElement { #region IChannelBindingElement Members /// /// Gets the message protection provided by this binding element. /// public MessageProtection Protection { get { return MessageProtection.TamperProtection; } } /// /// Signs the outgoing message. /// /// The message to sign. /// True if the message was signed. False otherwise. public bool PrepareMessageForSending(IProtocolMessage message) { var signedMessage = message as ITamperResistantOAuthMessage; if (signedMessage != null) { this.Sign(signedMessage); return true; } return false; } /// /// Verifies the signature on an incoming message. /// /// The message whose signature should be verified. /// True if the signature was verified. False if the message had no signature. /// Thrown if the signature is invalid. public bool PrepareMessageForReceiving(IProtocolMessage message) { var signedMessage = message as ITamperResistantOAuthMessage; if (signedMessage != null) { if (!this.IsSignatureValid(signedMessage)) { throw new InvalidSignatureException(message); } return true; } return false; } #endregion /// /// Applies a signature to the message. /// /// The message to sign. protected abstract void Sign(ITamperResistantOAuthMessage message); /// /// Validates the signature on a message. /// Does NOT throw an exception on failing signature verification. /// /// The message with a signature to verify. /// True if the signature is valid. False otherwise. protected abstract bool IsSignatureValid(ITamperResistantOAuthMessage message); } }