-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProxyConfig.cs
257 lines (224 loc) · 7.27 KB
/
ProxyConfig.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
using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;
using System.Xml.Serialization;
using Newtonsoft.Json;
namespace Proxy
{
enum TokenRequestType
{
None = 0,
User,
OAuth
}
[XmlRoot("ProxyConfig")]
public class ProxyConfig
{
#region Static Members
private static object _lockobject = new object();
public static ProxyConfig LoadProxyConfig(string fileName)
{
ProxyConfig config = null;
lock (_lockobject)
{
if (File.Exists(fileName))
{
XmlSerializer reader = new XmlSerializer(typeof(ProxyConfig));
using (var file = new StreamReader(fileName))
{
config = (ProxyConfig)reader.Deserialize(file);
}
}
}
return config;
}
public static ProxyConfig GetCurrentConfig()
{
ProxyConfig config = HttpRuntime.Cache["proxyConfig"] as ProxyConfig;
if (config == null)
{
string fileName = GetFilename(HttpContext.Current);
config = LoadProxyConfig(fileName);
if (config != null)
{
CacheDependency dep = new CacheDependency(fileName);
HttpRuntime.Cache.Insert("proxyConfig", config, dep);
}
}
return config;
}
public static string GetFilename(HttpContext context)
{
return context.Server.MapPath("~/proxy.config");
}
#endregion
ServerUrl[] serverUrls;
bool mustMatch;
[XmlArray("serverUrls")]
[XmlArrayItem("serverUrl")]
public ServerUrl[] ServerUrls
{
get { return this.serverUrls; }
set { this.serverUrls = value; }
}
[XmlAttribute("mustMatch")]
public bool MustMatch
{
get { return mustMatch; }
set { mustMatch = value; }
}
private static TokenRequestType GetTokenRequestType(string userName, string password, string clientId, string clientSecret)
{
return !string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password) ? TokenRequestType.User
: !string.IsNullOrEmpty(clientId) && !string.IsNullOrEmpty(clientSecret) ? TokenRequestType.OAuth
: TokenRequestType.None;
}
/// <summary>
/// Creates the token request URL.
/// </summary>
/// <param name="userName">User name</param>
/// <param name="password">password</param>
/// <param name="expires">Number of minutes until the token will expire</param>
/// <returns>Returns a <see cref="Uri"/>.</returns>
private static Uri CreateTokenRequestUrl(string userName, string password, string clientId, string clientSecret, out TokenRequestType requestType, int? expires=default(int?))
{
requestType = GetTokenRequestType(userName, password, clientId, clientSecret);
if (requestType == TokenRequestType.None)
{
throw new ArgumentNullException("None of the following parameters should be null: userName, password.", default(Exception));
}
// Get the requesting URL. The query string portion will be removed.
string referrer = Regex.Match(HttpContext.Current.Request.Url.ToString(), @"[^\?]+").Value;
string url;
if (requestType == TokenRequestType.User)
{
const string _tokenUrl = "https://www.arcgis.com/sharing/generateToken?username={0}&password={1}&referer={2}&expiration={3}&f=json";
url = string.Format(_tokenUrl, userName, password, referrer, expires.HasValue ? expires.ToString() : string.Empty);
}
else
{
const string _tokenUrl = "https://www.arcgis.com/sharing/oauth2/token?client_id={0}&grant_type=client_credentials&client_secret={1}&f=json";
url = string.Format(_tokenUrl, clientId, clientSecret, referrer, expires.HasValue ? expires.ToString() : string.Empty);
}
return new Uri(url);
}
/// <summary>
/// Gets the token for the specified service URI.
/// </summary>
/// <param name="uri"></param>
/// <returns>
/// Returns the token. Could return an empty string if there are no
/// matching URIs in proxy.config and "mustMatch" is set to "false".
/// </returns>
/// <exception cref="InvalidOperationException">
/// Raised if there is no matching URI in proxy.config and "mustMatch" is set to "true".
/// </exception>
public string GetToken(string uri)
{
#if DEBUG
Trace.TraceInformation("Entering GetToken(\"{0}\") method...", uri);
#endif
string agolUser, agolPW, agolClientId, agolClientSecret;
agolUser = ConfigurationManager.AppSettings["agolUser"];
agolPW = ConfigurationManager.AppSettings["agolPassword"];
agolClientId = ConfigurationManager.AppSettings["agolClientId"];
agolClientSecret = ConfigurationManager.AppSettings["agolClientSecret"];
Trace.TraceInformation("{0}:{1}", agolUser, agolPW);
string token = string.Empty;
foreach (ServerUrl su in serverUrls)
{
if (su.MatchAll && uri.StartsWith(su.Url, StringComparison.InvariantCultureIgnoreCase))
{
#if DEBUG
Trace.TraceInformation("URI partial match found: {0} matches {1}.", su.Url, uri);
#endif
token = su.DynamicToken ? GetTokenForUrl(agolUser, agolPW, agolClientId, agolClientSecret, su) : su.Token;
break;
}
else
{
if (String.Compare(uri, su.Url, StringComparison.InvariantCultureIgnoreCase) == 0)
{
#if DEBUG
Trace.TraceInformation("URI match found: {0} matches {1}.", su.Url, uri);
#endif
token = su.DynamicToken ? GetTokenForUrl(agolUser, agolPW, agolClientId, agolClientSecret, su) : su.Token;
break;
}
}
}
if (string.IsNullOrEmpty(token) && mustMatch)
{
const string msg = "The \"mustMatch\" option is specified in proxy.config, but no matching URLs were found.";
#if DEBUG
Trace.TraceError(msg);
#endif
throw new InvalidOperationException(msg);
}
return token;
}
private static string GetTokenForUrl(string agolUser, string agolPW, string agolClientId, string agolClientSecret, ServerUrl su)
{
// If the URL has the dynamic token attribute set to true,
// create a new token if there is no current token or if the
// current token is expired.
if (su.Token == null || su.TokenHasExpired)
{
TokenRequestType tokenRequestType;
var url = CreateTokenRequestUrl(agolUser, agolPW, agolClientId, agolClientSecret, out tokenRequestType);
#if DEBUG
Trace.TraceInformation("Requesting token from \"{0}\"...", url);
#endif
// Get a new token.
var request = WebRequest.Create(url);
string json;
using (var response = request.GetResponse())
{
using (var stream = response.GetResponseStream())
{
using (var reader = new StreamReader(stream))
{
json = reader.ReadToEnd();
}
}
}
#if DEBUG
Trace.TraceInformation("Response from token request: {0}", json);
#endif
try
{
if (tokenRequestType == TokenRequestType.OAuth)
{
OAuthToken token;
token = JsonConvert.DeserializeObject<OAuthToken>(json, new IntToDateConverter());
// Set the server URL properties corresponding to the token.
su.Token = token.AccessToken;
su.TokenExpires = token.Expires;
}
else
{
Token token;
token = JsonConvert.DeserializeObject<Token>(json, new IntToDateConverter());
// Set the server URL properties corresponding to the token.
su.Token = token.token;
su.TokenExpires = token.expires;
}
}
catch (JsonException ex)
{
#if DEBUG
Trace.TraceError("{0}", ex);
#endif
throw;
}
}
return su.Token;
}
}
}