forked from docusign/code-examples-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartup.cs
More file actions
328 lines (285 loc) · 13.7 KB
/
Startup.cs
File metadata and controls
328 lines (285 loc) · 13.7 KB
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
// <copyright file="Startup.cs" company="DocuSign">
// Copyright (c) DocuSign. All rights reserved.
// </copyright>
#nullable enable
namespace DocuSign.CodeExamples
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using DocuSign.CodeExamples.Common;
using DocuSign.CodeExamples.Models;
using DocuSign.Rooms.Api;
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.AspNetCore.Mvc.Razor;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json.Linq;
public class Startup
{
private readonly Dictionary<ExamplesApiType, List<string>> apiTypes = new Dictionary<ExamplesApiType, List<string>>();
public Startup(IConfiguration configuration)
{
this.Configuration = configuration;
this.apiTypes.Add(ExamplesApiType.ESignature, new List<string> { "signature", });
this.apiTypes.Add(ExamplesApiType.Rooms, new List<string>
{
"dtr.rooms.read",
"dtr.rooms.write",
"dtr.documents.read",
"dtr.documents.write",
"dtr.profile.read",
"dtr.profile.write",
"dtr.company.read",
"dtr.company.write",
"room_forms",
});
this.apiTypes.Add(ExamplesApiType.Click, new List<string>
{
"click.manage",
"click.send",
});
this.apiTypes.Add(ExamplesApiType.Monitor, new List<string>
{
"signature",
"impersonation",
});
this.apiTypes.Add(ExamplesApiType.Admin, new List<string>
{
"signature",
"user_read",
"user_write",
"account_read",
"organization_read",
"group_read",
"permission_read",
"identity_provider_read",
"user_data_redact",
"asset_group_account_read",
"asset_group_account_clone_write",
"asset_group_account_clone_read",
});
this.apiTypes.Add(ExamplesApiType.WebForms, new List<string>
{
"signature", "webforms_read", "webforms_instance_write", "webforms_instance_read",
});
this.apiTypes.Add(ExamplesApiType.Maestro, new List<string> { "signature", "aow_manage" });
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<RazorViewEngineOptions>(o =>
{
// {1} - controller
// {0} - action
o.ViewLocationFormats.Add("Views/Shared/{0}.cshtml");
o.ViewLocationFormats.Add("Views/{1}/{0}.cshtml");
o.ViewLocationFormats.Add($"/{{1}}/Views/{{0}}{RazorViewEngine.ViewExtension}");
o.AreaViewLocationFormats.Clear();
o.AreaViewLocationFormats.Add("/{2}/Views/{1}/{0}.cshtml");
o.AreaViewLocationFormats.Add("/{2}/Views/Shared/{0}.cshtml");
o.AreaViewLocationFormats.Add("/Views/Shared/{0}.cshtml");
});
services.ConfigureNonBreakingSameSiteCookies();
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
});
DsConfiguration config = new DsConfiguration();
this.Configuration.Bind("DocuSign", config);
this.Configuration["FirstLaunch"] = "true";
services.AddSingleton(config);
services.AddSingleton(new LauncherTexts(config, this.Configuration));
services.AddScoped<IRequestItemsService, RequestItemsService>();
services.AddScoped<IRoomsApi, RoomsApi>();
services.AddScoped<IRolesApi, RolesApi>();
services.AddScoped<IFormLibrariesApi, FormLibrariesApi>();
services.AddScoped<IRoomTemplatesApi, RoomTemplatesApi>();
services.AddScoped<IExternalFormFillSessionsApi, ExternalFormFillSessionsApi>();
services.AddMvc(options =>
{
options.Filters.Add<LocalsFilter>();
});
services.AddRazorPages();
services.AddMemoryCache();
// services.AddCaching();// Adds a default in-memory implementation of IDistributedCache
services.AddSession();
services.AddHttpContextAccessor();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddAuthentication(options =>
{
options.DefaultSignOutScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = "DocuSign";
})
.AddCookie()
.AddOAuth("DocuSign", options =>
{
options.ClientId = this.Configuration["DocuSign:ClientId"];
options.ClientSecret = this.Configuration["DocuSign:ClientSecret"];
options.CallbackPath = new PathString("/ds/callback");
options.AuthorizationEndpoint = this.Configuration["DocuSign:AuthorizationEndpoint"];
options.TokenEndpoint = this.Configuration["DocuSign:TokenEndpoint"];
options.UserInformationEndpoint = this.Configuration["DocuSign:UserInformationEndpoint"];
foreach (var apiType in this.apiTypes)
{
foreach (var scope in apiType.Value)
{
if (!options.Scope.Contains(scope.ToLower()))
{
options.Scope.Add(scope);
}
}
}
options.SaveTokens = true;
options.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "sub");
options.ClaimActions.MapJsonKey(ClaimTypes.Name, "name");
options.ClaimActions.MapJsonKey("accounts", "accounts");
options.ClaimActions.MapCustomJson("account_id", obj => this.ExtractDefaultAccountValue(obj, "account_id"));
options.ClaimActions.MapCustomJson("account_name", obj => this.ExtractDefaultAccountValue(obj, "account_name"));
options.ClaimActions.MapCustomJson("base_uri", obj => this.ExtractDefaultAccountValue(obj, "base_uri"));
options.ClaimActions.MapJsonKey("access_token", "access_token");
options.ClaimActions.MapJsonKey("refresh_token", "refresh_token");
options.ClaimActions.MapJsonKey("expires_in", "expires_in");
options.Events = new OAuthEvents
{
OnRedirectToAuthorizationEndpoint = redirectContext =>
{
List<string> scopesForCurrentApi = this.apiTypes.GetValueOrDefault(Enum.Parse<ExamplesApiType>(this.Configuration["API"]));
redirectContext.RedirectUri = this.UpdateRedirectUriScopes(redirectContext.RedirectUri, scopesForCurrentApi);
redirectContext.HttpContext.Response.Redirect(redirectContext.RedirectUri);
return Task.FromResult(0);
},
OnCreatingTicket = async context =>
{
var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken);
var response = await context.Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, context.HttpContext.RequestAborted);
response.EnsureSuccessStatusCode();
var user = JObject.Parse(await response.Content.ReadAsStringAsync());
user.Add("access_token", context.AccessToken);
user.Add("refresh_token", context.RefreshToken);
user.Add("expires_in", DateTime.Now.Add(context.ExpiresIn.Value).ToString());
using (JsonDocument payload = JsonDocument.Parse(user.ToString()))
{
context.RunClaimActions(payload.RootElement);
}
},
OnRemoteFailure = context =>
{
context.HandleResponse();
context.Response.Redirect("/Home/Error?message=" + context.Failure?.Message);
return Task.FromResult(0);
},
};
});
}
// 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.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseSession();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "Areas",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{area=Rooms}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{area=Click}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapAreaControllerRoute(
name: "default",
areaName: "Monitor",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{area=Admin}/{controller=Home}/{action=Index}/{id?}");
});
}
private string UpdateRedirectUriScopes(string uri, List<string> wantedScopes)
{
const string pattern = @"(?:&|\?)scope=([^&]+)";
var encodedScopes = string.Join(" ", wantedScopes);
return Regex.Replace(uri, pattern, $"&scope={HttpUtility.UrlPathEncode(encodedScopes)}");
}
private string? ExtractDefaultAccountValue(JsonElement obj, string key)
{
if (!obj.TryGetProperty("accounts", out var accounts))
{
return null;
}
string? keyValue = null;
string targetAccountIdString = this.Configuration["DocuSign:TargetAccountId"];
if (Guid.TryParse(targetAccountIdString, out Guid targetAccountId))
{
foreach (var account in accounts.EnumerateArray())
{
account.TryGetProperty("account_id", out var accountIdJson);
accountIdJson.TryGetGuid(out Guid accountId);
if (targetAccountId == accountId)
{
if (account.TryGetProperty(key, out var value))
{
keyValue = value.GetString();
}
}
}
if (keyValue == null)
{
string errorMessage = $"Targeted Account with Id {targetAccountId} not found.";
this.Configuration["ErrorMessage"] = errorMessage;
throw new Exception(errorMessage);
}
}
else
{
foreach (var account in accounts.EnumerateArray())
{
if (account.TryGetProperty("is_default", out var defaultAccount) && defaultAccount.GetBoolean())
{
if (account.TryGetProperty(key, out var value))
{
keyValue = value.GetString();
}
}
}
}
return keyValue;
}
}
}