@@ -11,6 +11,7 @@ public class AuthProvider
11
11
{
12
12
private const string AccessUrl = "https://ssl.reddit.com/api/v1/access_token" ;
13
13
private const string OauthGetMeUrl = "https://oauth.reddit.com/api/v1/me" ;
14
+ private const string RevokeUrl = "https://www.reddit.com/api/v1/revoke_token" ;
14
15
15
16
public static string OAuthToken { get ; set ; }
16
17
public static string RefreshToken { get ; set ; }
@@ -39,7 +40,7 @@ public enum Scope
39
40
wikiedit = 0x20000 ,
40
41
wikiread = 0x40000
41
42
}
42
- private readonly IWebAgent _webAgent ;
43
+ private IWebAgent _webAgent ;
43
44
private readonly string _redirectUri ;
44
45
private readonly string _clientId ;
45
46
private readonly string _clientSecret ;
@@ -57,6 +58,20 @@ public AuthProvider(string clientId, string clientSecret, string redirectUri)
57
58
_redirectUri = redirectUri ;
58
59
_webAgent = new WebAgent ( ) ;
59
60
}
61
+ /// <summary>
62
+ /// Allows use of reddit's OAuth interface, using an app set up at https://ssl.reddit.com/prefs/apps/.
63
+ /// </summary>
64
+ /// <param name="clientId">Granted by reddit as part of app.</param>
65
+ /// <param name="clientSecret">Granted by reddit as part of app.</param>
66
+ /// <param name="redirectUri">Selected as part of app. Reddit will send users back here.</param>
67
+ /// <param name="agent">Implementation of IWebAgent to use to make requests.</param>
68
+ public AuthProvider ( string clientId , string clientSecret , string redirectUri , IWebAgent agent )
69
+ {
70
+ _clientId = clientId ;
71
+ _clientSecret = clientSecret ;
72
+ _redirectUri = redirectUri ;
73
+ _webAgent = agent ;
74
+ }
60
75
61
76
/// <summary>
62
77
/// Creates the reddit OAuth2 Url to redirect the user to for authorization.
@@ -154,6 +169,26 @@ public string GetOAuthToken(string username, string password)
154
169
throw new AuthenticationException ( "Could not log in." ) ;
155
170
}
156
171
172
+ public void RevokeToken ( string token , bool isRefresh )
173
+ {
174
+ string tokenType = isRefresh ? "refresh_token" : "access_token" ;
175
+ var request = _webAgent . CreatePost ( RevokeUrl ) ;
176
+
177
+ request . Headers [ "Authorization" ] = "Basic " + Convert . ToBase64String ( Encoding . Default . GetBytes ( _clientId + ":" + _clientSecret ) ) ;
178
+
179
+ var stream = request . GetRequestStream ( ) ;
180
+
181
+ _webAgent . WritePostBody ( stream , new
182
+ {
183
+ token = token ,
184
+ token_type = tokenType
185
+ } ) ;
186
+
187
+ stream . Close ( ) ;
188
+
189
+ _webAgent . ExecuteRequest ( request ) ;
190
+
191
+ }
157
192
/// <summary>
158
193
/// Gets a user authenticated by OAuth2.
159
194
/// </summary>
0 commit comments