summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Test/OAuth2/OAuth2Coordinator.cs
blob: fa4ffef4377c0003190ee26fd152efd721040cd7 (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
69
70
71
72
73
//-----------------------------------------------------------------------
// <copyright file="OAuth2Coordinator.cs" company="Outercurve Foundation">
//     Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Test.OAuth2 {
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Net;
	using System.Text;
	using DotNetOpenAuth.OAuth2;
	using DotNetOpenAuth.Test.Mocks;

	internal class OAuth2Coordinator<TClient> : CoordinatorBase<TClient, AuthorizationServer>
		where TClient : ClientBase {
		private readonly AuthorizationServerDescription serverDescription;
		private readonly IAuthorizationServerHost authServerHost;
		private readonly TClient client;

		internal OAuth2Coordinator(
			AuthorizationServerDescription serverDescription,
			IAuthorizationServerHost authServerHost,
			TClient client,
			Action<TClient> clientAction,
			Action<AuthorizationServer> authServerAction)
			: base(clientAction, authServerAction) {
			Requires.NotNull(serverDescription, "serverDescription");
			Requires.NotNull(authServerHost, "authServerHost");
			Requires.NotNull(client, "client");

			this.serverDescription = serverDescription;
			this.authServerHost = authServerHost;
			this.client = client;

			this.client.ClientIdentifier = OAuth2TestBase.ClientId;
			this.client.ClientCredentialApplicator = ClientCredentialApplicator.NetworkCredential(new NetworkCredential(OAuth2TestBase.ClientId, OAuth2TestBase.ClientSecret));
		}

		internal override void Run() {
			var authServer = new AuthorizationServer(this.authServerHost);

			var rpCoordinatingChannel = new CoordinatingOAuth2ClientChannel(this.client.Channel, this.IncomingMessageFilter, this.OutgoingMessageFilter);
			var opCoordinatingChannel = new CoordinatingOAuth2AuthServerChannel(authServer.Channel, this.IncomingMessageFilter, this.OutgoingMessageFilter);
			rpCoordinatingChannel.RemoteChannel = opCoordinatingChannel;
			opCoordinatingChannel.RemoteChannel = rpCoordinatingChannel;

			this.client.Channel = rpCoordinatingChannel;
			authServer.Channel = opCoordinatingChannel;

			this.RunCore(this.client, authServer);
		}

		private static Action<WebServerClient> WrapAction(Action<WebServerClient> action) {
			Requires.NotNull(action, "action");

			return client => {
				action(client);
				((CoordinatingChannel)client.Channel).Close();
			};
		}

		private static Action<AuthorizationServer> WrapAction(Action<AuthorizationServer> action) {
			Requires.NotNull(action, "action");

			return authServer => {
				action(authServer);
				((CoordinatingChannel)authServer.Channel).Close();
			};
		}
	}
}