summaryrefslogtreecommitdiffstats
path: root/src/DotNetOAuth/OAuthMessageTypeProvider.cs
blob: 8e572d22206605faf50feffd2c87057d71f59197 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//-----------------------------------------------------------------------
// <copyright file="OAuthMessageTypeProvider.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOAuth {
	using System;
	using System.Collections.Generic;
	using DotNetOAuth.Messages;
	using DotNetOAuth.Messaging;

	/// <summary>
	/// An OAuth-protocol specific implementation of the <see cref="IMessageTypeProvider"/>
	/// interface.
	/// </summary>
	internal class OAuthMessageTypeProvider : IMessageTypeProvider {
		#region IMessageTypeProvider Members

		/// <summary>
		/// Analyzes an incoming request message payload to discover what kind of 
		/// message is embedded in it and returns the type, or null if no match is found.
		/// </summary>
		/// <param name="fields">The name/value pairs that make up the message payload.</param>
		/// <remarks>
		/// The request messages are:
		/// RequestTokenMessage
		/// RequestAccessTokenMessage
		/// DirectUserToServiceProviderMessage
		/// AccessProtectedResourcesMessage
		/// </remarks>
		/// <returns>
		/// The <see cref="IProtocolMessage"/>-derived concrete class that this message can
		/// deserialize to.  Null if the request isn't recognized as a valid protocol message.
		/// </returns>
		public Type GetRequestMessageType(IDictionary<string, string> fields) {
			if (fields == null) {
				throw new ArgumentNullException("fields");
			}

			if (fields.ContainsKey("oauth_consumer_key") &&
				!fields.ContainsKey("oauth_token")) {
				return typeof(RequestTokenMessage);
			}

			if (fields.ContainsKey("oauth_consumer_key") &&
				fields.ContainsKey("oauth_token")) {
				// Discern between RequestAccessToken and AccessProtectedResources,
				// which have all the same parameters, by figuring out what type of token
				// is in the token parameter.
				bool tokenTypeIsAccessToken = false; // TODO

				return tokenTypeIsAccessToken ? typeof(AccessProtectedResourcesMessage) :
					typeof(RequestAccessTokenMessage);
			}

			// fail over to the message with no required fields at all.
			return typeof(DirectUserToServiceProviderMessage);
		}

		/// <summary>
		/// Analyzes an incoming request message payload to discover what kind of 
		/// message is embedded in it and returns the type, or null if no match is found.
		/// </summary>
		/// <param name="request">
		/// The message that was sent as a request that resulted in the response.
		/// Null on a Consumer site that is receiving an indirect message from the Service Provider.
		/// </param>
		/// <param name="fields">The name/value pairs that make up the message payload.</param>
		/// <returns>
		/// The <see cref="IProtocolMessage"/>-derived concrete class that this message can
		/// deserialize to.  Null if the request isn't recognized as a valid protocol message.
		/// </returns>
		/// <remarks>
		/// The response messages are:
		/// UnauthorizedRequestTookenMessage
		/// DirectUserToConsumerMessage
		/// GrantAccessTokenMessage
		/// </remarks>
		public Type GetResponseMessageType(IProtocolMessage request, IDictionary<string, string> fields) {
			if (fields == null) {
				throw new ArgumentNullException("fields");
			}

			// All response messages have the oauth_token field.
			if (!fields.ContainsKey("oauth_token")) {
				return null;
			}

			if (request == null) {
				return typeof(DirectUserToConsumerMessage);
			}

			// All direct message responses should haev the oauth_token_secret field.
			if (!fields.ContainsKey("oauth_token_secret")) {
				Logger.Error("An OAuth message was expected to contain an oauth_token_secret but didn't.");
				return null;
			}

			if (request is RequestTokenMessage) {
				return typeof(UnauthorizedRequestTokenMessage);
			} else if (request is RequestAccessTokenMessage) {
				return typeof(GrantAccessTokenMessage);
			} else {
				Logger.ErrorFormat("Unexpected response message given the request type {0}", request.GetType().Name);
				throw new ProtocolException(Strings.InvalidIncomingMessage);
			}
		}

		#endregion
	}
}