Skip to content

Commit 2574b00

Browse files
authored
Merge pull request #8246 from NuGet/zhhyu-Cookies-Revert
Revert "[Cookie Compliance] Revert revert and fix the functional test…
2 parents 7c14fa2 + 7e70a21 commit 2574b00

33 files changed

Lines changed: 332 additions & 699 deletions

src/NuGetGallery.Core/Cookies/CookieComplianceService.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.Threading;
7+
using System.Threading.Tasks;
8+
using System.Web;
9+
using NuGetGallery.Diagnostics;
10+
11+
namespace NuGetGallery.Cookies
12+
{
13+
/// <summary>
14+
/// Base cookie compliance service with access to some Gallery resources.
15+
/// </summary>
16+
public abstract class CookieComplianceServiceBase : ICookieComplianceService
17+
{
18+
private string _domain;
19+
private IDiagnosticsSource _diagnostics;
20+
21+
protected string Domain => _domain ?? throw new InvalidOperationException(CoreStrings.CookieComplianceServiceNotInitialized);
22+
23+
protected IDiagnosticsSource Diagnostics => _diagnostics ?? throw new InvalidOperationException(CoreStrings.CookieComplianceServiceNotInitialized);
24+
25+
public virtual Task InitializeAsync(string domain, IDiagnosticsService diagnostics, CancellationToken cancellationToken)
26+
{
27+
// Service should only be initialized once.
28+
if (_domain != null)
29+
{
30+
throw new InvalidOperationException(CoreStrings.CookieComplianceServiceAlreadyInitialized);
31+
}
32+
33+
_domain = domain;
34+
_diagnostics = diagnostics.GetSource(GetType().Name);
35+
36+
return Task.Delay(0);
37+
}
38+
39+
public abstract bool CanWriteNonEssentialCookies(HttpRequestBase request);
40+
41+
public abstract bool NeedsConsentForNonEssentialCookies(HttpRequestBase request);
42+
43+
public abstract CookieConsentMessage GetConsentMessage(HttpRequestBase request, string locale = null);
44+
45+
public abstract string GetConsentMarkup(HttpRequestBase request, string locale = null);
46+
47+
public abstract IEnumerable<string> GetConsentScripts(HttpRequestBase request, string locale = null);
48+
49+
public abstract IEnumerable<string> GetConsentStylesheets(HttpRequestBase request, string locale = null);
50+
}
51+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
namespace NuGetGallery.Cookies
5+
{
6+
public class CookieConsentMessage
7+
{
8+
public string Message { get; set; }
9+
10+
public string MoreInfoUrl { get; set; }
11+
12+
public string MoreInfoText { get; set; }
13+
14+
public string MoreInfoAriaLabel { get; set; }
15+
16+
public string[] Javascripts { get; set; }
17+
}
18+
}

src/NuGetGallery.Core/Cookies/CookieExpirationService.cs

Lines changed: 0 additions & 105 deletions
This file was deleted.
Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System.Collections.Generic;
45
using System.Web;
5-
using System.Threading.Tasks;
66

77
namespace NuGetGallery.Cookies
88
{
@@ -12,21 +12,40 @@ namespace NuGetGallery.Cookies
1212
public interface ICookieComplianceService
1313
{
1414
/// <summary>
15-
/// Determine whether it's allowed to write analytics cookies.
15+
/// Determine if consent is still needed for writing non-essential cookies.
1616
/// </summary>
17-
/// <returns>True if it's allowed.</returns>
18-
Task<bool> CanWriteAnalyticsCookiesAsync(HttpRequestBase request);
17+
/// <returns>True if consent is needed, false if consent is already provided or not required.</returns>
18+
bool NeedsConsentForNonEssentialCookies(HttpRequestBase request);
1919

2020
/// <summary>
21-
/// Determine whether it's allowed to write social media cookies.
21+
/// Determine if non-essential cookies can be written.
2222
/// </summary>
23-
/// <returns>True if it's allowed</returns>
24-
Task<bool> CanWriteSocialMediaCookiesAsync(HttpRequestBase request);
23+
/// <returns>True if non-essential cookies can be written, false otherwise.</returns>
24+
bool CanWriteNonEssentialCookies(HttpRequestBase request);
2525

2626
/// <summary>
27-
/// Determine whether it's allowed to write advertising cookies.
27+
/// Get the cookie consent banner message and resources. This API is an alternative to the default
28+
/// rendering APIs below and can be used to customize the UI. Note that the messaging must remain intact.
2829
/// </summary>
29-
/// <returns>True if it's allowed.</returns>
30-
Task<bool> CanWriteAdvertisingCookiesAsync(HttpRequestBase request);
30+
CookieConsentMessage GetConsentMessage(HttpRequestBase request, string locale = null);
31+
32+
#region Default CookieConsent rendering
33+
34+
/// <summary>
35+
/// Get the default HTML markup for the cookie consent banner.
36+
/// </summary>
37+
string GetConsentMarkup(HttpRequestBase request, string locale = null);
38+
39+
/// <summary>
40+
/// Get the default CSS links for the cookie consent banner.
41+
/// </summary>
42+
IEnumerable<string> GetConsentStylesheets(HttpRequestBase request, string locale = null);
43+
44+
/// <summary>
45+
/// Get the default Javascript links for the cookie consent banner.
46+
/// </summary>
47+
IEnumerable<string> GetConsentScripts(HttpRequestBase request, string locale = null);
48+
49+
#endregion
3150
}
32-
}
51+
}

src/NuGetGallery.Core/Cookies/ICookieExpirationService.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System.Collections.Generic;
45
using System.Web;
5-
using System.Threading.Tasks;
66

77
namespace NuGetGallery.Cookies
88
{
99
/// <summary>
1010
/// Default, no-op instance of the cookie compliance service, used when no shim is registered.
1111
/// </summary>
12-
public class NullCookieComplianceService : ICookieComplianceService
12+
public class NullCookieComplianceService: CookieComplianceServiceBase
1313
{
14-
public Task<bool> CanWriteAnalyticsCookiesAsync(HttpRequestBase request) => Task.FromResult(false);
14+
private static readonly string[] EmptyStringArray = new string[0];
1515

16-
public Task<bool> CanWriteSocialMediaCookiesAsync(HttpRequestBase request) => Task.FromResult(false);
16+
// Consent is not necessary and cookies can be written.
1717

18-
public Task<bool> CanWriteAdvertisingCookiesAsync(HttpRequestBase request) => Task.FromResult(false);
18+
public override bool NeedsConsentForNonEssentialCookies(HttpRequestBase request) => false;
19+
20+
public override bool CanWriteNonEssentialCookies(HttpRequestBase request) => true;
21+
22+
// No markdown or scripts will be included.
23+
24+
public override CookieConsentMessage GetConsentMessage(HttpRequestBase request, string locale = null) => null;
25+
26+
public override string GetConsentMarkup(HttpRequestBase request, string locale = null) => string.Empty;
27+
28+
public override IEnumerable<string> GetConsentScripts(HttpRequestBase request, string locale = null) => EmptyStringArray;
29+
30+
public override IEnumerable<string> GetConsentStylesheets(HttpRequestBase request, string locale = null) => EmptyStringArray;
1931
}
20-
}
32+
}

src/NuGetGallery.Core/NuGetGallery.Core.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,9 @@
107107
<Compile Include="Authentication\CredentialTypeInfo.cs" />
108108
<Compile Include="Authentication\MicrosoftClaims.cs" />
109109
<Compile Include="Certificates\CertificateFile.cs" />
110-
<Compile Include="Cookies\CookieComplianceService.cs" />
111-
<Compile Include="Cookies\CookieExpirationService.cs" />
110+
<Compile Include="Cookies\CookieComplianceServiceBase.cs" />
111+
<Compile Include="Cookies\CookieConsentMessage.cs" />
112112
<Compile Include="Cookies\ICookieComplianceService.cs" />
113-
<Compile Include="Cookies\ICookieExpirationService.cs" />
114113
<Compile Include="Cookies\NullCookieComplianceService.cs" />
115114
<Compile Include="CoreConstants.cs" />
116115
<Compile Include="CredentialTypes.cs" />

src/NuGetGallery.Core/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@
3131
// The build will automatically inject the following attributes:
3232
// AssemblyVersion, AssemblyFileVersion, AssemblyInformationalVersion, AssemblyMetadata (for Branch, CommitId, and BuildDateUtc)
3333

34-
[assembly: AssemblyMetadata("RepositoryUrl", "https://www.github.com/NuGet/NuGetGallery")]
34+
[assembly: AssemblyMetadata("RepositoryUrl", "https://www.github.com/NuGet/NuGetGallery")]

src/NuGetGallery.Services/ServicesConstants.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ public static class ServicesConstants
4646

4747
public const string ApiKeyHeaderName = "X-NuGet-ApiKey";
4848

49-
/// <summary>
50-
/// Parameter for passing the cookie compliance permission.
51-
/// </summary>
52-
public const string CookieComplianceCanWriteAnalyticsCookies = "CanWriteAnalyticsCookies";
53-
5449
public static class ContentNames
5550
{
5651
public static readonly string LoginDiscontinuationConfiguration = "Login-Discontinuation-Configuration";

0 commit comments

Comments
 (0)