//----------------------------------------------------------------------- // // Copyright (c) Outercurve Foundation. All rights reserved. // //----------------------------------------------------------------------- namespace DotNetOpenAuth.OpenId.Messages { using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using DotNetOpenAuth.Messaging; using Validation; /// /// An indirect request from a Relying Party to a Provider where the response /// is expected to be signed. /// [Serializable] internal class SignedResponseRequest : RequestBase, IProtocolMessageWithExtensions { /// /// Backing store for the property. /// private IList extensions = new List(); /// /// Initializes a new instance of the class. /// /// The OpenID version to use. /// The Provider endpoint that receives this message. /// /// for asynchronous javascript clients; /// to allow the Provider to interact with the user in order to complete authentication. /// internal SignedResponseRequest(Version version, Uri providerEndpoint, AuthenticationRequestMode mode) : base(version, providerEndpoint, GetMode(version, mode), DotNetOpenAuth.Messaging.MessageTransport.Indirect) { } #region IProtocolMessageWithExtensions Members /// /// Gets the list of extensions that are included with this message. /// /// /// /// Implementations of this interface should ensure that this property never returns null. /// public IList Extensions { get { return this.extensions; } } #endregion /// /// Gets a value indicating whether the Provider is allowed to interact with the user /// as part of authentication. /// /// true if using OpenID immediate mode; otherwise, false. internal bool Immediate { get { return string.Equals(this.Mode, Protocol.Args.Mode.checkid_immediate, StringComparison.Ordinal); } } /// /// Gets or sets the handle of the association the RP would like the Provider /// to use for signing a positive assertion in the response message. /// /// A handle for an association between the Relying Party and the OP /// that SHOULD be used to sign the response. /// /// If no association handle is sent, the transaction will take place in Stateless Mode /// (Verifying Directly with the OpenID Provider). /// [MessagePart("openid.assoc_handle", IsRequired = false, AllowEmpty = false)] internal string AssociationHandle { get; set; } /// /// Gets or sets the URL the Provider should redirect the user agent to following /// the authentication attempt. /// /// URL to which the OP SHOULD return the User-Agent with the response /// indicating the status of the request. /// /// If this value is not sent in the request it signifies that the Relying Party /// does not wish for the end user to be returned. /// The return_to URL MAY be used as a mechanism for the Relying Party to attach /// context about the authentication request to the authentication response. /// This document does not define a mechanism by which the RP can ensure that query /// parameters are not modified by outside parties; such a mechanism can be defined /// by the RP itself. /// [MessagePart("openid.return_to", IsRequired = true, AllowEmpty = false)] [MessagePart("openid.return_to", IsRequired = false, AllowEmpty = false, MinVersion = "2.0")] internal Uri ReturnTo { get; set; } /// /// Gets or sets the Relying Party discovery URL the Provider may use to verify the /// source of the authentication request. /// /// /// URL pattern the OP SHOULD ask the end user to trust. See Section 9.2 (Realms). /// This value MUST be sent if openid.return_to is omitted. /// Default: The URL. /// [MessagePart("openid.trust_root", IsRequired = false, AllowEmpty = false)] [MessagePart("openid.realm", IsRequired = false, AllowEmpty = false, MinVersion = "2.0")] internal Realm Realm { get; set; } /// /// Gets or sets a value indicating whether the return_to value should be signed. /// internal bool SignReturnTo { get; set; } /// /// Checks the message state for conformity to the protocol specification /// and throws an exception if the message is invalid. /// /// /// Some messages have required fields, or combinations of fields that must relate to each other /// in specialized ways. After deserializing a message, this method checks the state of the /// message to see if it conforms to the protocol. /// Note that this property should not check signatures or perform any state checks /// outside this scope of this particular message. /// /// Thrown if the message is invalid. public override void EnsureValidMessage() { base.EnsureValidMessage(); if (this.Realm == null) { // Set the default Realm per the spec if it is not explicitly given. this.Realm = this.ReturnTo; } else if (this.ReturnTo != null) { // Verify that the realm and return_to agree. ErrorUtilities.VerifyProtocol(this.Realm.Contains(this.ReturnTo), OpenIdStrings.ReturnToNotUnderRealm, this.ReturnTo, this.Realm); } } /// /// Adds parameters to the return_to querystring. /// /// The keys=value pairs to add to the return_to query string. /// /// This method is useful if the Relying Party wants to recall some value /// when and if a positive assertion comes back from the Provider. /// internal void AddReturnToArguments(IEnumerable> keysValues) { Requires.NotNull(keysValues, "keysValues"); ErrorUtilities.VerifyOperation(this.ReturnTo != null, OpenIdStrings.ReturnToRequiredForOperation); UriBuilder returnToBuilder = new UriBuilder(this.ReturnTo); returnToBuilder.AppendAndReplaceQueryArgs(keysValues); this.ReturnTo = returnToBuilder.Uri; } /// /// Adds a parameter to the return_to querystring. /// /// The name of the parameter. /// The value of the argument. /// /// This method is useful if the Relying Party wants to recall some value /// when and if a positive assertion comes back from the Provider. /// internal void AddReturnToArguments(string key, string value) { var pair = new KeyValuePair(key, value); this.AddReturnToArguments(new[] { pair }); } /// /// Gets the value of the openid.mode parameter based on the protocol version and immediate flag. /// /// The OpenID version to use. /// /// for asynchronous javascript clients; /// to allow the Provider to interact with the user in order to complete authentication. /// /// checkid_immediate or checkid_setup private static string GetMode(Version version, AuthenticationRequestMode mode) { Requires.NotNull(version, "version"); Protocol protocol = Protocol.Lookup(version); return mode == AuthenticationRequestMode.Immediate ? protocol.Args.Mode.checkid_immediate : protocol.Args.Mode.checkid_setup; } } }