//-----------------------------------------------------------------------
//
// Copyright (c) Andrew Arnott. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.OAuth2.ChannelElements {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OAuth2.Messages;
using Validation;
///
/// A base class for extensions that can read incoming messages and extract the client identifier and
/// possibly authentication information (like a shared secret, signed nonce, etc.)
///
public abstract class ClientAuthenticationModule {
///
/// Initializes a new instance of the class.
///
protected ClientAuthenticationModule() {
}
///
/// Gets this module's contribution to an HTTP 401 WWW-Authenticate header so the client knows what kind of authentication this module supports.
///
public virtual string AuthenticateHeader {
get { return null; }
}
///
/// Attempts to extract client identification/authentication information from a message.
///
/// The authorization server host.
/// The incoming message.
/// Receives the client identifier, if one was found.
/// The level of the extracted client information.
public abstract ClientAuthenticationResult TryAuthenticateClient(IAuthorizationServerHost authorizationServerHost, AuthenticatedClientRequestBase requestMessage, out string clientIdentifier);
///
/// Validates a client identifier and shared secret against the authoriation server's database.
///
/// The authorization server host; cannot be null.
/// The alleged client identifier.
/// The alleged client secret to be verified.
/// An indication as to the outcome of the validation.
protected static ClientAuthenticationResult TryAuthenticateClientBySecret(IAuthorizationServerHost authorizationServerHost, string clientIdentifier, string clientSecret) {
Requires.NotNull(authorizationServerHost, "authorizationServerHost");
if (!string.IsNullOrEmpty(clientIdentifier)) {
var client = authorizationServerHost.GetClient(clientIdentifier);
if (client != null) {
if (!string.IsNullOrEmpty(clientSecret)) {
if (client.IsValidClientSecret(clientSecret)) {
return ClientAuthenticationResult.ClientAuthenticated;
} else { // invalid client secret
return ClientAuthenticationResult.ClientAuthenticationRejected;
}
} else { // no client secret provided
return ClientAuthenticationResult.ClientIdNotAuthenticated;
}
} else { // The client identifier is not recognized.
return ClientAuthenticationResult.ClientAuthenticationRejected;
}
} else { // no client id provided.
return ClientAuthenticationResult.NoAuthenticationRecognized;
}
}
}
}