Build auth API

This commit is contained in:
Robert Marshall 2020-04-12 13:50:39 +01:00
commit dafe603a06
43 changed files with 1153 additions and 0 deletions

View file

@ -0,0 +1,35 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Robware.Api.Auth.Models;
using Robware.Auth;
namespace Robware.Api.Auth.Controllers {
[ApiController]
[Route("[controller]")]
public class AuthController : ControllerBase {
private readonly ILogger<AuthController> _logger;
private readonly IAuthenticator _authenticator;
public AuthController(ILogger<AuthController> logger, IAuthenticator authenticator) {
_logger = logger;
_authenticator = authenticator;
}
[HttpPost(nameof(Authenticate))]
public async Task<ActionResult<User>> Authenticate(LoginRequest request) {
var (result, user) = await _authenticator.Authenticate(request.Username, request.Password);
switch (result) {
case AuthenticationResult.Success:
return user;
case AuthenticationResult.NotFound:
return NotFound();
case AuthenticationResult.IncorrectPassword:
return Unauthorized();
default:
throw new ArgumentOutOfRangeException();
}
}
}
}