Skip to content

Commit

Permalink
Switched to file scoped namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
scottbrady91 committed Mar 29, 2024
1 parent 0972cf4 commit d7ddbd7
Show file tree
Hide file tree
Showing 55 changed files with 3,094 additions and 3,149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,134 +14,133 @@
using ScottBrady.IdentityModel.Tokens.Branca;
using ScottBrady.IdentityModel.Tokens.Paseto;

namespace ScottBrady.IdentityModel.Samples.AspNetCore.Controllers
namespace ScottBrady.IdentityModel.Samples.AspNetCore.Controllers;

public class HomeController : Controller
{
public class HomeController : Controller
{
private readonly SampleOptions options;
private readonly UserManager<IdentityUser> userManager;
private readonly SampleOptions options;
private readonly UserManager<IdentityUser> userManager;

public HomeController(SampleOptions options, UserManager<IdentityUser> userManager)
{
this.options = options ?? throw new ArgumentNullException(nameof(options));
this.userManager = userManager ?? throw new ArgumentNullException(nameof(userManager));
}
public HomeController(SampleOptions options, UserManager<IdentityUser> userManager)
{
this.options = options ?? throw new ArgumentNullException(nameof(options));
this.userManager = userManager ?? throw new ArgumentNullException(nameof(userManager));
}

public IActionResult Index()
{
return View();
}

[HttpGet]
public IActionResult Branca()
{
var handler = new BrancaTokenHandler();
public IActionResult Index()
{
return View();
}

var token = handler.CreateToken(new SecurityTokenDescriptor
{
Issuer = "me",
Audience = "you",
EncryptingCredentials = options.BrancaEncryptingCredentials
});
[HttpGet]
public IActionResult Branca()
{
var handler = new BrancaTokenHandler();

var parsedToken = handler.DecryptToken(token, ((SymmetricSecurityKey) options.BrancaEncryptingCredentials.Key).Key);
var token = handler.CreateToken(new SecurityTokenDescriptor
{
Issuer = "me",
Audience = "you",
EncryptingCredentials = options.BrancaEncryptingCredentials
});

return View("Index", new TokenModel
{
Type = "Branca",
Token = token,
Payload = Encoding.UTF8.GetString(parsedToken.Payload)
});
}
var parsedToken = handler.DecryptToken(token, ((SymmetricSecurityKey) options.BrancaEncryptingCredentials.Key).Key);

[HttpGet]
public IActionResult Paseto(string version)
return View("Index", new TokenModel
{
var handler = new PasetoTokenHandler();

SigningCredentials signingCredentials;
if (version == PasetoConstants.Versions.V1)
signingCredentials = new SigningCredentials(options.PasetoV1PrivateKey, SecurityAlgorithms.RsaSsaPssSha384);
else if (version == PasetoConstants.Versions.V2)
signingCredentials = new SigningCredentials(options.EdDsaPrivateKey, ExtendedSecurityAlgorithms.EdDsa);
else
throw new NotSupportedException("Unsupported version");
Type = "Branca",
Token = token,
Payload = Encoding.UTF8.GetString(parsedToken.Payload)
});
}

[HttpGet]
public IActionResult Paseto(string version)
{
var handler = new PasetoTokenHandler();

SigningCredentials signingCredentials;
if (version == PasetoConstants.Versions.V1)
signingCredentials = new SigningCredentials(options.PasetoV1PrivateKey, SecurityAlgorithms.RsaSsaPssSha384);
else if (version == PasetoConstants.Versions.V2)
signingCredentials = new SigningCredentials(options.EdDsaPrivateKey, ExtendedSecurityAlgorithms.EdDsa);
else
throw new NotSupportedException("Unsupported version");

var descriptor = new PasetoSecurityTokenDescriptor(version, PasetoConstants.Purposes.Public)
{
Issuer = "me",
Audience = "you",
SigningCredentials = signingCredentials
};
var descriptor = new PasetoSecurityTokenDescriptor(version, PasetoConstants.Purposes.Public)
{
Issuer = "me",
Audience = "you",
SigningCredentials = signingCredentials
};

var token = handler.CreateToken(descriptor);
var payload = descriptor.ToJwtPayload(JwtDateTimeFormat.Iso);
var token = handler.CreateToken(descriptor);
var payload = descriptor.ToJwtPayload(JwtDateTimeFormat.Iso);

return View("Index", new TokenModel
{
Type = "PASETO",
Token = token,
Payload = payload
});
}
return View("Index", new TokenModel
{
Type = "PASETO",
Token = token,
Payload = payload
});
}

[HttpGet]
public IActionResult EdDsaJwt()
[HttpGet]
public IActionResult EdDsaJwt()
{
var handler = new JsonWebTokenHandler();

var descriptor = new SecurityTokenDescriptor
{
var handler = new JsonWebTokenHandler();
Issuer = "me",
Audience = "you",
SigningCredentials = new SigningCredentials(options.EdDsaPrivateKey, ExtendedSecurityAlgorithms.EdDsa)
};

var descriptor = new SecurityTokenDescriptor
{
Issuer = "me",
Audience = "you",
SigningCredentials = new SigningCredentials(options.EdDsaPrivateKey, ExtendedSecurityAlgorithms.EdDsa)
};
var token = handler.CreateToken(descriptor);
var payload = descriptor.ToJwtPayload(JwtDateTimeFormat.Iso);

var token = handler.CreateToken(descriptor);
var payload = descriptor.ToJwtPayload(JwtDateTimeFormat.Iso);
return View("Index", new TokenModel
{
Type = "EdDSA JWT",
Token = token,
Payload = payload
});
}

return View("Index", new TokenModel
{
Type = "EdDSA JWT",
Token = token,
Payload = payload
});
}
[HttpGet]
[Authorize(AuthenticationSchemes = "branca-bearer,paseto-bearer-v1,paseto-bearer-v2,eddsa")]
public IActionResult CallApi()
{
return Ok();
}

[HttpGet]
[Authorize(AuthenticationSchemes = "branca-bearer,paseto-bearer-v1,paseto-bearer-v2,eddsa")]
public IActionResult CallApi()
{
return Ok();
}
[HttpGet]
public IActionResult PasswordRules()
{
return View(new PasswordRulesModel());
}

[HttpGet]
public IActionResult PasswordRules()
{
return View(new PasswordRulesModel());
}
[HttpPost]
public async Task<IActionResult> PasswordRules(PasswordRulesModel model)
{
if (!ModelState.IsValid) return View(model);

[HttpPost]
public async Task<IActionResult> PasswordRules(PasswordRulesModel model)
var errors = new List<string>();
foreach (var validator in userManager.PasswordValidators)
{
if (!ModelState.IsValid) return View(model);

var errors = new List<string>();
foreach (var validator in userManager.PasswordValidators)
var result = await validator.ValidateAsync(userManager, new IdentityUser(), model.Password);
if (!result.Succeeded)
{
var result = await validator.ValidateAsync(userManager, new IdentityUser(), model.Password);
if (!result.Succeeded)
if (result.Errors.Any())
{
if (result.Errors.Any())
{
errors.AddRange(result.Errors.Select(x => x.Description));
}
errors.AddRange(result.Errors.Select(x => x.Description));
}
}

model.Errors = errors;
model.Message = errors.Any() ? "Password failed server-side validation" : "Password passed server-side validation";
return View(model);
}

model.Errors = errors;
model.Message = errors.Any() ? "Password failed server-side validation" : "Password passed server-side validation";
return View(model);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System.Collections.Generic;

namespace ScottBrady.IdentityModel.Samples.AspNetCore.Models
namespace ScottBrady.IdentityModel.Samples.AspNetCore.Models;

public class PasswordRulesModel
{
public class PasswordRulesModel
{
public string Message { get; set; }
public IEnumerable<string> Errors { get; set; } = new List<string>();
public string Password { get; set; }
}
public string Message { get; set; }
public IEnumerable<string> Errors { get; set; } = new List<string>();
public string Password { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace ScottBrady.IdentityModel.Samples.AspNetCore.Models
namespace ScottBrady.IdentityModel.Samples.AspNetCore.Models;

public class TokenModel
{
public class TokenModel
{
public string Type { get; set; }
public string Token { get; set; }
public string Payload { get; set; }
}
public string Type { get; set; }
public string Token { get; set; }
public string Payload { get; set; }
}
27 changes: 13 additions & 14 deletions samples/ScottBrady.IdentityModel.Samples.AspNetCore/Program.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace ScottBrady.IdentityModel.Samples.AspNetCore
namespace ScottBrady.IdentityModel.Samples.AspNetCore;

public class Program
{
public class Program
public static void Main(string[] args)
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
CreateHostBuilder(args).Build().Run();
}
}

private static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,36 @@
using ScottBrady.IdentityModel.Crypto;
using ScottBrady.IdentityModel.Tokens;

namespace ScottBrady.IdentityModel.Samples.AspNetCore
namespace ScottBrady.IdentityModel.Samples.AspNetCore;

public class SampleOptions
{
public class SampleOptions
{
private EncryptingCredentials encryptingCredentials;
private EncryptingCredentials encryptingCredentials;

public EncryptingCredentials BrancaEncryptingCredentials
public EncryptingCredentials BrancaEncryptingCredentials
{
get
{
get
if (encryptingCredentials == null)
{
if (encryptingCredentials == null)
{
var key = new byte[32];
RandomNumberGenerator.Create().GetBytes(key);
var key = new byte[32];
RandomNumberGenerator.Create().GetBytes(key);

encryptingCredentials = new EncryptingCredentials(
new SymmetricSecurityKey(key),
ExtendedSecurityAlgorithms.XChaCha20Poly1305);
}

return encryptingCredentials;
encryptingCredentials = new EncryptingCredentials(
new SymmetricSecurityKey(key),
ExtendedSecurityAlgorithms.XChaCha20Poly1305);
}

return encryptingCredentials;
}
}

public RsaSecurityKey PasetoV1PrivateKey = new RsaSecurityKey(RSA.Create());
public RsaSecurityKey PasetoV1PublicKey => new RsaSecurityKey(RSA.Create(PasetoV1PrivateKey.Rsa.ExportParameters(false)));
public RsaSecurityKey PasetoV1PrivateKey = new RsaSecurityKey(RSA.Create());
public RsaSecurityKey PasetoV1PublicKey => new RsaSecurityKey(RSA.Create(PasetoV1PrivateKey.Rsa.ExportParameters(false)));

public readonly EdDsaSecurityKey EdDsaPublicKey = new EdDsaSecurityKey(
EdDsa.Create(new EdDsaParameters(ExtendedSecurityAlgorithms.Curves.Ed25519) {X =Convert.FromBase64String("doaS7QILHBdnPULlgs1fX0MWpd1wak14r1yT6ae/b4M=")}));
public readonly EdDsaSecurityKey EdDsaPublicKey = new EdDsaSecurityKey(
EdDsa.Create(new EdDsaParameters(ExtendedSecurityAlgorithms.Curves.Ed25519) {X =Convert.FromBase64String("doaS7QILHBdnPULlgs1fX0MWpd1wak14r1yT6ae/b4M=")}));

public readonly EdDsaSecurityKey EdDsaPrivateKey= new EdDsaSecurityKey(
EdDsa.Create(new EdDsaParameters(ExtendedSecurityAlgorithms.Curves.Ed25519) {D =Convert.FromBase64String("TYXei5+8Qd2ZqKIlEuJJ3S50WYuocFTrqK+3/gHVH9B2hpLtAgscF2c9QuWCzV9fQxal3XBqTXivXJPpp79vgw==")}));
}
public readonly EdDsaSecurityKey EdDsaPrivateKey= new EdDsaSecurityKey(
EdDsa.Create(new EdDsaParameters(ExtendedSecurityAlgorithms.Curves.Ed25519) {D =Convert.FromBase64String("TYXei5+8Qd2ZqKIlEuJJ3S50WYuocFTrqK+3/gHVH9B2hpLtAgscF2c9QuWCzV9fQxal3XBqTXivXJPpp79vgw==")}));
}
Loading

0 comments on commit d7ddbd7

Please sign in to comment.