Skip to content

Commit f3b6286

Browse files
Merge pull request #104 from mdsol/authenticator
Expose MAuthenticator class
2 parents 303164f + edd8c76 commit f3b6286

File tree

6 files changed

+75
-5
lines changed

6 files changed

+75
-5
lines changed

.github/workflows/test-net60.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ jobs:
88
uses: actions/checkout@v2
99
with:
1010
submodules: 'recursive'
11+
- name: Setup dotnet
12+
uses: actions/setup-dotnet@v4
13+
with:
14+
dotnet-version: |
15+
6.0.x
1116
- name: Run the Core tests
1217
run: dotnet test $GITHUB_WORKSPACE/tests/Medidata.MAuth.CoreTests --framework net6.0
1318
- name: Run the ASP.NET Core tests

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Changes in Medidata.MAuth
2+
## v5.1.8
3+
- **[Core]** Updated MAuthenticator to be public
24
## v5.1.7
35
- **[Core]** Fix preprocessor for sync methods
46

src/Medidata.MAuth.AspNetCore/MAuthAspNetCoreExtensions.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
using Medidata.MAuth.Core;
66
using Microsoft.AspNetCore.Http;
77
using Microsoft.AspNetCore.Http.Extensions;
8-
using Microsoft.Extensions.Logging;
98

109
namespace Medidata.MAuth.AspNetCore
1110
{
12-
internal static class MAuthAspNetCoreExtensions
11+
/// <summary>
12+
/// MAuth extension methods.
13+
/// </summary>
14+
public static class MAuthAspNetCoreExtensions
1315
{
1416
/// <summary>
1517
/// Converts an <see cref="HttpRequest"/> object to an equivalent <see cref="HttpRequestMessage"/> object.
@@ -46,7 +48,7 @@ public static HttpRequestMessage ToHttpRequestMessage(this HttpRequest request)
4648
/// will throw an exception if any errors occurred during the authentication.
4749
/// </returns>
4850
public static async Task<bool> TryAuthenticate(
49-
this HttpContext context, MAuthAuthenticator authenticator, bool shouldIgnoreExceptions)
51+
this HttpContext context, IMAuthAuthenticator authenticator, bool shouldIgnoreExceptions)
5052
{
5153
try
5254
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Net.Http;
2+
using System.Threading.Tasks;
3+
4+
namespace Medidata.MAuth.Core;
5+
6+
/// <summary>
7+
/// MAuth authenticator.
8+
/// </summary>
9+
public interface IMAuthAuthenticator
10+
{
11+
/// <summary>
12+
/// Authenticate http request.
13+
/// </summary>
14+
/// <param name="request">Http context converted to a http request.</param>
15+
/// <returns>
16+
/// This method returns <see langword="true"/> if it successfully authenticated the request otherwise it will return <see langword="false"/>.
17+
/// </returns>
18+
Task<bool> AuthenticateRequest(HttpRequestMessage request);
19+
}

src/Medidata.MAuth.Core/MAuthAuthenticator.cs

+43-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111

1212
namespace Medidata.MAuth.Core
1313
{
14-
internal class MAuthAuthenticator
14+
/// <summary>
15+
/// MAuth service class to process authentication.
16+
/// </summary>
17+
public class MAuthAuthenticator : IMAuthAuthenticator
1518
{
1619
private const int AllowedDriftSeconds = 300;
1720
private static readonly TimeSpan AllowedDriftTimeSpan = TimeSpan.FromSeconds(AllowedDriftSeconds);
@@ -22,8 +25,17 @@ internal class MAuthAuthenticator
2225
private readonly IDateTimeOffsetWrapper _dateTimeOffsetWrapper;
2326
private readonly Lazy<HttpClient> _lazyHttpClient;
2427

28+
/// <summary>
29+
/// MAuth application uuid.
30+
/// </summary>
2531
public Guid ApplicationUuid => _options.ApplicationUuid;
2632

33+
/// <summary>
34+
/// Create a new instance <see cref="MAuthAuthenticator"/>
35+
/// </summary>
36+
/// <param name="options">MAuth options</param>
37+
/// <param name="logger">Logger</param>
38+
/// <param name="cacheService">Cache service. (Optional)</param>
2739
public MAuthAuthenticator(MAuthOptionsBase options, ILogger logger, ICacheService cacheService = null)
2840
{
2941
if (options.ApplicationUuid == default)
@@ -41,6 +53,36 @@ public MAuthAuthenticator(MAuthOptionsBase options, ILogger logger, ICacheServic
4153
_lazyHttpClient = new Lazy<HttpClient>(() => CreateHttpClient(options));
4254
_dateTimeOffsetWrapper = options.DateTimeOffsetWrapper;
4355
}
56+
57+
/// <summary>
58+
/// Create a new instance <see cref="MAuthAuthenticator"/>
59+
/// </summary>
60+
/// <param name="options">MAuth options</param>
61+
/// <param name="logger">Logger</param>
62+
/// <param name="httpClient">Http Client</param>
63+
/// <param name="cacheService">Cache service. (Optional)</param>
64+
public MAuthAuthenticator(MAuthOptionsBase options, ILogger logger, HttpClient httpClient, ICacheService cacheService = null)
65+
{
66+
if (options.ApplicationUuid == default)
67+
throw new ArgumentException(nameof(options.ApplicationUuid));
68+
69+
if (options.MAuthServiceUrl == null)
70+
throw new ArgumentNullException(nameof(options.MAuthServiceUrl));
71+
72+
if (string.IsNullOrWhiteSpace(options.PrivateKey))
73+
throw new ArgumentNullException(nameof(options.PrivateKey));
74+
75+
_cache = cacheService ?? new MemoryCacheService(new MemoryCache(new MemoryCacheOptions()));
76+
_options = options;
77+
_logger = logger;
78+
#if NET6_0_OR_GREATER
79+
_lazyHttpClient = new Lazy<HttpClient>(httpClient);
80+
#else
81+
_lazyHttpClient = new Lazy<HttpClient>(() => httpClient);
82+
#endif
83+
84+
_dateTimeOffsetWrapper = options.DateTimeOffsetWrapper;
85+
}
4486

4587
/// <summary>
4688
/// Verifies if the <see cref="HttpRequestMessage"/> request is authenticated or not.

version.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project>
33
<PropertyGroup>
4-
<Version>5.1.7</Version>
4+
<Version>5.1.8</Version>
55
</PropertyGroup>
66
</Project>

0 commit comments

Comments
 (0)