|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
4 | | -using System; |
5 | | -using System.IO; |
6 | | -using Microsoft.AspNetCore.Certificates.Generation; |
| 4 | +using System.Reflection; |
| 5 | +using System.Text.Json; |
7 | 6 |
|
8 | 7 | namespace Templates.Test.Helpers; |
9 | 8 |
|
10 | | -public readonly struct DevelopmentCertificate |
| 9 | +public readonly struct DevelopmentCertificate(string certificatePath, string certificatePassword, string certificateThumbprint) |
11 | 10 | { |
12 | | - public DevelopmentCertificate(string certificatePath, string certificatePassword, string certificateThumbprint) |
| 11 | + public readonly string CertificatePath { get; } = certificatePath; |
| 12 | + public readonly string CertificatePassword { get; } = certificatePassword; |
| 13 | + public readonly string CertificateThumbprint { get; } = certificateThumbprint; |
| 14 | + |
| 15 | + public static DevelopmentCertificate Get(Assembly assembly) |
13 | 16 | { |
14 | | - CertificatePath = certificatePath; |
15 | | - CertificatePassword = certificatePassword; |
16 | | - CertificateThumbprint = certificateThumbprint; |
17 | | - } |
| 17 | + string[] locations = [ |
| 18 | + Path.Combine(AppContext.BaseDirectory, "aspnetcore-https.json"), |
| 19 | + Path.Combine(Environment.CurrentDirectory, "aspnetcore-https.json"), |
| 20 | + Path.Combine(AppContext.BaseDirectory, "aspnetcore-https.json"), |
| 21 | + ]; |
18 | 22 |
|
19 | | - public readonly string CertificatePath { get; } |
20 | | - public readonly string CertificatePassword { get; } |
21 | | - public readonly string CertificateThumbprint { get; } |
| 23 | + var json = TryGetExistingFile(locations) |
| 24 | + ?? throw new InvalidOperationException($"The aspnetcore-https.json file does not exist. Searched locations: {Environment.NewLine}{string.Join(Environment.NewLine, locations)}"); |
22 | 25 |
|
23 | | - public static DevelopmentCertificate Create(string workingDirectory) |
24 | | - { |
25 | | - var certificatePath = Path.Combine(workingDirectory, $"{Guid.NewGuid()}.pfx"); |
26 | | - var certificatePassword = Guid.NewGuid().ToString(); |
27 | | - var certificateThumbprint = EnsureDevelopmentCertificates(certificatePath, certificatePassword); |
| 26 | + using var file = File.OpenRead(json); |
| 27 | + var certificateAttributes = JsonSerializer.Deserialize<CertificateAttributes>(file) ?? |
| 28 | + throw new InvalidOperationException($"The aspnetcore-https.json file does not contain valid JSON."); |
| 29 | + |
| 30 | + var path = Path.ChangeExtension(json, ".pfx"); |
28 | 31 |
|
29 | | - return new DevelopmentCertificate(certificatePath, certificatePassword, certificateThumbprint); |
| 32 | + if (!File.Exists(path)) |
| 33 | + { |
| 34 | + throw new InvalidOperationException($"The certificate file does not exist. Expected at: '{path}'."); |
| 35 | + } |
| 36 | + |
| 37 | + var password = certificateAttributes.Password; |
| 38 | + var thumbprint = certificateAttributes.Thumbprint; |
| 39 | + |
| 40 | + return new DevelopmentCertificate(path, password, thumbprint); |
30 | 41 | } |
31 | 42 |
|
32 | | - private static string EnsureDevelopmentCertificates(string certificatePath, string certificatePassword) |
| 43 | + private static string TryGetExistingFile(string[] locations) |
33 | 44 | { |
34 | | - var now = DateTimeOffset.Now; |
35 | | - var manager = CertificateManager.Instance; |
36 | | - var certificate = manager.CreateAspNetCoreHttpsDevelopmentCertificate(now, now.AddYears(1)); |
37 | | - var certificateThumbprint = certificate.Thumbprint; |
38 | | - manager.ExportCertificate(certificate, path: certificatePath, includePrivateKey: true, certificatePassword, CertificateKeyExportFormat.Pfx); |
| 45 | + foreach (var location in locations) |
| 46 | + { |
| 47 | + if (File.Exists(location)) |
| 48 | + { |
| 49 | + return location; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return null; |
| 54 | + } |
39 | 55 |
|
40 | | - return certificateThumbprint; |
| 56 | + private sealed class CertificateAttributes |
| 57 | + { |
| 58 | + public string Password { get; set; } |
| 59 | + public string Thumbprint { get; set; } |
41 | 60 | } |
42 | 61 | } |
0 commit comments