forked from rahulpnath/Blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOauth500px.cs
318 lines (286 loc) · 13.9 KB
/
Oauth500px.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
306
307
308
309
310
311
312
313
314
315
316
317
318
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using Windows.Security.Authentication.Web;
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using Windows.Storage.Streams;
using _500px.DataModel;
namespace _500px.Common
{
public class Oauth500px
{
private string consumerKey;
private string consumerSecret;
public bool IsAuthenticated { get; set; }
private string OAuthAccessUrl;
private string OAuthRequestUrl;
private string OAuthAuthorizeUrl ;
private string OAuthCallbackUrl;
private string OAuthSignatureMethod;
private string OAuthSignatureMethodName;
private string OAuthVersion;
private RequestType requestType;
private Dictionary<string, string> AuthorizationParameters;
private OauthToken Token;
private string Data;
public Oauth500px(string ConsumerKey,string ConsumerSecret,string CallbackUrl, string RequestUrl, string AuthorizeUrl, string AccessUrl)
{
consumerKey = ConsumerKey;
consumerSecret = ConsumerSecret;
OAuthAccessUrl = AccessUrl;
OAuthAuthorizeUrl = AuthorizeUrl;
OAuthRequestUrl = RequestUrl;
OAuthCallbackUrl = CallbackUrl;
OAuthSignatureMethod = "HMAC-SHA1";
OAuthSignatureMethodName = "HMAC_SHA1";
OAuthVersion = "1.0";
requestType = RequestType.POST;
Token = new OauthToken();
}
public class OauthToken
{
public string Token { get; set; }
public string SecretCode { get; set; }
public string Verifier { get; set; }
}
public enum RequestType
{
GET,
POST,
DELETE
}
public class OauthParameter
{
public const string OauthCallback = "oauth_callback";
public const string OauthConsumerKey = "oauth_consumer_key";
public const string OauthNonce = "oauth_nonce";
public const string OauthSignatureMethod = "oauth_signature_method";
public const string OauthTimestamp = "oauth_timestamp";
public const string OauthVersion = "oauth_version";
public const string OauthSignature = "oauth_signature";
public const string OauthToken = "oauth_token";
public const string OauthVerifier = "oauth_verifier";
}
public Oauth500px MakeRequest(RequestType RequestType)
{
this.requestType = RequestType;
return this;
}
private string Nonce()
{
return new Random().Next(1000000000).ToString();
}
private string TimeStamp()
{
return Math.Round((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime()).TotalSeconds).ToString();
}
public async Task<bool> Authenticate()
{
try
{
await this.MakeRequest(Oauth500px.RequestType.POST).RequestToken();
await this.MakeRequest(RequestType.POST).AuthorizeToken();
await this.MakeRequest(RequestType.POST).AccessToken();
return IsAuthenticated= true;
}
catch (Exception e)
{
return IsAuthenticated = false;
}
}
public async Task<OauthToken> RequestToken()
{
AuthorizationParameters = new Dictionary<string, string>()
{
{
OauthParameter.OauthCallback, OAuthCallbackUrl
},
{OauthParameter.OauthConsumerKey, consumerKey},
{OauthParameter.OauthNonce, Nonce()},
{
OauthParameter.OauthSignatureMethod,
OAuthSignatureMethod
},
{OauthParameter.OauthTimestamp, TimeStamp()},
{OauthParameter.OauthVersion, OAuthVersion}
};
string response = await this.MakeRequest(RequestType.POST).Sign(OAuthRequestUrl,String.Empty).ExecuteRequest(OAuthRequestUrl);
if (response != null)
{
String[] keyValPairs = response.Split('&');
for (int i = 0; i < keyValPairs.Length; i++)
{
String[] splits = keyValPairs[i].Split('=');
switch (splits[0])
{
case "oauth_token":
Token.Token = splits[1];
break;
case "oauth_token_secret":
Token.SecretCode = splits[1];
break;
}
}
}
return Token;
}
public Oauth500px WithData(string data)
{
Data = data;
return this;
}
public async Task<OauthToken> AuthorizeToken()
{
var tempAuthorizeUrl = OAuthAuthorizeUrl + "?oauth_token=" + Token.Token;
System.Uri StartUri = new Uri(tempAuthorizeUrl);
System.Uri EndUri = new Uri(OAuthCallbackUrl);
var auth =
await
WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, StartUri, EndUri);
var responseData = auth.ResponseData;
responseData = responseData.Replace("http://www.bing.com/?", "");
var split = responseData.Split('&');
var keyValue = split[1].Split('=');
Token.Verifier = keyValue[1];
return Token;
}
public async Task<OauthToken> AccessToken()
{
AuthorizationParameters = new Dictionary<string, string>()
{
{OauthParameter.OauthConsumerKey, consumerKey},
{OauthParameter.OauthNonce, Nonce()},
{
OauthParameter.OauthSignatureMethod,
OAuthSignatureMethod
},
{OauthParameter.OauthTimestamp, TimeStamp()},
{OauthParameter.OauthToken,Token.Token},
{OauthParameter.OauthVerifier,Token.Verifier},
{OauthParameter.OauthVersion, OAuthVersion}
};
var response = await this.MakeRequest(RequestType.POST).Sign(OAuthAccessUrl, Token.SecretCode).ExecuteRequest(OAuthAccessUrl);
if (response != null)
{
String[] keyValPairs = response.Split('&');
for (int i = 0; i < keyValPairs.Length; i++)
{
String[] splits = keyValPairs[i].Split('=');
switch (splits[0])
{
case "oauth_token":
Token.Token = splits[1];
break;
case "oauth_token_secret":
Token.SecretCode = splits[1];
break;
}
}
}
return Token;
}
private Oauth500px Sign(string Url, string tokenSecret)
{
String SigBaseStringParams = String.Join("&", AuthorizationParameters.Select(key => key.Key + "=" + Uri.EscapeDataString(key.Value)));
String SigBaseString = requestType.ToString() + "&";
SigBaseString += Uri.EscapeDataString(Url) + "&" + Uri.EscapeDataString(SigBaseStringParams);
IBuffer KeyMaterial = CryptographicBuffer.ConvertStringToBinary(consumerSecret + "&" + tokenSecret, BinaryStringEncoding.Utf8);
MacAlgorithmProvider HmacSha1Provider = MacAlgorithmProvider.OpenAlgorithm(OAuthSignatureMethodName);
CryptographicKey MacKey = HmacSha1Provider.CreateKey(KeyMaterial);
IBuffer DataToBeSigned = CryptographicBuffer.ConvertStringToBinary(SigBaseString, BinaryStringEncoding.Utf8);
IBuffer SignatureBuffer = CryptographicEngine.Sign(MacKey, DataToBeSigned);
String Signature = CryptographicBuffer.EncodeToBase64String(SignatureBuffer);
AuthorizationParameters.Add(OauthParameter.OauthSignature, Signature);
return this;
}
private async Task<String> ExecuteRequest(String Url)
{
string DataToPost = string.Empty;
if (AuthorizationParameters != null)
{
DataToPost = "OAuth " +
String.Join(", ",
AuthorizationParameters.Select(
key =>
key.Key +
(string.IsNullOrEmpty(key.Value)
? string.Empty
: "=\"" + Uri.EscapeDataString(key.Value) + "\"")));
AuthorizationParameters.Clear();
}
string m_PostResponse = string.Empty;
try
{
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(Url);
Request.Method = requestType.ToString();
Request.Headers["Authorization"] = DataToPost;
HttpWebResponse Response = (HttpWebResponse)await Request.GetResponseAsync();
StreamReader ResponseDataStream = new StreamReader(Response.GetResponseStream());
m_PostResponse = await ResponseDataStream.ReadToEndAsync();
}
catch (Exception Err)
{
}
return m_PostResponse;
}
public async Task<T> ExecuteRequest<T>(string Url,Dictionary<string,string> Parameters) where T:class
{
AuthorizationParameters = new Dictionary<string, string>()
{
{OauthParameter.OauthConsumerKey, consumerKey},
{OauthParameter.OauthNonce, Nonce()},
{OauthParameter.OauthSignatureMethod, OAuthSignatureMethod},
{OauthParameter.OauthTimestamp, TimeStamp()},
{OauthParameter.OauthToken, Token.Token},
{OauthParameter.OauthVersion, OAuthVersion}
};
string RequestUrl;
if (Parameters != null && Parameters.Count > 0)
{
RequestUrl = Url + "?" +
String.Join("&",
Parameters.Select(a => a.Key + (string.IsNullOrEmpty(a.Value) ?string.Empty : "=" + Uri.EscapeDataString(a.Value))).ToArray());
foreach (var parameter in Parameters)
{
AuthorizationParameters.Add(parameter.Key,parameter.Value);
}
}
else
RequestUrl = Url;
var response = await this.MakeRequest(requestType).Sign(Url,Token.SecretCode).ExecuteRequest(RequestUrl);
if (string.IsNullOrEmpty(response))
return null;
DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(T));
T dat = json.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(response))) as T;
return dat;
}
public async Task<T> ExecuteNonAuthorizedRequest<T>(string Url,Dictionary<string,string> Parameters ) where T: class
{
string RequestUrl;
if (Parameters != null && Parameters.Count > 0)
{
RequestUrl = Url + "?" +
String.Join("&",
Parameters.Select(a => a.Key + (string.IsNullOrEmpty(a.Value) ?string.Empty : "=" + Uri.EscapeDataString(a.Value))).ToArray());
foreach (var parameter in Parameters)
{
AuthorizationParameters.Add(parameter.Key,parameter.Value);
}
}
else
RequestUrl = Url;
var response = await this.MakeRequest(requestType).ExecuteRequest(RequestUrl);
if (string.IsNullOrEmpty(response))
return null;
DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(T));
T dat = json.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(response))) as T;
return dat;
}
}
}