Skip to content

Commit

Permalink
UPDATED: Changes Tracked by Git
Browse files Browse the repository at this point in the history
- ADDED: CacheStorageAccessor.cs file
- ADDED: Dgmjr.Blazor.Caching.csproj file
- ADDED: BlazorSecurityConstants.cs file
- ADDED: AccountController.cs file
- ADDED: Dgmjr.Blazor.Security.csproj file
DED: LICENSE.md file
- ADDED: ApplicationAuthenticationState.cs file
- ADDED: ApplicationClaim.cs file
- ADDED: ApplicationUser.cs file
- ADDED: SecurityServiceCollectionExtensions.cs file
- ADDED: ApplicationAuthenticationStateProvider.cs file
- ADDED: ISecurityService.cs file
- ADDED: SecurityService.cs file
- ADDED: icon.png file
- UPDATED: KeyPerJsonFileConfigurationExtensions.cs file
- ADDED: IndExtensions.cs file
  • Loading branch information
dgmjr committed Feb 27, 2024
1 parent e0a8bc0 commit cf94bfe
Show file tree
Hide file tree
Showing 16 changed files with 424 additions and 6 deletions.
98 changes: 98 additions & 0 deletions src/Blazor.Caching/CacheStorageAccessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
namespace Dgmjr.Blazor.Caching;

public class CacheStorageAccessor : IAsyncDisposable
{
private Lazy<IJSObjectReference> _accessorJsRef = new();
private readonly IJSRuntime _jsRuntime;

public CacheStorageAccessor(IJSRuntime jsRuntime)
{
_jsRuntime = jsRuntime;
}

private async Task WaitForReference()
{
if (_accessorJsRef.IsValueCreated is false)
{
_accessorJsRef = new(
await _jsRuntime.InvokeAsync<IJSObjectReference>(
"import",
"/js/CacheStorageAccessor.js"
)
);
}
}

public async ValueTask DisposeAsync()
{
if (_accessorJsRef.IsValueCreated)
{
await _accessorJsRef.Value.DisposeAsync();
}
}

public async Task StoreAsync(
HttpRequestMessage requestMessage,
HttpResponseMessage responseMessage
)
{
await WaitForReference();
string requestMethod = requestMessage.Method.Method;
string requestBody = await GetRequestBodyAsync(requestMessage);
string responseBody = await responseMessage.Content.ReadAsStringAsync();

await _accessorJsRef.Value.InvokeVoidAsync(
"store",
requestMessage.RequestUri,
requestMethod,
requestBody,
responseBody
);
}

public async Task<string> GetAsync(HttpRequestMessage requestMessage)
{
await WaitForReference();
string requestMethod = requestMessage.Method.Method;
string requestBody = await GetRequestBodyAsync(requestMessage);
string result = await _accessorJsRef.Value.InvokeAsync<string>(
"get",
requestMessage.RequestUri,
requestMethod,
requestBody
);

return result;
}

public async Task RemoveAsync(HttpRequestMessage requestMessage)
{
await WaitForReference();
string requestMethod = requestMessage.Method.Method;
string requestBody = await GetRequestBodyAsync(requestMessage);
await _accessorJsRef.Value.InvokeVoidAsync(
"remove",
requestMessage.RequestUri,
requestMethod,
requestBody
);
}

public async Task RemoveAllAsync()
{
await WaitForReference();
await _accessorJsRef.Value.InvokeVoidAsync("removeAll");
}

private static async Task<string> GetRequestBodyAsync(HttpRequestMessage requestMessage)
{
string requestBody = "";

if (requestMessage.Content is not null)
{
requestBody = await requestMessage.Content.ReadAsStringAsync() ?? "";
}

return requestBody;
}
}
8 changes: 8 additions & 0 deletions src/Blazor.Caching/Dgmjr.Blazor.Caching.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0;net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Blazored.LocalStorage"/>
</ItemGroup>
</Project>
6 changes: 6 additions & 0 deletions src/Blazor.Security/Constants/BlazorSecurityConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Dgmjr.Blazor.Security;

public static class BlazorSecurityConstants
{
public const string BlazorSecurity = nameof(BlazorSecurity);
}
51 changes: 51 additions & 0 deletions src/Blazor.Security/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Mvc;
using Dgmjr.Blazor.Security.Models;
using Microsoft.Extensions.Logging;
using Dgmjr.AspNetCore.Mvc;

namespace Dgmjr.Blazor.Security.Controllers;

[Route("Account/[action]")]
[ApiController]
public partial class AccountController(ILogger<AccountController> logger) : ApiControllerBase(logger)
{
public IActionResult Login(string redirectUri)
{
var redirectUrl = redirectUri ?? Url.Content("~/");

return Challenge(
new AuthenticationProperties { RedirectUri = redirectUrl },
OpenIdConnectDefaults.AuthenticationScheme
);
}

public IActionResult Logout()
{
var redirectUrl = Url.Content("~/");

return SignOut(
new AuthenticationProperties { RedirectUri = redirectUrl },
CookieAuthenticationDefaults.AuthenticationScheme,
OpenIdConnectDefaults.AuthenticationScheme
);
}

[HttpPost]
public ApplicationAuthenticationState CurrentUser()
{
return new ApplicationAuthenticationState
{
IsAuthenticated = User.Identities.Any(id => id.IsAuthenticated),
Name = User.Identity.Name,
Claims = User.Claims.Select(
c => new ApplicationClaim { Type = c.Type, Value = c.Value }
)
};
}
}
13 changes: 13 additions & 0 deletions src/Blazor.Security/Dgmjr.Blazor.Security.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Radzen.Blazor" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" />
<PackageReference Include="Microsoft.AspNetCore.HeaderPropagation" />
<PackageReference Include="Microsoft.Identity.Web" />
<PackageReference Include="Dgmjr.AspNetCore.Mvc" />
</ItemGroup>
</Project>
35 changes: 35 additions & 0 deletions src/Blazor.Security/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
date: 2023-07-13T05:44:46:00-05:00Z
description: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files, yadda, yadda, yadda...
keywords:
- IP
- copyright
- license
- mit
permissions:
- commercial-use
- modifications
- distribution
- private-use
conditions:
- include-copyright
limitations:
- liability
- warranty
lastmod: 2024-01-0T00:39:00.0000+05:00Z
license: MIT
slug: mit-license
title: MIT License
type: license
---

# MIT License

## Copyright © 2022-2024 [David G. Moore, Jr.](mailto:[email protected] "Send Dr. Moore") ([@dgmjr](https://github.com/dgmjr "Contact Dr. Moore on GitHub")), All Rights Reserved

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

11 changes: 11 additions & 0 deletions src/Blazor.Security/Models/ApplicationAuthenticationState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;

namespace Dgmjr.Blazor.Security.Models;

public partial class ApplicationAuthenticationState
{
public bool IsAuthenticated { get; set; }
public string Name { get; set; }
public IEnumerable<ApplicationClaim> Claims { get; set; }
}
7 changes: 7 additions & 0 deletions src/Blazor.Security/Models/ApplicationClaim.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Dgmjr.Blazor.Security.Models;

public class ApplicationClaim
{
public string Type { get; set; }
public string Value { get; set; }
}
9 changes: 9 additions & 0 deletions src/Blazor.Security/Models/ApplicationUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;
using System.Collections.Generic;

namespace Dgmjr.Blazor.Security.Models;

public partial class ApplicationUser
{
public string Name { get; set; }
}
12 changes: 12 additions & 0 deletions src/Blazor.Security/SecurityServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Dgmjr.Blazor.Security.Services;

namespace Microsoft.Extensions.DependencyInjection;

public static class SecurityServiceCollectionExtensions
{
public static IServiceCollection AddSecurityService(this IServiceCollection services)
{
services.AddScoped<ISecurityService, SecurityService>();
return services;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.Authorization;

using Dgmjr.Blazor.Security.Models;

namespace Dgmjr.Blazor.Security.Services;

public class ApplicationAuthenticationStateProvider(ISecurityService securityService) : AuthenticationStateProvider
{
private ApplicationAuthenticationState _authenticationState;

public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
var identity = new ClaimsIdentity();

try
{
var state = await GetApplicationAuthenticationStateAsync();

if (state.IsAuthenticated)
{
identity = new ClaimsIdentity(
state.Claims.Select(c => new Claim(c.Type, c.Value)),
BlazorSecurityConstants.BlazorSecurity
);
}
}
catch (HttpRequestException) { /* swallow the exception */ }

var result = new AuthenticationState(new ClaimsPrincipal(identity));

securityService.Initialize(result);

return result;
}

private async Task<ApplicationAuthenticationState> GetApplicationAuthenticationStateAsync()
{
return _authenticationState ??= await securityService.GetAuthenticationStateAsync();
}
}
20 changes: 20 additions & 0 deletions src/Blazor.Security/Services/ISecurityService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Security.Claims;

using Dgmjr.Blazor.Security.Models;

using Microsoft.AspNetCore.Components.Authorization;

namespace Dgmjr.Blazor.Security.Services;

public interface ISecurityService
{
ApplicationUser User { get; }
ClaimsPrincipal Principal { get; }

Task<ApplicationAuthenticationState> GetAuthenticationStateAsync();
bool Initialize(AuthenticationState result);
bool IsAuthenticated();
bool IsInRole(params string[] roles);
void Login();
void Logout();
}
Loading

0 comments on commit cf94bfe

Please sign in to comment.