Skip to content

Commit 42760f6

Browse files
committed
add missing providers
1 parent 0f84f78 commit 42760f6

12 files changed

Lines changed: 211 additions & 19 deletions

File tree

KK.AspNetCore.EasyAuthAuthentication.code-workspace

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,9 @@
2626
"path": "./"
2727
}
2828
],
29-
"settings": {}
29+
"settings": {
30+
"cSpell.words": [
31+
"authentification"
32+
]
33+
}
3034
}

samples/KK.AspNetCore.EasyAuthAuthentication.Samples.Web/KK.AspNetCore.EasyAuthAuthentication.Samples.Web.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<!-- Set this to true if you enable server-side prerendering -->
1212
<BuildServerSideRenderer>false</BuildServerSideRenderer>
13+
<UserSecretsId>795deb04-f3bb-4d51-a58e-9d84a7b987fd</UserSecretsId>
1314
</PropertyGroup>
1415

1516
<ItemGroup>

samples/KK.AspNetCore.EasyAuthAuthentication.Samples.Web/appsettings.json

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,23 @@
1515
"Enabled": true
1616
},
1717
{
18-
"ProviderName": "EasyAuthWithHeaderService",
18+
"ProviderName": "EasyAuthAzureAdService",
19+
"Enabled": true
20+
},
21+
{
22+
"ProviderName": "EasyAuthMicrosoftService",
23+
"Enabled": true
24+
},
25+
{
26+
"ProviderName": "EasyAuthFacebookService",
27+
"Enabled": true
28+
},
29+
{
30+
"ProviderName": "EasyAuthTwitterService",
31+
"Enabled": true
32+
},
33+
{
34+
"ProviderName": "EasyAuthGoogleService",
1935
"Enabled": true
2036
}
2137
]

src/KK.AspNetCore.EasyAuthAuthentication/Models/ProviderOptions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public ProviderOptions(string providerName)
2727
public ProviderOptions(string providerName, string nameClaimType)
2828
{
2929
this.ProviderName = providerName;
30-
this.NameClaimType = nameClaimType;
30+
this.NameClaimType = nameClaimType;
3131
}
3232

3333
/// <summary>
@@ -41,20 +41,20 @@ public ProviderOptions(string providerName, string nameClaimType, string roleNam
4141
{
4242
this.ProviderName = providerName;
4343
this.NameClaimType = nameClaimType;
44-
this.RoleClaimType = roleNameClaimType;
44+
this.RoleClaimType = roleNameClaimType;
4545
}
4646

4747
/// <summary>
4848
/// The <c>ClaimType</c> for the Idendity User.
4949
/// </summary>
5050
/// <value>The Claim Type to use for the User. Default is <c>ClaimType</c> of the auth provider.</value>
51-
public string NameClaimType { get; set; } = ClaimTypes.Name;
51+
public string NameClaimType { get; set; } = string.Empty;
5252

5353
/// <summary>
5454
/// The <c>ClaimType</c> for the Idendity Role.
5555
/// </summary>
5656
/// <value>The Claim Type to use for the Roles. Default is <c>ClaimType</c> of the auth provider.</value>
57-
public string RoleClaimType { get; set; } = ClaimTypes.Role;
57+
public string RoleClaimType { get; set; } = string.Empty;
5858

5959
/// <summary>
6060
/// The provider name for this options object.
@@ -71,7 +71,7 @@ public ProviderOptions(string providerName, string nameClaimType, string roleNam
7171
/// </summary>
7272
/// <param name="options">The provider options model with the new options.</param>
7373
public void ChangeModel(ProviderOptions? options)
74-
{
74+
{
7575
if (options == null)
7676
{
7777
return;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace KK.AspNetCore.EasyAuthAuthentication.Services
2+
{
3+
using System.Security.Claims;
4+
using KK.AspNetCore.EasyAuthAuthentication.Interfaces;
5+
using KK.AspNetCore.EasyAuthAuthentication.Models;
6+
using Microsoft.AspNetCore.Authentication;
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.Extensions.Logging;
9+
10+
public class EasyAuthAzureAdService: EasyAuthWithHeaderService<EasyAuthAzureAdService>, IEasyAuthAuthentificationService
11+
{
12+
private readonly ILogger<EasyAuthAzureAdService> logger;
13+
14+
public EasyAuthAzureAdService(ILogger<EasyAuthAzureAdService> logger) : base(logger)
15+
{
16+
this.logger = logger;
17+
this.defaultOptions = new ProviderOptions(typeof(EasyAuthAzureAdService).Name, "name", ClaimTypes.Role);
18+
}
19+
20+
public new AuthenticateResult AuthUser(HttpContext context)
21+
{
22+
this.logger.LogInformation("Try authentification with azure ad account.");
23+
return base.AuthUser(context);
24+
}
25+
26+
public new AuthenticateResult AuthUser(HttpContext context, ProviderOptions options)
27+
{
28+
this.logger.LogInformation("Try authentification with azure ad account.");
29+
return base.AuthUser(context, options);
30+
}
31+
32+
public new bool CanHandleAuthentification(HttpContext httpContext) => base.CanHandleAuthentification(httpContext) && httpContext.Request.Headers[PrincipalIdpHeaderName] == "aad" && IsHeaderSet(httpContext.Request.Headers, AuthTokenHeaderNames.AADIdToken);
33+
}
34+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace KK.AspNetCore.EasyAuthAuthentication.Services
2+
{
3+
using System.Security.Claims;
4+
using KK.AspNetCore.EasyAuthAuthentication.Interfaces;
5+
using KK.AspNetCore.EasyAuthAuthentication.Models;
6+
using Microsoft.AspNetCore.Authentication;
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.Extensions.Logging;
9+
10+
public class EasyAuthFacebookService : EasyAuthWithHeaderService<EasyAuthFacebookService>, IEasyAuthAuthentificationService
11+
{
12+
private readonly ILogger<EasyAuthFacebookService> logger;
13+
public EasyAuthFacebookService(ILogger<EasyAuthFacebookService> logger) : base(logger){
14+
this.logger = logger;
15+
this.defaultOptions = new ProviderOptions(typeof(EasyAuthFacebookService).Name, "name", ClaimTypes.Role);
16+
}
17+
18+
public new AuthenticateResult AuthUser(HttpContext context)
19+
{
20+
this.logger.LogInformation("Try authentification with facebook account.");
21+
return base.AuthUser(context);
22+
}
23+
24+
public new AuthenticateResult AuthUser(HttpContext context, ProviderOptions options)
25+
{
26+
this.logger.LogInformation("Try authentification with facebook account.");
27+
return base.AuthUser(context, options);
28+
}
29+
30+
public new bool CanHandleAuthentification(HttpContext httpContext) => base.CanHandleAuthentification(httpContext) && httpContext.Request.Headers[PrincipalIdpHeaderName] == "facebook" && IsHeaderSet(httpContext.Request.Headers, AuthTokenHeaderNames.FacebookAccessToken);
31+
}
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace KK.AspNetCore.EasyAuthAuthentication.Services
2+
{
3+
using System.Security.Claims;
4+
using KK.AspNetCore.EasyAuthAuthentication.Interfaces;
5+
using KK.AspNetCore.EasyAuthAuthentication.Models;
6+
using Microsoft.AspNetCore.Authentication;
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.Extensions.Logging;
9+
10+
public class EasyAuthGoogleService : EasyAuthWithHeaderService<EasyAuthGoogleService>, IEasyAuthAuthentificationService
11+
{
12+
private readonly ILogger<EasyAuthGoogleService> logger;
13+
public EasyAuthGoogleService(ILogger<EasyAuthGoogleService> logger) : base(logger)
14+
{
15+
this.logger = logger;
16+
this.defaultOptions = new ProviderOptions(typeof(EasyAuthGoogleService).Name, "name", ClaimTypes.Role);
17+
}
18+
19+
public new AuthenticateResult AuthUser(HttpContext context)
20+
{
21+
this.logger.LogInformation("Try authentification with google account.");
22+
return base.AuthUser(context);
23+
}
24+
25+
public new AuthenticateResult AuthUser(HttpContext context, ProviderOptions options)
26+
{
27+
this.logger.LogInformation("Try authentification with google account.");
28+
return base.AuthUser(context, options);
29+
}
30+
31+
public new bool CanHandleAuthentification(HttpContext httpContext) => base.CanHandleAuthentification(httpContext) && httpContext.Request.Headers[PrincipalIdpHeaderName] == "google" && IsHeaderSet(httpContext.Request.Headers, AuthTokenHeaderNames.GoogleIdToken);
32+
}
33+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace KK.AspNetCore.EasyAuthAuthentication.Services
2+
{
3+
using System.Security.Claims;
4+
using KK.AspNetCore.EasyAuthAuthentication.Interfaces;
5+
using KK.AspNetCore.EasyAuthAuthentication.Models;
6+
using Microsoft.AspNetCore.Authentication;
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.Extensions.Logging;
9+
10+
public class EasyAuthMicrosoftService : EasyAuthWithHeaderService<EasyAuthMicrosoftService>, IEasyAuthAuthentificationService
11+
{
12+
private readonly ILogger<EasyAuthMicrosoftService> logger;
13+
14+
public EasyAuthMicrosoftService(ILogger<EasyAuthMicrosoftService> logger) : base(logger)
15+
{
16+
this.logger = logger;
17+
this.defaultOptions = new ProviderOptions(typeof(EasyAuthMicrosoftService).Name, "name", ClaimTypes.Role);
18+
}
19+
20+
public new AuthenticateResult AuthUser(HttpContext context)
21+
{
22+
this.logger.LogInformation("Try authentification with microsoft account.");
23+
return base.AuthUser(context);
24+
}
25+
26+
public new AuthenticateResult AuthUser(HttpContext context, ProviderOptions options)
27+
{
28+
this.logger.LogInformation("Try authentification with microsoft account.");
29+
return base.AuthUser(context, options);
30+
}
31+
32+
public new bool CanHandleAuthentification(HttpContext httpContext) => base.CanHandleAuthentification(httpContext) && httpContext.Request.Headers[PrincipalIdpHeaderName] == "microsoftaccount" && IsHeaderSet(httpContext.Request.Headers, AuthTokenHeaderNames.MicrosoftAccessToken);
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace KK.AspNetCore.EasyAuthAuthentication.Services
2+
{
3+
using System.Security.Claims;
4+
using KK.AspNetCore.EasyAuthAuthentication.Interfaces;
5+
using KK.AspNetCore.EasyAuthAuthentication.Models;
6+
using Microsoft.AspNetCore.Authentication;
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.Extensions.Logging;
9+
10+
public class EasyAuthTwitterService : EasyAuthWithHeaderService<EasyAuthTwitterService>, IEasyAuthAuthentificationService
11+
{
12+
private readonly ILogger<EasyAuthTwitterService> logger;
13+
14+
public EasyAuthTwitterService(ILogger<EasyAuthTwitterService> logger) : base(logger)
15+
{
16+
this.logger = logger;
17+
this.defaultOptions = new ProviderOptions(typeof(EasyAuthTwitterService).Name, "name", ClaimTypes.Role);
18+
}
19+
20+
public new AuthenticateResult AuthUser(HttpContext context)
21+
{
22+
this.logger.LogInformation("Try authentification with twitter account.");
23+
return base.AuthUser(context);
24+
}
25+
26+
public new AuthenticateResult AuthUser(HttpContext context, ProviderOptions options)
27+
{
28+
this.logger.LogInformation("Try authentification with twitter account.");
29+
return base.AuthUser(context, options);
30+
}
31+
32+
public new bool CanHandleAuthentification(HttpContext httpContext) => base.CanHandleAuthentification(httpContext) && httpContext.Request.Headers[PrincipalIdpHeaderName] == "twitter" && IsHeaderSet(httpContext.Request.Headers, AuthTokenHeaderNames.TwitterAccessToken);
33+
}
34+
}

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,39 @@ namespace KK.AspNetCore.EasyAuthAuthentication.Services
1515
/// <summary>
1616
/// A service that can be used to authentificat a user principal in the <see cref="EasyAuthAuthenticationHandler"/>.
1717
/// </summary>
18-
public class EasyAuthWithHeaderService : IEasyAuthAuthentificationService
18+
public abstract class EasyAuthWithHeaderService<T> : IEasyAuthAuthentificationService where T : IEasyAuthAuthentificationService
1919
{
2020
private const string PrincipalObjectHeader = "X-MS-CLIENT-PRINCIPAL";
21-
private const string PrincipalIdpHeaderName = "X-MS-CLIENT-PRINCIPAL-IDP";
21+
protected const string PrincipalIdpHeaderName = "X-MS-CLIENT-PRINCIPAL-IDP";
2222

2323
private static readonly Func<ClaimsPrincipal, bool> IsContextUserNotAuthenticated =
2424
user => user == null || user.Identity == null || user.Identity.IsAuthenticated == false;
2525

26-
private static readonly Func<IHeaderDictionary, string, bool> IsHeaderSet =
26+
protected static readonly Func<IHeaderDictionary, string, bool> IsHeaderSet =
2727
(headers, headerName) => !string.IsNullOrEmpty(headers[headerName].ToString());
2828

29-
private readonly ProviderOptions defaultOptions = new ProviderOptions(typeof(EasyAuthWithHeaderService).Name, ClaimTypes.Email, ClaimTypes.Role);
29+
#pragma warning disable IDE1006 // Naming Styles rule is broken
30+
protected ProviderOptions defaultOptions = new ProviderOptions(typeof(T).Name, ClaimTypes.Email, ClaimTypes.Role);
31+
#pragma warning restore IDE1006 // Naming Styles
32+
33+
3034

3135
/// <summary>
3236
/// Initializes a new instance of the <see cref="EasyAuthWithHeaderService"/> class.
3337
/// </summary>
3438
/// <param name="logger">The logger for this service.</param>
3539
public EasyAuthWithHeaderService(
36-
ILogger<EasyAuthWithHeaderService> logger) => this.Logger = logger;
40+
ILogger<EasyAuthWithHeaderService<T>> logger) => this.Logger = logger;
3741

38-
private ILogger Logger { get; }
42+
protected ILogger Logger { get; }
3943

4044
/// <inheritdoc/>
4145
public bool CanHandleAuthentification(HttpContext httpContext)
4246
{
4347
var headers = httpContext.Request.Headers;
4448
var user = httpContext.User;
45-
return IsContextUserNotAuthenticated(user) &&
46-
IsHeaderSet(headers, AuthTokenHeaderNames.AADIdToken);
49+
return IsContextUserNotAuthenticated(user);
50+
4751
}
4852

4953
/// <summary>

0 commit comments

Comments
 (0)