-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOAuthProperties.cs
149 lines (131 loc) · 3.95 KB
/
OAuthProperties.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
using System;
using System.Text;
using System.Collections.Generic;
using System.Collections.Specialized;
using OAwesomeAuth.Extensions;
namespace OAwesomeAuth
{
//class that is a container for all of the oauth properties
//Indexer time!
public class OAuthProperties
{
//some stuff is named a bit differently to avoid namespace collisions
//but mostly because I'm terrible at everything
private enum OAuthProperty
{
ConsumerKey,
ConsumerSecret,
TokenSecret,
Version,
Nonce,
Timestamp,
Token,
AccessSecret
}
private String[] _dataMembers;
public String ConsumerKey
{
get { return (String)_dataMembers[(int)OAuthProperty.ConsumerKey]; }
set { _dataMembers[(int)OAuthProperty.ConsumerKey] = value; }
}
public String ConsumerSecret
{
get { return (String)_dataMembers[(int)OAuthProperty.ConsumerSecret]; }
set { _dataMembers[(int)OAuthProperty.ConsumerSecret] = value; }
}
public String Version
{
get { return (String)_dataMembers[(int)OAuthProperty.Version]; }
set { _dataMembers[(int)OAuthProperty.Version] = value; }
}
public String TokenSecret
{
get { return _dataMembers[(int)OAuthProperty.TokenSecret]; }
set { _dataMembers[(int)OAuthProperty.TokenSecret] = value; }
}
public String Nonce
{
get { return _dataMembers[(int)OAuthProperty.Nonce]; }
set { _dataMembers[(int)OAuthProperty.Nonce] = value; }
}
public String Timestamp
{
get { return _dataMembers[(int)OAuthProperty.Timestamp]; }
set { _dataMembers[(int)OAuthProperty.Timestamp] = value; }
}
public String Token
{
get { return _dataMembers[(int)OAuthProperty.Token]; }
set { _dataMembers[(int)OAuthProperty.Token] = value; }
}
public SignatureMethod SignatureMethod { get; set; }
public String Signature { get; set; }
public OAuthProperties ()
{
_dataMembers = new String[(int)Enum.GetValues (typeof(OAuthProperty)).Length];
this.SignatureMethod = SignatureMethod.HmacSha1;
this.Version = "1.0";
}
public String ToQueryString ()
{
NameValueCollection nvc = this.ToNameValueCollection();
nvc.Remove("oauth_consumer_secret");
nvc.Remove("oauth_access_secret");
nvc.Remove("oauth_token_secret");
StringBuilder sb = new StringBuilder();
foreach(String key in nvc.SortKeys())
{
sb.Append(String.Format("{0}={1}&", key.EncodeRfc(), nvc[key].EncodeRfc()));
}
sb.Remove((sb.Length - 1), 1);
return sb.ToString();
}
public NameValueCollection ToNameValueCollection()
{
NameValueCollection nvc = new NameValueCollection();
Array properties = Enum.GetValues(typeof(OAuthProperty));
foreach (int n in properties)
{
if(_dataMembers[n] != null)
{
String propertyName = String.Format("oauth_{0}", properties.GetValue(n).ToString().ToUnderscore());
nvc.Add(propertyName, _dataMembers[n]);
}
}
// Signature method is a bit oddly named and doesn't fit into enum naming convetions well,
// we have to deal with it separately
nvc.Add("oauth_signature_method", SignatureMethodString(this.SignatureMethod));
return nvc;
}
public String ToHeaders ()
{
NameValueCollection nvc = this.ToNameValueCollection ();
//Never send secrets
nvc.Remove("oauth_access_secret");
nvc.Remove("oauth_token_secret");
StringBuilder sb = new StringBuilder ();
sb.Append ("OAuth ");
foreach (String key in nvc.SortKeys()) {
sb.Append (String.Format ("{0}=\"{1}\", ", key, nvc[key]));
}
sb.Remove((sb.Length - 2), 2);
return sb.ToString ();
}
public String SignatureMethodString (SignatureMethod s)
{
String x = String.Empty;
switch (s) {
case SignatureMethod.HmacSha1:
x = "HMAC-SHA1";
break;
case SignatureMethod.RsaSha1:
x = "RSA-SHA1";
break;
case SignatureMethod.Plaintext:
x = "PLAINTEXT";
break;
}
return x;
}
}
}