This repository has been archived by the owner on Dec 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Startup.cs
244 lines (199 loc) · 7.73 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
using System;
using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using OOL_API.Data;
using OOL_API.Models;
using OOL_API.Services;
using QRCoder;
using static OOL_API.Services.PictureStorageFactory;
namespace OOL_API
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Settings = new AppSettings(configuration);
}
// public IConfiguration Configuration { get; }
public IAppSettings Settings { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
ConfigureControllers(services);
ConfigureDatabase(services);
ConfigureCors(services);
ConfigureSwagger(services);
ConfigurePictureStorage(services);
ConfigureJwt(services);
ConfigureSettings(services);
ConfigureHash(services);
ConfigureAuth(services);
ConfigureQR(services);
}
private void ConfigureQR(IServiceCollection services)
{
services.AddScoped<QRCodeGenerator>();
services.AddScoped<QrHandler>();
}
private void ConfigureAuth(IServiceCollection services)
{
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped<CurrentUserInfo, CurrentUserInfo>();
}
private void ConfigureControllers(IServiceCollection services)
{
services.AddControllers(config =>
{
if (Settings.RequireAuth)
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
config.Filters.Add(new AuthorizeFilter(policy));
}
});
}
private void ConfigureHash(IServiceCollection services)
{
services.AddScoped<IPasswordHash, Sha256PasswordHash>();
}
private void ConfigureSettings(IServiceCollection services)
{
services.AddSingleton(Settings);
}
private void ConfigureJwt(IServiceCollection services)
{
var issuer = Settings.JwtIssuer;
var key = Settings.JwtKey;
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(
options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = issuer,
ValidAudience = issuer,
IssuerSigningKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(key)
)
};
}
);
}
private static void ConfigurePictureStorage(IServiceCollection services)
{
services.AddScoped<IPictureStorageDelegate, PictureStorageDelegate>();
services.AddScoped(StorageOf<PhotoShootImage, Guid>(
"Photoshoot_Images",
img => img.Id
));
services.AddScoped(StorageOf<Package, int>(
"Package_Images",
pkg => pkg.Id
));
services.AddScoped(StorageOf<EquipmentDetails, int>(
"EquipmentDetails_Images",
details => details.Id
));
services.AddScoped(
StorageOf<User, string>(
"User_Images",
employee => employee.Cpf
)
);
}
private static void ConfigureSwagger(IServiceCollection services)
{
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "OutOfLens API",
Description = "An API for fetching data to OutOfLens applications",
TermsOfService = new Uri("https://github.com/WidestView/OOL-API/blob/main/README.md"),
License = new OpenApiLicense
{
Name = "Use under GPL-v3",
Url = new Uri("https://github.com/WidestView/OOL-API/blob/main/LICENSE")
}
});
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Name = "Authorization",
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer",
BearerFormat = "JWT",
In = ParameterLocation.Header,
Description = "Jwt Authorization Header"
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new string[] { }
}
});
// allows nested classes to have the same name
// but different schema ids
c.CustomSchemaIds(type => (type.DeclaringType?.Name ?? "") + type.Name);
c.EnableAnnotations();
});
}
private void ConfigureDatabase(IServiceCollection services)
{
services.AddScoped<DbInitializer>();
services.AddDbContext<StudioContext>(options =>
options.UseMySQL(Settings.DefaultConnectionString));
}
private void ConfigureCors(IServiceCollection services)
{
var allowedUrls = Settings.AllowedCorsUrls;
services.AddCors(options => options.AddPolicy("ApiCorsPolicy",
builder => { builder.WithOrigins(allowedUrls).AllowAnyMethod().AllowAnyHeader(); }));
}
// 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.UseCors("ApiCorsPolicy");
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "OOL-API v1");
c.RoutePrefix = string.Empty;
});
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}
}
}