summaryrefslogtreecommitdiffstats
path: root/projecttemplates/RelyingPartyLogic/OAuthServiceProvider.cs
blob: 9b6fb5057e38bba8fc1842d763703553f8d11b8b (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
74
75
76
77
//-----------------------------------------------------------------------
// <copyright file="OAuthServiceProvider.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace RelyingPartyLogic {
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Web;
	using DotNetOpenAuth.Messaging;
	using DotNetOpenAuth.OAuth2;
	using DotNetOpenAuth.OAuth2.ChannelElements;
	using DotNetOpenAuth.OAuth2.Messages;

	public class OAuthServiceProvider {
		private const string PendingAuthorizationRequestSessionKey = "PendingAuthorizationRequest";

		/// <summary>
		/// The shared service description for this web site.
		/// </summary>
		private static AuthorizationServerDescription authorizationServerDescription;

		/// <summary>
		/// The shared authorization server.
		/// </summary>
		private static WebServerAuthorizationServer authorizationServer;

		/// <summary>
		/// The lock to synchronize initialization of the <see cref="authorizationServer"/> field.
		/// </summary>
		private static readonly object InitializerLock = new object();

		/// <summary>
		/// Gets the service provider.
		/// </summary>
		/// <value>The service provider.</value>
		public static WebServerAuthorizationServer AuthorizationServer {
			get {
				EnsureInitialized();
				return authorizationServer;
			}
		}

		/// <summary>
		/// Gets the service description.
		/// </summary>
		/// <value>The service description.</value>
		public static AuthorizationServerDescription AuthorizationServerDescription {
			get {
				EnsureInitialized();
				return authorizationServerDescription;
			}
		}

		/// <summary>
		/// Initializes the <see cref="authorizationServer"/> field if it has not yet been initialized.
		/// </summary>
		private static void EnsureInitialized() {
			if (authorizationServer == null) {
				lock (InitializerLock) {
					if (authorizationServerDescription == null) {
						authorizationServerDescription = new AuthorizationServerDescription {
							AuthorizationEndpoint = new Uri(Utilities.ApplicationRoot, "OAuth.ashx"),
							TokenEndpoint = new Uri(Utilities.ApplicationRoot, "OAuth.ashx"),
						};
					}

					if (authorizationServer == null) {
						authorizationServer = new WebServerAuthorizationServer(new OAuthAuthorizationServer());
					}
				}
			}
		}
	}
}