-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathRepositorySignatureResource.cs
More file actions
83 lines (67 loc) · 3.8 KB
/
RepositorySignatureResource.cs
File metadata and controls
83 lines (67 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// 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.
#nullable disable
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Newtonsoft.Json.Linq;
using NuGet.Packaging;
using NuGet.Packaging.Core;
using NuGet.Protocol.Core.Types;
using NuGet.Protocol.Model;
namespace NuGet.Protocol
{
public class RepositorySignatureResource : INuGetResource
{
public string Source { get; }
public bool AllRepositorySigned { get; }
public IEnumerable<IRepositoryCertificateInfo> RepositoryCertificateInfos { get; }
public RepositorySignatureResource(JObject repoSignInformationContent, SourceRepository source)
{
var allRepositorySigned = repoSignInformationContent.GetBoolean(JsonProperties.AllRepositorySigned) ??
throw new FatalProtocolException(string.Format(CultureInfo.CurrentCulture, Strings.Log_FailedToParseRepoSignInfor, JsonProperties.AllRepositorySigned, source.PackageSource.Source));
var data = repoSignInformationContent[JsonProperties.SigningCertificates] as JArray ??
throw new FatalProtocolException(string.Format(CultureInfo.CurrentCulture, Strings.Log_FailedToParseRepoSignInfor, JsonProperties.SigningCertificates, source.PackageSource.Source));
AllRepositorySigned = allRepositorySigned;
RepositoryCertificateInfos = data.OfType<JObject>().Select(p => p.FromJToken<RepositoryCertificateInfo>());
foreach (var repositoryCertificateInfo in RepositoryCertificateInfos)
{
var validUri = Uri.TryCreate(repositoryCertificateInfo.ContentUrl, UriKind.Absolute, out var repositoryContentUrl);
if (!validUri || !string.Equals(repositoryContentUrl.Scheme, "https", StringComparison.OrdinalIgnoreCase))
{
throw new FatalProtocolException(Strings.RepositoryContentUrlMustBeHttps);
}
}
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)
{
AllRepositorySigned = allRepositorySigned;
RepositoryCertificateInfos = repositoryCertInfos;
}
public void UpdateRepositorySignatureInfo()
{
RepositorySignatureInfoProvider.Instance.AddOrUpdateRepositorySignatureInfo
(Source, new RepositorySignatureInfo(AllRepositorySigned, RepositoryCertificateInfos));
}
}
}