Compare commits

..

5 Commits

Author SHA1 Message Date
YuukanOO
e62390abc9 remove password verification 2020-12-29 21:31:26 +01:00
YuukanOO
8a80b80e5c add register endpoint 2020-12-29 21:24:35 +01:00
YuukanOO
0d8082448d add migration and env to Api 2020-12-29 21:17:42 +01:00
YuukanOO
0972ec304a update Dockerfile 2020-12-29 21:08:20 +01:00
YuukanOO
2776586ad7 add heroku yml 2020-12-29 21:05:56 +01:00
6 changed files with 107 additions and 1 deletions

View File

@ -26,6 +26,22 @@ namespace Api.Controllers
_tokenParameters = tokenParameters;
}
[HttpPost("register")]
[AllowAnonymous]
public async Task<ActionResult> Register(RegisterViewModel command)
{
var user = new User(command.Username);
var result = await _usersManager.CreateAsync(user, command.Password);
if (!result.Succeeded)
{
return BadRequest();
}
return NoContent();
}
/// <summary>
/// Récupère un jeton d'accès pour un utilisateur particulier.
/// </summary>

View File

@ -0,0 +1,13 @@
using System.ComponentModel.DataAnnotations;
namespace Api.Models
{
public sealed class RegisterViewModel
{
[Required]
public string Username { get; set; }
[Required]
public string Password { get; set; }
}
}

View File

@ -9,6 +9,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.AspNetCore.Routing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
@ -55,7 +56,11 @@ namespace Api
services.AddSingleton(tokenParams);
services.AddIdentityCore<User>()
services.AddIdentityCore<User>(o =>
{
o.Password.RequiredLength = o.Password.RequiredUniqueChars = 0;
o.Password.RequireDigit = o.Password.RequireLowercase = o.Password.RequireNonAlphanumeric = o.Password.RequireUppercase = false;
})
.AddRoles<Role>()
.AddEntityFrameworkStores<HNDbContext>()
.AddSignInManager();
@ -106,6 +111,8 @@ namespace Api
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
MigrateDatabase(app);
app.UseOpenApi();
if (env.IsDevelopment())
@ -135,5 +142,17 @@ namespace Api
});
});
}
/// <summary>
/// Lance les migrations. En production, il est plutôt conseillé de générer
/// les scripts avec `dotnet ef migrations script` et de les passer à la main.
/// </summary>
/// <param name="app"></param>
private void MigrateDatabase(IApplicationBuilder app)
{
using var scope = app.ApplicationServices.CreateScope();
using var ctx = scope.ServiceProvider.GetRequiredService<HNDbContext>();
ctx.Database.Migrate();
}
}
}

View File

@ -6,6 +6,39 @@
"version": "1.0.0"
},
"paths": {
"/api/accounts/register": {
"post": {
"tags": [
"Accounts"
],
"operationId": "Accounts_Register",
"requestBody": {
"x-name": "command",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/RegisterViewModel"
}
}
},
"required": true,
"x-position": 1
},
"responses": {
"200": {
"description": "",
"content": {
"application/octet-stream": {
"schema": {
"type": "string",
"format": "binary"
}
}
}
}
}
}
},
"/api/accounts/login": {
"post": {
"tags": [
@ -309,6 +342,24 @@
},
"components": {
"schemas": {
"RegisterViewModel": {
"type": "object",
"additionalProperties": false,
"required": [
"username",
"password"
],
"properties": {
"username": {
"type": "string",
"minLength": 1
},
"password": {
"type": "string",
"minLength": 1
}
}
},
"LoginViewModel": {
"type": "object",
"additionalProperties": false,

View File

@ -6,8 +6,11 @@ COPY Application/*.csproj ./Application/
COPY Domain/*.csproj ./Domain/
COPY Infrastructure/*.csproj ./Infrastructure/
COPY Apps/Api/*.csproj ./Apps/Api/
WORKDIR /source/Apps/Api
RUN dotnet restore
WORKDIR /source
COPY Application/. ./Application/
COPY Domain/. ./Domain/
COPY Infrastructure/. ./Infrastructure/
@ -19,5 +22,6 @@ RUN dotnet publish -c release -o /app --no-restore
FROM mcr.microsoft.com/dotnet/aspnet:5.0
WORKDIR /app
COPY --from=build /app ./
ENV "ConnectionStrings:Default"="Data Source=hn.db"
EXPOSE 80
ENTRYPOINT ["dotnet", "Api.dll"]

3
heroku.yml Normal file
View File

@ -0,0 +1,3 @@
build:
docker:
web: Dockerfile