Skip to content

Commit d0e7f88

Browse files
authored
Merge pull request #1 from myusrn/paule96refactorBranch
I refactor a lot of you and our changes
2 parents 3ddf25e + 49c7a40 commit d0e7f88

10 files changed

Lines changed: 416 additions & 206 deletions

KK.AspNetCore.EasyAuthAuthentication.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
44
VisualStudioVersion = 15.0.26124.0

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,10 @@ For example:
106106
107107
## Authors
108108

109-
* **Kirsten Kluge** - *Initial work* - [kirkone](https://github.com/kirkone)
110-
* **paule96** - *Refactoring* - [paule96](https://github.com/paule96)
111-
* **Christoph Sonntag** - *Made things even more uber* - [Compufreak345](https://github.com/Compufreak345)
109+
- **Kirsten Kluge** - _Initial work_ - [kirkone](https://github.com/kirkone)
110+
- **paule96** - _Refactoring_ - [paule96](https://github.com/paule96)
111+
- **Christoph Sonntag** - _Made things even more uber_ - [Compufreak345](https://github.com/Compufreak345)
112+
- **myusrn** - _Dropped some knowledge about making IsInRoles work_ - [myusrn](https://github.com/myusrn)
112113

113114
See also the list of [contributors](https://github.com/kirkone/KK.AspNetCore.EasyAuthAuthentication/graphs/contributors) who participated in this project.
114115

@@ -118,4 +119,4 @@ This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md
118119

119120
## Acknowledgments
120121

121-
* Inspired by this [StackOverflow post](https://stackoverflow.com/a/42402163/6526640) and this [GitHub](https://github.com/lpunderscore/azureappservice-authentication-middleware) repo
122+
- Inspired by this [StackOverflow post](https://stackoverflow.com/a/42402163/6526640) and this [GitHub](https://github.com/lpunderscore/azureappservice-authentication-middleware) repo
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace KK.AspNetCore.EasyAuthAuthentication
2+
{
3+
/// <summary>
4+
/// This class contains all header names that are possible to make an authentication.
5+
/// The source of the list can find here: https://docs.microsoft.com/en-us/azure/app-service/app-service-authentication-how-to#retrieve-tokens-in-app-code
6+
/// </summary>
7+
public static class AuthTokenHeaderNames
8+
{
9+
#region AzureAd
10+
public const string AADIdToken = "X-MS-TOKEN-AAD-ID-TOKEN";
11+
public const string AADAccessToken = "X-MS-TOKEN-AAD-ACCESS-TOKEN";
12+
public const string AADExpiresOn = "X-MS-TOKEN-AAD-EXPIRES-ON";
13+
public const string AADRefreshToken = "X-MS-TOKEN-AAD-REFRESH-TOKEN";
14+
#endregion
15+
#region Facebook
16+
public const string FacebookAccessToken = "X-MS-TOKEN-FACEBOOK-ACCESS-TOKEN";
17+
public const string FacebookExpiresOn = "X-MS-TOKEN-FACEBOOK-EXPIRES-ON";
18+
#endregion
19+
#region Google
20+
public const string GoogleIdToken = "X-MS-TOKEN-GOOGLE-ID-TOKEN";
21+
public const string GoogleAccessToken = "X-MS-TOKEN-GOOGLE-ACCESS-TOKEN";
22+
public const string GoogleExpiresOn = "X-MS-TOKEN-GOOGLE-EXPIRES-ON";
23+
public const string GoogleRefreshToken = "X-MS-TOKEN-GOOGLE-REFRESH-TOKEN";
24+
25+
#endregion
26+
#region Microsoft Account
27+
public const string MicrosoftAccessToken = "X-MS-TOKEN-MICROSOFTACCOUNT-ACCESS-TOKEN";
28+
public const string MicrosoftExpiresOn = "X-MS-TOKEN-MICROSOFTACCOUNT-EXPIRES-ON";
29+
public const string MicrosoftAuthenticationToken = "X-MS-TOKEN-MICROSOFTACCOUNT-AUTHENTICATION-TOKEN";
30+
public const string MicrosoftRefreshToken = "X-MS-TOKEN-MICROSOFTACCOUNT-REFRESH-TOKEN";
31+
#endregion
32+
#region Twitter
33+
public const string TwitterAccessToken = "X-MS-TOKEN-TWITTER-ACCESS-TOKEN";
34+
public const string TwitterAccessTokenSecret = "X-MS-TOKEN-TWITTER-ACCESS-TOKEN-SECRET";
35+
#endregion
36+
}
37+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Security.Claims;
4+
using System.Security.Principal;
5+
using Microsoft.AspNetCore.Authentication;
6+
using Newtonsoft.Json.Linq;
7+
8+
namespace KK.AspNetCore.EasyAuthAuthentication
9+
{
10+
public static class AuthenticationTicketBuilder
11+
{
12+
/// <summary>
13+
/// Build a `AuthenticationTicket` from the given payload, the principal name and the provider name
14+
/// </summary>
15+
/// <param name="claimsPayload">A array of JObjects that have a `type` and a `val` property</param>
16+
/// <param name="providerName">The provider name of the current auth provider.</param>
17+
/// <returns>A `AuthenticationTicket`</returns>
18+
public static AuthenticationTicket Build(IEnumerable<JObject> claimsPayload, string providerName)
19+
{
20+
var identity = new ClaimsIdentity(
21+
createClaims(claimsPayload),
22+
// setting ClaimsIdentity.AuthenticationType to value that Azure AD non-EasyAuth setups use
23+
AuthenticationTypesNames.Federation
24+
);
25+
26+
addScopeClaim(identity);
27+
addProviderNameClaim(identity, providerName);
28+
var genericPrincipal = new ClaimsPrincipal(identity);
29+
30+
return new AuthenticationTicket(genericPrincipal, EasyAuthAuthenticationDefaults.AuthenticationScheme);
31+
}
32+
33+
private static IEnumerable<Claim> createClaims(IEnumerable<JObject> claimsAsJson)
34+
{
35+
foreach (var claim in claimsAsJson)
36+
{
37+
var claimType = claim["typ"].ToString();
38+
switch (claimType)
39+
{
40+
case Schemas.AuthMethod:
41+
foreach (var item in claim["val"].ToString().Split(','))
42+
{
43+
yield return new Claim(ClaimTypes.Authentication, item);
44+
}
45+
break;
46+
case "roles":
47+
foreach (var item in claim["val"].ToString().Split(','))
48+
{
49+
yield return new Claim(ClaimTypes.Role, item);
50+
}
51+
break;
52+
default:
53+
yield return new Claim(claimType, claim["val"].ToString());
54+
break;
55+
}
56+
}
57+
}
58+
59+
private static void addScopeClaim(ClaimsIdentity identity)
60+
{
61+
if (!identity.Claims.Any(claim => claim.Type == "scp"))
62+
{
63+
// We are not sure if we should add this in to match what non-EasyAuth authenticated result would look like
64+
// with EasyAuth + Express based application configurations the scope claim will always be `user_impersonation`
65+
identity.AddClaim(new Claim("scp", "user_impersonation"));
66+
}
67+
}
68+
69+
private static void addProviderNameClaim(ClaimsIdentity identity, string providerName)
70+
{
71+
if (!identity.Claims.Any(claim => claim.Type == "provider_name"))
72+
{
73+
identity.AddClaim(new Claim("provider_name", providerName));
74+
}
75+
}
76+
}
77+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace KK.AspNetCore.EasyAuthAuthentication
2+
{
3+
/// <summary>
4+
/// This class contains all Authentication type names.
5+
/// Source of this is: https://docs.microsoft.com/en-us/dotnet/api/system.security.claims.authenticationtypes?view=netframework-4.7.2
6+
/// </summary>
7+
public class AuthenticationTypesNames
8+
{
9+
public const string Basic = "AuthenticationTypes.Basic";
10+
public const string Federation = "AuthenticationTypes.Federation";
11+
public const string Kerberos = "AuthenticationTypes.Kerberos";
12+
public const string Negotiate = "AuthenticationTypes.Negotiate";
13+
public const string Password = "AuthenticationTypes.Password";
14+
public const string Signature = "AuthenticationTypes.Signature";
15+
public const string Windows = "AuthenticationTypes.Windows";
16+
public const string X509 = "AuthenticationTypes.X509";
17+
}
18+
}

0 commit comments

Comments
 (0)