diff options
Diffstat (limited to 'src/DotNetOAuth.Test/Scenarios')
-rw-r--r-- | src/DotNetOAuth.Test/Scenarios/AppendixScenarios.cs | 53 | ||||
-rw-r--r-- | src/DotNetOAuth.Test/Scenarios/CoordinatingOAuthChannel.cs | 72 | ||||
-rw-r--r-- | src/DotNetOAuth.Test/Scenarios/Coordinator.cs | 70 |
3 files changed, 195 insertions, 0 deletions
diff --git a/src/DotNetOAuth.Test/Scenarios/AppendixScenarios.cs b/src/DotNetOAuth.Test/Scenarios/AppendixScenarios.cs new file mode 100644 index 0000000..328255b --- /dev/null +++ b/src/DotNetOAuth.Test/Scenarios/AppendixScenarios.cs @@ -0,0 +1,53 @@ +//-----------------------------------------------------------------------
+// <copyright file="Scenarios.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOAuth.Test {
+ using System;
+ using System.Collections.Specialized;
+ using System.IO;
+ using System.Net;
+ using System.Web;
+ using DotNetOAuth.Messaging;
+ using Microsoft.VisualStudio.TestTools.UnitTesting;
+ using DotNetOAuth.Test.Scenarios;
+ using DotNetOAuth.ChannelElements;
+
+ [TestClass]
+ public class AppendixScenarios : TestBase {
+ [TestMethod]
+ public void SpecAppendixAExample() {
+ ServiceProvider sp = new ServiceProvider {
+ RequestTokenEndpoint = new ServiceProviderEndpoint("https://photos.example.net/request_token", HttpDeliveryMethod.PostRequest),
+ UserAuthorizationEndpoint = new ServiceProviderEndpoint("http://photos.example.net/authorize", HttpDeliveryMethod.GetRequest),
+ AccessTokenEndpoint = new ServiceProviderEndpoint("https://photos.example.net/access_token", HttpDeliveryMethod.PostRequest),
+ };
+
+ Coordinator coordinator = new Coordinator(
+ channel => {
+ Consumer consumer = new Consumer {
+ Channel = channel,
+ ConsumerKey = "dpf43f3p2l4k3l03",
+ ConsumerSecret = "kd94hf93k423kf44",
+ ServiceProvider = sp,
+ };
+
+ consumer.RequestUserAuthorization(new Uri("http://printer.example.com/request_token_ready"));
+ var accessTokenMessage = consumer.ProcessUserAuthorization();
+ },
+ channel => {
+ sp.Channel = channel;
+ var requestTokenMessage = sp.ReadTokenRequest();
+ sp.SendUnauthorizedTokenResponse("hh5s93j4hdidpola", "hdhd0244k9j7ao03");
+ var authRequest = sp.ReadAuthorizationRequest();
+ sp.SendAuthorizationResponse(authRequest);
+ var accessRequest = sp.ReadAccessTokenRequest();
+ sp.SendAccessToken("nnch734d00sl2jdk", "pfkkdhi9sl3r4s00");
+ });
+ coordinator.SigningElement = new PlainTextSigningBindingElement();
+ coordinator.Start();
+ }
+ }
+}
diff --git a/src/DotNetOAuth.Test/Scenarios/CoordinatingOAuthChannel.cs b/src/DotNetOAuth.Test/Scenarios/CoordinatingOAuthChannel.cs new file mode 100644 index 0000000..4c34a0d --- /dev/null +++ b/src/DotNetOAuth.Test/Scenarios/CoordinatingOAuthChannel.cs @@ -0,0 +1,72 @@ +//-----------------------------------------------------------------------
+// <copyright file="CoordinatingOAuthChannel.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOAuth.Test.Scenarios {
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using DotNetOAuth.ChannelElements;
+ using DotNetOAuth.Messaging.Bindings;
+ using DotNetOAuth.Messaging;
+using System.Threading;
+
+ /// <summary>
+ /// A special channel used in test simulations to pass messages directly between two parties.
+ /// </summary>
+ internal class CoordinatingOAuthChannel : OAuthChannel {
+ /// <summary>
+ /// Initializes a new instance of the <see cref="CoordinatingOAuthChannel"/> class for Consumers.
+ /// </summary>
+ /// <param name="signingBindingElement">
+ /// The signing element for the Consumer to use. Null for the Service Provider.
+ /// </param>
+ internal CoordinatingOAuthChannel(SigningBindingElementBase signingBindingElement)
+ : base(signingBindingElement, new NonceMemoryStore(StandardExpirationBindingElement.DefaultMaximumMessageAge), new OAuthMessageTypeProvider(), new Mocks.TestWebRequestHandler()) {
+ }
+
+ /// <summary>
+ /// Gets or sets the coordinating channel used by the other party.
+ /// </summary>
+ internal CoordinatingOAuthChannel RemoteChannel { get; set; }
+
+ private EventWaitHandle incomingMessageSignal = new AutoResetEvent(false);
+ private IProtocolMessage incomingMessage;
+
+ protected override IProtocolMessage RequestInternal(IDirectedProtocolMessage request) {
+ // Drop the outgoing message in the other channel's in-slot and let them know it's there.
+ RemoteChannel.incomingMessage = request;
+ RemoteChannel.incomingMessageSignal.Set();
+ // Now wait for a response...
+ return AwaitIncomingMessage();
+ }
+
+ protected override void SendDirectMessageResponse(IProtocolMessage response) {
+ RemoteChannel.incomingMessage = response;
+ RemoteChannel.incomingMessageSignal.Set();
+ }
+
+ protected override void SendIndirectMessage(IDirectedProtocolMessage message) {
+ // In this mock transport, direct and indirect messages are the same.
+ SendDirectMessageResponse(message);
+ }
+
+ protected override HttpRequestInfo GetRequestFromContext() {
+ return new HttpRequestInfo(AwaitIncomingMessage());
+ }
+
+ protected override IProtocolMessage ReadFromRequestInternal(HttpRequestInfo request) {
+ return request.Message;
+ }
+
+ private IProtocolMessage AwaitIncomingMessage() {
+ this.incomingMessageSignal.WaitOne();
+ IProtocolMessage response = this.incomingMessage;
+ this.incomingMessage = null;
+ return response;
+ }
+ }
+}
diff --git a/src/DotNetOAuth.Test/Scenarios/Coordinator.cs b/src/DotNetOAuth.Test/Scenarios/Coordinator.cs new file mode 100644 index 0000000..42cda0b --- /dev/null +++ b/src/DotNetOAuth.Test/Scenarios/Coordinator.cs @@ -0,0 +1,70 @@ +//-----------------------------------------------------------------------
+// <copyright file="Coordinator.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOAuth.Test.Scenarios {
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using System.Threading;
+ using DotNetOAuth.Messaging;
+ using DotNetOAuth.ChannelElements;
+
+ /// <summary>
+ /// Runs a Consumer and Service Provider simultaneously so they can interact in a full simulation.
+ /// </summary>
+ internal class Coordinator {
+ Actor consumerAction;
+ Actor serviceProviderAction;
+
+ /// <summary>Initializes a new instance of the <see cref="Coordinator"/> class.</summary>
+ /// <param name="consumerAction">The code path of the Consumer.</param>
+ /// <param name="serviceProviderAction">The code path of the Service Provider.</param>
+ internal Coordinator(Actor consumerAction, Actor serviceProviderAction) {
+ if (consumerAction == null) {
+ throw new ArgumentNullException("consumerAction");
+ }
+ if (serviceProviderAction == null) {
+ throw new ArgumentNullException("serviceProviderAction");
+ }
+
+ this.consumerAction = consumerAction;
+ this.serviceProviderAction = serviceProviderAction;
+ }
+
+ /// <summary>
+ /// Gets or sets the signing element the Consumer channel should use.
+ /// </summary>
+ /// <remarks>
+ /// The Service Provider never signs a message, so no property is necessary for that.
+ /// </remarks>
+ internal SigningBindingElementBase SigningElement { get; set; }
+
+ internal delegate void Actor(OAuthChannel channel);
+
+ /// <summary>
+ /// Starts the simulation.
+ /// </summary>
+ internal void Start() {
+ if (SigningElement == null) {
+ throw new InvalidOperationException("SigningElement must be set first.");
+ }
+
+ // Prepare channels that will pass messages directly back and forth.
+ CoordinatingOAuthChannel consumerChannel = new CoordinatingOAuthChannel(SigningElement);
+ CoordinatingOAuthChannel serviceProviderChannel = new CoordinatingOAuthChannel(SigningElement);
+ consumerChannel.RemoteChannel = serviceProviderChannel;
+ serviceProviderChannel.RemoteChannel = consumerChannel;
+
+ Thread consumerThread = new Thread(() => { consumerAction(consumerChannel); });
+ Thread serviceProviderThread = new Thread(() => { serviceProviderAction(serviceProviderChannel); });
+ consumerThread.Start();
+ serviceProviderThread.Start();
+ consumerThread.Join();
+ serviceProviderThread.Join();
+ }
+ }
+}
|