summaryrefslogtreecommitdiffstats
path: root/projecttemplates/WebFormsRelyingParty/Code/OAuthServiceProvider.cs
diff options
context:
space:
mode:
authorAndrew Arnott <andrewarnott@gmail.com>2009-11-10 21:00:37 -0800
committerAndrew Arnott <andrewarnott@gmail.com>2009-11-10 21:00:37 -0800
commit6df46c9875f82f5a5f94a082a0b699fde2978d6f (patch)
tree56df88c1a4fe02a363e5a0d7d087c1947322ab09 /projecttemplates/WebFormsRelyingParty/Code/OAuthServiceProvider.cs
parent5d4d80b4a83bb9d6af62cc5f6d78f6f7e541e67d (diff)
downloadDotNetOpenAuth-6df46c9875f82f5a5f94a082a0b699fde2978d6f.zip
DotNetOpenAuth-6df46c9875f82f5a5f94a082a0b699fde2978d6f.tar.gz
DotNetOpenAuth-6df46c9875f82f5a5f94a082a0b699fde2978d6f.tar.bz2
Added a bunch more OAuth SP supporting code, but it's not done yet.
Diffstat (limited to 'projecttemplates/WebFormsRelyingParty/Code/OAuthServiceProvider.cs')
-rw-r--r--projecttemplates/WebFormsRelyingParty/Code/OAuthServiceProvider.cs117
1 files changed, 117 insertions, 0 deletions
diff --git a/projecttemplates/WebFormsRelyingParty/Code/OAuthServiceProvider.cs b/projecttemplates/WebFormsRelyingParty/Code/OAuthServiceProvider.cs
new file mode 100644
index 0000000..473b6d2
--- /dev/null
+++ b/projecttemplates/WebFormsRelyingParty/Code/OAuthServiceProvider.cs
@@ -0,0 +1,117 @@
+//-----------------------------------------------------------------------
+// <copyright file="OAuthServiceProvider.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace WebFormsRelyingParty.Code {
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Web;
+ using DotNetOpenAuth.Messaging;
+ using DotNetOpenAuth.OAuth;
+ using DotNetOpenAuth.OAuth.ChannelElements;
+ using DotNetOpenAuth.OAuth.Messages;
+
+ public class OAuthServiceProvider {
+ private const string PendingAuthorizationRequestSessionKey = "PendingAuthorizationRequest";
+
+ /// <summary>
+ /// The shared service description for this web site.
+ /// </summary>
+ private static ServiceProviderDescription serviceDescription;
+
+ private static OAuthServiceProviderTokenManager tokenManager;
+
+ /// <summary>
+ /// The shared service provider object.
+ /// </summary>
+ private static ServiceProvider serviceProvider;
+
+ /// <summary>
+ /// The lock to synchronize initialization of the <see cref="serviceProvider"/> field.
+ /// </summary>
+ private static object initializerLock = new object();
+
+ /// <summary>
+ /// Gets the service provider.
+ /// </summary>
+ /// <value>The service provider.</value>
+ public static ServiceProvider ServiceProvider {
+ get {
+ EnsureInitialized();
+ return serviceProvider;
+ }
+ }
+
+ /// <summary>
+ /// Gets the service description.
+ /// </summary>
+ /// <value>The service description.</value>
+ public static ServiceProviderDescription ServiceDescription {
+ get {
+ EnsureInitialized();
+ return serviceDescription;
+ }
+ }
+
+ public static UserAuthorizationRequest PendingAuthorizationRequest {
+ get { return HttpContext.Current.Session[PendingAuthorizationRequestSessionKey] as UserAuthorizationRequest; }
+ set { HttpContext.Current.Session[PendingAuthorizationRequestSessionKey] = value; }
+ }
+
+ public static WebFormsRelyingParty.Consumer PendingAuthorizationConsumer {
+ get {
+ ITokenContainingMessage message = PendingAuthorizationRequest;
+ if (message == null) {
+ throw new InvalidOperationException();
+ }
+
+ return Global.DataContext.IssuedToken.OfType<IssuedRequestToken>().First(t => t.Token == message.Token).Consumer;
+ }
+ }
+
+ public static void AuthorizePendingRequestToken() {
+ var pendingRequest = PendingAuthorizationRequest;
+ if (pendingRequest == null) {
+ throw new InvalidOperationException("No pending authorization request to authorize.");
+ }
+
+ ITokenContainingMessage msg = pendingRequest;
+ var token = Global.DataContext.IssuedToken.OfType<IssuedRequestToken>().First(t => t.Token == msg.Token);
+ token.Authorize();
+
+ var response = serviceProvider.PrepareAuthorizationResponse(pendingRequest);
+ serviceProvider.Channel.Send(response);
+ PendingAuthorizationRequest = null;
+ }
+
+ /// <summary>
+ /// Initializes the <see cref="serviceProvider"/> field if it has not yet been initialized.
+ /// </summary>
+ private static void EnsureInitialized() {
+ if (serviceProvider == null) {
+ lock (initializerLock) {
+ if (serviceDescription == null) {
+ var endpoint = new MessageReceivingEndpoint(Utilities.ApplicationRoot + "OAuth.ashx", HttpDeliveryMethods.PostRequest);
+ serviceDescription = new ServiceProviderDescription {
+ TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() },
+ RequestTokenEndpoint = endpoint,
+ AccessTokenEndpoint = endpoint,
+ UserAuthorizationEndpoint = endpoint,
+ };
+ }
+
+ if (tokenManager == null) {
+ tokenManager = new OAuthServiceProviderTokenManager();
+ }
+
+ if (serviceProvider == null) {
+ serviceProvider = new ServiceProvider(serviceDescription, tokenManager);
+ }
+ }
+ }
+ }
+ }
+}