-
Notifications
You must be signed in to change notification settings - Fork 0
/
OAuthConsumer.cs
105 lines (89 loc) · 2.78 KB
/
OAuthConsumer.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
using System;
using System.Net;
using System.Text;
using OAwesomeAuth.Extensions;
using System.Collections.Specialized;
namespace OAwesomeAuth
{
public class OAuthConsumer
{
public OAuthProperties AuthProperties { get; set; }
public RequestMethod AuthRequestMethod { get; set; }
public HttpAction HttpMethod;
public String RequestTokenUrl { get; set; }
public String AccessTokenUrl { get; set; }
public String ResourceUrl { get; set; }
private String _hashLine;
public String HashLine { get { return _hashLine; } }
private IDispatcher _dispatcher;
public OAuthConsumer ()
{
AuthProperties = new OAuthProperties();
AuthRequestMethod = RequestMethod.Header;
HttpMethod = HttpAction.GET;
_dispatcher = new Dispatcher();
}
public OAuthConsumer(IDispatcher d) : this()
{
_dispatcher = d;
}
public void GetRequestToken()
{
GenerateHashLine(RequestTokenUrl, AuthProperties.ToNameValueCollection(), null, null);
WebClient c = BuildRequest(RequestTokenUrl);
String r = _dispatcher.GetText(c);
}
private WebClient BuildRequest(String url, NameValueCollection post)
{
WebClient c = new WebClient();
Uri uri = new Uri(url);
StampTime();
AuthProperties.Nonce = Encryption.GenerateNonce();
GenerateHashLine(url, AuthProperties.ToNameValueCollection(), null , post);
AuthProperties.Signature = Encryption.SignRequest(_hashLine, AuthProperties);
switch (AuthRequestMethod) {
case(RequestMethod.Header):
c.Headers["Authorization"] = String.Format("OAuth {0}, {1}={2}", AuthProperties.ToHeaders(), "oauth_signature", "fart");
break;
case(RequestMethod.Query):
break;
case(RequestMethod.Form):
break;
}
return c;
}
private WebClient BuildRequest(String url)
{
return BuildRequest(url, null);
}
private void StampTime()
{
AuthProperties.Timestamp = ((int)(DateTime.UtcNow - new DateTime (1970, 1, 1)).TotalSeconds).ToString ();
}
private void GenerateHashLine(String url, NameValueCollection authParams, NameValueCollection queryParams, NameValueCollection postParams)
{
StringBuilder sb = new StringBuilder ();
sb.Append (HttpMethod);
sb.Append ("&");
sb.Append (url.EncodeRfc());
sb.Append ("&");
foreach(String key in authParams.SortKeys())
{
sb.Append(key.EncodeRfc());
sb.Append("%3D");
sb.Append(authParams[key].EncodeRfc());
sb.Append("%26");
}
if( queryParams != null )
{
//process
}
if( postParams != null )
{
//process
}
sb.Remove ((sb.Length - 3), 3);
_hashLine = sb.ToString();
}
}
}