//----------------------------------------------------------------------- // // Copyright (c) Outercurve Foundation. All rights reserved. // //----------------------------------------------------------------------- namespace DotNetOpenAuth.OAuth2 { using System; using System.Collections.Generic; using System.Linq; using System.Text; using DotNetOpenAuth.Messaging; /// /// A default implementation of the interface. /// public class ClientDescription : IClientDescription { /// /// A delegate that determines whether the callback is allowed. /// private readonly Func isCallbackAllowed; /// /// The client's secret, if any. /// private readonly string secret; /// /// Initializes a new instance of the class. /// /// The secret. /// The default callback. /// Type of the client. /// A delegate that determines whether the callback is allowed. public ClientDescription(string secret, Uri defaultCallback, ClientType clientType, Func isCallbackAllowed = null) { this.secret = secret; this.DefaultCallback = defaultCallback; this.ClientType = clientType; this.isCallbackAllowed = isCallbackAllowed; } /// /// Gets the callback to use when an individual authorization request /// does not include an explicit callback URI. /// /// /// An absolute URL; or null if none is registered. /// public Uri DefaultCallback { get; private set; } /// /// Gets the type of the client. /// public ClientType ClientType { get; private set; } /// /// Gets a value indicating whether a non-empty secret is registered for this client. /// public bool HasNonEmptySecret { get { return !string.IsNullOrEmpty(this.secret); } } /// /// Determines whether a callback URI included in a client's authorization request /// is among those allowed callbacks for the registered client. /// /// The absolute URI the client has requested the authorization result be received at. /// /// true if the callback URL is allowable for this client; otherwise, false. /// public bool IsCallbackAllowed(Uri callback) { if (this.isCallbackAllowed != null) { return this.isCallbackAllowed(callback); } return EqualityComparer.Default.Equals(this.DefaultCallback, callback); } #region IClientDescription Members /// /// Checks whether the specified client secret is correct. /// /// The secret obtained from the client. /// true if the secret matches the one in the authorization server's record for the client; false otherwise. /// /// All string equality checks, whether checking secrets or their hashes, /// should be done using to mitigate timing attacks. /// public bool IsValidClientSecret(string secret) { Requires.NotNullOrEmpty(secret, "secret"); return MessagingUtilities.EqualsConstantTime(secret, this.secret); } #endregion } }