This repository has been archived by the owner on May 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathStartup.cs
426 lines (359 loc) · 17.6 KB
/
Startup.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Text.Encodings.Web;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http.Headers;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OAuth;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System.Text.Json;
using System.IO;
namespace app
{
public class JsonResponse
{
public String access_token { get; set; }
public long expires_in { get; set; }
public String bearer { get; set; }
public String refresh_token { get; set; }
public long refresh_token_expires_in { get; set; }
public String scope { get; set; }
public String requested_by_id { get; set; }
}
public class JsonAccessTokenFile
{
public String access_token { get; set; }
public long expires_at { get; set; }
public String refresh_token { get; set; }
public long refresh_token_expires_at { get; set; }
}
public class JsonClientApplicationFile
{
public JsonClientApplicationFileConfigSection config { get; set; }
}
public class JsonClientApplicationFileConfigSection
{
public String client_id { get; set; }
public String client_secret { get; set; }
public String callback_url { get; set; }
}
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
const string API_URL = "https://api.accounting.sage.com/v3.1/";
const string AUTHORIZATION_ENDPOINT = "https://www.sageone.com/oauth2/auth/central?filter=apiv3.1";
const string TOKEN_ENDPOINT = "https://oauth.accounting.sage.com/token";
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
String config_client_id = "initial";
String config_client_secret = "initial";
String config_calback_url = "initial";
if (!(getPathOfConfigFile().Equals("")))
{
String fs = File.ReadAllText(getPathOfConfigFile());
{
var content = System.Text.Json.JsonSerializer.Deserialize<JsonClientApplicationFile>(fs);
config_client_id = content.config.client_id;
config_client_secret = content.config.client_secret;
config_calback_url = content.config.callback_url;
}
}
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.Cookie.HttpOnly = false;
options.Cookie.IsEssential = true;
options.IdleTimeout = TimeSpan.FromHours(1);
});
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddRazorPages();
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(o => o.LoginPath = new PathString("/login"))
.AddOAuth("oauth2", "Sage Accounting", o =>
{
o.ClientId = config_client_id;
o.ClientSecret = config_client_secret;
o.CallbackPath = new PathString("/auth/callback");
o.AuthorizationEndpoint = AUTHORIZATION_ENDPOINT;
o.TokenEndpoint = TOKEN_ENDPOINT;
o.SaveTokens = true;
o.Scope.Add("full_access");
o.Events = new OAuthEvents
{
OnRemoteFailure = HandleOnRemoteFailure,
OnCreatingTicket = async context =>
{
long tok_expires_in = context.TokenResponse.Response.RootElement.GetProperty("expires_in").GetInt64();
long tok_refresh_token_expires_in = context.TokenResponse.Response.RootElement.GetProperty("refresh_token_expires_in").GetInt64();
tokenfileWrite(context.AccessToken,
calculateUnixtimestampWithOffset(tok_expires_in),
context.RefreshToken,
calculateUnixtimestampWithOffset(tok_refresh_token_expires_in),
context.HttpContext);
return;
}
};
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseRouting();
app.UseStaticFiles();
app.UseSession();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseEndpoints(endpoints => endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}"));
// get access token
app.Map("/login", signinApp =>
{
signinApp.Run(async context =>
{
await context.ChallengeAsync("oauth2", new AuthenticationProperties() { RedirectUri = "/" });
return;
});
});
// Refresh the access token
app.Map("/refresh_token", signinApp =>
{
signinApp.Run(async context =>
{
var response = context.Response;
// This is what [Authorize] calls
var userResult = await context.AuthenticateAsync();
var user = userResult.Principal;
var authProperties = userResult.Properties;
// Deny anonymous request beyond this point.
if (!userResult.Succeeded || user == null || !user.Identities.Any(identity => identity.IsAuthenticated))
{
// This is what [Authorize] calls
// The cookie middleware will handle this and redirect to /login
await context.ChallengeAsync();
return;
}
var currentAuthType = user.Identities.First().AuthenticationType;
var refreshToken = authProperties.GetTokenValue("refresh_token");
if (string.IsNullOrEmpty(refreshToken))
{
response.ContentType = "text/html";
await response.WriteAsync("<html><body>");
await response.WriteAsync("No refresh_token is available.<br>");
await response.WriteAsync("<a href=\"/\">Home</a>");
await response.WriteAsync("</body></html>");
return;
}
var options = await GetOAuthOptionsAsync(context, currentAuthType);
var pairs = new Dictionary<string, string>()
{
{ "client_id", options.ClientId },
{ "client_secret", options.ClientSecret },
{ "grant_type", "refresh_token" },
{ "refresh_token", refreshToken }
};
var content = new FormUrlEncodedContent(pairs);
var refreshResponse = await options.Backchannel.PostAsync(options.TokenEndpoint, content, context.RequestAborted);
refreshResponse.EnsureSuccessStatusCode();
var jsonResponse = JsonSerializer.Deserialize<JsonResponse>((string)await refreshResponse.Content.ReadAsStringAsync());
authProperties.UpdateTokenValue("access_token", jsonResponse.access_token);
authProperties.UpdateTokenValue("refresh_token", jsonResponse.refresh_token);
var expiresAt = DateTimeOffset.UtcNow + TimeSpan.FromSeconds(jsonResponse.expires_in);
authProperties.UpdateTokenValue("expires_at", expiresAt.ToString("o", CultureInfo.InvariantCulture));
await context.SignInAsync(user, authProperties);
// write new tokens and times to file
tokenfileWrite(jsonResponse.access_token,
calculateUnixtimestampWithOffset(jsonResponse.expires_in),
jsonResponse.refresh_token,
calculateUnixtimestampWithOffset(jsonResponse.refresh_token_expires_in),
context);
context.Response.Redirect("/");
return;
});
});
app.Map("/query_api", signinApp =>
{
signinApp.Run(async context =>
{
String qry_http_verb = context.Request.Query["http_verb"].ToString() ?? "";
String qry_resource = context.Request.Query["resource"].ToString() ?? "";
String qry_post_data = context.Request.Query["post_data"].ToString() ?? "";
System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
timer.Start();
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", context.Response.HttpContext.Session.GetString("access_token"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage request = null;
if (qry_http_verb.Equals("get"))
{
request = await client.GetAsync(API_URL + qry_resource);
}
else if (qry_http_verb.Equals("post"))
{
request = await client.PostAsync(API_URL + qry_resource, new StringContent(qry_post_data, Encoding.UTF8, "application/json"));
}
else if (qry_http_verb.Equals("put"))
{
request = await client.PutAsync(API_URL + qry_resource, new StringContent(qry_post_data, Encoding.UTF8, "application/json"));
}
else if (qry_http_verb.Equals("delete"))
{
request = await client.DeleteAsync(API_URL + qry_resource);
}
Task<string> respContent = request.Content.ReadAsStringAsync();
Task.WaitAll(respContent);
dynamic parsedJson = Newtonsoft.Json.JsonConvert.DeserializeObject(respContent.Result.ToString());
String responseContentPretty = Newtonsoft.Json.JsonConvert.SerializeObject(parsedJson, Newtonsoft.Json.Formatting.Indented);
context.Response.HttpContext.Session.SetString("responseStatusCode", (int)request.StatusCode + " - " + request.StatusCode.ToString());
context.Response.HttpContext.Session.SetString("reqEndpoint", qry_resource);
context.Response.HttpContext.Session.SetString("responseContent", responseContentPretty);
context.Response.HttpContext.Session.SetString("responseTimespan", timer.Elapsed.Seconds + "." + timer.Elapsed.Milliseconds);
}
context.Response.Redirect("/");
return;
});
});
app.Run(async context =>
{
tokenfileRead(context);
// Setting DefaultAuthenticateScheme causes User to be set
var user = context.User;
// Deny anonymous request beyond this point.
if (user == null || !user.Identities.Any(identity => identity.IsAuthenticated))
{
// This is what [Authorize] calls
// The cookie middleware will handle this and redirect to /login
await context.ChallengeAsync();
return;
}
});
// Sign-out to remove the user cookie.
app.Map("/logout", signoutApp =>
{
signoutApp.Run(async context =>
{
var response = context.Response;
response.ContentType = "text/html";
await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
await response.WriteAsync("<html><body>");
await response.WriteAsync("You have been logged out. Goodbye " + context.User.Identity.Name + "<br>");
await response.WriteAsync("<a href=\"/\">Home</a>");
await response.WriteAsync("</body></html>");
});
});
// Display the remote error
app.Map("/error", errorApp =>
{
errorApp.Run(async context =>
{
var response = context.Response;
response.ContentType = "text/html";
await response.WriteAsync("<html><body>");
await response.WriteAsync("An remote failure has occurred: " + context.Request.Query["FailureMessage"] + "<br>");
await response.WriteAsync("<a href=\"/\">Home</a>");
await response.WriteAsync("</body></html>");
});
});
}
#region helper
private async Task HandleOnRemoteFailure(RemoteFailureContext context)
{
context.Response.StatusCode = 500;
context.Response.ContentType = "text/html";
await context.Response.WriteAsync("<html><body>");
await context.Response.WriteAsync("A remote failure has occurred: <br>" +
context.Failure.Message.Split(Environment.NewLine).Select(s => HtmlEncoder.Default.Encode(s) + "<br>").Aggregate((s1, s2) => s1 + s2));
if (context.Properties != null)
{
await context.Response.WriteAsync("Properties:<br>");
foreach (var pair in context.Properties.Items)
{
await context.Response.WriteAsync($"-{ HtmlEncoder.Default.Encode(pair.Key)}={ HtmlEncoder.Default.Encode(pair.Value)}<br>");
}
}
await context.Response.WriteAsync("<a href=\"/\">Home</a>");
await context.Response.WriteAsync("</body></html>");
context.HandleResponse();
}
private Task<OAuthOptions> GetOAuthOptionsAsync(HttpContext context, string currentAuthType)
{
return Task.FromResult<OAuthOptions>(context.RequestServices.GetRequiredService<IOptionsMonitor<OAuthOptions>>().Get(currentAuthType));
}
public string tokenfileWrite(string access_token, long expires_at, string refresh_token, long refresh_token_expires_at, HttpContext context)
{
JsonAccessTokenFile content = new JsonAccessTokenFile();
content.access_token = access_token;
content.expires_at = expires_at;
content.refresh_token = refresh_token;
content.refresh_token_expires_at = refresh_token_expires_at;
var options = new JsonSerializerOptions { WriteIndented = true };
var newContent = JsonSerializer.Serialize(content, options);
File.WriteAllText(Path.Combine(Directory.GetCurrentDirectory(), "access_token.json"), newContent.ToString());
context.Request.HttpContext.Session.SetString("access_token", access_token);
context.Request.HttpContext.Session.SetString("expires_at", expires_at.ToString());
context.Request.HttpContext.Session.SetString("refresh_token", refresh_token);
context.Request.HttpContext.Session.SetString("refresh_token_expires_at", refresh_token_expires_at.ToString());
return "0";
}
public static Dictionary<string, string> tokenfileRead(HttpContext context)
{
Dictionary<string, string> contentFromFile = new Dictionary<string, string>();
if (File.Exists(Path.Combine(Directory.GetCurrentDirectory(), "access_token.json")))
{
String fs = File.ReadAllText(Path.Combine(Directory.GetCurrentDirectory(), "access_token.json"));
var content = JsonSerializer.Deserialize<JsonAccessTokenFile>(fs);
context.Request.HttpContext.Session.SetString("access_token", content.access_token);
contentFromFile.Add("access_token", content.access_token);
context.Request.HttpContext.Session.SetString("expires_at", content.expires_at.ToString());
contentFromFile.Add("expires_at", content.expires_at.ToString());
context.Request.HttpContext.Session.SetString("refresh_token", content.refresh_token);
contentFromFile.Add("refresh_token", content.refresh_token);
context.Request.HttpContext.Session.SetString("refresh_token_expires_at", content.refresh_token_expires_at.ToString());
contentFromFile.Add("refresh_token_expires_at", content.refresh_token_expires_at.ToString());
}
return contentFromFile;
}
public static long calculateUnixtimestampWithOffset(long offset = 0)
{
long seconds = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds + offset;
return seconds;
}
public static String getPathOfConfigFile()
{
if (System.IO.File.Exists(Path.Combine(Directory.GetCurrentDirectory(), "client_application.json")))
{
return Path.Combine(Directory.GetCurrentDirectory(), "client_application.json");
}
else if (System.IO.File.Exists(Path.Combine(Directory.GetCurrentDirectory(), "app/client_application.json")))
{
return Path.Combine(Directory.GetCurrentDirectory(), "app/client_application.json");
}
return "";
}
}
#endregion
}