diff options
Diffstat (limited to 'src/DotNetOpenAuth.Test/OAuth')
-rw-r--r-- | src/DotNetOpenAuth.Test/OAuth/AppendixScenarios.cs | 66 | ||||
-rw-r--r-- | src/DotNetOpenAuth.Test/OAuth/OAuthCoordinator.cs | 67 |
2 files changed, 133 insertions, 0 deletions
diff --git a/src/DotNetOpenAuth.Test/OAuth/AppendixScenarios.cs b/src/DotNetOpenAuth.Test/OAuth/AppendixScenarios.cs new file mode 100644 index 0000000..5d25cf9 --- /dev/null +++ b/src/DotNetOpenAuth.Test/OAuth/AppendixScenarios.cs @@ -0,0 +1,66 @@ +//----------------------------------------------------------------------- +// <copyright file="AppendixScenarios.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test.OAuth { + using System; + using System.IO; + using System.Net; + using DotNetOpenAuth.Messaging; + using DotNetOpenAuth.OAuth; + using DotNetOpenAuth.OAuth.ChannelElements; + using DotNetOpenAuth.Test.Mocks; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class AppendixScenarios : TestBase { + [TestMethod] + public void SpecAppendixAExample() { + ServiceProviderDescription serviceDescription = new ServiceProviderDescription() { + RequestTokenEndpoint = new MessageReceivingEndpoint("https://photos.example.net/request_token", HttpDeliveryMethods.PostRequest), + UserAuthorizationEndpoint = new MessageReceivingEndpoint("http://photos.example.net/authorize", HttpDeliveryMethods.GetRequest), + AccessTokenEndpoint = new MessageReceivingEndpoint("https://photos.example.net/access_token", HttpDeliveryMethods.PostRequest), + TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { + new PlaintextSigningBindingElement(), + new HmacSha1SigningBindingElement(), + }, + }; + MessageReceivingEndpoint accessPhotoEndpoint = new MessageReceivingEndpoint("http://photos.example.net/photos?file=vacation.jpg&size=original", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest); + ConsumerDescription consumerDescription = new ConsumerDescription("dpf43f3p2l4k3l03", "kd94hf93k423kf44"); + + OAuthCoordinator coordinator = new OAuthCoordinator( + consumerDescription, + serviceDescription, + consumer => { + consumer.Channel.Send(consumer.PrepareRequestUserAuthorization(new Uri("http://printer.example.com/request_token_ready"), null, null)); // .Send() dropped because this is just a simulation + string accessToken = consumer.ProcessUserAuthorization().AccessToken; + var photoRequest = consumer.CreateAuthorizingMessage(accessPhotoEndpoint, accessToken); + Response protectedPhoto = ((CoordinatingOAuthChannel)consumer.Channel).RequestProtectedResource(photoRequest); + Assert.IsNotNull(protectedPhoto); + Assert.AreEqual(HttpStatusCode.OK, protectedPhoto.Status); + Assert.AreEqual("image/jpeg", protectedPhoto.Headers[HttpResponseHeader.ContentType]); + Assert.AreNotEqual(0, protectedPhoto.ResponseStream.Length); + }, + sp => { + var requestTokenMessage = sp.ReadTokenRequest(); + sp.Channel.Send(sp.PrepareUnauthorizedTokenMessage(requestTokenMessage)); // .Send() dropped because this is just a simulation + var authRequest = sp.ReadAuthorizationRequest(); + ((InMemoryTokenManager)sp.TokenManager).AuthorizeRequestToken(authRequest.RequestToken); + sp.Channel.Send(sp.PrepareAuthorizationResponse(authRequest)); // .Send() dropped because this is just a simulation + var accessRequest = sp.ReadAccessTokenRequest(); + sp.Channel.Send(sp.PrepareAccessTokenMessage(accessRequest)); // .Send() dropped because this is just a simulation + string accessToken = sp.ReadProtectedResourceAuthorization().AccessToken; + ((CoordinatingOAuthChannel)sp.Channel).SendDirectRawResponse(new Response { + ResponseStream = new MemoryStream(new byte[] { 0x33, 0x66 }), + Headers = new WebHeaderCollection { + { HttpResponseHeader.ContentType, "image/jpeg" }, + }, + }); + }); + + coordinator.Run(); + } + } +} diff --git a/src/DotNetOpenAuth.Test/OAuth/OAuthCoordinator.cs b/src/DotNetOpenAuth.Test/OAuth/OAuthCoordinator.cs new file mode 100644 index 0000000..37d7e5b --- /dev/null +++ b/src/DotNetOpenAuth.Test/OAuth/OAuthCoordinator.cs @@ -0,0 +1,67 @@ +//----------------------------------------------------------------------- +// <copyright file="OAuthCoordinator.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test { + using System; + using DotNetOpenAuth.Messaging; + using DotNetOpenAuth.OAuth; + using DotNetOpenAuth.Test.Mocks; + + /// <summary> + /// Runs a Consumer and Service Provider simultaneously so they can interact in a full simulation. + /// </summary> + internal class OAuthCoordinator : CoordinatorBase<WebConsumer, ServiceProvider> { + private ConsumerDescription consumerDescription; + private ServiceProviderDescription serviceDescription; + + /// <summary>Initializes a new instance of the <see cref="OAuthCoordinator"/> class.</summary> + /// <param name="consumerDescription">The description of the consumer.</param> + /// <param name="serviceDescription">The service description that will be used to construct the Consumer and ServiceProvider objects.</param> + /// <param name="consumerAction">The code path of the Consumer.</param> + /// <param name="serviceProviderAction">The code path of the Service Provider.</param> + internal OAuthCoordinator(ConsumerDescription consumerDescription, ServiceProviderDescription serviceDescription, Action<WebConsumer> consumerAction, Action<ServiceProvider> serviceProviderAction) + : base(consumerAction, serviceProviderAction) { + ErrorUtilities.VerifyArgumentNotNull(consumerDescription, "consumerDescription"); + ErrorUtilities.VerifyArgumentNotNull(serviceDescription, "serviceDescription"); + + this.consumerDescription = consumerDescription; + this.serviceDescription = serviceDescription; + } + + /// <summary> + /// Starts the simulation. + /// </summary> + internal void Run() { + // Clone the template signing binding element. + var signingElement = this.serviceDescription.CreateTamperProtectionElement(); + var consumerSigningElement = signingElement.Clone(); + var spSigningElement = signingElement.Clone(); + + // Prepare token managers + InMemoryTokenManager consumerTokenManager = new InMemoryTokenManager(); + InMemoryTokenManager serviceTokenManager = new InMemoryTokenManager(); + consumerTokenManager.AddConsumer(this.consumerDescription); + serviceTokenManager.AddConsumer(this.consumerDescription); + + // Prepare channels that will pass messages directly back and forth. + CoordinatingOAuthChannel consumerChannel = new CoordinatingOAuthChannel(consumerSigningElement, true, consumerTokenManager); + CoordinatingOAuthChannel serviceProviderChannel = new CoordinatingOAuthChannel(spSigningElement, false, serviceTokenManager); + consumerChannel.RemoteChannel = serviceProviderChannel; + serviceProviderChannel.RemoteChannel = consumerChannel; + + // Prepare the Consumer and Service Provider objects + WebConsumer consumer = new WebConsumer(this.serviceDescription, consumerTokenManager) { + OAuthChannel = consumerChannel, + ConsumerKey = this.consumerDescription.ConsumerKey, + }; + ServiceProvider serviceProvider = new ServiceProvider(this.serviceDescription, serviceTokenManager) { + OAuthChannel = serviceProviderChannel, + }; + + this.RunCore(consumer, serviceProvider); + } + } +} |