Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using NuGet.Packaging.Core;

namespace NuGet.Protocol.Converters
{
internal sealed class FingerprintsStjConverter : JsonConverter<Fingerprints>
{
public override Fingerprints Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartObject)
{
throw new JsonException();
}

var dict = new Dictionary<string, string>();
while (reader.Read() && reader.TokenType == JsonTokenType.PropertyName)
{
string key = reader.GetString()!;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a comment every place the bang operator is used. It means that there is certainty that there is data for property and that allowing for the null case is an imposition.

reader.Read();
dict[key] = reader.GetString() ?? string.Empty;
}

return new Fingerprints(dict);
}

public override void Write(Utf8JsonWriter writer, Fingerprints value, JsonSerializerOptions options)
{
writer.WriteStartObject();
foreach (KeyValuePair<string, string> kvp in value)
{
writer.WriteString(kvp.Key, kvp.Value);
}
writer.WriteEndObject();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,53 @@
using System;
using Newtonsoft.Json;
using NuGet.Packaging.Core;
using System.Text.Json.Serialization;

namespace NuGet.Protocol
{
public class RepositoryCertificateInfo : IRepositoryCertificateInfo
{
[JsonProperty(PropertyName = JsonProperties.Fingerprints, Required = Required.Always)]
[JsonPropertyName(JsonProperties.Fingerprints)]
public Fingerprints Fingerprints { get; private set; } = null!;

[JsonProperty(PropertyName = JsonProperties.Subject, Required = Required.Always)]
[JsonPropertyName(JsonProperties.Subject)]
public string Subject { get; private set; } = null!;

[JsonProperty(PropertyName = JsonProperties.Issuer, Required = Required.Always)]
[JsonPropertyName(JsonProperties.Issuer)]
public string Issuer { get; private set; } = null!;

[JsonProperty(PropertyName = JsonProperties.NotBefore, Required = Required.Always)]
[JsonPropertyName(JsonProperties.NotBefore)]
public DateTimeOffset NotBefore { get; private set; }

[JsonProperty(PropertyName = JsonProperties.NotAfter, Required = Required.Always)]
[JsonPropertyName(JsonProperties.NotAfter)]
public DateTimeOffset NotAfter { get; private set; }

[JsonProperty(PropertyName = JsonProperties.ContentUrl, Required = Required.Always)]
[JsonPropertyName(JsonProperties.ContentUrl)]
public string ContentUrl { get; private set; } = null!;

public RepositoryCertificateInfo() { }

[System.Text.Json.Serialization.JsonConstructor]
internal RepositoryCertificateInfo(
Fingerprints fingerprints,
string subject,
string issuer,
DateTimeOffset notBefore,
DateTimeOffset notAfter,
string contentUrl)
{
Fingerprints = fingerprints;
Subject = subject;
Issuer = issuer;
NotBefore = notBefore;
NotAfter = notAfter;
ContentUrl = contentUrl;
}
}
}
16 changes: 16 additions & 0 deletions src/NuGet.Core/NuGet.Protocol/Model/RepositorySignatureModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Text.Json.Serialization;

namespace NuGet.Protocol.Model
{
internal sealed class RepositorySignatureModel
{
[JsonPropertyName(JsonProperties.AllRepositorySigned)]
public bool? AllRepositorySigned { get; set; }

[JsonPropertyName(JsonProperties.SigningCertificates)]
public RepositoryCertificateInfo[]? SigningCertificates { get; set; }
}
}
1 change: 1 addition & 0 deletions src/NuGet.Core/NuGet.Protocol/NuGet.Protocol.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<Compile Include="$(SharedDirectory)\EncodingUtility.cs" />
<Compile Include="$(SharedDirectory)\EqualityUtility.cs" />
<Compile Include="$(SharedDirectory)\HashCodeCombiner.cs" />
<Compile Include="$(SharedDirectory)\IsExternalInit.cs" />
<Compile Include="$(SharedDirectory)\NoAllocEnumerateExtensions.cs" />
<Compile Include="$(SharedDirectory)\NullableAttributes.cs" />
<Compile Include="$(SharedDirectory)\SimplePool.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
using System;
using System.Globalization;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Common;
using NuGet.Protocol.Core.Types;
using NuGet.Protocol.Model;
using NuGet.Protocol.Utility;

namespace NuGet.Protocol
{
Expand Down Expand Up @@ -80,9 +83,12 @@ private async Task<RepositorySignatureResource> GetRepositorySignatureResourceAs
},
async httpSourceResult =>
{
var json = await httpSourceResult.Stream.AsJObjectAsync(token);
RepositorySignatureModel model = await JsonSerializer.DeserializeAsync(
httpSourceResult.Stream,
JsonContext.Default.RepositorySignatureModel,
token);

return new RepositorySignatureResource(json, source);
return new RepositorySignatureResource(model, source);
},
log,
token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using NuGet.Packaging;
using NuGet.Packaging.Core;
using NuGet.Protocol.Core.Types;
using NuGet.Protocol.Model;

namespace NuGet.Protocol
{
Expand Down Expand Up @@ -44,6 +45,27 @@ public RepositorySignatureResource(JObject repoSignInformationContent, SourceRep
Source = source.PackageSource.Source;
}

internal RepositorySignatureResource(RepositorySignatureModel model, SourceRepository source)
{
AllRepositorySigned = model.AllRepositorySigned ??
throw new FatalProtocolException(string.Format(CultureInfo.CurrentCulture, Strings.Log_FailedToParseRepoSignInfor, JsonProperties.AllRepositorySigned, source.PackageSource.Source));

RepositoryCertificateInfo[] certs = model.SigningCertificates ??
throw new FatalProtocolException(string.Format(CultureInfo.CurrentCulture, Strings.Log_FailedToParseRepoSignInfor, JsonProperties.SigningCertificates, source.PackageSource.Source));

foreach (RepositoryCertificateInfo cert in certs)
{
if (!Uri.TryCreate(cert.ContentUrl, UriKind.Absolute, out Uri contentUrl)
|| !string.Equals(contentUrl.Scheme, "https", StringComparison.OrdinalIgnoreCase))
{
throw new FatalProtocolException(Strings.RepositoryContentUrlMustBeHttps);
}
}

RepositoryCertificateInfos = certs;
Source = source.PackageSource.Source;
}

// Test only.
public RepositorySignatureResource(bool allRepositorySigned, IEnumerable<IRepositoryCertificateInfo> repositoryCertInfos)
{
Expand Down
3 changes: 2 additions & 1 deletion src/NuGet.Core/NuGet.Protocol/Utility/JsonContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ namespace NuGet.Protocol.Utility
[JsonSourceGenerationOptions(PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
GenerationMode = JsonSourceGenerationMode.Metadata,
Converters = [typeof(VersionRangeStjConverter)])]
Converters = [typeof(VersionRangeStjConverter), typeof(FingerprintsStjConverter)])]
#pragma warning restore CS3016 // Arrays as attribute arguments is not CLS-compliant
[JsonSerializable(typeof(HttpFileSystemBasedFindPackageByIdResource.FlatContainerVersionList))]
[JsonSerializable(typeof(IReadOnlyList<V3VulnerabilityIndexEntry>), TypeInfoPropertyName = "VulnerabilityIndex")]
[JsonSerializable(typeof(CaseInsensitiveDictionary<IReadOnlyList<PackageVulnerabilityInfo>>), TypeInfoPropertyName = "VulnerabilityPage")]
[JsonSerializable(typeof(RepositorySignatureModel))]
internal partial class JsonContext : JsonSerializerContext
{
}
Expand Down