//-----------------------------------------------------------------------
//
// Copyright (c) Andrew Arnott. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.OAuth2.ChannelElements {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OAuth2.Messages;
///
/// The base messaging channel used by OAuth 2.0 parties.
///
internal abstract class OAuth2ChannelBase : StandardMessageFactoryChannel {
///
/// The messages receivable by this channel.
///
private static readonly Type[] MessageTypes = new Type[] {
typeof(AccessTokenRefreshRequest),
typeof(AccessTokenAuthorizationCodeRequest),
typeof(AccessTokenResourceOwnerPasswordCredentialsRequest),
typeof(AccessTokenClientCredentialsRequest),
typeof(AccessTokenSuccessResponse),
typeof(AccessTokenFailedResponse),
typeof(EndUserAuthorizationRequest),
typeof(EndUserAuthorizationSuccessAuthCodeResponse),
typeof(EndUserAuthorizationSuccessAccessTokenResponse),
typeof(EndUserAuthorizationFailedResponse),
typeof(UnauthorizedResponse),
};
///
/// The protocol versions supported by this channel.
///
private static readonly Version[] Versions = Protocol.AllVersions.Select(v => v.Version).ToArray();
///
/// Initializes a new instance of the class.
///
/// The channel binding elements.
internal OAuth2ChannelBase(params IChannelBindingElement[] channelBindingElements)
: base(MessageTypes, Versions, channelBindingElements) {
}
///
/// Allows preprocessing and validation of message data before an appropriate message type is
/// selected or deserialized.
///
/// The received message data.
protected override void FilterReceivedFields(IDictionary fields) {
base.FilterReceivedFields(fields);
// Apply the OAuth 2.0 section 2.1 requirement:
// Parameters sent without a value MUST be treated as if they were omitted from the request.
// The authorization server SHOULD ignore unrecognized request parameters.
var emptyKeys = from pair in fields
where String.IsNullOrEmpty(pair.Value)
select pair.Key;
foreach (string emptyKey in emptyKeys.ToList()) {
fields.Remove(emptyKey);
}
}
}
}