-
Notifications
You must be signed in to change notification settings - Fork 2
/
OAuth2.cs
305 lines (249 loc) · 8.2 KB
/
OAuth2.cs
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
* A simple C# OAuth2 auth-code wrapper library.
*
* @author
* Stian Hanger ([email protected])
*
* @source
* https://github.com/nagilum/oauth2-csharp
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Web;
/// <summary>
/// A simple C# OAuth2 auth-code wrapper library.
/// </summary>
public class OAuth2 {
#region Class functionality
/// <summary>
/// Construct a OAuth2 forwarding URI to redirect with.
/// </summary>
/// <param name="provider">OAuth2 provider wrapper.</param>
/// <param name="redirectUri">URI to redirect back to the system.</param>
/// <param name="locale">Language locale for provider interface.</param>
/// <returns>URI to redirect system to, for user authorization.</returns>
public static string CreateRedirect(OAuth2Provider provider, string redirectUri, string locale = "en") {
var parameters = new Dictionary<string, string> {
{"client_id", provider.ClientId},
{"display", "page"},
{"locale", locale},
{"redirect_uri", redirectUri},
{"response_type", "code"}
};
if (provider.Offline)
parameters.Add(
"access_type",
"offline");
if (!string.IsNullOrWhiteSpace(provider.Scope))
parameters.Add(
"scope",
provider.Scope);
if (!string.IsNullOrWhiteSpace(provider.State))
parameters.Add(
"state",
provider.State);
var qs = buildQueryString(parameters);
var url =
provider.AuthUri + "?" +
qs;
return url;
}
/// <summary>
/// Request a access token by exchanging a auth code.
/// </summary>
/// <param name="provider">OAuth2 provider wrapper.</param>
/// <param name="redirectUri">URI to redirect back to the system.</param>
/// <param name="code">Authorization code.</param>
/// <returns>Authentication response object.</returns>
public static OAuth2AuthenticateResponse AuthenticateByCode(OAuth2Provider provider, string redirectUri, string code) {
var parameters = new Dictionary<string, string> {
{"client_id", provider.ClientId},
{"client_secret", provider.ClientSecret},
{"redirect_uri", redirectUri},
{"code", code},
{"grant_type", "authorization_code"}
};
if (!string.IsNullOrWhiteSpace(provider.Scope))
parameters.Add(
"scope",
provider.Scope);
if (!string.IsNullOrWhiteSpace(provider.State))
parameters.Add(
"state",
provider.State);
var reply = request(
provider.AccessTokenUri,
payload: buildQueryString(parameters));
return interpretReply(reply);
}
/// <summary>
/// Request a new access token by refreshing an old.
/// </summary>
/// <param name="provider">OAuth2 provider wrapper.</param>
/// <param name="refreshToken">Access/refresh token to use.</param>
/// <returns>Authentication response object.</returns>
public static OAuth2AuthenticateResponse AuthenticateByToken(OAuth2Provider provider, string refreshToken) {
var parameters = new Dictionary<string, string> {
{"client_id", provider.ClientId},
{"client_secret", provider.ClientSecret},
{"refresh_token", refreshToken},
{"grant_type", "refresh_token"}
};
if (!string.IsNullOrWhiteSpace(provider.Scope))
parameters.Add(
"scope",
provider.Scope);
if (!string.IsNullOrWhiteSpace(provider.State))
parameters.Add(
"state",
provider.State);
var reply = request(
provider.AccessTokenUri,
payload: buildQueryString(parameters));
return interpretReply(reply);
}
/// <summary>
/// Get user info from the providers user endpoint.
/// </summary>
/// <param name="provider">OAuth2 provider wrapper.</param>
/// <param name="accessToken">Access token to use.</param>
/// <returns>Raw data from the provider.</returns>
public static string GetUserInfo(OAuth2Provider provider, string accessToken) {
var parameters = new Dictionary<string, string> {
{"access_token", accessToken}
};
return request(
provider.UserInfoUri,
"GET",
buildQueryString(parameters));
}
#endregion
#region Helper functions
/// <summary>
/// Construct a query-string from dictionary.
/// </summary>
/// <param name="parameters">Set of parameters in dictionary form to construct from.</param>
/// <returns>Query-string.</returns>
private static string buildQueryString(Dictionary<string, string> parameters) {
return parameters.Aggregate(
"",
(c, p) => c + ("&" + p.Key + "=" + HttpUtility.UrlEncode(p.Value)))
.Substring(1);
}
/// <summary>
/// Interpret the reply from the auth-call.
/// </summary>
/// <param name="reply">The string body from the web-request.</param>
/// <returns>Authentication response object.</returns>
private static OAuth2AuthenticateResponse interpretReply(string reply) {
var response = new OAuth2AuthenticateResponse();
if (reply.StartsWith("{")) { // JSON
var dict = JsonSerializer.Deserialize<Dictionary<string, object>>(reply);
if (dict.ContainsKey("access_token")) response.AccessToken = dict["access_token"].ToString();
if (dict.ContainsKey("refresh_token")) response.RefreshToken = dict["refresh_token"].ToString();
if (dict.ContainsKey("state")) response.State = dict["state"].ToString();
var seconds = 0;
if (dict.ContainsKey("expires")) int.TryParse(dict["expires"].ToString(), out seconds);
if (dict.ContainsKey("expires_in")) int.TryParse(dict["expires_in"].ToString(), out seconds);
if (seconds > 0)
response.Expires = DateTime.Now.AddSeconds(seconds);
}
else if (reply.Contains('&')) { // QueryString
var dict = reply.Split('&');
foreach (var entry in dict) {
var index = entry.IndexOf('=');
if (index == -1)
continue;
var key = entry.Substring(0, index);
var value = entry.Substring(index + 1);
switch (key) {
case "access_token":
response.AccessToken = value;
break;
case "refresh_token":
response.RefreshToken = value;
break;
case "state":
response.State = value;
break;
case "expires":
case "expires_in":
int seconds;
if (int.TryParse(value, out seconds))
response.Expires = DateTime.Now.AddSeconds(seconds);
break;
}
}
}
return response;
}
/// <summary>
/// Make a web-request and return response string.
/// </summary>
/// <param name="uri">URI to contact.</param>
/// <param name="method">HTTP method to use.</param>
/// <param name="payload">Payload to deliver via query-string or body.</param>
/// <returns>Response from web-request.</returns>
private static string request(string uri, string method = "POST", string payload = null) {
if (method == "GET" &&
!string.IsNullOrWhiteSpace(payload))
uri += "?" + payload;
var request = WebRequest.Create(uri) as HttpWebRequest;
if (request == null)
throw new WebException("Could not create WebRequest.");
request.Method = method;
request.ContentType = "application/x-www-form-urlencoded;";
request.Expect = null;
Stream stream;
if (method == "POST" &&
!string.IsNullOrWhiteSpace(payload)) {
var buffer = Encoding.UTF8.GetBytes(payload);
request.ContentLength = buffer.Length;
stream = request.GetRequestStream();
stream.Write(buffer, 0, buffer.Length);
stream.Close();
}
else {
request.ContentLength = 0;
}
var response = request.GetResponse() as HttpWebResponse;
if (response == null)
throw new WebException("Could not get response from request.");
stream = response.GetResponseStream();
if (stream == null)
throw new WebException("Could not get stream from response.");
var reader = new StreamReader(stream);
var body = reader.ReadToEnd();
reader.Close();
stream.Close();
return body;
}
#endregion
}
/// <summary>
/// OAuth2 provider wrapper.
/// </summary>
public class OAuth2Provider {
public string ClientId { get; set; }
public string ClientSecret { get; set; }
public string AuthUri { get; set; }
public string AccessTokenUri { get; set; }
public string UserInfoUri { get; set; }
public string Scope { get; set; }
public string State { get; set; }
public bool Offline = false;
}
/// <summary>
/// Authentication response object.
/// </summary>
public class OAuth2AuthenticateResponse {
public string AccessToken { get; set; }
public string RefreshToken { get; set; }
public DateTime Expires { get; set; }
public string State { get; set; }
}