-
Notifications
You must be signed in to change notification settings - Fork 0
/
OAuthParameters.cs
268 lines (233 loc) · 7.34 KB
/
OAuthParameters.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
/* Copyright (c) 2006 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
using System;
using System.Text;
using System.Collections.Generic;
namespace Google.GData.Client
{
/// <summary>
/// Stores the parameters used to make OAuth requests
/// </summary>
public class OAuthParameters
{
public readonly SortedDictionary<string, string> BaseProperties = new SortedDictionary<string, string>();
public readonly Dictionary<string, string> ExtraProperties = new Dictionary<string, string>();
public string Callback
{
get
{
return safeGet(ExtraProperties, OAuthBase.OAuthCallbackKey);
}
set
{
addOrUpdate(ExtraProperties, OAuthBase.OAuthCallbackKey, value);
}
}
public string ConsumerKey
{
get
{
return safeGet(BaseProperties, OAuthBase.OAuthConsumerKeyKey);
}
set
{
addOrUpdate(BaseProperties, OAuthBase.OAuthConsumerKeyKey, value);
}
}
public string ConsumerSecret
{
get
{
return safeGet(ExtraProperties, OAuthBase.OAuthConsumerSecretKey);
}
set
{
addOrUpdate(ExtraProperties, OAuthBase.OAuthConsumerSecretKey, value);
}
}
public string Nonce
{
get
{
return safeGet(BaseProperties, OAuthBase.OAuthNonceKey);
}
set
{
addOrUpdate(BaseProperties, OAuthBase.OAuthNonceKey, value);
}
}
public string Scope
{
get
{
return safeGet(ExtraProperties, OAuthBase.OAuthScopeKey);
}
set
{
addOrUpdate(ExtraProperties, OAuthBase.OAuthScopeKey, value);
}
}
public string Signature
{
get
{
return safeGet(ExtraProperties, OAuthBase.OAuthSignatureKey);
}
set
{
addOrUpdate(ExtraProperties, OAuthBase.OAuthSignatureKey, value);
}
}
public string SignatureMethod
{
get
{
return safeGet(BaseProperties, OAuthBase.OAuthSignatureMethodKey);
}
set
{
addOrUpdate(BaseProperties, OAuthBase.OAuthSignatureMethodKey, value);
}
}
public string Timestamp
{
get
{
return safeGet(BaseProperties, OAuthBase.OAuthTimestampKey);
}
set
{
addOrUpdate(BaseProperties, OAuthBase.OAuthTimestampKey, value);
}
}
public string Token
{
get
{
return safeGet(BaseProperties, OAuthBase.OAuthTokenKey);
}
set
{
addOrUpdate(BaseProperties, OAuthBase.OAuthTokenKey, value);
}
}
public string TokenSecret
{
get
{
return safeGet(ExtraProperties, OAuthBase.OAuthTokenSecretKey);
}
set
{
addOrUpdate(ExtraProperties, OAuthBase.OAuthTokenSecretKey, value);
}
}
public string Verifier
{
get
{
return safeGet(BaseProperties, OAuthBase.OAuthVerifierKey);
}
set
{
addOrUpdate(BaseProperties, OAuthBase.OAuthVerifierKey, value);
}
}
/// <summary>
/// Adds a new key-value pair to the dictionary or updates the value if the key is already present
/// </summary>
protected void addOrUpdate(IDictionary<string, string> dictionary, string key, string value)
{
if (dictionary.ContainsKey(key))
{
if (value == null)
{
dictionary.Remove(key);
}
else
{
dictionary[key] = value;
}
}
else if (value != null)
{
dictionary.Add(key, value);
}
}
/// <summary>
/// Returns the value corresponding to the key in the dictionary or null if the key is not present
/// </summary>
protected string safeGet(IDictionary<string, string> dictionary, string key)
{
if (dictionary.ContainsKey(key))
{
return dictionary[key];
}
else
{
return null;
}
}
}
/// <summary>
/// Stores the parameters used to make OAuth 2.0 requests
/// </summary>
public class OAuth2Parameters
{
public static string GoogleAuthUri = "https://accounts.google.com/o/oauth2/auth";
public static string GoogleTokenUri = "https://accounts.google.com/o/oauth2/token";
public OAuth2Parameters()
{
TokenUri = GoogleTokenUri;
AuthUri = GoogleAuthUri;
AccessType = "offline";
ResponseType = "code";
TokenType = "Bearer";
ApprovalPrompt = "auto";
}
public string ClientId { get; set; }
public string ClientSecret { get; set; }
public string RedirectUri { get; set; }
/// <summary>
/// Valid values are:
/// * "offline" (default): token endpoint returns both an access and refresh token.
/// * "online": only an access token is returned by the token endpoint.
/// </summary>
public string AccessType { get; set; }
/// <summary>
/// Valid values are:
/// * "code" (default): retrieve a code to be exchanged for an acces token.
/// * "token": directly retrieve an access token from the auth endpoint.
/// </summary>
public string ResponseType { get; set; }
/// <summary>
/// Valid values are:
/// * "auto" (default): only show the approval prompt if the user never approved.
/// * "force": always show the approval prompt.
/// </summary>
public String ApprovalPrompt { get; set; }
public String State { get; set; }
public String Scope { get; set; }
public string TokenUri { get; set; }
public string AuthUri { get; set; }
public string AccessCode { get; set; }
public string AccessToken { get; set; }
public string TokenType { get; set; }
public string RefreshToken { get; set; }
public DateTime TokenExpiry { get; set; }
}
}