1
//-----------------------------------------------------------------------
2 // <copyright file="OAuthConsumerMessageTypeProvider.cs" company="Andrew Arnott">
3 // Copyright (c) Andrew Arnott. All rights reserved.
5 //-----------------------------------------------------------------------
7 namespace DotNetOAuth
.ChannelElements
{
9 using System
.Collections
.Generic
;
10 using DotNetOAuth
.Messages
;
11 using DotNetOAuth
.Messaging
;
14 /// An OAuth-protocol specific implementation of the <see cref="IMessageTypeProvider"/>
17 public class OAuthConsumerMessageTypeProvider
: IMessageTypeProvider
{
19 /// The token manager to use for discerning between request and access tokens.
21 private ITokenManager tokenManager
;
24 /// Initializes a new instance of the <see cref="OAuthConsumerMessageTypeProvider"/> class.
26 /// <param name="tokenManager">The token manager instance to use.</param>
27 protected internal OAuthConsumerMessageTypeProvider(ITokenManager tokenManager
) {
28 if (tokenManager
== null) {
29 throw new ArgumentNullException("tokenManager");
32 this.tokenManager
= tokenManager
;
35 #region IMessageTypeProvider Members
38 /// Analyzes an incoming request message payload to discover what kind of
39 /// message is embedded in it and returns the type, or null if no match is found.
41 /// <param name="fields">The name/value pairs that make up the message payload.</param>
43 /// The request messages are:
44 /// DirectUserToConsumerMessage
47 /// The <see cref="IProtocolMessage"/>-derived concrete class that this message can
48 /// deserialize to. Null if the request isn't recognized as a valid protocol message.
50 public virtual Type
GetRequestMessageType(IDictionary
<string, string> fields
) {
52 throw new ArgumentNullException("fields");
55 if (fields
.ContainsKey("oauth_token")) {
56 return typeof(DirectUserToConsumerMessage
);
63 /// Analyzes an incoming request message payload to discover what kind of
64 /// message is embedded in it and returns the type, or null if no match is found.
66 /// <param name="request">
67 /// The message that was sent as a request that resulted in the response.
68 /// Null on a Consumer site that is receiving an indirect message from the Service Provider.
70 /// <param name="fields">The name/value pairs that make up the message payload.</param>
72 /// The <see cref="IProtocolMessage"/>-derived concrete class that this message can
73 /// deserialize to. Null if the request isn't recognized as a valid protocol message.
76 /// The response messages are:
77 /// UnauthorizedRequestTokenMessage
78 /// GrantAccessTokenMessage
80 public virtual Type
GetResponseMessageType(IProtocolMessage request
, IDictionary
<string, string> fields
) {
82 throw new ArgumentNullException("fields");
85 // All response messages have the oauth_token field.
86 if (!fields
.ContainsKey("oauth_token")) {
90 // All direct message responses should have the oauth_token_secret field.
91 if (!fields
.ContainsKey("oauth_token_secret")) {
92 Logger
.Error("An OAuth message was expected to contain an oauth_token_secret but didn't.");
96 if (request
is RequestTokenMessage
) {
97 return typeof(UnauthorizedRequestTokenMessage
);
98 } else if (request
is RequestAccessTokenMessage
) {
99 return typeof(GrantAccessTokenMessage
);
101 Logger
.ErrorFormat("Unexpected response message given the request type {0}", request
.GetType().Name
);
102 throw new ProtocolException(Strings
.InvalidIncomingMessage
);