summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.AspNet/Clients/OAuth/CookieOAuthTokenManager.cs
blob: 398ee8508d09eb1aa0e1bd28b804a3dadf0801d3 (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
//-----------------------------------------------------------------------
// <copyright file="CookieOAuthTokenManager.cs" company="Microsoft">
//     Copyright (c) Microsoft. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

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

	/// <summary>
	/// Stores OAuth tokens in the current request's cookie.
	/// </summary>
	/// <remarks>
	/// This class is different from the <see cref="AuthenticationOnlyCookieOAuthTokenManager"/> in that 
	/// it also stores the access token after the authentication has succeeded.
	/// </remarks>
	public class CookieOAuthTokenManager : AuthenticationOnlyCookieOAuthTokenManager {
		/// <summary>
		/// Initializes a new instance of the <see cref="CookieOAuthTokenManager"/> class.
		/// </summary>
		public CookieOAuthTokenManager() {
		}

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

		/// <summary>
		/// Gets the token secret from the specified token.
		/// </summary>
		/// <param name="token">The token.</param>
		/// <returns>
		/// The token's secret
		/// </returns>
		public override string GetTokenSecret(string token) {
			string secret = base.GetTokenSecret(token);
			if (secret != null) {
				return secret;
			}

			// The base class checks for cookies in the Request object. 
			// Here we check in the Response object as well because we 
			// may have set it earlier in the request life cycle.
			HttpCookie cookie = this.Context.Response.Cookies[TokenCookieKey];
			if (cookie == null || string.IsNullOrEmpty(cookie.Values[token])) {
				return null;
			}

			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 override void ReplaceRequestTokenWithAccessToken(string requestToken, string accessToken, string accessTokenSecret) {
			var cookie = new HttpCookie(TokenCookieKey) {
				HttpOnly = true
			};

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

			var encryptedToken = ProtectAndEncodeToken(accessToken, accessTokenSecret);
			cookie.Values[accessToken] = encryptedToken;

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