Skip to content

Commit

Permalink
Merge pull request #128 from jinaga/interfaces
Browse files Browse the repository at this point in the history
Inject AuthenticationService by interface
  • Loading branch information
michaellperry authored Jul 14, 2024
2 parents 9e01487 + ea6ad37 commit b61fd29
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Jinaga.Maui/Authentication/AuthenticationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Jinaga.Maui.Authentication;
/// <summary>
/// Provides authentication services, including token management and user authentication.
/// </summary>
public class AuthenticationService : IHttpAuthenticationProvider
public class AuthenticationService : IHttpAuthenticationProvider, IAuthenticationService
{
private readonly ITokenStorage tokenStorage;
private readonly UserProvider userProvider;
Expand Down Expand Up @@ -103,7 +103,7 @@ public async Task<bool> Initialize()
/// </summary>
/// <param name="provider">The identifier of the authentication provider to use</param>
/// <returns>True if the user successfully authenticated</returns>
public async Task<bool> Login(string provider)
public async Task<bool> LogIn(string provider)
{
lock (stateLock)
{
Expand Down
24 changes: 24 additions & 0 deletions Jinaga.Maui/Authentication/IAuthenticationService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace Jinaga.Maui.Authentication;

/// <summary>
/// Provides authentication services, including token management and user authentication.
/// </summary>
public interface IAuthenticationService
{
/// <summary>
/// Initializes the authentication service. Call at application startup.
/// </summary>
/// <returns>True if the user is authenticated</returns>
Task<bool> Initialize();
/// <summary>
/// Log the user in.
/// </summary>
/// <param name="provider">The identifier of the authentication provider to use</param>
/// <returns>True if the user successfully authenticated</returns>
Task<bool> LogIn(string provider);
/// <summary>
/// Log the user out.
/// </summary>
/// <returns>Resolves when logout is successful</returns>
Task LogOut();
}
2 changes: 1 addition & 1 deletion Jinaga.Maui/ConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static IServiceCollection AddJinagaAuthentication(this IServiceCollection
services.AddSingleton<IHttpAuthenticationProvider>(
s => s.GetRequiredService<AuthenticationProviderProxy>());
services.AddSingleton<OAuthClient>();
services.AddSingleton<AuthenticationService>();
services.AddSingleton<IAuthenticationService, AuthenticationService>();
return services;
}

Expand Down
11 changes: 11 additions & 0 deletions Jinaga/Http/IHttpAuthenticationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,20 @@

namespace Jinaga.Http
{
/// <summary>
/// Adds authentication details to HTTP requests.
/// </summary>
public interface IHttpAuthenticationProvider
{
/// <summary>
/// Set the authentication token in the request headers.
/// </summary>
/// <param name="headers">HTTP headers to modify</param>
void SetRequestHeaders(HttpRequestHeaders headers);
/// <summary>
/// Reauthenticate the user. Called when a request fails with a 401 Unauthorized status.
/// </summary>
/// <returns>True if the token was successfully refreshed</returns>
Task<JinagaAuthenticationState> Reauthenticate();
}
}

0 comments on commit b61fd29

Please sign in to comment.