init
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
using Blazored.SessionStorage;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SecureBank.Website.Authentication
|
||||
{
|
||||
public class AuthenticationHelper
|
||||
{
|
||||
#region CONSTANTS
|
||||
|
||||
private const string TOKEN_KEY = "token";
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region SERVICES
|
||||
|
||||
private readonly ISessionStorageService _sessionStorageService;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTIONS
|
||||
|
||||
public AuthenticationHelper(ISessionStorageService sessionStorageService)
|
||||
{
|
||||
_sessionStorageService = sessionStorageService;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public async Task<string> GetToken() => await _sessionStorageService.GetItemAsync<string>(TOKEN_KEY);
|
||||
|
||||
public async Task SaveToken(string token) => await _sessionStorageService.SetItemAsync(TOKEN_KEY, token);
|
||||
|
||||
public async Task RemoveToken() => await _sessionStorageService.RemoveItemAsync(TOKEN_KEY);
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Blazored.SessionStorage" Version="2.4.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="8.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\SecureBank.Helpers\SecureBank.Helpers.csproj" />
|
||||
<ProjectReference Include="..\SecureBank.Website.Services\SecureBank.Website.Services.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,75 @@
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using SecureBank.Common;
|
||||
using SecureBank.Website.Services;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SecureBank.Website.Authentication
|
||||
{
|
||||
public class TokenAuthenticationStateProvider : AuthenticationStateProvider
|
||||
{
|
||||
#region SERVICES
|
||||
|
||||
private readonly IAccountsService _accountsService;
|
||||
|
||||
private readonly AuthenticationHelper _authenticationHelper;
|
||||
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public TokenAuthenticationStateProvider(IAccountsService accountsService, AuthenticationHelper authenticationHelper, HttpClient httpClient)
|
||||
{
|
||||
_accountsService = accountsService;
|
||||
_authenticationHelper = authenticationHelper;
|
||||
_httpClient = httpClient;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
|
||||
{
|
||||
_httpClient.DefaultRequestHeaders.Authorization = null;
|
||||
AuthenticationState state = new AuthenticationState(new ClaimsPrincipal());
|
||||
|
||||
string token = await _authenticationHelper.GetToken();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(token))
|
||||
{
|
||||
return state;
|
||||
}
|
||||
|
||||
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
||||
|
||||
APIResponse<string> refreshResponse = await _accountsService.AuthenticationRefresh();
|
||||
|
||||
if (!refreshResponse.Success)
|
||||
{
|
||||
_httpClient.DefaultRequestHeaders.Authorization = null;
|
||||
return state;
|
||||
}
|
||||
|
||||
token = refreshResponse.Data;
|
||||
|
||||
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
||||
state = new AuthenticationState(new ClaimsPrincipal()); //TODO: Add claims
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user