aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Web/Configurations/Extensions
diff options
context:
space:
mode:
Diffstat (limited to 'src/DevHive.Web/Configurations/Extensions')
-rw-r--r--src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs39
-rw-r--r--src/DevHive.Web/Configurations/Extensions/ConfigureJWT.cs54
-rw-r--r--src/DevHive.Web/Configurations/Extensions/ConfigureSwagger.cs23
3 files changed, 116 insertions, 0 deletions
diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs
new file mode 100644
index 0000000..178d345
--- /dev/null
+++ b/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs
@@ -0,0 +1,39 @@
+using Microsoft.Extensions.DependencyInjection;
+using DevHive.Data.Repositories;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.Configuration;
+using DevHive.Data.Models;
+using Microsoft.AspNetCore.Identity;
+using Microsoft.AspNetCore.Builder;
+
+namespace DevHive.Web.Configurations.Extensions
+{
+ public static class DatabaseExtensions
+ {
+ public static void DatabaseConfiguration(this IServiceCollection services, IConfiguration configuration)
+ {
+ services.AddDbContext<DevHiveContext>(options =>
+ options.UseNpgsql(configuration.GetConnectionString("DEV")));
+
+ services.AddIdentity<User, IdentityRole>()
+ .AddEntityFrameworkStores<DevHiveContext>();
+
+ services.Configure<IdentityOptions>(options =>
+ {
+ //TODO: Add more validations
+ options.User.RequireUniqueEmail = true;
+
+ options.Password.RequiredLength = 5;
+ });
+ }
+
+ public static void UseDatabaseConfiguration(this IApplicationBuilder app)
+ {
+ app.UseHttpsRedirection();
+ app.UseRouting();
+
+ app.UseAuthentication();
+ app.UseAuthorization();
+ }
+ }
+}
diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureJWT.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureJWT.cs
new file mode 100644
index 0000000..bc5ac15
--- /dev/null
+++ b/src/DevHive.Web/Configurations/Extensions/ConfigureJWT.cs
@@ -0,0 +1,54 @@
+using System.Text;
+using System.Threading.Tasks;
+using DevHive.Services.Options;
+using Microsoft.AspNetCore.Authentication.JwtBearer;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.IdentityModel.Tokens;
+
+namespace DevHive.Web.Configurations.Extensions
+{
+ public static class JWTExtensions
+ {
+ public static void JWTConfiguration(this IServiceCollection services, IConfiguration configuration)
+ {
+ services.AddSingleton(new JWTOptions(configuration
+ .GetSection("AppSettings")
+ .GetSection("Secret")
+ .Value));
+
+ // Get key from appsettings.json
+ var key = Encoding.ASCII.GetBytes(configuration
+ .GetSection("AppSettings")
+ .GetSection("Secret")
+ .Value);
+
+ // Setup Jwt Authentication
+ services.AddAuthentication(x =>
+ {
+ x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
+ x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
+ })
+ .AddJwtBearer(x =>
+ {
+ x.Events = new JwtBearerEvents
+ {
+ OnTokenValidated = context =>
+ {
+ // TODO: add more authentication
+ return Task.CompletedTask;
+ }
+ };
+ x.RequireHttpsMetadata = false;
+ x.SaveToken = true;
+ x.TokenValidationParameters = new TokenValidationParameters
+ {
+ ValidateIssuerSigningKey = true,
+ IssuerSigningKey = new SymmetricSecurityKey(key),
+ ValidateIssuer = false,
+ ValidateAudience = false
+ };
+ });
+ }
+ }
+} \ No newline at end of file
diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureSwagger.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureSwagger.cs
new file mode 100644
index 0000000..a0641ab
--- /dev/null
+++ b/src/DevHive.Web/Configurations/Extensions/ConfigureSwagger.cs
@@ -0,0 +1,23 @@
+using Microsoft.AspNetCore.Builder;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.OpenApi.Models;
+
+namespace DevHive.Web.Configurations.Extensions
+{
+ public static class SwaggerExtensions
+ {
+ public static void SwaggerConfiguration(this IServiceCollection services)
+ {
+ services.AddSwaggerGen(c =>
+ {
+ c.SwaggerDoc("v1", new OpenApiInfo { Title = "API", Version = "v1" });
+ });
+ }
+
+ public static void UseSwaggerConfiguration(this IApplicationBuilder app)
+ {
+ app.UseSwagger();
+ app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1"));
+ }
+ }
+} \ No newline at end of file