-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMvcInstaller.cs
113 lines (100 loc) · 4.32 KB
/
MvcInstaller.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
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using System.Collections.Generic;
using System.Text;
using Tweetbook.Options;
using Tweetbook.Services;
namespace Tweetbook.Installers
{
public class MvcInstaller : IInstaller
{
public void InstallServices(IServiceCollection services, IConfiguration configuration)
{
services.AddMvc(options =>
{
options.EnableEndpointRouting = false;
}).SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
this.InstallSecurity(services, configuration);
this.InstallSwagger(services);
}
private void InstallSecurity(IServiceCollection services, IConfiguration configuration)
{
var jwtSettings = new JwtSettings();
configuration.GetSection(nameof(JwtSettings)).Bind(jwtSettings);
var tokenValidationParameters = this.GetTokenValidationParameters(jwtSettings);
services.AddSingleton(jwtSettings);
services.AddSingleton(tokenValidationParameters);
services.AddScoped<IIdentityService, IdentityService>();
services.AddAuthentication(authOptions =>
{
authOptions.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
authOptions.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
authOptions.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(jwtBearerOptions =>
{
jwtBearerOptions.SaveToken = true;
jwtBearerOptions.TokenValidationParameters = tokenValidationParameters;
});
services.AddAuthorization();
}
private TokenValidationParameters GetTokenValidationParameters(JwtSettings jwtSettings)
{
return new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtSettings.Secret)),
ValidateIssuer = false,
ValidateAudience = false,
RequireExpirationTime = false,
ValidateLifetime = true
};
}
private void InstallSwagger(IServiceCollection services)
{
// Register the Swagger Generator as a service. We can define 1 or more Swagger documents here
services.AddSwaggerGen(x =>
{
x.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
{
Title = "Tweetbook API",
Version = "v1",
Description = "Test documentation for the Tweetbook API",
TermsOfService = new System.Uri("https://example.com/terms"),
Contact = new Microsoft.OpenApi.Models.OpenApiContact
{
Name = "Alvin Leung",
Email = string.Empty,
Url = new System.Uri("https://gooddevbaddev.com"),
},
License = new Microsoft.OpenApi.Models.OpenApiLicense
{
Name = "Use under LICX",
Url = new System.Uri("https://example.com/license"),
}
});
var reference = new OpenApiReference
{
Id = "Bearer",
Type = ReferenceType.SecurityScheme
};
var security = new OpenApiSecurityRequirement
{
{ new OpenApiSecurityScheme { Reference = reference }, new List<string>() }
};
x.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description = "JWT Authorization header using the bearer scheme",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey
});
x.AddSecurityRequirement(security);
});
}
}
}