Add login
Some checks failed
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is failing

This commit is contained in:
Kasper Juul Hermansen 2021-11-16 22:06:33 +01:00
parent 0ad741463d
commit d785150426
Signed by: kjuulh
GPG Key ID: DCD9397082D97069
7 changed files with 83 additions and 2 deletions

View File

@ -0,0 +1,46 @@
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Todo.Infrastructure;
public static class DependencyInjection
{
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
{
var giteaAuthOptions = new GiteaAuthOptions();
var giteaOptions = configuration.GetRequiredSection("GITEA");
giteaOptions.Bind(giteaAuthOptions);
services
.AddOptions<GiteaAuthOptions>()
.Bind(giteaOptions)
.ValidateDataAnnotations();
return services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
options.Authority = giteaAuthOptions.Url;
options.ClientId = giteaAuthOptions.ClientId;
options.ClientSecret = giteaAuthOptions.ClientSecret;
options.ResponseType = "code";
options.SaveTokens = true;
}).Services;
}
public static IApplicationBuilder UseInfrastructure(this IApplicationBuilder app) => app;
}
public class GiteaAuthOptions
{
[Required] public string Url { get; set; }
[Required] public string ClientId { get; init; }
[Required] public string ClientSecret { get; init; }
}

View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.0" />
</ItemGroup>
</Project>

View File

@ -6,6 +6,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Todo.Persistence", "src\Tod
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Todo.Core", "src\Todo.Core\Todo.Core.csproj", "{F134CAB6-15A5-45CB-8782-B61AB67B5C9C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Todo.Infrastructure", "Todo.Infrastructure\Todo.Infrastructure.csproj", "{F6EE7E8D-6248-421E-9FB0-D4342908B319}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -24,5 +26,9 @@ Global
{F134CAB6-15A5-45CB-8782-B61AB67B5C9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F134CAB6-15A5-45CB-8782-B61AB67B5C9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F134CAB6-15A5-45CB-8782-B61AB67B5C9C}.Release|Any CPU.Build.0 = Release|Any CPU
{F6EE7E8D-6248-421E-9FB0-D4342908B319}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F6EE7E8D-6248-421E-9FB0-D4342908B319}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F6EE7E8D-6248-421E-9FB0-D4342908B319}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F6EE7E8D-6248-421E-9FB0-D4342908B319}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View File

@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Todo.Core.Interfaces.Persistence;
@ -20,10 +21,11 @@ public class TodosController : ControllerBase
Ok(await _todoRepository.CreateTodoAsync(request.Title, String.Empty));
[HttpGet]
[Authorize]
public async Task<ActionResult<IEnumerable<Core.Entities.Todo>>> GetTodos() =>
Ok(await _todoRepository.GetTodosAsync());
[HttpGet]
[HttpGet("not-done")]
public async Task<ActionResult<IEnumerable<Core.Entities.Todo>>> GetNotDoneTodos() =>
Ok(await _todoRepository.GetNotDoneTodos());

View File

@ -21,7 +21,10 @@
"MONGODB__Password": "example",
"MONGODB__Database": "todo",
"MONGODB__Host": "localhost",
"MONGODB__Port": "27017"
"MONGODB__Port": "27017",
"GITEA__Url": "https://git.front.kjuulh.io",
"GITEA__ClientId": "6982ef4f-cfc1-431c-a442-fad98355a059",
"GITEA__": "stabKPEZ6di0VfPjYT4rb0jRGLA2gPSd2NEkGoBi0xLO"
}
}
}

View File

@ -1,3 +1,4 @@
using System.IdentityModel.Tokens.Jwt;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
@ -7,6 +8,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using Todo.Api.Hubs;
using Todo.Infrastructure;
using Todo.Persistence;
using Todo.Persistence.Mongo;
@ -38,6 +40,9 @@ namespace Todo.Api
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Todo.Api", Version = "v1" });
});
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
services.AddInfrastructure(Configuration);
services.AddPersistence(Configuration, out var mongoDbOptions);
services
@ -61,6 +66,7 @@ namespace Todo.Api
app.UseRouting();
app.UseCors();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>

View File

@ -10,6 +10,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Todo.Infrastructure\Todo.Infrastructure.csproj" />
<ProjectReference Include="..\Todo.Core\Todo.Core.csproj" />
<ProjectReference Include="..\Todo.Persistence\Todo.Persistence.csproj" />
</ItemGroup>