Added strong-typed request token message to sample.
[dotnetoauth.git] / src / DotNetOAuth / ChannelElements / OAuthConsumerMessageTypeProvider.cs
blobaf17527ed5e94ddd7b79b897c85f9b4646a71738
1 //-----------------------------------------------------------------------
2 // <copyright file="OAuthConsumerMessageTypeProvider.cs" company="Andrew Arnott">
3 // Copyright (c) Andrew Arnott. All rights reserved.
4 // </copyright>
5 //-----------------------------------------------------------------------
7 namespace DotNetOAuth.ChannelElements {
8 using System;
9 using System.Collections.Generic;
10 using DotNetOAuth.Messages;
11 using DotNetOAuth.Messaging;
13 /// <summary>
14 /// An OAuth-protocol specific implementation of the <see cref="IMessageTypeProvider"/>
15 /// interface.
16 /// </summary>
17 public class OAuthConsumerMessageTypeProvider : IMessageTypeProvider {
18 /// <summary>
19 /// The token manager to use for discerning between request and access tokens.
20 /// </summary>
21 private ITokenManager tokenManager;
23 /// <summary>
24 /// Initializes a new instance of the <see cref="OAuthConsumerMessageTypeProvider"/> class.
25 /// </summary>
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
37 /// <summary>
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.
40 /// </summary>
41 /// <param name="fields">The name/value pairs that make up the message payload.</param>
42 /// <remarks>
43 /// The request messages are:
44 /// DirectUserToConsumerMessage
45 /// </remarks>
46 /// <returns>
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.
49 /// </returns>
50 public virtual Type GetRequestMessageType(IDictionary<string, string> fields) {
51 if (fields == null) {
52 throw new ArgumentNullException("fields");
55 if (fields.ContainsKey("oauth_token")) {
56 return typeof(DirectUserToConsumerMessage);
59 return null;
62 /// <summary>
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.
65 /// </summary>
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.
69 /// </param>
70 /// <param name="fields">The name/value pairs that make up the message payload.</param>
71 /// <returns>
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.
74 /// </returns>
75 /// <remarks>
76 /// The response messages are:
77 /// UnauthorizedRequestTokenMessage
78 /// GrantAccessTokenMessage
79 /// </remarks>
80 public virtual Type GetResponseMessageType(IProtocolMessage request, IDictionary<string, string> fields) {
81 if (fields == null) {
82 throw new ArgumentNullException("fields");
85 // All response messages have the oauth_token field.
86 if (!fields.ContainsKey("oauth_token")) {
87 return null;
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.");
93 return null;
96 if (request is RequestTokenMessage) {
97 return typeof(UnauthorizedRequestTokenMessage);
98 } else if (request is RequestAccessTokenMessage) {
99 return typeof(GrantAccessTokenMessage);
100 } else {
101 Logger.ErrorFormat("Unexpected response message given the request type {0}", request.GetType().Name);
102 throw new ProtocolException(Strings.InvalidIncomingMessage);
106 #endregion