summaryrefslogtreecommitdiffstats
path: root/samples/OpenIdProviderWebForms/Code/Util.cs
blob: 5333124342cd3b4616128224f9e3132eb6be12d6 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//-----------------------------------------------------------------------
// <copyright file="Util.cs" company="Outercurve Foundation">
//     Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace OpenIdProviderWebForms.Code {
	using System;
	using System.Threading;
	using System.Threading.Tasks;
	using System.Web;
	using DotNetOpenAuth.OpenId;
	using DotNetOpenAuth.OpenId.Provider;

	public static class Util {
		public static string ExtractUserName(Uri url) {
			return url.Segments[url.Segments.Length - 1];
		}

		public static string ExtractUserName(Identifier identifier) {
			return ExtractUserName(new Uri(identifier.ToString()));
		}

		public static Identifier BuildIdentityUrl() {
			return BuildIdentityUrl(HttpContext.Current.User.Identity.Name);
		}

		public static Identifier BuildIdentityUrl(string username) {
			// This sample Provider has a custom policy for normalizing URIs, which is that the whole
			// path of the URI be lowercase except for the first letter of the username.
			username = username.Substring(0, 1).ToUpperInvariant() + username.Substring(1).ToLowerInvariant();
			return new Uri(HttpContext.Current.Request.Url, HttpContext.Current.Response.ApplyAppPathModifier("~/user.aspx/" + username));
		}

		internal static void ProcessAuthenticationChallenge(IAuthenticationRequest idrequest) {
			if (idrequest.Immediate) {
				if (idrequest.IsDirectedIdentity) {
					if (HttpContext.Current.User.Identity.IsAuthenticated) {
						idrequest.LocalIdentifier = Util.BuildIdentityUrl();
						idrequest.IsAuthenticated = true;
					} else {
						idrequest.IsAuthenticated = false;
					}
				} else {
					string userOwningOpenIdUrl = Util.ExtractUserName(idrequest.LocalIdentifier);

					// NOTE: in a production provider site, you may want to only 
					// respond affirmatively if the user has already authorized this consumer
					// to know the answer.
					idrequest.IsAuthenticated = userOwningOpenIdUrl == HttpContext.Current.User.Identity.Name;
				}

				if (idrequest.IsAuthenticated.Value) {
					// add extension responses here.
				}
			} else {
				HttpContext.Current.Response.Redirect("~/decide.aspx", false);
			}
		}

		internal static void ProcessAnonymousRequest(IAnonymousRequest request) {
			if (request.Immediate) {
				// NOTE: in a production provider site, you may want to only
				// respond affirmatively if the user has already authorized this consumer
				// to know the answer.
				request.IsApproved = HttpContext.Current.User.Identity.IsAuthenticated;

				if (request.IsApproved.Value) {
					// Add extension responses here.
					// These would typically be filled in from a user database
				}
			} else {
				HttpContext.Current.Response.Redirect("~/decide.aspx", false);
			}
		}

		internal static Task ToApm(this Task task, AsyncCallback callback, object state) {
			if (task == null) {
				throw new ArgumentNullException("task");
			}

			var tcs = new TaskCompletionSource<object>(state);
			task.ContinueWith(
				t => {
					if (t.IsFaulted) {
						tcs.TrySetException(t.Exception.InnerExceptions);
					} else if (t.IsCanceled) {
						tcs.TrySetCanceled();
					} else {
						tcs.TrySetResult(null);
					}

					if (callback != null) {
						callback(tcs.Task);
					}
				},
				CancellationToken.None,
				TaskContinuationOptions.None,
				TaskScheduler.Default);

			return tcs.Task;
		}
	}
}