diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2009-06-06 21:51:33 -0700 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2009-06-06 21:51:33 -0700 |
commit | 955712e1fb5db9ad69665617adc61fafe609d62b (patch) | |
tree | b632ee953c8dc5d7636979d78b04180387ad0fef /src | |
parent | 362c874345506eff436a15a18b9681d73d3207f5 (diff) | |
download | DotNetOpenAuth-955712e1fb5db9ad69665617adc61fafe609d62b.zip DotNetOpenAuth-955712e1fb5db9ad69665617adc61fafe609d62b.tar.gz DotNetOpenAuth-955712e1fb5db9ad69665617adc61fafe609d62b.tar.bz2 |
Refactored where token manager integration occurs.
Diffstat (limited to 'src')
-rw-r--r-- | src/DotNetOpenAuth/OAuth/ChannelElements/OAuthChannel.cs | 2 | ||||
-rw-r--r-- | src/DotNetOpenAuth/OAuth/ChannelElements/TokenHandlingBindingElement.cs (renamed from src/DotNetOpenAuth/OAuth/ChannelElements/VerificationCodeBindingElement.cs) | 35 | ||||
-rw-r--r-- | src/DotNetOpenAuth/OAuth/ServiceProvider.cs | 32 |
3 files changed, 29 insertions, 40 deletions
diff --git a/src/DotNetOpenAuth/OAuth/ChannelElements/OAuthChannel.cs b/src/DotNetOpenAuth/OAuth/ChannelElements/OAuthChannel.cs index f0aa64a..e2a58d0 100644 --- a/src/DotNetOpenAuth/OAuth/ChannelElements/OAuthChannel.cs +++ b/src/DotNetOpenAuth/OAuth/ChannelElements/OAuthChannel.cs @@ -257,7 +257,7 @@ namespace DotNetOpenAuth.OAuth.ChannelElements { var spTokenManager = tokenManager as IServiceProviderTokenManager; if (spTokenManager != null) { - bindingElements.Insert(0, new VerificationCodeBindingElement(spTokenManager)); + bindingElements.Insert(0, new TokenHandlingBindingElement(spTokenManager)); } return bindingElements.ToArray(); diff --git a/src/DotNetOpenAuth/OAuth/ChannelElements/VerificationCodeBindingElement.cs b/src/DotNetOpenAuth/OAuth/ChannelElements/TokenHandlingBindingElement.cs index f1e14f4..e077128 100644 --- a/src/DotNetOpenAuth/OAuth/ChannelElements/VerificationCodeBindingElement.cs +++ b/src/DotNetOpenAuth/OAuth/ChannelElements/TokenHandlingBindingElement.cs @@ -1,5 +1,5 @@ //----------------------------------------------------------------------- -// <copyright file="VerificationCodeBindingElement.cs" company="Andrew Arnott"> +// <copyright file="TokenHandlingBindingElement.cs" company="Andrew Arnott"> // Copyright (c) Andrew Arnott. All rights reserved. // </copyright> //----------------------------------------------------------------------- @@ -14,24 +14,20 @@ namespace DotNetOpenAuth.OAuth.ChannelElements { using DotNetOpenAuth.OAuth.Messages; /// <summary> - /// A binding element for Service Providers to manage the verification code on applicable messages. + /// A binding element for Service Providers to manage the + /// callbacks and verification codes on applicable messages. /// </summary> - internal class VerificationCodeBindingElement : IChannelBindingElement { - /// <summary> - /// The length of the verifier code (in raw bytes before base64 encoding) to generate. - /// </summary> - private const int VerifierCodeLength = 5; - + internal class TokenHandlingBindingElement : IChannelBindingElement { /// <summary> /// The token manager offered by the service provider. /// </summary> private IServiceProviderTokenManager tokenManager; /// <summary> - /// Initializes a new instance of the <see cref="VerificationCodeBindingElement"/> class. + /// Initializes a new instance of the <see cref="TokenHandlingBindingElement"/> class. /// </summary> /// <param name="tokenManager">The token manager.</param> - internal VerificationCodeBindingElement(IServiceProviderTokenManager tokenManager) { + internal TokenHandlingBindingElement(IServiceProviderTokenManager tokenManager) { Contract.Requires(tokenManager != null); ErrorUtilities.VerifyArgumentNotNull(tokenManager, "tokenManager"); @@ -73,11 +69,20 @@ namespace DotNetOpenAuth.OAuth.ChannelElements { public MessageProtections? ProcessOutgoingMessage(IProtocolMessage message) { ErrorUtilities.VerifyArgumentNotNull(message, "message"); - var response = message as UserAuthorizationResponse; - if (response != null && response.Version >= Protocol.V10a.Version) { - ErrorUtilities.VerifyInternal(response.VerificationCode == null, "VerificationCode was unexpectedly already set."); - response.VerificationCode = MessagingUtilities.GetCryptoRandomDataAsBase64(VerifierCodeLength); - this.tokenManager.SetRequestTokenVerifier(response.RequestToken, response.VerificationCode); + var userAuthResponse = message as UserAuthorizationResponse; + if (userAuthResponse != null && userAuthResponse.Version >= Protocol.V10a.Version) { + this.tokenManager.SetRequestTokenVerifier(userAuthResponse.RequestToken, userAuthResponse.VerificationCode); + return MessageProtections.None; + } + + // Hook to store the token and secret on its way down to the Consumer. + var grantRequestTokenResponse = message as UnauthorizedTokenResponse; + if (grantRequestTokenResponse != null) { + this.tokenManager.StoreNewRequestToken(grantRequestTokenResponse.RequestMessage, grantRequestTokenResponse); + if (grantRequestTokenResponse.RequestMessage.Callback != null) { + this.tokenManager.SetRequestTokenCallback(grantRequestTokenResponse.RequestToken, grantRequestTokenResponse.RequestMessage.Callback); + } + return MessageProtections.None; } diff --git a/src/DotNetOpenAuth/OAuth/ServiceProvider.cs b/src/DotNetOpenAuth/OAuth/ServiceProvider.cs index e207823..e7c8e04 100644 --- a/src/DotNetOpenAuth/OAuth/ServiceProvider.cs +++ b/src/DotNetOpenAuth/OAuth/ServiceProvider.cs @@ -29,6 +29,11 @@ namespace DotNetOpenAuth.OAuth { /// </remarks> public class ServiceProvider : IDisposable { /// <summary> + /// The length of the verifier code (in raw bytes before base64 encoding) to generate. + /// </summary> + private const int VerifierCodeLength = 5; + + /// <summary> /// The field behind the <see cref="OAuthChannel"/> property. /// </summary> private OAuthChannel channel; @@ -93,15 +98,9 @@ namespace DotNetOpenAuth.OAuth { } set { - if (this.channel != null) { - this.channel.Sending -= this.OAuthChannel_Sending; - } - + Contract.Requires(value != null); + ErrorUtilities.VerifyArgumentNotNull(value, "value"); this.channel = value; - - if (this.channel != null) { - this.channel.Sending += this.OAuthChannel_Sending; - } } } @@ -237,6 +236,7 @@ namespace DotNetOpenAuth.OAuth { var authorization = new UserAuthorizationResponse(callback, this.ServiceDescription.Version) { RequestToken = request.RequestToken, + VerificationCode = MessagingUtilities.GetCryptoRandomDataAsBase64(VerifierCodeLength), }; return authorization; } @@ -373,21 +373,5 @@ namespace DotNetOpenAuth.OAuth { } #endregion - - /// <summary> - /// Hooks the channel in order to perform some operations on some outgoing messages. - /// </summary> - /// <param name="sender">The source of the event.</param> - /// <param name="e">The <see cref="DotNetOpenAuth.Messaging.ChannelEventArgs"/> instance containing the event data.</param> - private void OAuthChannel_Sending(object sender, ChannelEventArgs e) { - // Hook to store the token and secret on its way down to the Consumer. - var grantRequestTokenResponse = e.Message as UnauthorizedTokenResponse; - if (grantRequestTokenResponse != null) { - this.TokenManager.StoreNewRequestToken(grantRequestTokenResponse.RequestMessage, grantRequestTokenResponse); - if (grantRequestTokenResponse.RequestMessage.Callback != null) { - this.TokenManager.SetRequestTokenCallback(grantRequestTokenResponse.RequestToken, grantRequestTokenResponse.RequestMessage.Callback); - } - } - } } } |