Skip to content

Commit 02eb4ec

Browse files
authored
Merge pull request #97 from damienbod/dev-improve-fido2
FIDO2 Anti-forgery tokens, .NET Core 3.1.8
2 parents e3a29cb + 531b35c commit 02eb4ec

28 files changed

Lines changed: 106 additions & 74 deletions

Changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
[Readme](https://github.com/damienbod/IdentityServer4AspNetCoreIdentityTemplate/blob/master/README.md)
44

5+
2020-09-12 5.0.4
6+
- FIDO2 Anti-forgery tokens
7+
- updated nuget packages 3.1.7
8+
- update npm packages
9+
510
2020-08-28 5.0.3
611
- updated nuget packages 3.1.7
712

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ dotnet new -i IdentityServer4AspNetCoreIdentityTemplate
5353
Locally built nupkg:
5454

5555
```
56-
dotnet new -i IdentityServer4AspNetCoreIdentityTemplate.5.0.3.nupkg
56+
dotnet new -i IdentityServer4AspNetCoreIdentityTemplate.5.0.4.nupkg
5757
```
5858

5959
Local folder:

content/IdentityServer4AspNetCoreIdentityTemplate.nuspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
33
<metadata>
44
<id>IdentityServer4AspNetCoreIdentityTemplate</id>
5-
<version>5.0.3</version>
5+
<version>5.0.4</version>
66
<title>IdentityServer4.Identity.Template</title>
77
<license type="file">LICENSE</license>
88
<description>
@@ -17,7 +17,7 @@
1717
<requireLicenseAcceptance>false</requireLicenseAcceptance>
1818
<copyright>2020 damienbod</copyright>
1919
<summary>This template provides a simle getting started for IdentityServer4 with Identity. Identity is Localized and the UI uses Bootstrap 4, Remove AllowAnonymous from the logout</summary>
20-
<releaseNotes>updated nuget packages 3.1.7</releaseNotes>
20+
<releaseNotes>FIDO2 Anti-forgery tokens, updated nuget packages 3.1.7, update npm packages</releaseNotes>
2121
<repository type="git" url="https://github.com/damienbod/IdentityServer4AspNetCoreIdentityTemplate" />
2222
<packageTypes>
2323
<packageType name="Template" />

content/StsServerIdentity/Controllers/AccountController.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public async Task<IActionResult> Login(LoginInputModel model)
9898
var requires2Fa = context?.AcrValues.Count(t => t.Contains("mfa")) >= 1;
9999

100100
var user = await _userManager.FindByNameAsync(model.Email);
101-
if(user != null && !user.TwoFactorEnabled && requires2Fa)
101+
if (user != null && !user.TwoFactorEnabled && requires2Fa)
102102
{
103103
return RedirectToAction(nameof(ErrorEnable2FA));
104104
}
@@ -161,7 +161,7 @@ public async Task<IActionResult> LoginFido2Mfa(string provider, bool rememberMe,
161161

162162
return View(new MfaModel { /*Provider = provider,*/ ReturnUrl = returnUrl, RememberMe = rememberMe });
163163
}
164-
164+
165165
[HttpGet]
166166
[AllowAnonymous]
167167
public IActionResult ErrorEnable2FA()
@@ -211,7 +211,7 @@ public async Task<IActionResult> Logout(LogoutViewModel model)
211211
await _signInManager.SignOutAsync();
212212
// await HttpContext.Authentication.SignOutAsync(idp, new AuthenticationProperties { RedirectUri = url });
213213
}
214-
catch(NotSupportedException)
214+
catch (NotSupportedException)
215215
{
216216
}
217217
}
@@ -260,7 +260,8 @@ public async Task<IActionResult> Register(RegisterViewModel model, string return
260260
ViewData["ReturnUrl"] = returnUrl;
261261
if (ModelState.IsValid)
262262
{
263-
var user = new ApplicationUser {
263+
var user = new ApplicationUser
264+
{
264265
UserName = model.Email,
265266
Email = model.Email,
266267
IsAdmin = false
@@ -320,7 +321,7 @@ public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null,
320321
}
321322

322323
var email = info.Principal.FindFirstValue(ClaimTypes.Email);
323-
324+
324325
if (!string.IsNullOrEmpty(email))
325326
{
326327
var user = await _userManager.FindByNameAsync(email);
@@ -450,9 +451,9 @@ public async Task<IActionResult> ForgotPassword(ForgotPasswordViewModel model)
450451
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
451452
var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
452453
await _emailSender.SendEmail(
453-
model.Email,
454+
model.Email,
454455
"Reset Password",
455-
$"Please reset your password by clicking here: {HtmlEncoder.Default.Encode(callbackUrl)}",
456+
$"Please reset your password by clicking here: {HtmlEncoder.Default.Encode(callbackUrl)}",
456457
"Hi Sir");
457458

458459
return View("ForgotPasswordConfirmation");
@@ -585,7 +586,7 @@ public async Task<IActionResult> VerifyCode(string provider, bool rememberMe, st
585586
return View("Error");
586587
}
587588

588-
if(string.IsNullOrEmpty(provider))
589+
if (string.IsNullOrEmpty(provider))
589590
{
590591
provider = "Authenticator";
591592
}

content/StsServerIdentity/Controllers/ConsentController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ private ConsentViewModel CreateConsentViewModel(
204204
vm.IdentityScopes = request.ValidatedResources.Resources.IdentityResources.Select(x => CreateScopeViewModel(x, vm.ScopesConsented.Contains(x.Name) || model == null)).ToArray();
205205

206206
var apiScopes = new List<ScopeViewModel>();
207-
foreach(var parsedScope in request.ValidatedResources.ParsedScopes)
207+
foreach (var parsedScope in request.ValidatedResources.ParsedScopes)
208208
{
209209
var apiScope = request.ValidatedResources.Resources.FindApiScope(parsedScope.ParsedName);
210210
if (apiScope != null)

content/StsServerIdentity/Controllers/GrantsController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private async Task<GrantsViewModel> BuildViewModelAsync()
6262
var grants = await _interaction.GetAllUserGrantsAsync();
6363

6464
var list = new List<GrantViewModel>();
65-
foreach(var grant in grants)
65+
foreach (var grant in grants)
6666
{
6767
var client = await _clients.FindClientByIdAsync(grant.ClientId);
6868
if (client != null)

content/StsServerIdentity/Extensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public static bool IsNativeClient(this AuthorizationRequest context)
2020
public static IActionResult LoadingPage(this Controller controller, string viewName, string redirectUri)
2121
{
2222
controller.HttpContext.Response.StatusCode = 200;
23-
controller.HttpContext.Response.Headers["Location"] = "";
24-
23+
controller.HttpContext.Response.Headers["Location"] = "";
24+
2525
return controller.View(viewName, new RedirectViewModel { RedirectUrl = redirectUri });
2626
}
2727
}

content/StsServerIdentity/Fido2/Fido2Storage.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace StsServerIdentity
1111
{
1212
public class Fido2Storage
1313
{
14-
private readonly ApplicationDbContext _applicationDbContext;
14+
private readonly ApplicationDbContext _applicationDbContext;
1515

1616
public Fido2Storage(ApplicationDbContext applicationDbContext)
1717
{
@@ -26,9 +26,9 @@ public async Task<List<FidoStoredCredential>> GetCredentialsByUsername(string us
2626
public async Task RemoveCredentialsByUsername(string username)
2727
{
2828
var items = await _applicationDbContext.FidoStoredCredential.Where(c => c.Username == username).ToListAsync();
29-
if(items != null)
29+
if (items != null)
3030
{
31-
foreach(var fido2Key in items)
31+
foreach (var fido2Key in items)
3232
{
3333
_applicationDbContext.FidoStoredCredential.Remove(fido2Key);
3434
};

content/StsServerIdentity/Fido2/FidoStoredCredential.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@
55

66
namespace StsServerIdentity
77
{
8-
public class FidoStoredCredential
8+
public class FidoStoredCredential
99
{
10-
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
11-
public int Id { get; set; }
12-
public string Username { get; set; }
13-
public byte[] UserId { get; set; }
14-
public byte[] PublicKey { get; set; }
15-
public byte[] UserHandle { get; set; }
16-
public uint SignatureCounter { get; set; }
17-
public string CredType { get; set; }
18-
public DateTime RegDate { get; set; }
10+
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
11+
public int Id { get; set; }
12+
public string Username { get; set; }
13+
public byte[] UserId { get; set; }
14+
public byte[] PublicKey { get; set; }
15+
public byte[] UserHandle { get; set; }
16+
public uint SignatureCounter { get; set; }
17+
public string CredType { get; set; }
18+
public DateTime RegDate { get; set; }
1919
public Guid AaGuid { get; set; }
2020

21-
[NotMapped]
21+
[NotMapped]
2222
public PublicKeyCredentialDescriptor Descriptor
2323
{
2424
get { return string.IsNullOrWhiteSpace(DescriptorJson) ? null : JsonConvert.DeserializeObject<PublicKeyCredentialDescriptor>(DescriptorJson); }
2525
set { DescriptorJson = JsonConvert.SerializeObject(value); }
2626
}
27-
public string DescriptorJson { get; set; }
27+
public string DescriptorJson { get; set; }
2828
}
2929
}

content/StsServerIdentity/Fido2/MfaFido2RegisterController.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class MfaFido2RegisterController : Controller
2929
private readonly IStringLocalizer _sharedLocalizer;
3030

3131
public MfaFido2RegisterController(
32-
Fido2Storage fido2Storage,
32+
Fido2Storage fido2Storage,
3333
UserManager<ApplicationUser> userManager,
3434
IOptions<Fido2Configuration> optionsFido2Configuration,
3535
IStringLocalizerFactory factory)
@@ -57,6 +57,7 @@ private string FormatException(Exception e)
5757
}
5858

5959
[HttpPost]
60+
[ValidateAntiForgeryToken]
6061
[Route("/mfamakeCredentialOptions")]
6162
public async Task<JsonResult> MakeCredentialOptions([FromForm] string username, [FromForm] string displayName, [FromForm] string attType, [FromForm] string authType, [FromForm] bool requireResidentKey, [FromForm] string userVerification)
6263
{
@@ -78,7 +79,7 @@ public async Task<JsonResult> MakeCredentialOptions([FromForm] string username,
7879
// 2. Get user existing keys by username
7980
var items = await _fido2Storage.GetCredentialsByUsername(identityUser.UserName);
8081
var existingKeys = new List<PublicKeyCredentialDescriptor>();
81-
foreach(var publicKeyCredentialDescriptor in items)
82+
foreach (var publicKeyCredentialDescriptor in items)
8283
{
8384
existingKeys.Add(publicKeyCredentialDescriptor.Descriptor);
8485
}
@@ -110,6 +111,7 @@ public async Task<JsonResult> MakeCredentialOptions([FromForm] string username,
110111
}
111112

112113
[HttpPost]
114+
[ValidateAntiForgeryToken]
113115
[Route("/mfamakeCredential")]
114116
public async Task<JsonResult> MakeCredential([FromBody] AuthenticatorAttestationRawResponse attestationResponse)
115117
{

0 commit comments

Comments
 (0)