summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.OAuth2.Client/OAuth2/ClientCredentialApplicator.cs
blob: a95f3d023fcd25f5509945f4621bd5bf3f86fa1a (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
113
114
115
116
117
118
119
120
//-----------------------------------------------------------------------
// <copyright file="ClientCredentialApplicator.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.OAuth2 {
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Net;
	using System.Text;
	using DotNetOpenAuth.Messaging;
	using DotNetOpenAuth.OAuth2.Messages;

	/// <summary>
	/// A base class for extensions that apply client authentication to messages for the authorization server in specific ways.
	/// </summary>
	public abstract class ClientCredentialApplicator {
		/// <summary>
		/// Initializes a new instance of the <see cref="ClientCredentialApplicator"/> class.
		/// </summary>
		protected ClientCredentialApplicator() {
		}

		/// <summary>
		/// Transmits the secret the client shares with the authorization server as a parameter in the POST entity payload.
		/// </summary>
		/// <param name="clientSecret">The secret the client shares with the authorization server.</param>
		/// <returns>The credential applicator to provide to the <see cref="ClientBase"/> instance.</returns>
		public static ClientCredentialApplicator PostParameter(string clientSecret) {
			Requires.NotNullOrEmpty(clientSecret, "clientSecret");
			return new PostParameterApplicator(clientSecret);
		}

		/// <summary>
		/// Transmits the client identifier and secret in the HTTP Authorization header via HTTP Basic authentication.
		/// </summary>
		/// <param name="clientSecret">The secret the client shares with the authorization server.</param>
		/// <returns>The credential applicator to provide to the <see cref="ClientBase"/> instance.</returns>
		public static ClientCredentialApplicator HttpBasic(string clientSecret) {
			Requires.NotNullOrEmpty(clientSecret, "clientSecret");
			return new HttpBasicApplicator(clientSecret);
		}

		/// <summary>
		/// Never transmits a secret.  Useful for anonymous clients or clients unable to keep a secret.
		/// </summary>
		/// <returns>The credential applicator to provide to the <see cref="ClientBase"/> instance.</returns>
		public static ClientCredentialApplicator NoSecret() {
			return null;
		}

		/// <summary>
		/// Applies the client identifier and (when applicable) the client authentication to an outbound message.
		/// </summary>
		/// <param name="clientIdentifier">The identifier by which the authorization server should recognize this client.</param>
		/// <param name="request">The outbound message to apply authentication information to.</param>
		public abstract void ApplyClientCredential(string clientIdentifier, AuthenticatedClientRequestBase request);

		/// <summary>
		/// Authenticates the client via HTTP Basic.
		/// </summary>
		private class HttpBasicApplicator : ClientCredentialApplicator {
			/// <summary>
			/// The client secret.
			/// </summary>
			private readonly string clientSecret;

			/// <summary>
			/// Initializes a new instance of the <see cref="HttpBasicApplicator"/> class.
			/// </summary>
			/// <param name="clientSecret">The client secret.</param>
			internal HttpBasicApplicator(string clientSecret) {
				Requires.NotNullOrEmpty(clientSecret, "clientSecret");
				this.clientSecret = clientSecret;
			}

			/// <summary>
			/// Applies the client identifier and (when applicable) the client authentication to an outbound message.
			/// </summary>
			/// <param name="clientIdentifier">The identifier by which the authorization server should recognize this client.</param>
			/// <param name="request">The outbound message to apply authentication information to.</param>
			public override void ApplyClientCredential(string clientIdentifier, AuthenticatedClientRequestBase request) {
				// When using network credentials, the client authentication is not done as standard message parts.
				request.ClientIdentifier = null;
				request.ClientSecret = null;
				OAuthUtilities.ApplyHttpBasicAuth(request.Headers, clientIdentifier, this.clientSecret);
			}
		}

		/// <summary>
		/// Authenticates the client via a client_secret parameter in the message.
		/// </summary>
		private class PostParameterApplicator : ClientCredentialApplicator {
			/// <summary>
			/// The client secret.
			/// </summary>
			private readonly string secret;

			/// <summary>
			/// Initializes a new instance of the <see cref="PostParameterApplicator"/> class.
			/// </summary>
			/// <param name="clientSecret">The client secret.</param>
			internal PostParameterApplicator(string clientSecret) {
				Requires.NotNullOrEmpty(clientSecret, "clientSecret");
				this.secret = clientSecret;
			}

			/// <summary>
			/// Applies the client identifier and (when applicable) the client authentication to an outbound message.
			/// </summary>
			/// <param name="clientIdentifier">The identifier by which the authorization server should recognize this client.</param>
			/// <param name="request">The outbound message to apply authentication information to.</param>
			public override void ApplyClientCredential(string clientIdentifier, AuthenticatedClientRequestBase request) {
				request.ClientSecret = this.secret;
			}
		}
	}
}