Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/create browl service auth security #31

Merged
merged 20 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: ''
assignees: ''

---

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

**Expected behavior**
A clear and concise description of what you expected to happen.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Desktop (please complete the following information):**
- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]

**Smartphone (please complete the following information):**
- Device: [e.g. iPhone6]
- OS: [e.g. iOS8.1]
- Browser [e.g. stock browser, safari]
- Version [e.g. 22]

**Additional context**
Add any other context about the problem here.
10 changes: 10 additions & 0 deletions .github/ISSUE_TEMPLATE/custom.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
name: Custom issue template
about: Describe this issue template's purpose here.
title: ''
labels: ''
assignees: ''

---


20 changes: 20 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: ''
assignees: ''

---

**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

**Describe the solution you'd like**
A clear and concise description of what you want to happen.

**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.

**Additional context**
Add any other context or screenshots about the feature request here.
24 changes: 22 additions & 2 deletions Browl.sln
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,17 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Browl.Service.MarketDataCol
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Browl.Service.MarketDataCollector.Application.Test", "test\Services\Browl.Service.MarketDataCollector.Test\Browl.Service.MarketDataCollector.Application.Test\Browl.Service.MarketDataCollector.Application.Test.csproj", "{D3458E46-B1F2-4CAF-BDFD-94AB31F6528A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Browl.Service.MarketDataCollector.Infrastructure.Test", "test\Services\Browl.Service.MarketDataCollector.Test\Browl.Service.MarketDataCollector.Infrastructure.Test\Browl.Service.MarketDataCollector.Infrastructure.Test.csproj", "{14DFBDD1-124E-4E8F-9F90-270FE83F0704}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Browl.Service.MarketDataCollector.Infrastructure.Test", "test\Services\Browl.Service.MarketDataCollector.Test\Browl.Service.MarketDataCollector.Infrastructure.Test\Browl.Service.MarketDataCollector.Infrastructure.Test.csproj", "{14DFBDD1-124E-4E8F-9F90-270FE83F0704}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Browl.Client.Test", "test\Presentation\Browl.Client.Test\Browl.Client.Test.csproj", "{72694572-0D50-4A39-A657-73133F074AF7}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Browl.Client.Test", "test\Presentation\Browl.Client.Test\Browl.Client.Test.csproj", "{72694572-0D50-4A39-A657-73133F074AF7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Browl.Service.MarketDataCollector.FakeData", "test\Services\Browl.Service.MarketDataCollector.Test\Browl.Service.MarketDataCollector.FakeData\Browl.Service.MarketDataCollector.FakeData.csproj", "{7A01F807-7685-48FB-BAD0-9303E96A53B5}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "FakeData", "FakeData", "{361920A2-8B95-47B1-AA42-C9ADD6D28D23}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Browl.Service.AuthSecurity", "Browl.Service.AuthSecurity", "{09EE67D6-ADE1-4A0A-8766-974D00A71209}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Browl.Service.AuthSecurity.API", "src\Services\Browl.Service.AuthSecurity\Browl.Service.AuthSecurity.API\Browl.Service.AuthSecurity.API.csproj", "{051A7F26-D824-420B-B642-741C6CD41F13}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -130,6 +138,14 @@ Global
{72694572-0D50-4A39-A657-73133F074AF7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{72694572-0D50-4A39-A657-73133F074AF7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{72694572-0D50-4A39-A657-73133F074AF7}.Release|Any CPU.Build.0 = Release|Any CPU
{7A01F807-7685-48FB-BAD0-9303E96A53B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7A01F807-7685-48FB-BAD0-9303E96A53B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7A01F807-7685-48FB-BAD0-9303E96A53B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7A01F807-7685-48FB-BAD0-9303E96A53B5}.Release|Any CPU.Build.0 = Release|Any CPU
{051A7F26-D824-420B-B642-741C6CD41F13}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{051A7F26-D824-420B-B642-741C6CD41F13}.Debug|Any CPU.Build.0 = Debug|Any CPU
{051A7F26-D824-420B-B642-741C6CD41F13}.Release|Any CPU.ActiveCfg = Release|Any CPU
{051A7F26-D824-420B-B642-741C6CD41F13}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -154,6 +170,10 @@ Global
{D3458E46-B1F2-4CAF-BDFD-94AB31F6528A} = {DB8F8AA6-A0F4-487D-B807-787C5117DD35}
{14DFBDD1-124E-4E8F-9F90-270FE83F0704} = {DB8F8AA6-A0F4-487D-B807-787C5117DD35}
{72694572-0D50-4A39-A657-73133F074AF7} = {1B3CDB1E-8F3E-4900-A61F-B13C974872FF}
{7A01F807-7685-48FB-BAD0-9303E96A53B5} = {361920A2-8B95-47B1-AA42-C9ADD6D28D23}
{361920A2-8B95-47B1-AA42-C9ADD6D28D23} = {DB8F8AA6-A0F4-487D-B807-787C5117DD35}
{09EE67D6-ADE1-4A0A-8766-974D00A71209} = {758EAD6C-01B5-4816-97E5-CA0E5F217D34}
{051A7F26-D824-420B-B642-741C6CD41F13} = {09EE67D6-ADE1-4A0A-8766-974D00A71209}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {09C8AF50-40D4-48F2-A03D-9BC7AB6D3538}
Expand Down
5 changes: 5 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"sdk": {
"version": "7.0.306"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.10" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="7.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.32.3" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace Browl.Service.AuthSecurity.API.Configuration;

public static class ApiConfig
{
public static IServiceCollection AddApiConfiguration(this IServiceCollection services, IWebHostEnvironment hostEnvironment)
{
_ = services.AddControllers();

IConfigurationBuilder builder = new ConfigurationBuilder()
.SetBasePath(hostEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json", true, true)
.AddJsonFile($"appsettings.{hostEnvironment.EnvironmentName}.json", true, true)
.AddEnvironmentVariables();

if (hostEnvironment.IsDevelopment())
{
_ = builder.AddUserSecrets<Program>();
}

return services;
}

public static IApplicationBuilder UseApiConfiguration(this IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
IApplicationBuilder unused5 = app.UseDeveloperExceptionPage();
}

IApplicationBuilder unused4 = app.UseHttpsRedirection();

IApplicationBuilder unused3 = app.UseRouting();

IApplicationBuilder unused2 = app.UseIdentityConfiguration();

IApplicationBuilder unused1 = app.UseEndpoints(endpoints =>
{
ControllerActionEndpointConventionBuilder unused = endpoints.MapControllers();
});

return app;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Browl.Service.AuthSecurity.API.Data;
using Browl.Service.AuthSecurity.Domain.Entities;

using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

namespace Browl.Service.AuthSecurity.API.Configuration;

public static class DatabaseConfig
{

public static IServiceCollection AddDatabaseConfiguration(this IServiceCollection services, IConfiguration configuration)
{
services.AddDbContext<BrowlAuthSecurityDbContext>(options => options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));

services.AddIdentity<User, IdentityRole>()
.AddEntityFrameworkStores<BrowlAuthSecurityDbContext>()
.AddDefaultUI()
.AddDefaultTokenProviders();

return services;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using Browl.Service.AuthSecurity.API.Data;
using Browl.Service.AuthSecurity.API.Entities;

using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;

using System.Text;

namespace Browl.Service.AuthSecurity.API.Configuration;

public static class IdentityConfig
{
public static IServiceCollection AddIdentityConfiguration(this IServiceCollection services,
IConfiguration configuration)
{
IServiceCollection unused3 = services.AddDbContext<BrowlAuthSecurityDbContext>(options =>
options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));

IdentityBuilder unused2 = services.AddDefaultIdentity<IdentityUser>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<BrowlAuthSecurityDbContext>()
.AddDefaultTokenProviders();

// JWT

IConfigurationSection appSettingsSection = configuration.GetSection("AppSettings");
IServiceCollection unused1 = services.Configure<AppSettings>(appSettingsSection);

AppSettings? appSettings = appSettingsSection.Get<AppSettings>();
byte[] key = Encoding.ASCII.GetBytes(appSettings.Secret);

Microsoft.AspNetCore.Authentication.AuthenticationBuilder unused = services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(bearerOptions =>
{
bearerOptions.RequireHttpsMetadata = true;
bearerOptions.SaveToken = true;
bearerOptions.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = true,
ValidateAudience = true,
ValidAudience = appSettings.ValidOn,
ValidIssuer = appSettings.Issuer
};
});

return services;
}



public static IServiceCollection AddJWConfiguration(this IServiceCollection services, IConfiguration configuration)
{
services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(o =>
{
var Key = Encoding.UTF8.GetBytes(configuration["JWT:Key"]);
o.SaveToken = true;
o.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = false,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = configuration["JWT:Issuer"],
ValidAudience = configuration["JWT:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Key)
};
});

return services;
}

public static IApplicationBuilder UseIdentityConfiguration(this IApplicationBuilder app)
{
_ = app.UseAuthentication();
_ = app.UseAuthorization();

return app;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Microsoft.OpenApi.Models;

namespace Browl.Service.AuthSecurity.API.Configuration;

public static class SwaggerConfig
{
public static IServiceCollection AddSwaggerConfiguration(this IServiceCollection services)
{
IServiceCollection unused = services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo()
{
Title = "Browl Service AuthSecurity API",
Description = "This API guarantees the security and proper authentication of users in the application.",
Contact = new OpenApiContact() { Name = "Rondinele Guimarães", Email = "[email protected]" },
License = new OpenApiLicense() { Name = "MIT", Url = new Uri("https://opensource.org/licenses/MIT") }
});

});

return services;
}

public static IApplicationBuilder UseSwaggerConfiguration(this IApplicationBuilder app)
{
IApplicationBuilder unused1 = app.UseSwagger();
IApplicationBuilder unused = app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
});

return app;
}
}
Loading