Build auth API
This commit is contained in:
commit
dafe603a06
43 changed files with 1153 additions and 0 deletions
35
src/Robware.Api.Auth/Controllers/AuthController.cs
Normal file
35
src/Robware.Api.Auth/Controllers/AuthController.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
6
src/Robware.Api.Auth/Models/LoginRequest.cs
Normal file
6
src/Robware.Api.Auth/Models/LoginRequest.cs
Normal file
|
@ -0,0 +1,6 @@
|
|||
namespace Robware.Api.Auth.Models {
|
||||
public class LoginRequest {
|
||||
public string Username { get; set; }
|
||||
public string Password { get; set; }
|
||||
}
|
||||
}
|
12
src/Robware.Api.Auth/Program.cs
Normal file
12
src/Robware.Api.Auth/Program.cs
Normal 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();
|
||||
}
|
||||
}
|
28
src/Robware.Api.Auth/Properties/launchSettings.json
Normal file
28
src/Robware.Api.Auth/Properties/launchSettings.json
Normal 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"
|
||||
}
|
||||
}
|
||||
}
|
13
src/Robware.Api.Auth/Robware.Api.Auth.csproj
Normal file
13
src/Robware.Api.Auth/Robware.Api.Auth.csproj
Normal 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>
|
44
src/Robware.Api.Auth/Startup.cs
Normal file
44
src/Robware.Api.Auth/Startup.cs
Normal 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();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
12
src/Robware.Api.Auth/appsettings.Development.json
Normal file
12
src/Robware.Api.Auth/appsettings.Development.json
Normal 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"
|
||||
}
|
||||
}
|
20
src/Robware.Api.Auth/appsettings.json
Normal file
20
src/Robware.Api.Auth/appsettings.json
Normal 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>"
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue