@@ -17,7 +17,7 @@ public class AuthenticationOnlyCookieOAuthTokenManager : IOAuthTokenManager {
1717 /// <summary>
1818 /// Key used for token cookie
1919 /// </summary>
20- private const string TokenCookieKey = "OAuthTokenSecret" ;
20+ protected const string TokenCookieKey = "OAuthTokenSecret" ;
2121
2222 /// <summary>
2323 /// Primary request context.
@@ -41,7 +41,7 @@ public AuthenticationOnlyCookieOAuthTokenManager(HttpContextBase context) {
4141 /// <summary>
4242 /// Gets the effective HttpContext object to use.
4343 /// </summary>
44- private HttpContextBase Context {
44+ protected HttpContextBase Context {
4545 get {
4646 return this . primaryContext ?? new HttpContextWrapper ( HttpContext . Current ) ;
4747 }
@@ -54,15 +54,13 @@ private HttpContextBase Context {
5454 /// <returns>
5555 /// The token's secret
5656 /// </returns>
57- public string GetTokenSecret ( string token ) {
57+ public virtual string GetTokenSecret ( string token ) {
5858 HttpCookie cookie = this . Context . Request . Cookies [ TokenCookieKey ] ;
5959 if ( cookie == null || string . IsNullOrEmpty ( cookie . Values [ token ] ) ) {
6060 return null ;
6161 }
62- byte [ ] cookieBytes = HttpServerUtility . UrlTokenDecode ( cookie . Values [ token ] ) ;
63- byte [ ] clearBytes = MachineKeyUtil . Unprotect ( cookieBytes , TokenCookieKey , "Token:" + token ) ;
6462
65- string secret = Encoding . UTF8 . GetString ( clearBytes ) ;
63+ string secret = DecodeAndUnprotectToken ( token , cookie . Values [ token ] ) ;
6664 return secret ;
6765 }
6866
@@ -72,7 +70,7 @@ public string GetTokenSecret(string token) {
7270 /// <param name="requestToken">The request token.</param>
7371 /// <param name="accessToken">The access token.</param>
7472 /// <param name="accessTokenSecret">The access token secret.</param>
75- public void ReplaceRequestTokenWithAccessToken ( string requestToken , string accessToken , string accessTokenSecret ) {
73+ public virtual void ReplaceRequestTokenWithAccessToken ( string requestToken , string accessToken , string accessTokenSecret ) {
7674 var cookie = new HttpCookie ( TokenCookieKey ) {
7775 Value = string . Empty ,
7876 Expires = DateTime . UtcNow . AddDays ( - 5 )
@@ -85,7 +83,7 @@ public void ReplaceRequestTokenWithAccessToken(string requestToken, string acces
8583 /// </summary>
8684 /// <param name="requestToken">The request token.</param>
8785 /// <param name="requestTokenSecret">The request token secret.</param>
88- public void StoreRequestToken ( string requestToken , string requestTokenSecret ) {
86+ public virtual void StoreRequestToken ( string requestToken , string requestTokenSecret ) {
8987 var cookie = new HttpCookie ( TokenCookieKey ) {
9088 HttpOnly = true
9189 } ;
@@ -94,10 +92,36 @@ public void StoreRequestToken(string requestToken, string requestTokenSecret) {
9492 cookie . Secure = true ;
9593 }
9694
97- byte [ ] cookieBytes = Encoding . UTF8 . GetBytes ( requestTokenSecret ) ;
98- var secretBytes = MachineKeyUtil . Protect ( cookieBytes , TokenCookieKey , "Token:" + requestToken ) ;
99- cookie . Values [ requestToken ] = HttpServerUtility . UrlTokenEncode ( secretBytes ) ;
95+ var encryptedToken = ProtectAndEncodeToken ( requestToken , requestTokenSecret ) ;
96+ cookie . Values [ requestToken ] = encryptedToken ;
97+
10098 this . Context . Response . Cookies . Set ( cookie ) ;
10199 }
100+
101+ /// <summary>
102+ /// Protect and url-encode the specified token secret.
103+ /// </summary>
104+ /// <param name="token">The token to be used as a key.</param>
105+ /// <param name="tokenSecret">The token secret to be protected</param>
106+ /// <returns>The encrypted and protected string.</returns>
107+ protected static string ProtectAndEncodeToken ( string token , string tokenSecret )
108+ {
109+ byte [ ] cookieBytes = Encoding . UTF8 . GetBytes ( tokenSecret ) ;
110+ var secretBytes = MachineKeyUtil . Protect ( cookieBytes , TokenCookieKey , "Token:" + token ) ;
111+ return HttpServerUtility . UrlTokenEncode ( secretBytes ) ;
112+ }
113+
114+ /// <summary>
115+ /// Url-decode and unprotect the specified encrypted token string.
116+ /// </summary>
117+ /// <param name="token">The token to be used as a key.</param>
118+ /// <param name="encryptedToken">The encrypted token to be decrypted</param>
119+ /// <returns>The original token secret</returns>
120+ protected static string DecodeAndUnprotectToken ( string token , string encryptedToken )
121+ {
122+ byte [ ] cookieBytes = HttpServerUtility . UrlTokenDecode ( encryptedToken ) ;
123+ byte [ ] clearBytes = MachineKeyUtil . Unprotect ( cookieBytes , TokenCookieKey , "Token:" + token ) ;
124+ return Encoding . UTF8 . GetString ( clearBytes ) ;
125+ }
102126 }
103127}
0 commit comments