Use new Auth API

This commit is contained in:
Robert Marshall 2020-04-12 14:25:16 +01:00
parent 38e76d3539
commit 79c17f75cd
16 changed files with 94 additions and 150 deletions

View file

@ -10,9 +10,9 @@ using Website.ViewModels;
namespace Website.Controllers {
public class AccountController:Controller {
private readonly UserRepository _repo;
private readonly IAuthenticationProvider _authenticationProvider;
public AccountController(UserRepository repo) => _repo = repo;
public AccountController(IAuthenticationProvider authenticationProvider) => _authenticationProvider = authenticationProvider;
[Authorize]
public IActionResult Index() => View();
@ -29,8 +29,8 @@ namespace Website.Controllers {
[HttpPost]
public async Task<IActionResult> Login(LoginRequest request) {
try {
var user = await _repo.GetUserByEmail(request.Username);
return user.ValidatePassword(request.Password)
var user = await _authenticationProvider.Authenticate(request);
return user != null
? await SetIdentityAndRedirect(request.ReturnUrl, user)
: Login(request.ReturnUrl, true);
}

View file

@ -0,0 +1,19 @@
using System.Net.Http;
using System.Threading.Tasks;
using Website.Models;
namespace Website.Data {
public class AuthenticationProvider:ApiClient, IAuthenticationProvider {
public AuthenticationProvider(HttpClient client) : base(client) {
}
public async Task<User> Authenticate(LoginRequest request) {
try {
return await Post<User>("authenticate", request);
}
catch (ApiCallException) {
return null;
}
}
}
}

View file

@ -0,0 +1,8 @@
using System.Threading.Tasks;
using Website.Models;
namespace Website.Data {
public interface IAuthenticationProvider {
Task<User> Authenticate(LoginRequest request);
}
}

View file

@ -1,9 +0,0 @@
using System.Data;
namespace Website.Data
{
public interface IDatabaseProvider
{
IDbConnection NewConnection();
}
}

View file

@ -1,17 +0,0 @@
using System.Data;
using Microsoft.Extensions.Configuration;
using MySql.Data.MySqlClient;
namespace Website.Data
{
public class MySQLDatabaseProvider:IDatabaseProvider
{
private readonly string _connectionString;
public MySQLDatabaseProvider(IConfiguration config) => _connectionString = config.GetConnectionString("database");
public MySQLDatabaseProvider(string connectionString) => _connectionString = connectionString;
public IDbConnection NewConnection() => new MySqlConnection(_connectionString);
}
}

View file

@ -1,10 +0,0 @@
namespace Website.Data.States {
public class UserState {
public string User_Id { get; set; }
public string User_Email { get; set; }
public string User_Password { get; set; }
public string User_Created { get; set; }
public string User_Deleted { get; set; }
public string Group_Id { get; set; }
}
}

View file

@ -1,25 +0,0 @@
using System.Linq;
using System.Threading.Tasks;
using Dapper;
using Website.Data.States;
using Website.Models;
namespace Website.Data {
public class UserRepository {
private readonly IDatabaseProvider _dbProvider;
public UserRepository(IDatabaseProvider dbProvider) {
_dbProvider = dbProvider;
}
public async Task<User> GetUserByEmail(string email) {
const string query = "SELECT * FROM users WHERE user_email=@email";
using (var connection = _dbProvider.NewConnection()) {
connection.Open();
var result = await connection.QueryAsync<UserState>(query, new {email});
return new User(result.Single());
}
}
}
}

View file

@ -1,31 +1,5 @@
using System.Security.Cryptography;
using System.Text;
using Website.Data.States;
namespace Website.Models {
namespace Website.Models {
public class User {
public User() {
}
public User(UserState state) {
Username = state.User_Email;
Password = state.User_Password;
}
public bool ValidatePassword(string password) {
using (var sha256 = SHA256.Create()) {
var hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(password));
var builder = new StringBuilder();
foreach (var b in hash)
builder.Append(b.ToString("x2"));
var hashString = builder.ToString();
return hashString == Password;
}
}
public string Username { get; set; }
public string Password { get; set; }
}

View file

@ -1,5 +1,4 @@
using System;
using System.Net;
using System.Net.Http;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
@ -32,8 +31,6 @@ namespace Website
});
services.AddSingleton(Configuration);
services.AddSingleton<IDatabaseProvider, MySQLDatabaseProvider>()
.AddSingleton<UserRepository, UserRepository>();
services.AddHttpClient<IGitApi, GitApi>(client => client.BaseAddress = new Uri(Configuration["gitApiEndpoint"]))
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler {ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true});
@ -41,6 +38,9 @@ namespace Website
services.AddHttpClient<IBlogApi, BlogApi>(client => client.BaseAddress = new Uri(Configuration["blogApiEndpoint"]))
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler {ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true});
services.AddHttpClient<IAuthenticationProvider, AuthenticationProvider>(client => client.BaseAddress = new Uri(Configuration["authApiEndpoint"]))
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler {ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true});
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
services.AddMvc(options => options.EnableEndpointRouting = false)

View file

@ -8,11 +8,8 @@
<ItemGroup>
<PackageReference Include="BuildBundlerMinifier" Version="3.2.435" />
<PackageReference Include="dapper" Version="2.0.30" />
<PackageReference Include="dapper.contrib" Version="2.0.30" />
<PackageReference Include="markdig" Version="0.18.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.0" />
<PackageReference Include="mysqlconnector" Version="0.61.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Pek.Markdig.HighlightJs" Version="0.5.1" />
</ItemGroup>

View file

@ -10,5 +10,6 @@
"database": "Server=localhost;User ID=user;Password=pass;Database=db"
},
"blogApiEndpoint": "",
"gitApiEndpoint": ""
"gitApiEndpoint": "",
"authApiEndpoint": ""
}

View file

@ -9,6 +9,7 @@
},
"blogApiEndpoint": "<BlogEndpoint>",
"gitApiEndpoint": "<GitEndpoint>",
"authApiEndpoint": "<AuthEndpoint>",
"AllowedHosts": "*",
"Kestrel": {
"EndPoints": {