Skip to content

Commit

Permalink
Add Authentication Service
Browse files Browse the repository at this point in the history
  • Loading branch information
oveldman committed Apr 1, 2024
1 parent 7ae8f3b commit 8117187
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 0 deletions.
3 changes: 3 additions & 0 deletions sources/Clients.Admin.Web/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using MadWorldNL.Clients.Admin.Web;
using MadWorldNL.Clients.Identity.Blazor.Shared.Extensions;

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

builder.AddIdentity();

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

await builder.Build().RunAsync();
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Identity": {
"Host": "https://localhost:7047/"
}
}
5 changes: 5 additions & 0 deletions sources/Clients.Admin.Web/wwwroot/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Identity": {
"Host": "https://localhost:7047/"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Net.Http.Json;
using MadWorldNL.Clients.Identity.Api.Contracts.Authentications;
using MadWorldNL.Clients.Identity.Blazor.Shared.Settings;

namespace MadWorldNL.Clients.Identity.Blazor.Shared.Authentications;

public class AuthenticationService : IAuthenticationService
{
private const string Endpoint = "Authentication";

private readonly HttpClient _client;
public AuthenticationService(IHttpClientFactory clientFactory)
{
_client = clientFactory.CreateClient(ApiTypes.AnonymousIdentity);
}

public async Task<LoginProxyResponse> LoginAsync(LoginProxyRequest request)
{
var response = await _client.PostAsJsonAsync(Endpoint, request);
return await response.Content.ReadFromJsonAsync<LoginProxyResponse>() ?? new LoginProxyResponse();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using MadWorldNL.Clients.Identity.Api.Contracts.Authentications;

namespace MadWorldNL.Clients.Identity.Blazor.Shared.Authentications;

public interface IAuthenticationService
{
Task<LoginProxyResponse> LoginAsync(LoginProxyRequest request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="8.0.2"/>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" />
<PackageReference Include="Microsoft.Extensions.Http" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using MadWorldNL.Clients.Identity.Blazor.Shared.Authentications;
using MadWorldNL.Clients.Identity.Blazor.Shared.Settings;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace MadWorldNL.Clients.Identity.Blazor.Shared.Extensions;

public static class WebAssemblyHostBuilderExtensions
{
public static void AddIdentity(this WebAssemblyHostBuilder builder)
{
builder.Services.AddOptions<IdentitySettings>()
.Bind(builder.Configuration.GetSection(IdentitySettings.Entry));

builder.Services.AddScoped<IAuthenticationService, AuthenticationService>();

builder.AddIdentityClients();
}

private static void AddIdentityClients(this WebAssemblyHostBuilder builder)
{
builder.Services.AddHttpClient(ApiTypes.AnonymousIdentity, (serviceProvider, client) =>
{
var identitySettingsOption = serviceProvider.GetService<IOptions<IdentitySettings>>()!;
client.BaseAddress = new Uri(identitySettingsOption.Value.Host!);
});

builder.Services.AddHttpClient(ApiTypes.Identity, (serviceProvider, client) =>
{
var identitySettingsOption = serviceProvider.GetService<IOptions<IdentitySettings>>()!;
client.BaseAddress = new Uri(identitySettingsOption.Value.Host!);
});
}
}
7 changes: 7 additions & 0 deletions sources/Clients.Identity.Blazor.Shared/Settings/ApiTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace MadWorldNL.Clients.Identity.Blazor.Shared.Settings;

public static class ApiTypes
{
public const string AnonymousIdentity = nameof(AnonymousIdentity);
public const string Identity = nameof(Identity);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace MadWorldNL.Clients.Identity.Blazor.Shared.Settings;

public class IdentitySettings
{
public const string Entry = "Identity";
public string Host { get; set; } = string.Empty;
}
6 changes: 6 additions & 0 deletions sources/Clients.Identity.Blazor.Shared/pages/Login.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace MadWorldNL.Clients.Identity.Blazor.Shared.pages;

public partial class Login
{

}
1 change: 1 addition & 0 deletions sources/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="8.0.2" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.2" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageVersion Include="Microsoft.IdentityModel.Tokens" Version="7.4.0" />
Expand Down

0 comments on commit 8117187

Please sign in to comment.