|
4 | 4 |
|
5 | 5 | using Microsoft.AspNetCore.Mvc;
|
6 | 6 | using DevProxy.Jwt;
|
| 7 | +using System.Security.Cryptography.X509Certificates; |
| 8 | +using System.ComponentModel.DataAnnotations; |
7 | 9 |
|
8 | 10 | namespace DevProxy.ApiControllers;
|
9 | 11 |
|
@@ -65,4 +67,33 @@ public IActionResult CreateJwtToken([FromBody] JwtOptions jwtOptions)
|
65 | 67 |
|
66 | 68 | return Ok(new JwtInfo { Token = token });
|
67 | 69 | }
|
| 70 | + |
| 71 | + [HttpGet("rootCertificate")] |
| 72 | + public IActionResult GetRootCertificate([FromQuery][Required] string format) |
| 73 | + { |
| 74 | + if (!format.Equals("crt", StringComparison.OrdinalIgnoreCase)) |
| 75 | + { |
| 76 | + ModelState.AddModelError("format", "Invalid format. Supported format is 'crt'."); |
| 77 | + return ValidationProblem(ModelState); |
| 78 | + } |
| 79 | + |
| 80 | + var certificate = ProxyEngine.ProxyServer.CertificateManager.RootCertificate; |
| 81 | + if (certificate == null) |
| 82 | + { |
| 83 | + var problemDetails = new ProblemDetails |
| 84 | + { |
| 85 | + Title = "Certificate Not Found", |
| 86 | + Detail = "No root certificate found.", |
| 87 | + Status = StatusCodes.Status404NotFound |
| 88 | + }; |
| 89 | + return NotFound(problemDetails); |
| 90 | + } |
| 91 | + |
| 92 | + var certBytes = certificate.Export(X509ContentType.Cert); |
| 93 | + var base64Cert = Convert.ToBase64String(certBytes, Base64FormattingOptions.InsertLineBreaks); |
| 94 | + var pem = "-----BEGIN CERTIFICATE-----\n" + base64Cert + "\n-----END CERTIFICATE-----"; |
| 95 | + var pemBytes = System.Text.Encoding.ASCII.GetBytes(pem); |
| 96 | + |
| 97 | + return File(pemBytes, "application/x-x509-ca-cert", "devProxy.pem"); |
| 98 | + } |
68 | 99 | }
|
0 commit comments