summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.OAuth2.AuthorizationServer/OAuth2/ChannelElements/MessageValidationBindingElement.cs
blob: ac2977bd4350dd7e61ca013a21bd1994648b8783 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//-----------------------------------------------------------------------
// <copyright file="MessageValidationBindingElement.cs" company="Outercurve Foundation">
//     Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.OAuth2.ChannelElements {
	using System;
	using System.Collections.Generic;
	using System.Globalization;
	using System.Linq;
	using System.Text;
	using System.Threading;
	using System.Threading.Tasks;
	using DotNetOpenAuth.OAuth2.Messages;
	using Messaging;
	using Validation;

	/// <summary>
	/// A guard for all messages to or from an Authorization Server to ensure that they are well formed,
	/// have valid secrets, callback URIs, etc.
	/// </summary>
	/// <remarks>
	/// This binding element also ensures that the code/token coming in is issued to
	/// the same client that is sending the code/token and that the authorization has
	/// not been revoked and that an access token has not expired.
	/// </remarks>
	internal class MessageValidationBindingElement : AuthServerBindingElementBase {
		/// <summary>
		/// The aggregating client authentication module.
		/// </summary>
		private readonly ClientAuthenticationModule clientAuthenticationModule;

		/// <summary>
		/// Initializes a new instance of the <see cref="MessageValidationBindingElement"/> class.
		/// </summary>
		/// <param name="clientAuthenticationModule">The aggregating client authentication module.</param>
		internal MessageValidationBindingElement(ClientAuthenticationModule clientAuthenticationModule) {
			Requires.NotNull(clientAuthenticationModule, "clientAuthenticationModule");
			this.clientAuthenticationModule = clientAuthenticationModule;
		}

		/// <summary>
		/// Gets the protection commonly offered (if any) by this binding element.
		/// </summary>
		/// <remarks>
		/// This value is used to assist in sorting binding elements in the channel stack.
		/// </remarks>
		public override MessageProtections Protection {
			get { return MessageProtections.None; }
		}

		/// <summary>
		/// Prepares a message for sending based on the rules of this channel binding element.
		/// </summary>
		/// <param name="message">The message to prepare for sending.</param>
		/// <returns>
		/// The protections (if any) that this binding element applied to the message.
		/// Null if this binding element did not even apply to this binding element.
		/// </returns>
		/// <remarks>
		/// Implementations that provide message protection must honor the
		/// <see cref="MessagePartAttribute.RequiredProtection"/> properties where applicable.
		/// </remarks>
		public override Task<MessageProtections?> ProcessOutgoingMessageAsync(IProtocolMessage message, CancellationToken cancellationToken) {
			var accessTokenResponse = message as AccessTokenSuccessResponse;
			if (accessTokenResponse != null) {
				var directResponseMessage = (IDirectResponseProtocolMessage)accessTokenResponse;
				var accessTokenRequest = (AccessTokenRequestBase)directResponseMessage.OriginatingRequest;
				ErrorUtilities.VerifyProtocol(accessTokenRequest.GrantType != GrantType.ClientCredentials || accessTokenResponse.RefreshToken == null, OAuthStrings.NoGrantNoRefreshToken);
			}

			return null;
		}

		/// <summary>
		/// Performs any transformation on an incoming message that may be necessary and/or
		/// validates an incoming message based on the rules of this channel binding element.
		/// </summary>
		/// <param name="message">The incoming message to process.</param>
		/// <returns>
		/// The protections (if any) that this binding element applied to the message.
		/// Null if this binding element did not even apply to this binding element.
		/// </returns>
		/// <exception cref="ProtocolException">
		/// Thrown when the binding element rules indicate that this message is invalid and should
		/// NOT be processed.
		/// </exception>
		/// <remarks>
		/// Implementations that provide message protection must honor the
		/// <see cref="MessagePartAttribute.RequiredProtection"/> properties where applicable.
		/// </remarks>
		public override async Task<MessageProtections?> ProcessIncomingMessageAsync(IProtocolMessage message, CancellationToken cancellationToken) {
			bool applied = false;

			// Check that the client secret is correct for client authenticated messages.
			var clientCredentialOnly = message as AccessTokenClientCredentialsRequest;
			var authenticatedClientRequest = message as AuthenticatedClientRequestBase;
			var accessTokenRequest = authenticatedClientRequest as AccessTokenRequestBase; // currently the only type of message.
			var resourceOwnerPasswordCarrier = message as AccessTokenResourceOwnerPasswordCredentialsRequest;
			if (authenticatedClientRequest != null) {
				string clientIdentifier;
				var result = this.clientAuthenticationModule.TryAuthenticateClient(this.AuthServerChannel.AuthorizationServer, authenticatedClientRequest, out clientIdentifier);
				switch (result) {
					case ClientAuthenticationResult.ClientAuthenticated:
						break;
					case ClientAuthenticationResult.NoAuthenticationRecognized:
					case ClientAuthenticationResult.ClientIdNotAuthenticated:
						// The only grant type that allows no client credentials is the resource owner credentials grant.
						AuthServerUtilities.TokenEndpointVerify(resourceOwnerPasswordCarrier != null, accessTokenRequest, Protocol.AccessTokenRequestErrorCodes.InvalidClient, this.clientAuthenticationModule, AuthServerStrings.ClientSecretMismatch);
						break;
					default:
						AuthServerUtilities.TokenEndpointVerify(false, accessTokenRequest, Protocol.AccessTokenRequestErrorCodes.InvalidClient, this.clientAuthenticationModule, AuthServerStrings.ClientSecretMismatch);
						break;
				}

				authenticatedClientRequest.ClientIdentifier = result == ClientAuthenticationResult.NoAuthenticationRecognized ? null : clientIdentifier;
				accessTokenRequest.ClientAuthenticated = result == ClientAuthenticationResult.ClientAuthenticated;
				applied = true;
			}

			// Check that any resource owner password credential is correct.
			if (resourceOwnerPasswordCarrier != null) {
				try {
					var authorizeResult =
						this.AuthorizationServer.CheckAuthorizeResourceOwnerCredentialGrant(
							resourceOwnerPasswordCarrier.RequestingUserName, resourceOwnerPasswordCarrier.Password, resourceOwnerPasswordCarrier);
					if (authorizeResult.IsApproved) {
						resourceOwnerPasswordCarrier.CredentialsValidated = true;
						resourceOwnerPasswordCarrier.RequestingUserName = authorizeResult.CanonicalUserName;
						resourceOwnerPasswordCarrier.Scope.ResetContents(authorizeResult.ApprovedScope);
					} else {
						Logger.OAuth.ErrorFormat(
							"Resource owner password credential for user \"{0}\" rejected by authorization server host.",
							resourceOwnerPasswordCarrier.RequestingUserName);
						throw new TokenEndpointProtocolException(accessTokenRequest, Protocol.AccessTokenRequestErrorCodes.InvalidGrant, AuthServerStrings.InvalidResourceOwnerPasswordCredential);
					}
				} catch (NotSupportedException) {
					throw new TokenEndpointProtocolException(accessTokenRequest, Protocol.AccessTokenRequestErrorCodes.UnsupportedGrantType);
				} catch (NotImplementedException) {
					throw new TokenEndpointProtocolException(accessTokenRequest, Protocol.AccessTokenRequestErrorCodes.UnsupportedGrantType);
				}

				applied = true;
			} else if (clientCredentialOnly != null) {
				try {
					var authorizeResult = this.AuthorizationServer.CheckAuthorizeClientCredentialsGrant(clientCredentialOnly);
					if (!authorizeResult.IsApproved) {
						Logger.OAuth.ErrorFormat(
							"Client credentials grant access request for client \"{0}\" rejected by authorization server host.",
							clientCredentialOnly.ClientIdentifier);
						throw new TokenEndpointProtocolException(accessTokenRequest, Protocol.AccessTokenRequestErrorCodes.UnauthorizedClient);
					}

					clientCredentialOnly.Scope.ResetContents(authorizeResult.ApprovedScope);
				} catch (NotSupportedException) {
					throw new TokenEndpointProtocolException(accessTokenRequest, Protocol.AccessTokenRequestErrorCodes.UnsupportedGrantType);
				} catch (NotImplementedException) {
					throw new TokenEndpointProtocolException(accessTokenRequest, Protocol.AccessTokenRequestErrorCodes.UnsupportedGrantType);
				}
			} else {
				// Check that authorization requests come with an acceptable callback URI.
				var authorizationRequest = message as EndUserAuthorizationRequest;
				if (authorizationRequest != null) {
					var client = this.AuthorizationServer.GetClientOrThrow(authorizationRequest.ClientIdentifier);
					ErrorUtilities.VerifyProtocol(authorizationRequest.Callback == null || client.IsCallbackAllowed(authorizationRequest.Callback), AuthServerStrings.ClientCallbackDisallowed, authorizationRequest.Callback);
					ErrorUtilities.VerifyProtocol(authorizationRequest.Callback != null || client.DefaultCallback != null, AuthServerStrings.NoCallback);
					applied = true;
				}

				// Check that the callback URI in a direct message from the client matches the one in the indirect message received earlier.
				var request = message as AccessTokenAuthorizationCodeRequestAS;
				if (request != null) {
					IAuthorizationCodeCarryingRequest tokenRequest = request;
					tokenRequest.AuthorizationDescription.VerifyCallback(request.Callback);
					applied = true;
				}

				var authCarrier = message as IAuthorizationCarryingRequest;
				if (authCarrier != null) {
					var accessRequest = authCarrier as AccessTokenRequestBase;
					if (accessRequest != null) {
						// Make sure the client sending us this token is the client we issued the token to.
						AuthServerUtilities.TokenEndpointVerify(string.Equals(accessRequest.ClientIdentifier, authCarrier.AuthorizationDescription.ClientIdentifier, StringComparison.Ordinal), accessTokenRequest, Protocol.AccessTokenRequestErrorCodes.InvalidClient);

						var scopedAccessRequest = accessRequest as ScopedAccessTokenRequest;
						if (scopedAccessRequest != null) {
							// Make sure the scope the client is requesting does not exceed the scope in the grant.
							if (!this.AuthServerChannel.ScopeSatisfiedCheck.IsScopeSatisfied(requiredScope: scopedAccessRequest.Scope, grantedScope: authCarrier.AuthorizationDescription.Scope)) {
								Logger.OAuth.ErrorFormat("The requested access scope (\"{0}\") exceeds the grant scope (\"{1}\").", scopedAccessRequest.Scope, authCarrier.AuthorizationDescription.Scope);
								throw new TokenEndpointProtocolException(accessTokenRequest, Protocol.AccessTokenRequestErrorCodes.InvalidScope, AuthServerStrings.AccessScopeExceedsGrantScope);
							}
						}
					}

					// Make sure the authorization this token represents hasn't already been revoked.
					if (!this.AuthorizationServer.IsAuthorizationValid(authCarrier.AuthorizationDescription)) {
						Logger.OAuth.Error("Rejecting access token request because the IAuthorizationServerHost.IsAuthorizationValid method returned false.");
						throw new TokenEndpointProtocolException(accessTokenRequest, Protocol.AccessTokenRequestErrorCodes.InvalidGrant);
					}

					applied = true;
				}
			}

			return applied ? (MessageProtections?)MessageProtections.None : null;
		}
	}
}