Skip to content

Commit 822d97f

Browse files
committed
make IEasyAuthAuthentificationService less accesible.
This avoids that users calls services in the wrong way!
1 parent 51236a1 commit 822d97f

10 files changed

Lines changed: 65 additions & 31 deletions

src/KK.AspNetCore.EasyAuthAuthentication/Interfaces/IEasyAuthAuthentificationService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ public interface IEasyAuthAuthentificationService
1515
/// </summary>
1616
/// <param name="httpContext">The <see cref="HttpContext"/> of the http request.</param>
1717
/// <returns>If the return is true the service can be used for this request.</returns>
18-
bool CanHandleAuthentification(HttpContext httpContext);
18+
internal bool CanHandleAuthentification(HttpContext httpContext);
1919

2020
/// <summary>
2121
/// Try to create a <see cref="AuthenticateResult"/> out of the <see cref="HttpContext"/> from the incomming request.
2222
/// </summary>
2323
/// <param name="context">The <see cref="HttpContext"/> of the http request.</param>
2424
/// <returns>If the user can be authentificated it will returend a full <see cref="AuthenticateResult"/>. If not this will return a <see cref="AuthenticateResult.Fail(string)"/> result. (only the <see cref="LocalAuthMeService"/> can return this.</returns>
25-
AuthenticateResult AuthUser(HttpContext context);
25+
internal AuthenticateResult AuthUser(HttpContext context);
2626

2727
/// <summary>
2828
/// Try to create a <see cref="AuthenticateResult"/> out of the <see cref="HttpContext"/> from the incomming request.
2929
/// </summary>
3030
/// <param name="context">The <see cref="HttpContext"/> of the http request.</param>
3131
/// <param name="options">The <see cref="ProviderOptions"/> that can change the behavior of the <see cref="AuthUser(HttpContext, ProviderOptions)"/> method.</param>
3232
/// <returns>If the user can be authentificated it will returend a full <see cref="AuthenticateResult"/>. If not this will return a <see cref="AuthenticateResult.Fail(string)"/> result. (only the <see cref="LocalAuthMeService"/> can return this.</returns>
33-
AuthenticateResult AuthUser(HttpContext context, ProviderOptions options);
33+
internal AuthenticateResult AuthUser(HttpContext context, ProviderOptions options);
3434
}
3535
}

src/KK.AspNetCore.EasyAuthAuthentication/Services/Base/EasyAuthWithHeaderService.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public EasyAuthWithHeaderService(
4242
protected ILogger Logger { get; }
4343

4444
/// <inheritdoc/>
45-
public bool CanHandleAuthentification(HttpContext httpContext)
45+
protected bool CanHandleAuthentification(HttpContext httpContext)
4646
{
4747
var headers = httpContext.Request.Headers;
4848
var user = httpContext.User;
@@ -55,15 +55,15 @@ public bool CanHandleAuthentification(HttpContext httpContext)
5555
/// </summary>
5656
/// <param name="context">Http context of the request.</param>
5757
/// <returns>An <see cref="AuthenticateResult" />.</returns>
58-
public AuthenticateResult AuthUser(HttpContext context) => this.AuthUser(context, null);
58+
protected AuthenticateResult AuthUser(HttpContext context) => this.AuthUser(context, null);
5959

6060
/// <summary>
6161
/// build up identity from <see cref="PrincipalObjectHeader"/> header set by EasyAuth filters if user openId connect session cookie or oauth bearer token authenticated ...
6262
/// </summary>
6363
/// <param name="context">Http context of the request.</param>
6464
/// <param name="options">The <c>EasyAuthAuthenticationOptions</c> to use.</param>
6565
/// <returns>An <see cref="AuthenticateResult" />.</returns>
66-
public AuthenticateResult AuthUser(HttpContext context, ProviderOptions? options)
66+
protected AuthenticateResult AuthUser(HttpContext context, ProviderOptions? options)
6767
{
6868
this.defaultOptions.ChangeModel(options);
6969

@@ -90,5 +90,9 @@ private AuthenticationTicket BuildIdentityFromEasyAuthRequestHeaders(IHeaderDict
9090
var claims = JsonConvert.DeserializeObject<IEnumerable<AADClaimsModel>>(xMsClientPrincipal["claims"].ToString());
9191
return AuthenticationTicketBuilder.Build(claims, providerName, options);
9292
}
93+
94+
bool IEasyAuthAuthentificationService.CanHandleAuthentification(HttpContext httpContext) => this.CanHandleAuthentification(httpContext);
95+
AuthenticateResult IEasyAuthAuthentificationService.AuthUser(HttpContext context) => this.AuthUser(context);
96+
AuthenticateResult IEasyAuthAuthentificationService.AuthUser(HttpContext context, ProviderOptions options) => this.AuthUser(context, options);
9397
}
9498
}

src/KK.AspNetCore.EasyAuthAuthentication/Services/EasyAuthAzureAdService.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,23 @@ public EasyAuthAzureAdService(ILogger<EasyAuthAzureAdService> logger) : base(log
1818
this.defaultOptions = new ProviderOptions(typeof(EasyAuthAzureAdService).Name, "name", ClaimTypes.Role);
1919
}
2020

21-
public new AuthenticateResult AuthUser(HttpContext context)
21+
private new AuthenticateResult AuthUser(HttpContext context)
2222
{
2323
this.logger.LogInformation("Try authentification with azure ad account.");
2424
return base.AuthUser(context);
2525
}
2626

27-
public new AuthenticateResult AuthUser(HttpContext context, ProviderOptions options)
27+
private new AuthenticateResult AuthUser(HttpContext context, ProviderOptions options)
2828
{
2929
this.logger.LogInformation("Try authentification with azure ad account.");
3030
return base.AuthUser(context, options);
3131
}
3232

33-
public new bool CanHandleAuthentification(HttpContext httpContext) => base.CanHandleAuthentification(httpContext) && httpContext.Request.Headers[PrincipalIdpHeaderName] == "aad" && IsHeaderSet(httpContext.Request.Headers, AuthTokenHeaderNames.AADIdToken);
33+
private new bool CanHandleAuthentification(HttpContext httpContext) => base.CanHandleAuthentification(httpContext) && httpContext.Request.Headers[PrincipalIdpHeaderName] == "aad" && IsHeaderSet(httpContext.Request.Headers, AuthTokenHeaderNames.AADIdToken);
34+
35+
36+
bool IEasyAuthAuthentificationService.CanHandleAuthentification(HttpContext httpContext) => this.CanHandleAuthentification(httpContext);
37+
AuthenticateResult IEasyAuthAuthentificationService.AuthUser(HttpContext context) => this.AuthUser(context);
38+
AuthenticateResult IEasyAuthAuthentificationService.AuthUser(HttpContext context, ProviderOptions options) => this.AuthUser(context, options);
3439
}
3540
}

src/KK.AspNetCore.EasyAuthAuthentication/Services/EasyAuthFacebookService.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,22 @@ public EasyAuthFacebookService(ILogger<EasyAuthFacebookService> logger) : base(l
1616
this.defaultOptions = new ProviderOptions(typeof(EasyAuthFacebookService).Name, "name", ClaimTypes.Role);
1717
}
1818

19-
public new AuthenticateResult AuthUser(HttpContext context)
19+
private new AuthenticateResult AuthUser(HttpContext context)
2020
{
2121
this.logger.LogInformation("Try authentification with facebook account.");
2222
return base.AuthUser(context);
2323
}
2424

25-
public new AuthenticateResult AuthUser(HttpContext context, ProviderOptions options)
25+
private new AuthenticateResult AuthUser(HttpContext context, ProviderOptions options)
2626
{
2727
this.logger.LogInformation("Try authentification with facebook account.");
2828
return base.AuthUser(context, options);
2929
}
3030

31-
public new bool CanHandleAuthentification(HttpContext httpContext) => base.CanHandleAuthentification(httpContext) && httpContext.Request.Headers[PrincipalIdpHeaderName] == "facebook" && IsHeaderSet(httpContext.Request.Headers, AuthTokenHeaderNames.FacebookAccessToken);
31+
private new bool CanHandleAuthentification(HttpContext httpContext) => base.CanHandleAuthentification(httpContext) && httpContext.Request.Headers[PrincipalIdpHeaderName] == "facebook" && IsHeaderSet(httpContext.Request.Headers, AuthTokenHeaderNames.FacebookAccessToken);
32+
33+
bool IEasyAuthAuthentificationService.CanHandleAuthentification(HttpContext httpContext) => this.CanHandleAuthentification(httpContext);
34+
AuthenticateResult IEasyAuthAuthentificationService.AuthUser(HttpContext context) => this.AuthUser(context);
35+
AuthenticateResult IEasyAuthAuthentificationService.AuthUser(HttpContext context, ProviderOptions options) => this.AuthUser(context, options);
3236
}
3337
}

src/KK.AspNetCore.EasyAuthAuthentication/Services/EasyAuthForAuthorizationTokenService.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public class EasyAuthForAuthorizationTokenService : IEasyAuthAuthentificationSer
3838
public EasyAuthForAuthorizationTokenService(ILogger<EasyAuthForAuthorizationTokenService> logger) => this.logger = logger;
3939

4040
/// <inheritdoc/>
41-
public AuthenticateResult AuthUser(HttpContext context) => this.AuthUser(context, null);
41+
private AuthenticateResult AuthUser(HttpContext context) => this.AuthUser(context, null);
4242

4343
/// <inheritdoc/>
44-
public AuthenticateResult AuthUser(HttpContext context, ProviderOptions? options)
44+
private AuthenticateResult AuthUser(HttpContext context, ProviderOptions? options)
4545
{
4646
this.defaultOptions.ChangeModel(options);
4747

@@ -64,7 +64,7 @@ public AuthenticateResult AuthUser(HttpContext context, ProviderOptions? options
6464
}
6565

6666
/// <inheritdoc/>
67-
public bool CanHandleAuthentification(HttpContext httpContext) =>
67+
private bool CanHandleAuthentification(HttpContext httpContext) =>
6868
IsHeaderSet(httpContext.Request.Headers, AuthorizationHeader) &&
6969
httpContext.Request.Headers[AuthorizationHeader].FirstOrDefault().Contains(JWTIdentifier);
7070

@@ -111,5 +111,9 @@ private JObject GetTokenJson(string headerContent)
111111
);
112112
return xMsClientPrincipal;
113113
}
114+
115+
bool IEasyAuthAuthentificationService.CanHandleAuthentification(HttpContext httpContext) => this.CanHandleAuthentification(httpContext);
116+
AuthenticateResult IEasyAuthAuthentificationService.AuthUser(HttpContext context) => this.AuthUser(context);
117+
AuthenticateResult IEasyAuthAuthentificationService.AuthUser(HttpContext context, ProviderOptions options) => this.AuthUser(context, options);
114118
}
115119
}

src/KK.AspNetCore.EasyAuthAuthentication/Services/EasyAuthGoogleService.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,22 @@ public EasyAuthGoogleService(ILogger<EasyAuthGoogleService> logger) : base(logge
1717
this.defaultOptions = new ProviderOptions(typeof(EasyAuthGoogleService).Name, "name", ClaimTypes.Role);
1818
}
1919

20-
public new AuthenticateResult AuthUser(HttpContext context)
20+
private new AuthenticateResult AuthUser(HttpContext context)
2121
{
2222
this.logger.LogInformation("Try authentification with google account.");
2323
return base.AuthUser(context);
2424
}
2525

26-
public new AuthenticateResult AuthUser(HttpContext context, ProviderOptions options)
26+
private new AuthenticateResult AuthUser(HttpContext context, ProviderOptions options)
2727
{
2828
this.logger.LogInformation("Try authentification with google account.");
2929
return base.AuthUser(context, options);
3030
}
3131

32-
public new bool CanHandleAuthentification(HttpContext httpContext) => base.CanHandleAuthentification(httpContext) && httpContext.Request.Headers[PrincipalIdpHeaderName] == "google" && IsHeaderSet(httpContext.Request.Headers, AuthTokenHeaderNames.GoogleIdToken);
32+
private new bool CanHandleAuthentification(HttpContext httpContext) => base.CanHandleAuthentification(httpContext) && httpContext.Request.Headers[PrincipalIdpHeaderName] == "google" && IsHeaderSet(httpContext.Request.Headers, AuthTokenHeaderNames.GoogleIdToken);
33+
34+
bool IEasyAuthAuthentificationService.CanHandleAuthentification(HttpContext httpContext) => this.CanHandleAuthentification(httpContext);
35+
AuthenticateResult IEasyAuthAuthentificationService.AuthUser(HttpContext context) => this.AuthUser(context);
36+
AuthenticateResult IEasyAuthAuthentificationService.AuthUser(HttpContext context, ProviderOptions options) => this.AuthUser(context, options);
3337
}
3438
}

src/KK.AspNetCore.EasyAuthAuthentication/Services/EasyAuthMicrosoftService.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,22 @@ public EasyAuthMicrosoftService(ILogger<EasyAuthMicrosoftService> logger) : base
1818
this.defaultOptions = new ProviderOptions(typeof(EasyAuthMicrosoftService).Name, "name", ClaimTypes.Role);
1919
}
2020

21-
public new AuthenticateResult AuthUser(HttpContext context)
21+
private new AuthenticateResult AuthUser(HttpContext context)
2222
{
2323
this.logger.LogInformation("Try authentification with microsoft account.");
2424
return base.AuthUser(context);
2525
}
2626

27-
public new AuthenticateResult AuthUser(HttpContext context, ProviderOptions options)
27+
private new AuthenticateResult AuthUser(HttpContext context, ProviderOptions options)
2828
{
2929
this.logger.LogInformation("Try authentification with microsoft account.");
3030
return base.AuthUser(context, options);
3131
}
3232

33-
public new bool CanHandleAuthentification(HttpContext httpContext) => base.CanHandleAuthentification(httpContext) && httpContext.Request.Headers[PrincipalIdpHeaderName] == "microsoftaccount" && IsHeaderSet(httpContext.Request.Headers, AuthTokenHeaderNames.MicrosoftAccessToken);
33+
private new bool CanHandleAuthentification(HttpContext httpContext) => base.CanHandleAuthentification(httpContext) && httpContext.Request.Headers[PrincipalIdpHeaderName] == "microsoftaccount" && IsHeaderSet(httpContext.Request.Headers, AuthTokenHeaderNames.MicrosoftAccessToken);
34+
35+
bool IEasyAuthAuthentificationService.CanHandleAuthentification(HttpContext httpContext) => this.CanHandleAuthentification(httpContext);
36+
AuthenticateResult IEasyAuthAuthentificationService.AuthUser(HttpContext context) => this.AuthUser(context);
37+
AuthenticateResult IEasyAuthAuthentificationService.AuthUser(HttpContext context, ProviderOptions options) => this.AuthUser(context, options);
3438
}
3539
}

src/KK.AspNetCore.EasyAuthAuthentication/Services/EasyAuthTwitterService.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,22 @@ public EasyAuthTwitterService(ILogger<EasyAuthTwitterService> logger) : base(log
1818
this.defaultOptions = new ProviderOptions(typeof(EasyAuthTwitterService).Name, "name", ClaimTypes.Role);
1919
}
2020

21-
public new AuthenticateResult AuthUser(HttpContext context)
21+
private new AuthenticateResult AuthUser(HttpContext context)
2222
{
2323
this.logger.LogInformation("Try authentification with twitter account.");
2424
return base.AuthUser(context);
2525
}
2626

27-
public new AuthenticateResult AuthUser(HttpContext context, ProviderOptions options)
27+
private new AuthenticateResult AuthUser(HttpContext context, ProviderOptions options)
2828
{
2929
this.logger.LogInformation("Try authentification with twitter account.");
3030
return base.AuthUser(context, options);
3131
}
3232

33-
public new bool CanHandleAuthentification(HttpContext httpContext) => base.CanHandleAuthentification(httpContext) && httpContext.Request.Headers[PrincipalIdpHeaderName] == "twitter" && IsHeaderSet(httpContext.Request.Headers, AuthTokenHeaderNames.TwitterAccessToken);
33+
private new bool CanHandleAuthentification(HttpContext httpContext) => base.CanHandleAuthentification(httpContext) && httpContext.Request.Headers[PrincipalIdpHeaderName] == "twitter" && IsHeaderSet(httpContext.Request.Headers, AuthTokenHeaderNames.TwitterAccessToken);
34+
35+
bool IEasyAuthAuthentificationService.CanHandleAuthentification(HttpContext httpContext) => this.CanHandleAuthentification(httpContext);
36+
AuthenticateResult IEasyAuthAuthentificationService.AuthUser(HttpContext context) => this.AuthUser(context);
37+
AuthenticateResult IEasyAuthAuthentificationService.AuthUser(HttpContext context, ProviderOptions options) => this.AuthUser(context, options);
3438
}
3539
}

test/KK.AspNetCore.EasyAuthAuthentication.Test/EasyAuthAuthenticationHandlerTest.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ public AuthenticateResult AuthUser(HttpContext context, ProviderOptions options)
302302
}
303303

304304
public bool CanHandleAuthentification(HttpContext httpContext) => true;
305+
306+
bool IEasyAuthAuthentificationService.CanHandleAuthentification(HttpContext httpContext) => this.CanHandleAuthentification(httpContext);
307+
AuthenticateResult IEasyAuthAuthentificationService.AuthUser(HttpContext context) => this.AuthUser(context);
308+
AuthenticateResult IEasyAuthAuthentificationService.AuthUser(HttpContext context, ProviderOptions options) => this.AuthUser(context, options);
305309
}
306310
}
307311
}

0 commit comments

Comments
 (0)