This repository was archived by the owner on Sep 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStartup.cs
176 lines (148 loc) · 4.84 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
using System.Collections.Generic;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using achieve_backend.Models;
using Microsoft.Extensions.Options;
using achieve_backend.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Authorization;
using achieve_backend.Hubs;
using Microsoft.AspNetCore.Http.Connections;
using achieve_backend.Utils;
using System;
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.Mvc.Cors;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using System.Threading.Tasks;
using System.Security.Claims;
namespace achieve_backend
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
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)
{
SetupDB();
services.Configure<AchieveDBSettings>(
Configuration.GetSection(nameof(AchieveDBSettings)));
services.AddSingleton<IAchieveDBSettings>(sp =>
sp.GetRequiredService<IOptions<AchieveDBSettings>>().Value);
services.AddSingleton<UserService>();
services.AddSingleton<DomainService>();
DefineDomains(services.BuildServiceProvider().GetService<DomainService>());
services.AddControllers().AddNewtonsoftJson(options => options.UseMemberCasing());
services.AddSignalR();
services.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", options =>
{
options.Authority = "https://auth.it108.org";
options.RequireHttpsMetadata = false;
options.Audience = "achieve-api";
});
services.AddAuthentication("access_token")
.AddJwtBearer("access_token", options =>
{
options.Authority = "https://auth.it108.org";
options.RequireHttpsMetadata = false;
options.Audience = "achieve-api";
options.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
var accessToken = context.Request.Query["access_token"];
// If the request is for our hub...
var path = context.HttpContext.Request.Path;
if (!string.IsNullOrEmpty(accessToken) &&
(path.StartsWithSegments("/hubs/users")))
{
// Read the token out of the query string
context.Token = accessToken;
}
return Task.CompletedTask;
}
};
});
services.AddAuthorization(options =>
{
options.AddPolicy("Student", policy => policy.RequireClaim(ClaimTypes.Role, "Student"));
});
services.AddCors(options =>
{
// this defines a CORS policy called "default"
options.AddPolicy("default", policy =>
{
policy
.WithOrigins("https://localhost:4200", "http://localhost:4200", "https://auth.it108.org")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
});
}
// 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();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseStaticFiles();
app.UseCors("default");
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers().RequireCors("default");
endpoints.MapHub<AuthHub>("/hubs/auth", options =>
{
options.Transports =
HttpTransportType.WebSockets;
}).RequireCors("default");
endpoints.MapHub<UserHub>("/hubs/users", options =>
{
options.Transports =
HttpTransportType.WebSockets;
}).RequireCors("default");
});
app.Use(async (context, next) =>
{
await Edge.ConfigureEdge(Configuration, context.RequestServices
.GetRequiredService<IHubContext<AuthHub>>());
});
}
private void SetupDB()
{
Configuration["AchieveDBSettings:ConnectionString"] = Configuration["DB_CONN_STRING"];
}
private void DefineDomains(DomainService ds)
{
DomainModel.KeyLength = int.Parse(Configuration["DOMAIN_KEY_LENGTH"]);
List<DomainModel> domains = new List<DomainModel>() { new DomainModel("it108.org", "IT108") };
DomainsConfig.DefineDomains(domains, Configuration, ds);
}
}
}