//----------------------------------------------------------------------- // // Copyright (c) Outercurve Foundation. All rights reserved. // //----------------------------------------------------------------------- namespace DotNetOpenAuth.OAuth2.ChannelElements { using System; using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Linq; using System.Text; using DotNetOpenAuth.OAuth2.Messages; using Messaging; /// /// A guard for all messages to or from an Authorization Server to ensure that they are well formed, /// have valid secrets, callback URIs, etc. /// internal class MessageValidationBindingElement : AuthServerBindingElementBase { /// /// Gets the protection commonly offered (if any) by this binding element. /// /// /// This value is used to assist in sorting binding elements in the channel stack. /// public override MessageProtections Protection { get { return MessageProtections.None; } } /// /// Prepares a message for sending based on the rules of this channel binding element. /// /// The message to prepare for sending. /// /// 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. /// /// /// Implementations that provide message protection must honor the /// properties where applicable. /// public override MessageProtections? ProcessOutgoingMessage(IProtocolMessage message) { 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; } /// /// 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. /// /// The incoming message to process. /// /// 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. /// /// /// Thrown when the binding element rules indicate that this message is invalid and should /// NOT be processed. /// /// /// Implementations that provide message protection must honor the /// properties where applicable. /// public override MessageProtections? ProcessIncomingMessage(IProtocolMessage message) { bool applied = false; // Check that the client secret is correct for client authenticated messages. var authenticatedClientRequest = message as AuthenticatedClientRequestBase; if (authenticatedClientRequest != null) { var client = this.AuthorizationServer.GetClientOrThrow(authenticatedClientRequest.ClientIdentifier); string secret = client.Secret; ErrorUtilities.VerifyProtocol(!string.IsNullOrEmpty(secret), Protocol.unauthorized_client); // an empty secret is not allowed for client authenticated calls. ErrorUtilities.VerifyProtocol(MessagingUtilities.EqualsConstantTime(secret, authenticatedClientRequest.ClientSecret), Protocol.incorrect_client_credentials); applied = true; } // 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), OAuthStrings.ClientCallbackDisallowed, authorizationRequest.Callback); ErrorUtilities.VerifyProtocol(authorizationRequest.Callback != null || client.DefaultCallback != null, OAuthStrings.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; } return applied ? (MessageProtections?)MessageProtections.None : null; } } }