summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.AspNet/Clients/OAuth/AuthenticationOnlyCookieOAuthTokenManager.cs
blob: efc382f8c383272c73561d205b3b3f1876af702c (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//-----------------------------------------------------------------------
// <copyright file="AuthenticationOnlyCookieOAuthTokenManager.cs" company="Microsoft">
//     Copyright (c) Microsoft. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.AspNet.Clients {
	using System;
	using System.Text;
	using System.Web;
	using System.Web.Security;

	/// <summary>
	/// Stores OAuth tokens in the current request's cookie
	/// </summary>
	public class AuthenticationOnlyCookieOAuthTokenManager : IOAuthTokenManager {
		/// <summary>
		/// Key used for token cookie
		/// </summary>
		protected const string TokenCookieKey = "OAuthTokenSecret";

		/// <summary>
		/// Primary request context.
		/// </summary>
		private readonly HttpContextBase primaryContext;

		/// <summary>
		/// Initializes a new instance of the <see cref="AuthenticationOnlyCookieOAuthTokenManager"/> class.
		/// </summary>
		public AuthenticationOnlyCookieOAuthTokenManager() {
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="AuthenticationOnlyCookieOAuthTokenManager"/> class.
		/// </summary>
		/// <param name="context">The current request context.</param>
		public AuthenticationOnlyCookieOAuthTokenManager(HttpContextBase context) {
			this.primaryContext = context;
		}

		/// <summary>
		/// Gets the effective HttpContext object to use.
		/// </summary>
		protected HttpContextBase Context {
			get {
				return this.primaryContext ?? new HttpContextWrapper(HttpContext.Current);
			}
		}

		/// <summary>
		/// Gets the token secret from the specified token.
		/// </summary>
		/// <param name="token">The token.</param>
		/// <returns>
		/// The token's secret
		/// </returns>
		public virtual string GetTokenSecret(string token) {
			HttpCookie cookie = this.Context.Request.Cookies[TokenCookieKey];
			if (cookie == null || string.IsNullOrEmpty(cookie.Values[token])) {
				return null;
			}

			string secret = DecodeAndUnprotectToken(token, cookie.Values[token]);
			return secret;
		}

		/// <summary>
		/// Replaces the request token with access token.
		/// </summary>
		/// <param name="requestToken">The request token.</param>
		/// <param name="accessToken">The access token.</param>
		/// <param name="accessTokenSecret">The access token secret.</param>
		public virtual void ReplaceRequestTokenWithAccessToken(string requestToken, string accessToken, string accessTokenSecret) {
			var cookie = new HttpCookie(TokenCookieKey) {
				Value = string.Empty,
				Expires = DateTime.UtcNow.AddDays(-5)
			};
			this.Context.Response.Cookies.Set(cookie);
		}

		/// <summary>
		/// Stores the request token together with its secret.
		/// </summary>
		/// <param name="requestToken">The request token.</param>
		/// <param name="requestTokenSecret">The request token secret.</param>
		public virtual void StoreRequestToken(string requestToken, string requestTokenSecret) {
			var cookie = new HttpCookie(TokenCookieKey) {
				HttpOnly = true
			};

			if (FormsAuthentication.RequireSSL) {
				cookie.Secure = true;
			}

			var encryptedToken = ProtectAndEncodeToken(requestToken, requestTokenSecret);
			cookie.Values[requestToken] = encryptedToken;

			this.Context.Response.Cookies.Set(cookie);
		}

		/// <summary>
		/// Protect and url-encode the specified token secret.
		/// </summary>
		/// <param name="token">The token to be used as a key.</param>
		/// <param name="tokenSecret">The token secret to be protected</param>
		/// <returns>The encrypted and protected string.</returns>
		protected static string ProtectAndEncodeToken(string token, string tokenSecret)
		{
			byte[] cookieBytes = Encoding.UTF8.GetBytes(tokenSecret);
			var secretBytes = MachineKeyUtil.Protect(cookieBytes, TokenCookieKey, "Token:" + token);
			return HttpServerUtility.UrlTokenEncode(secretBytes);
		}

		/// <summary>
		/// Url-decode and unprotect the specified encrypted token string.
		/// </summary>
		/// <param name="token">The token to be used as a key.</param>
		/// <param name="encryptedToken">The encrypted token to be decrypted</param>
		/// <returns>The original token secret</returns>
		protected static string DecodeAndUnprotectToken(string token, string encryptedToken)
		{
			byte[] cookieBytes = HttpServerUtility.UrlTokenDecode(encryptedToken);
			byte[] clearBytes = MachineKeyUtil.Unprotect(cookieBytes, TokenCookieKey, "Token:" + token);
			return Encoding.UTF8.GetString(clearBytes);
		}
	}
}