This commit is contained in:
@@ -7,6 +7,8 @@ using System.IdentityModel.Tokens.Jwt;
|
|||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using Auth.Services;
|
using Auth.Services;
|
||||||
using Auth.DTO;
|
using Auth.DTO;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace Auth.Controllers {
|
namespace Auth.Controllers {
|
||||||
[ApiController]
|
[ApiController]
|
||||||
@@ -15,7 +17,27 @@ namespace Auth.Controllers {
|
|||||||
|
|
||||||
public MAuthController(DatabaseService db) : base(db) { }
|
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")]
|
[HttpPost("login")]
|
||||||
public async Task<ActionResult> Authenticate([FromBody] LoginRequest request) {
|
public async Task<ActionResult> Authenticate([FromBody] LoginRequest request) {
|
||||||
try {
|
try {
|
||||||
@@ -54,6 +76,7 @@ namespace Auth.Controllers {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Use your ticket to get the JWT - Verified working
|
||||||
[HttpPost("token")]
|
[HttpPost("token")]
|
||||||
public ActionResult Token([FromBody] JWTRequest request) {
|
public ActionResult Token([FromBody] JWTRequest request) {
|
||||||
try {
|
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")]
|
[HttpPost("renew")]
|
||||||
public IActionResult Session([FromBody] JWTRenewRequest request) {
|
public IActionResult Session([FromBody] JWTRenewRequest request) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user