diff --git a/src/Server/Controllers/MAuth.cs b/src/Server/Controllers/MAuth.cs index 17f75d0..5583771 100644 --- a/src/Server/Controllers/MAuth.cs +++ b/src/Server/Controllers/MAuth.cs @@ -7,6 +7,8 @@ using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using Auth.Services; using Auth.DTO; +using System.Security.Cryptography; +using System.Text; namespace Auth.Controllers { [ApiController] @@ -15,7 +17,27 @@ namespace Auth.Controllers { public MAuthController(DatabaseService db) : base(db) { } - // Login and return a ticket to retreive your JWT + // Sends the public key to clients so they can verify sessions. + [HttpGet("publickey")] + public IActionResult PublicKey() { + try { + RSA rsa = AuthJWT.RsaPublicKey.Rsa; + rsa.ImportParameters(AuthJWT.RsaPublicKey.Parameters); + byte[] publicKey = rsa.ExportSubjectPublicKeyInfo(); + string base64 = Convert.ToBase64String(publicKey); + StringBuilder sb = new StringBuilder(); + sb.AppendLine("-----BEGIN PUBLIC KEY-----"); + for (int i = 0; i < base64.Length; i += 64) { + sb.AppendLine(base64.Substring(i, Math.Min(64, base64.Length - i))); + } + sb.AppendLine("-----END PUBLIC KEY-----"); + return Ok(sb.ToString()); + } catch (SecurityTokenException ex) { + return BadRequest("Token invalid: " + ex.Message); + } + } + + // Login and return a ticket to retreive your JWT - Verified working [HttpPost("login")] public async Task Authenticate([FromBody] LoginRequest request) { try { @@ -54,6 +76,7 @@ namespace Auth.Controllers { } } + // Use your ticket to get the JWT - Verified working [HttpPost("token")] public ActionResult Token([FromBody] JWTRequest request) { try { @@ -75,7 +98,7 @@ namespace Auth.Controllers { } } - // Renews an old JWT before it expires + // Renews an old JWT before it expires - Not Tested [HttpPost("renew")] public IActionResult Session([FromBody] JWTRenewRequest request) { try {