Skip to content

Commit

Permalink
Merge pull request #104 from mdsol/authenticator
Browse files Browse the repository at this point in the history
Expose MAuthenticator class
  • Loading branch information
dcassidy-mdsol authored Dec 16, 2024
2 parents 303164f + edd8c76 commit f3b6286
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/test-net60.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ jobs:
uses: actions/checkout@v2
with:
submodules: 'recursive'
- name: Setup dotnet
uses: actions/setup-dotnet@v4
with:
dotnet-version: |
6.0.x
- name: Run the Core tests
run: dotnet test $GITHUB_WORKSPACE/tests/Medidata.MAuth.CoreTests --framework net6.0
- name: Run the ASP.NET Core tests
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Changes in Medidata.MAuth
## v5.1.8
- **[Core]** Updated MAuthenticator to be public
## v5.1.7
- **[Core]** Fix preprocessor for sync methods

Expand Down
8 changes: 5 additions & 3 deletions src/Medidata.MAuth.AspNetCore/MAuthAspNetCoreExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
using Medidata.MAuth.Core;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.Extensions.Logging;

namespace Medidata.MAuth.AspNetCore
{
internal static class MAuthAspNetCoreExtensions
/// <summary>
/// MAuth extension methods.
/// </summary>
public static class MAuthAspNetCoreExtensions
{
/// <summary>
/// Converts an <see cref="HttpRequest"/> object to an equivalent <see cref="HttpRequestMessage"/> object.
Expand Down Expand Up @@ -46,7 +48,7 @@ public static HttpRequestMessage ToHttpRequestMessage(this HttpRequest request)
/// will throw an exception if any errors occurred during the authentication.
/// </returns>
public static async Task<bool> TryAuthenticate(
this HttpContext context, MAuthAuthenticator authenticator, bool shouldIgnoreExceptions)
this HttpContext context, IMAuthAuthenticator authenticator, bool shouldIgnoreExceptions)
{
try
{
Expand Down
19 changes: 19 additions & 0 deletions src/Medidata.MAuth.Core/IMAuthAuthenticator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Net.Http;
using System.Threading.Tasks;

namespace Medidata.MAuth.Core;

/// <summary>
/// MAuth authenticator.
/// </summary>
public interface IMAuthAuthenticator
{
/// <summary>
/// Authenticate http request.
/// </summary>
/// <param name="request">Http context converted to a http request.</param>
/// <returns>
/// This method returns <see langword="true"/> if it successfully authenticated the request otherwise it will return <see langword="false"/>.
/// </returns>
Task<bool> AuthenticateRequest(HttpRequestMessage request);
}
44 changes: 43 additions & 1 deletion src/Medidata.MAuth.Core/MAuthAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

namespace Medidata.MAuth.Core
{
internal class MAuthAuthenticator
/// <summary>
/// MAuth service class to process authentication.
/// </summary>
public class MAuthAuthenticator : IMAuthAuthenticator
{
private const int AllowedDriftSeconds = 300;
private static readonly TimeSpan AllowedDriftTimeSpan = TimeSpan.FromSeconds(AllowedDriftSeconds);
Expand All @@ -22,8 +25,17 @@ internal class MAuthAuthenticator
private readonly IDateTimeOffsetWrapper _dateTimeOffsetWrapper;
private readonly Lazy<HttpClient> _lazyHttpClient;

/// <summary>
/// MAuth application uuid.
/// </summary>
public Guid ApplicationUuid => _options.ApplicationUuid;

/// <summary>
/// Create a new instance <see cref="MAuthAuthenticator"/>
/// </summary>
/// <param name="options">MAuth options</param>
/// <param name="logger">Logger</param>
/// <param name="cacheService">Cache service. (Optional)</param>
public MAuthAuthenticator(MAuthOptionsBase options, ILogger logger, ICacheService cacheService = null)
{
if (options.ApplicationUuid == default)
Expand All @@ -41,6 +53,36 @@ public MAuthAuthenticator(MAuthOptionsBase options, ILogger logger, ICacheServic
_lazyHttpClient = new Lazy<HttpClient>(() => CreateHttpClient(options));
_dateTimeOffsetWrapper = options.DateTimeOffsetWrapper;
}

/// <summary>
/// Create a new instance <see cref="MAuthAuthenticator"/>
/// </summary>
/// <param name="options">MAuth options</param>
/// <param name="logger">Logger</param>
/// <param name="httpClient">Http Client</param>
/// <param name="cacheService">Cache service. (Optional)</param>
public MAuthAuthenticator(MAuthOptionsBase options, ILogger logger, HttpClient httpClient, ICacheService cacheService = null)
{
if (options.ApplicationUuid == default)
throw new ArgumentException(nameof(options.ApplicationUuid));

if (options.MAuthServiceUrl == null)
throw new ArgumentNullException(nameof(options.MAuthServiceUrl));

if (string.IsNullOrWhiteSpace(options.PrivateKey))
throw new ArgumentNullException(nameof(options.PrivateKey));

_cache = cacheService ?? new MemoryCacheService(new MemoryCache(new MemoryCacheOptions()));
_options = options;
_logger = logger;
#if NET6_0_OR_GREATER
_lazyHttpClient = new Lazy<HttpClient>(httpClient);
#else
_lazyHttpClient = new Lazy<HttpClient>(() => httpClient);
#endif

_dateTimeOffsetWrapper = options.DateTimeOffsetWrapper;
}

/// <summary>
/// Verifies if the <see cref="HttpRequestMessage"/> request is authenticated or not.
Expand Down
2 changes: 1 addition & 1 deletion version.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<Version>5.1.7</Version>
<Version>5.1.8</Version>
</PropertyGroup>
</Project>

0 comments on commit f3b6286

Please sign in to comment.