Skip to content

Commit 1a3711b

Browse files
committed
Add RevokeToken to AuthProvider
Allow AuthProvider to be initialized with IWebAgent Tweak WebAgent to allow empty response
1 parent b122343 commit 1a3711b

File tree

2 files changed

+63
-20
lines changed

2 files changed

+63
-20
lines changed

RedditSharp/AuthProvider.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class AuthProvider
1111
{
1212
private const string AccessUrl = "https://ssl.reddit.com/api/v1/access_token";
1313
private const string OauthGetMeUrl = "https://oauth.reddit.com/api/v1/me";
14+
private const string RevokeUrl = "https://www.reddit.com/api/v1/revoke_token";
1415

1516
public static string OAuthToken { get; set; }
1617
public static string RefreshToken { get; set; }
@@ -39,7 +40,7 @@ public enum Scope
3940
wikiedit = 0x20000,
4041
wikiread = 0x40000
4142
}
42-
private readonly IWebAgent _webAgent;
43+
private IWebAgent _webAgent;
4344
private readonly string _redirectUri;
4445
private readonly string _clientId;
4546
private readonly string _clientSecret;
@@ -57,6 +58,20 @@ public AuthProvider(string clientId, string clientSecret, string redirectUri)
5758
_redirectUri = redirectUri;
5859
_webAgent = new WebAgent();
5960
}
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+
}
6075

6176
/// <summary>
6277
/// Creates the reddit OAuth2 Url to redirect the user to for authorization.
@@ -154,6 +169,26 @@ public string GetOAuthToken(string username, string password)
154169
throw new AuthenticationException("Could not log in.");
155170
}
156171

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+
}
157192
/// <summary>
158193
/// Gets a user authenticated by OAuth2.
159194
/// </summary>

RedditSharp/WebAgent.cs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,34 +105,42 @@ public JToken CreateAndExecuteRequest(string url)
105105
public JToken ExecuteRequest(HttpWebRequest request)
106106
{
107107
EnforceRateLimit();
108-
var response = request.GetResponse();
108+
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
109109
var result = GetResponseString(response.GetResponseStream());
110110

111-
var json = JToken.Parse(result);
112-
try
111+
JToken json;
112+
if (!string.IsNullOrEmpty(result))
113113
{
114-
if (json["json"] != null)
114+
json = JToken.Parse(result);
115+
try
115116
{
116-
json = json["json"]; //get json object if there is a root node
117-
}
118-
if (json["error"] != null)
119-
{
120-
switch (json["error"].ToString())
117+
if (json["json"] != null)
118+
{
119+
json = json["json"]; //get json object if there is a root node
120+
}
121+
if (json["error"] != null)
121122
{
122-
case "404":
123-
throw new Exception("File Not Found");
124-
case "403":
125-
throw new Exception("Restricted");
126-
case "invalid_grant":
127-
//Refresh authtoken
128-
//AccessToken = authProvider.GetRefreshToken();
129-
//ExecuteRequest(request);
130-
break;
123+
switch (json["error"].ToString())
124+
{
125+
case "404":
126+
throw new Exception("File Not Found");
127+
case "403":
128+
throw new Exception("Restricted");
129+
case "invalid_grant":
130+
//Refresh authtoken
131+
//AccessToken = authProvider.GetRefreshToken();
132+
//ExecuteRequest(request);
133+
break;
134+
}
131135
}
132136
}
137+
catch
138+
{
139+
}
133140
}
134-
catch
141+
else
135142
{
143+
json = JToken.Parse("{'method':'" + response.Method + "','uri':'" + response.ResponseUri.AbsoluteUri + "','status':'" + response.StatusCode.ToString() + "'}");
136144
}
137145
return json;
138146

0 commit comments

Comments
 (0)