summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.AspNet/Clients/OAuth/AuthenticationOnlyCookieOAuthTokenManager.cs
diff options
context:
space:
mode:
authorAndrew Arnott <andrewarnott@gmail.com>2012-05-07 10:03:12 -0700
committerAndrew Arnott <andrewarnott@gmail.com>2012-05-07 10:03:12 -0700
commit957a1811bc69a033a16b00d755a88ceeaf3fced6 (patch)
treeded97d06a1bec55e0d6bad85d079c2d4b412aa1d /src/DotNetOpenAuth.AspNet/Clients/OAuth/AuthenticationOnlyCookieOAuthTokenManager.cs
parenta85cd1c7bb0a22ee08056a19ce60173e3ab8e0e0 (diff)
parentb6dff7d1a6b5b07450b82688ec4727b3e2617ff5 (diff)
downloadDotNetOpenAuth-957a1811bc69a033a16b00d755a88ceeaf3fced6.zip
DotNetOpenAuth-957a1811bc69a033a16b00d755a88ceeaf3fced6.tar.gz
DotNetOpenAuth-957a1811bc69a033a16b00d755a88ceeaf3fced6.tar.bz2
Merge pull request #140 from dotnetjunky/v4.0
Use cookie to store OAuth token and set it as default mechanism.
Diffstat (limited to 'src/DotNetOpenAuth.AspNet/Clients/OAuth/AuthenticationOnlyCookieOAuthTokenManager.cs')
-rw-r--r--src/DotNetOpenAuth.AspNet/Clients/OAuth/AuthenticationOnlyCookieOAuthTokenManager.cs96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/DotNetOpenAuth.AspNet/Clients/OAuth/AuthenticationOnlyCookieOAuthTokenManager.cs b/src/DotNetOpenAuth.AspNet/Clients/OAuth/AuthenticationOnlyCookieOAuthTokenManager.cs
new file mode 100644
index 0000000..10cf39d
--- /dev/null
+++ b/src/DotNetOpenAuth.AspNet/Clients/OAuth/AuthenticationOnlyCookieOAuthTokenManager.cs
@@ -0,0 +1,96 @@
+//-----------------------------------------------------------------------
+// <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>
+ private 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>
+ private 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 string GetTokenSecret(string token) {
+ HttpCookie cookie = this.Context.Request.Cookies[TokenCookieKey];
+ if (cookie == null || string.IsNullOrEmpty(cookie.Values[token])) {
+ return null;
+ }
+ byte[] cookieBytes = HttpServerUtility.UrlTokenDecode(cookie.Values[token]);
+ byte[] clearBytes = MachineKeyUtil.Unprotect(cookieBytes, TokenCookieKey, "Token:" + token);
+
+ string secret = Encoding.UTF8.GetString(clearBytes);
+ 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 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 void StoreRequestToken(string requestToken, string requestTokenSecret) {
+ var cookie = new HttpCookie(TokenCookieKey);
+ byte[] cookieBytes = Encoding.UTF8.GetBytes(requestTokenSecret);
+ var secretBytes = MachineKeyUtil.Protect(cookieBytes, TokenCookieKey, "Token:" + requestToken);
+ cookie.Values[requestToken] = HttpServerUtility.UrlTokenEncode(secretBytes);
+ this.Context.Response.Cookies.Set(cookie);
+ }
+ }
+} \ No newline at end of file