//-----------------------------------------------------------------------
//
// Copyright (c) Outercurve Foundation. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.OAuth2.ChannelElements {
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Net.Mime;
using System.Web;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OAuth2.AuthServer.Messages;
using DotNetOpenAuth.OAuth2.Messages;
///
/// The channel for the OAuth protocol.
///
internal class OAuth2AuthorizationServerChannel : OAuth2ChannelBase, IOAuth2ChannelWithAuthorizationServer {
///
/// The messages receivable by this channel.
///
private static readonly Type[] MessageTypes = new Type[] {
typeof(AccessTokenRefreshRequestAS),
typeof(AccessTokenAuthorizationCodeRequestAS),
typeof(AccessTokenResourceOwnerPasswordCredentialsRequest),
typeof(AccessTokenClientCredentialsRequest),
typeof(EndUserAuthorizationRequest),
typeof(EndUserAuthorizationImplicitRequest),
typeof(EndUserAuthorizationFailedResponse),
};
///
/// Initializes a new instance of the class.
///
/// The authorization server.
protected internal OAuth2AuthorizationServerChannel(IAuthorizationServer authorizationServer)
: base(MessageTypes, InitializeBindingElements(authorizationServer)) {
Requires.NotNull(authorizationServer, "authorizationServer");
this.AuthorizationServer = authorizationServer;
}
///
/// Gets the authorization server.
///
/// The authorization server.
public IAuthorizationServer AuthorizationServer { get; private set; }
///
/// Gets the protocol message that may be in the given HTTP response.
///
/// The response that is anticipated to contain an protocol message.
///
/// The deserialized message parts, if found. Null otherwise.
///
/// Thrown when the response is not valid.
protected override IDictionary ReadFromResponseCore(IncomingWebResponse response) {
throw new NotImplementedException();
}
///
/// Queues a message for sending in the response stream.
///
/// The message to send as a response.
///
/// The pending user agent redirect based message to be sent as an HttpResponse.
///
///
/// This method implements spec OAuth V1.0 section 5.3.
///
protected override OutgoingWebResponse PrepareDirectResponse(IProtocolMessage response) {
var webResponse = new OutgoingWebResponse();
ApplyMessageTemplate(response, webResponse);
string json = this.SerializeAsJson(response);
webResponse.SetResponse(json, new ContentType(JsonEncoded));
return webResponse;
}
///
/// Gets the protocol message that may be embedded in the given HTTP request.
///
/// The request to search for an embedded message.
///
/// The deserialized message, if one is found. Null otherwise.
///
protected override IDirectedProtocolMessage ReadFromRequestCore(HttpRequestBase request) {
if (!string.IsNullOrEmpty(request.Url.Fragment)) {
var fields = HttpUtility.ParseQueryString(request.Url.Fragment.Substring(1)).ToDictionary();
MessageReceivingEndpoint recipient;
try {
recipient = request.GetRecipient();
} catch (ArgumentException ex) {
Logger.Messaging.WarnFormat("Unrecognized HTTP request: " + ex.ToString());
return null;
}
return (IDirectedProtocolMessage)this.Receive(fields, recipient);
}
return base.ReadFromRequestCore(request);
}
///
/// Initializes the binding elements for the OAuth channel.
///
/// The authorization server.
///
/// An array of binding elements used to initialize the channel.
///
private static IChannelBindingElement[] InitializeBindingElements(IAuthorizationServer authorizationServer) {
Requires.NotNull(authorizationServer, "authorizationServer");
var bindingElements = new List();
bindingElements.Add(new IncomingMessageValidationBindingElement());
bindingElements.Add(new AuthorizationCodeBindingElement());
bindingElements.Add(new AccessTokenBindingElement());
bindingElements.Add(new AccessRequestBindingElement());
return bindingElements.ToArray();
}
}
}