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();
}
}
}
}

View file

@ -0,0 +1,6 @@
namespace Robware.Api.Auth.Models {
public class LoginRequest {
public string Username { get; set; }
public string Password { get; set; }
}
}

View file

@ -0,0 +1,12 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace Robware.Api.Auth {
public class Program {
public static void Main(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => {
webBuilder.UseStartup<Startup>();
}).Build().Run();
}
}

View file

@ -0,0 +1,28 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:61069",
"sslPort": 44309
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Robware.Api.Auth": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}

View file

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Robware.Auth\Robware.Auth.csproj" />
<ProjectReference Include="..\Robware.Data\Robware.Data.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,44 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Robware.Auth;
using Robware.Data;
namespace Robware.Api.Auth {
public class Startup {
public Startup(IConfiguration configuration) {
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) {
services.AddControllers();
services.AddSingleton<ICryptographyProvider, CryptographyProvider>()
.AddSingleton<IAuthenticator, Authenticator>()
.AddSingleton<IDatabaseProvider>(new MySQLDatabaseProvider(Configuration.GetConnectionString("database")))
.AddSingleton<IUsers, UserRepository>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
});
}
}
}

View file

@ -0,0 +1,12 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings": {
"database": "Server=localhost;User ID=user;Password=pass;Database=db"
}
}

View file

@ -0,0 +1,20 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Kestrel": {
"EndPoints": {
"Http": {
"Url": "http://0.0.0.0:5003"
}
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"database": "<DatabaseConnectionString>"
}
}