diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2008-09-28 15:04:04 -0700 |
---|---|---|
committer | Andrew <andrewarnott@gmail.com> | 2008-09-28 15:04:04 -0700 |
commit | ad79dec82e43910db29d1a259155f2cf3f86e731 (patch) | |
tree | 34fc5eab2d77fdd3e6c2aa48f2d056c5e6ba6ff5 /src/DotNetOAuth/Messaging | |
parent | 92ec674bd52330d4847f75150cd34263336219b6 (diff) | |
download | DotNetOpenAuth-ad79dec82e43910db29d1a259155f2cf3f86e731.zip DotNetOpenAuth-ad79dec82e43910db29d1a259155f2cf3f86e731.tar.gz DotNetOpenAuth-ad79dec82e43910db29d1a259155f2cf3f86e731.tar.bz2 |
Appendix scenario test passing (again), this time with HMAC signing of HTTP requests.
Diffstat (limited to 'src/DotNetOAuth/Messaging')
-rw-r--r-- | src/DotNetOAuth/Messaging/Channel.cs | 7 | ||||
-rw-r--r-- | src/DotNetOAuth/Messaging/HttpRequestInfo.cs | 4 | ||||
-rw-r--r-- | src/DotNetOAuth/Messaging/MessageReceivingEndpoint.cs | 49 | ||||
-rw-r--r-- | src/DotNetOAuth/Messaging/MessageSerializer.cs | 50 | ||||
-rw-r--r-- | src/DotNetOAuth/Messaging/MessagingUtilities.cs | 9 |
5 files changed, 106 insertions, 13 deletions
diff --git a/src/DotNetOAuth/Messaging/Channel.cs b/src/DotNetOAuth/Messaging/Channel.cs index fdca618..63ee1cc 100644 --- a/src/DotNetOAuth/Messaging/Channel.cs +++ b/src/DotNetOAuth/Messaging/Channel.cs @@ -327,15 +327,16 @@ namespace DotNetOAuth.Messaging { fields = request.QueryString.ToDictionary();
}
- return this.Receive(fields);
+ return this.Receive(fields, request.GetRecipient());
}
/// <summary>
/// Deserializes a dictionary of values into a message.
/// </summary>
/// <param name="fields">The dictionary of values that were read from an HTTP request or response.</param>
+ /// <param name="recipient">Information about where the message was been directed. Null for direct response messages.</param>
/// <returns>The deserialized message, or null if no message could be recognized in the provided data.</returns>
- protected virtual IProtocolMessage Receive(Dictionary<string, string> fields) {
+ protected virtual IProtocolMessage Receive(Dictionary<string, string> fields, MessageReceivingEndpoint recipient) {
if (fields == null) {
throw new ArgumentNullException("fields");
}
@@ -349,7 +350,7 @@ namespace DotNetOAuth.Messaging { // We have a message! Assemble it.
var serializer = MessageSerializer.Get(messageType);
- IProtocolMessage message = serializer.Deserialize(fields);
+ IProtocolMessage message = serializer.Deserialize(fields, recipient);
return message;
}
diff --git a/src/DotNetOAuth/Messaging/HttpRequestInfo.cs b/src/DotNetOAuth/Messaging/HttpRequestInfo.cs index d9d0c72..f095a6f 100644 --- a/src/DotNetOAuth/Messaging/HttpRequestInfo.cs +++ b/src/DotNetOAuth/Messaging/HttpRequestInfo.cs @@ -75,9 +75,9 @@ namespace DotNetOAuth.Messaging { }
/// <summary>
- /// Gets the message that is being sent over a mock transport (for testing).
+ /// Gets or sets the message that is being sent over a mock transport (for testing).
/// </summary>
- internal IProtocolMessage Message { get; private set; }
+ internal IProtocolMessage Message { get; set; }
/// <summary>
/// Gets or sets the verb in the request (i.e. GET, POST, etc.)
diff --git a/src/DotNetOAuth/Messaging/MessageReceivingEndpoint.cs b/src/DotNetOAuth/Messaging/MessageReceivingEndpoint.cs new file mode 100644 index 0000000..9ad749b --- /dev/null +++ b/src/DotNetOAuth/Messaging/MessageReceivingEndpoint.cs @@ -0,0 +1,49 @@ +//-----------------------------------------------------------------------
+// <copyright file="MessageReceivingEndpoint.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOAuth.Messaging {
+ using System;
+
+ /// <summary>
+ /// An immutable description of a URL that receives messages.
+ /// </summary>
+ public class MessageReceivingEndpoint {
+ /// <summary>
+ /// Initializes a new instance of the <see cref="MessageReceivingEndpoint"/> class.
+ /// </summary>
+ /// <param name="locationUri">The URL of this endpoint.</param>
+ /// <param name="method">The HTTP method(s) allowed.</param>
+ public MessageReceivingEndpoint(string locationUri, HttpDeliveryMethod method)
+ : this(new Uri(locationUri), method) { }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="MessageReceivingEndpoint"/> class.
+ /// </summary>
+ /// <param name="location">The URL of this endpoint.</param>
+ /// <param name="method">The HTTP method(s) allowed.</param>
+ public MessageReceivingEndpoint(Uri location, HttpDeliveryMethod method) {
+ if (location == null) {
+ throw new ArgumentNullException("location");
+ }
+ if (method == HttpDeliveryMethod.None) {
+ throw new ArgumentOutOfRangeException("method");
+ }
+
+ this.Location = location;
+ this.AllowedMethods = method;
+ }
+
+ /// <summary>
+ /// Gets the URL of this endpoint.
+ /// </summary>
+ public Uri Location { get; private set; }
+
+ /// <summary>
+ /// Gets the HTTP method(s) allowed.
+ /// </summary>
+ public HttpDeliveryMethod AllowedMethods { get; private set; }
+ }
+}
diff --git a/src/DotNetOAuth/Messaging/MessageSerializer.cs b/src/DotNetOAuth/Messaging/MessageSerializer.cs index 0e1e491..fd42108 100644 --- a/src/DotNetOAuth/Messaging/MessageSerializer.cs +++ b/src/DotNetOAuth/Messaging/MessageSerializer.cs @@ -9,6 +9,8 @@ namespace DotNetOAuth.Messaging { using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
+ using System.Reflection;
+ using DotNetOAuth.ChannelElements;
using DotNetOAuth.Messaging.Reflection;
/// <summary>
@@ -75,8 +77,9 @@ namespace DotNetOAuth.Messaging { /// Reads name=value pairs into an OAuth message.
/// </summary>
/// <param name="fields">The name=value pairs that were read in from the transport.</param>
+ /// <param name="recipient">The recipient of the message.</param>
/// <returns>The instantiated and initialized <see cref="IProtocolMessage"/> instance.</returns>
- internal IProtocolMessage Deserialize(IDictionary<string, string> fields) {
+ internal IProtocolMessage Deserialize(IDictionary<string, string> fields, MessageReceivingEndpoint recipient) {
if (fields == null) {
throw new ArgumentNullException("fields");
}
@@ -84,18 +87,49 @@ namespace DotNetOAuth.Messaging { // Before we deserialize the message, make sure all the required parts are present.
MessageDescription.Get(this.messageType).EnsureRequiredMessagePartsArePresent(fields.Keys);
- IProtocolMessage result;
- try {
- result = (IProtocolMessage)Activator.CreateInstance(this.messageType, true);
- } catch (MissingMethodException ex) {
- throw new ProtocolException("Failed to instantiate type " + this.messageType.FullName, ex);
- }
+ IProtocolMessage result = this.CreateMessage(recipient);
foreach (var pair in fields) {
IDictionary<string, string> dictionary = new MessageDictionary(result);
- dictionary.Add(pair);
+ dictionary[pair.Key] = pair.Value;
}
result.EnsureValidMessage();
return result;
}
+
+ /// <summary>
+ /// Instantiates a new message to deserialize data into.
+ /// </summary>
+ /// <param name="recipient">The recipient this message is directed to, if any.</param>
+ /// <returns>The newly created message object.</returns>
+ private IProtocolMessage CreateMessage(MessageReceivingEndpoint recipient) {
+ IProtocolMessage result;
+ if (typeof(IOAuthDirectedMessage).IsAssignableFrom(this.messageType)) {
+ // Some OAuth messages take just the recipient, while others take the whole endpoint
+ ConstructorInfo ctor;
+ if ((ctor = this.messageType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(Uri) }, null)) != null) {
+ if (recipient == null) {
+ // We need a recipient to deserialize directed messages.
+ throw new ArgumentNullException("recipient");
+ }
+
+ result = (IProtocolMessage)ctor.Invoke(new object[] { recipient.Location });
+ } else if ((ctor = this.messageType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(MessageReceivingEndpoint) }, null)) != null) {
+ if (recipient == null) {
+ // We need a recipient to deserialize directed messages.
+ throw new ArgumentNullException("recipient");
+ }
+
+ result = (IProtocolMessage)ctor.Invoke(new object[] { recipient });
+ } else if ((ctor = this.messageType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[0], null)) != null) {
+ result = (IProtocolMessage)ctor.Invoke(new object[0]);
+ } else {
+ throw new InvalidOperationException("Unrecognized constructor signature on type " + this.messageType);
+ }
+ } else {
+ result = (IProtocolMessage)Activator.CreateInstance(this.messageType, true);
+ }
+
+ return result;
+ }
}
}
diff --git a/src/DotNetOAuth/Messaging/MessagingUtilities.cs b/src/DotNetOAuth/Messaging/MessagingUtilities.cs index 88aea0c..7793276 100644 --- a/src/DotNetOAuth/Messaging/MessagingUtilities.cs +++ b/src/DotNetOAuth/Messaging/MessagingUtilities.cs @@ -131,6 +131,15 @@ namespace DotNetOAuth.Messaging { }
/// <summary>
+ /// Extracts the recipient from an HttpRequestInfo.
+ /// </summary>
+ /// <param name="request">The request to get recipient information from.</param>
+ /// <returns>The recipient.</returns>
+ internal static MessageReceivingEndpoint GetRecipient(this HttpRequestInfo request) {
+ return new MessageReceivingEndpoint(request.Url, request.HttpMethod == "GET" ? HttpDeliveryMethod.GetRequest : HttpDeliveryMethod.PostRequest);
+ }
+
+ /// <summary>
/// Converts a <see cref="NameValueCollection"/> to an IDictionary<string, string>.
/// </summary>
/// <param name="nvc">The NameValueCollection to convert. May be null.</param>
|