Skip to content

Commit

Permalink
Merge pull request #6 from Aguafrommars/user/ole
Browse files Browse the repository at this point in the history
style: remove empty statement
  • Loading branch information
aguacongas authored Jan 8, 2023
2 parents 66b7f75 + b8eb66b commit 623e01d
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,29 @@

namespace Aguacongas.AspNetCore.Components.Maui.Authentication.Oidc.Abstraction;

/// <summary>
/// Authentication store interface
/// </summary>
public interface IAuthenticationStore
{
/// <summary>
/// Deletes a scope
/// </summary>
/// <param name="scope">The scope</param>
void Delete(string scope);

/// <summary>
/// Gets <see cref="AuthenticationEntity"/> for a scope
/// </summary>
/// <param name="scope"></param>
/// <returns>The stored entity</returns>
Task<AuthenticationEntity> GetAsync(string scope);

/// <summary>
/// Sets <see cref="AuthenticationEntity"/> for a scope
/// </summary>
/// <param name="scope">The scope</param>
/// <param name="value">The entity to store</param>
/// <returns></returns>
Task SetAsync(string scope, AuthenticationEntity value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@
<CodeAnalysisRuleSet>..\..\.sonarlint\aguafrommars_maui.blazor.authenticationcsharp.ruleset</CodeAnalysisRuleSet>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PublishReadyToRun>false</PublishReadyToRun>
<Authors>Olivier Lefebvre</Authors>
<Description>Simplify OIDC authentication for MAUI Blazor app.</Description>
<Copyright>Copyright (c) 2023 @Olivier Lefebvre</Copyright>
<PackageProjectUrl>https://github.com/Aguafrommars/Maui.Blazor.Authentication/tree/main/src/Aguacongas.AspNetCore.Components.Maui.Authentication.Oidc</PackageProjectUrl>
<RepositoryUrl>https://github.com/Aguafrommars/Maui.Blazor.Authentication</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>
openid-connect;oidc;openidconnect-client;maui;blazor;maui-blazor;auhtentication</PackageTags>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageIcon>package-icon.png</PackageIcon>
<Title>Maui.Blazor.Authentication</Title>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand All @@ -30,7 +42,9 @@
</ItemGroup>

<ItemGroup>
<None Include="README.md" Pack="true" PackagePath="\"/>
<None Include="README.md" Pack="true" PackagePath="\" />
</ItemGroup>
<ItemGroup>
<None Include="package-icon.png" Pack="true" PackagePath="" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,17 +1,47 @@
namespace Aguacongas.AspNetCore.Components.Maui.Authentication.Oidc.Models;

/// <summary>
/// Define an authentication
/// </summary>
public record AuthenticationEntity
{
/// <summary>
/// Collection of claims
/// </summary>
public IEnumerable<ClaimEntity> Claims { get; init; }

/// <summary>
/// Access token
/// </summary>
public string AccessToken { get; set; }

/// <summary>
/// Refresh token
/// </summary>
public string RefreshToken { get; set; }

/// <summary>
/// Identity token
/// </summary>
public string IdentityToken { get; set; }

/// <summary>
/// Access token expiration date
/// </summary>
public DateTimeOffset AccessTokenExpiration { get; set; }

/// <summary>
/// Type of authentication
/// </summary>
public string AuthenticationType { get; init; }

/// <summary>
/// Type of role claim
/// </summary>
public string RoleClaintType { get; init; }

/// <summary>
/// Type of name claim
/// </summary>
public string NameClaimType { get; init; }
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
namespace Aguacongas.AspNetCore.Components.Maui.Authentication.Oidc.Models;

/// <summary>
/// Define a claim
/// </summary>
public record ClaimEntity
{
/// <summary>
/// Claim type
/// </summary>
public string Type { get; init; }

/// <summary>
/// Claim value
/// </summary>
public string Value { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ namespace WinUIEx;
/// </remarks>
internal sealed class WebAuthenticator: IWebAuthenticator
{
/// <summary>
/// Singleton instance of <see cref="WebAuthenticator"/>
/// </summary>
public static readonly WebAuthenticator Instance = new();

private readonly Dictionary<string, TaskCompletionSource<Uri>> tasks = new();
Expand All @@ -32,11 +35,16 @@ internal sealed class WebAuthenticator: IWebAuthenticator

private WebAuthenticator()
{
_appInstance = AppLifecycle.AppInstance.GetCurrent() ?? throw new InvalidOperationException("The WebAuthenticator requires an app instance"); ;
_package = Package.Current ?? throw new InvalidOperationException("The WebAuthenticator requires a packaged app with an AppxManifest"); ;
_appInstance = AppLifecycle.AppInstance.GetCurrent() ?? throw new InvalidOperationException("The WebAuthenticator requires an app instance");
_package = Package.Current ?? throw new InvalidOperationException("The WebAuthenticator requires a packaged app with an AppxManifest");
SubcribeToActivated(_appInstance);
}

/// <summary>
/// Anthenticates the user
/// </summary>
/// <param name="webAuthenticatorOptions">The authentication options</param>
/// <returns></returns>
public Task<WebAuthenticatorResult> AuthenticateAsync(WebAuthenticatorOptions webAuthenticatorOptions)
=> AuthenticateAsync(webAuthenticatorOptions.Url, webAuthenticatorOptions.CallbackUrl);

Expand All @@ -54,6 +62,10 @@ internal static void Init()
Trace.WriteLine($"WinUIEx: Failed to initialize the WebAuthenticator: {ex.Message}", "WinUIEx");
}
}

/// <summary>
/// Method call on application initialization.
/// </summary>
public void OnAppCreation()
{
var activatedEventArgs = _appInstance?.GetActivatedEventArgs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

namespace Aguacongas.AspNetCore.Components.Maui.Authentication.Oidc.Services;

/// <summary>
/// OIDC authentication service
/// </summary>
/// <typeparam name="TRemoteAuthenticationState"></typeparam>
public class OidcAuthenticationService<TRemoteAuthenticationState> :
AuthenticationStateProvider,
IRemoteAuthenticationService<TRemoteAuthenticationState>,
Expand All @@ -36,6 +40,13 @@ private DateTimeOffset ExpireAt
}
}

/// <summary>
/// Initialize a new instance of <see cref="OidcAuthenticationService{TRemoteAuthenticationState}"/>
/// </summary>
/// <param name="oidcClient">An <see cref="OidcClient"/></param>
/// <param name="store">A <see cref="IAuthenticationStore"/></param>
/// <param name="navigation">A <see cref="NavigationManager"/></param>
/// <param name="options"><see cref="OidcProviderOptions"/></param>
public OidcAuthenticationService(OidcClient oidcClient,
IAuthenticationStore store,
NavigationManager navigation,
Expand All @@ -47,12 +58,15 @@ public OidcAuthenticationService(OidcClient oidcClient,
_options = options;
}

/// <inheritdoc />
public Task<RemoteAuthenticationResult<TRemoteAuthenticationState>> CompleteSignInAsync(RemoteAuthenticationContext<TRemoteAuthenticationState> context)
=> throw new NotImplementedException();

/// <inheritdoc />
public Task<RemoteAuthenticationResult<TRemoteAuthenticationState>> CompleteSignOutAsync(RemoteAuthenticationContext<TRemoteAuthenticationState> context)
=> throw new NotImplementedException();

/// <inheritdoc />
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
if (ExpireAt < DateTimeOffset.Now.AddMinutes(5))
Expand All @@ -74,12 +88,14 @@ public override async Task<AuthenticationState> GetAuthenticationStateAsync()
return new AuthenticationState(_principal);
}

/// <inheritdoc />
public ValueTask<AccessTokenResult> RequestAccessToken()
=> RequestAccessToken(new AccessTokenRequestOptions
{
Scopes = _options.Value.ProviderOptions.DefaultScopes
});

/// <inheritdoc />
public async ValueTask<AccessTokenResult> RequestAccessToken(AccessTokenRequestOptions options)
{
var authentication = await GetAuthenticationAsync(string.Join(' ', options.Scopes)).ConfigureAwait(false);
Expand All @@ -99,6 +115,7 @@ public async ValueTask<AccessTokenResult> RequestAccessToken(AccessTokenRequestO
} : null);
}

/// <inheritdoc />
public async Task<RemoteAuthenticationResult<TRemoteAuthenticationState>> SignInAsync(RemoteAuthenticationContext<TRemoteAuthenticationState> context)
{
var result = await _oidcClient.LoginAsync().ConfigureAwait(false);
Expand All @@ -114,6 +131,7 @@ public async Task<RemoteAuthenticationResult<TRemoteAuthenticationState>> SignIn
};
}

/// <inheritdoc />
public async Task<RemoteAuthenticationResult<TRemoteAuthenticationState>> SignOutAsync(RemoteAuthenticationContext<TRemoteAuthenticationState> context)
{
await Task.Factory.StartNew(() => _oidcClient.LogoutAsync());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@

namespace Aguacongas.AspNetCore.Components.Maui.Authentication.Oidc.Services;

/// <summary>
/// Web browser authenticator
/// </summary>
public class WebBrowserAuthenticator : IBrowser
{
private readonly IWebAuthenticator _authenticator;

/// <summary>
/// Initialize a new instance of <see cref="WebBrowserAuthenticator"/>
/// </summary>
/// <param name="authenticator"></param>
public WebBrowserAuthenticator(IWebAuthenticator authenticator)
{
_authenticator = authenticator;
}

/// <inheritdoc/>
public async Task<BrowserResult> InvokeAsync(BrowserOptions options, CancellationToken cancellationToken = default)
{
try
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 623e01d

Please sign in to comment.