Skip to content

Commit d5f58de

Browse files
committed
Migrate RepositorySignature
1 parent 106309a commit d5f58de

7 files changed

Lines changed: 104 additions & 9 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text.Json;
7+
using System.Text.Json.Serialization;
8+
using NuGet.Packaging.Core;
9+
10+
namespace NuGet.Protocol.Converters
11+
{
12+
internal sealed class FingerprintsStjConverter : JsonConverter<Fingerprints>
13+
{
14+
public override Fingerprints Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
15+
{
16+
if (reader.TokenType != JsonTokenType.StartObject)
17+
{
18+
throw new JsonException();
19+
}
20+
21+
var dict = new Dictionary<string, string>();
22+
while (reader.Read() && reader.TokenType == JsonTokenType.PropertyName)
23+
{
24+
string key = reader.GetString()!;
25+
reader.Read();
26+
dict[key] = reader.GetString() ?? string.Empty;
27+
}
28+
29+
return new Fingerprints(dict);
30+
}
31+
32+
public override void Write(Utf8JsonWriter writer, Fingerprints value, JsonSerializerOptions options)
33+
{
34+
writer.WriteStartObject();
35+
foreach (KeyValuePair<string, string> kvp in value)
36+
{
37+
writer.WriteString(kvp.Key, kvp.Value);
38+
}
39+
writer.WriteEndObject();
40+
}
41+
}
42+
}

src/NuGet.Core/NuGet.Protocol/Model/RepositoryCertificateInfo.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,34 @@
44
using System;
55
using Newtonsoft.Json;
66
using NuGet.Packaging.Core;
7+
using System.Text.Json.Serialization;
78

89
namespace NuGet.Protocol
910
{
1011
public class RepositoryCertificateInfo : IRepositoryCertificateInfo
1112
{
1213
[JsonProperty(PropertyName = JsonProperties.Fingerprints, Required = Required.Always)]
13-
public Fingerprints Fingerprints { get; private set; } = null!;
14+
[JsonPropertyName(JsonProperties.Fingerprints)]
15+
public Fingerprints Fingerprints { get; internal init; } = null!;
1416

1517
[JsonProperty(PropertyName = JsonProperties.Subject, Required = Required.Always)]
16-
public string Subject { get; private set; } = null!;
18+
[JsonPropertyName(JsonProperties.Subject)]
19+
public string Subject { get; internal init; } = null!;
1720

1821
[JsonProperty(PropertyName = JsonProperties.Issuer, Required = Required.Always)]
19-
public string Issuer { get; private set; } = null!;
22+
[JsonPropertyName(JsonProperties.Issuer)]
23+
public string Issuer { get; internal init; } = null!;
2024

2125
[JsonProperty(PropertyName = JsonProperties.NotBefore, Required = Required.Always)]
22-
public DateTimeOffset NotBefore { get; private set; }
26+
[JsonPropertyName(JsonProperties.NotBefore)]
27+
public DateTimeOffset NotBefore { get; internal init; }
2328

2429
[JsonProperty(PropertyName = JsonProperties.NotAfter, Required = Required.Always)]
25-
public DateTimeOffset NotAfter { get; private set; }
30+
[JsonPropertyName(JsonProperties.NotAfter)]
31+
public DateTimeOffset NotAfter { get; internal init; }
2632

2733
[JsonProperty(PropertyName = JsonProperties.ContentUrl, Required = Required.Always)]
28-
public string ContentUrl { get; private set; } = null!;
34+
[JsonPropertyName(JsonProperties.ContentUrl)]
35+
public string ContentUrl { get; internal init; } = null!;
2936
}
3037
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Text.Json.Serialization;
5+
6+
namespace NuGet.Protocol.Model
7+
{
8+
internal sealed class RepositorySignatureModel
9+
{
10+
[JsonPropertyName(JsonProperties.AllRepositorySigned)]
11+
public bool? AllRepositorySigned { get; set; }
12+
13+
[JsonPropertyName(JsonProperties.SigningCertificates)]
14+
public RepositoryCertificateInfo[]? SigningCertificates { get; set; }
15+
}
16+
}

src/NuGet.Core/NuGet.Protocol/NuGet.Protocol.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<Compile Include="$(SharedDirectory)\EncodingUtility.cs" />
3838
<Compile Include="$(SharedDirectory)\EqualityUtility.cs" />
3939
<Compile Include="$(SharedDirectory)\HashCodeCombiner.cs" />
40+
<Compile Include="$(SharedDirectory)\IsExternalInit.cs" />
4041
<Compile Include="$(SharedDirectory)\NoAllocEnumerateExtensions.cs" />
4142
<Compile Include="$(SharedDirectory)\NullableAttributes.cs" />
4243
<Compile Include="$(SharedDirectory)\SimplePool.cs" />

src/NuGet.Core/NuGet.Protocol/Providers/RepositorySignatureResourceProvider.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
using System;
77
using System.Globalization;
88
using System.Linq;
9+
using System.Text.Json;
910
using System.Threading;
1011
using System.Threading.Tasks;
1112
using NuGet.Common;
1213
using NuGet.Protocol.Core.Types;
14+
using NuGet.Protocol.Model;
15+
using NuGet.Protocol.Utility;
1316

1417
namespace NuGet.Protocol
1518
{
@@ -80,9 +83,12 @@ private async Task<RepositorySignatureResource> GetRepositorySignatureResourceAs
8083
},
8184
async httpSourceResult =>
8285
{
83-
var json = await httpSourceResult.Stream.AsJObjectAsync(token);
86+
RepositorySignatureModel model = await JsonSerializer.DeserializeAsync(
87+
httpSourceResult.Stream,
88+
JsonContext.Default.RepositorySignatureModel,
89+
token);
8490

85-
return new RepositorySignatureResource(json, source);
91+
return new RepositorySignatureResource(model, source);
8692
},
8793
log,
8894
token);

src/NuGet.Core/NuGet.Protocol/Resources/RepositorySignatureResource.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using NuGet.Packaging;
1212
using NuGet.Packaging.Core;
1313
using NuGet.Protocol.Core.Types;
14+
using NuGet.Protocol.Model;
1415

1516
namespace NuGet.Protocol
1617
{
@@ -44,6 +45,27 @@ public RepositorySignatureResource(JObject repoSignInformationContent, SourceRep
4445
Source = source.PackageSource.Source;
4546
}
4647

48+
internal RepositorySignatureResource(RepositorySignatureModel model, SourceRepository source)
49+
{
50+
AllRepositorySigned = model.AllRepositorySigned ??
51+
throw new FatalProtocolException(string.Format(CultureInfo.CurrentCulture, Strings.Log_FailedToParseRepoSignInfor, JsonProperties.AllRepositorySigned, source.PackageSource.Source));
52+
53+
RepositoryCertificateInfo[] certs = model.SigningCertificates ??
54+
throw new FatalProtocolException(string.Format(CultureInfo.CurrentCulture, Strings.Log_FailedToParseRepoSignInfor, JsonProperties.SigningCertificates, source.PackageSource.Source));
55+
56+
foreach (RepositoryCertificateInfo cert in certs)
57+
{
58+
if (!Uri.TryCreate(cert.ContentUrl, UriKind.Absolute, out Uri contentUrl)
59+
|| !string.Equals(contentUrl.Scheme, "https", StringComparison.OrdinalIgnoreCase))
60+
{
61+
throw new FatalProtocolException(Strings.RepositoryContentUrlMustBeHttps);
62+
}
63+
}
64+
65+
RepositoryCertificateInfos = certs;
66+
Source = source.PackageSource.Source;
67+
}
68+
4769
// Test only.
4870
public RepositorySignatureResource(bool allRepositorySigned, IEnumerable<IRepositoryCertificateInfo> repositoryCertInfos)
4971
{

src/NuGet.Core/NuGet.Protocol/Utility/JsonContext.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ namespace NuGet.Protocol.Utility
1212
[JsonSourceGenerationOptions(PropertyNameCaseInsensitive = true,
1313
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
1414
GenerationMode = JsonSourceGenerationMode.Metadata,
15-
Converters = [typeof(VersionRangeStjConverter)])]
15+
Converters = [typeof(VersionRangeStjConverter), typeof(FingerprintsStjConverter)])]
1616
#pragma warning restore CS3016 // Arrays as attribute arguments is not CLS-compliant
1717
[JsonSerializable(typeof(HttpFileSystemBasedFindPackageByIdResource.FlatContainerVersionList))]
1818
[JsonSerializable(typeof(IReadOnlyList<V3VulnerabilityIndexEntry>), TypeInfoPropertyName = "VulnerabilityIndex")]
1919
[JsonSerializable(typeof(CaseInsensitiveDictionary<IReadOnlyList<PackageVulnerabilityInfo>>), TypeInfoPropertyName = "VulnerabilityPage")]
20+
[JsonSerializable(typeof(RepositorySignatureModel))]
2021
internal partial class JsonContext : JsonSerializerContext
2122
{
2223
}

0 commit comments

Comments
 (0)