summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Test/OAuth/OAuthCoordinator.cs
blob: c3ef6c2ed799cf86f58b67d63d624505907f844f (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
//-----------------------------------------------------------------------
// <copyright file="OAuthCoordinator.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Test.OAuth {
	using System;
	using DotNetOpenAuth.Messaging;
	using DotNetOpenAuth.Messaging.Bindings;
	using DotNetOpenAuth.OAuth;
	using DotNetOpenAuth.OAuth.ChannelElements;
	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 override 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, (IConsumerTokenManager)consumerTokenManager);
			CoordinatingOAuthChannel serviceProviderChannel = new CoordinatingOAuthChannel(spSigningElement, (IServiceProviderTokenManager)serviceTokenManager);
			consumerChannel.RemoteChannel = serviceProviderChannel;
			serviceProviderChannel.RemoteChannel = consumerChannel;

			// Prepare the Consumer and Service Provider objects
			WebConsumer consumer = new WebConsumer(this.serviceDescription, consumerTokenManager) {
				OAuthChannel = consumerChannel,
			};
			ServiceProvider serviceProvider = new ServiceProvider(this.serviceDescription, serviceTokenManager, new NonceMemoryStore()) {
				OAuthChannel = serviceProviderChannel,
			};

			this.RunCore(consumer, serviceProvider);
		}
	}
}