Skip to content

Commit 11311ae

Browse files
committed
Address CodeRabbit review feedback (round 2)
1 parent 5cc2fc3 commit 11311ae

5 files changed

Lines changed: 20 additions & 15 deletions

File tree

examples/EncryptPdf/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818

1919
var path = await CreateEncryptedPdf(destinationDirectory, options);
2020
Console.WriteLine($"Encrypted PDF created: {path}");
21-
Console.WriteLine("Open with password: reader123");
2221

2322
static async Task<string> CreateEncryptedPdf(string destinationDirectory, GotenbergSharpClientOptions options)
2423
{
2524
var handler = new HttpClientHandler();
2625
HttpMessageHandler effectiveHandler = handler;
26+
if (string.IsNullOrWhiteSpace(options.BasicAuthUsername) != string.IsNullOrWhiteSpace(options.BasicAuthPassword))
27+
throw new InvalidOperationException("Both BasicAuthUsername and BasicAuthPassword must be provided, or neither.");
2728
if (!string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword))
2829
effectiveHandler = new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler };
2930

src/Gotenberg.Sharp.Api.Client/Domain/Builders/Faceted/PdfOutputOptionsBuilder.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,12 @@ public PdfOutputOptionsBuilder SetOwnerPassword(string password)
167167
/// <returns>The builder instance for method chaining.</returns>
168168
public PdfOutputOptionsBuilder SetEncryption(string userPassword, string ownerPassword)
169169
{
170-
SetUserPassword(userPassword);
171-
SetOwnerPassword(ownerPassword);
170+
// Validate both passwords first before mutating any state
171+
var validatedUser = PdfPassword.Create(userPassword);
172+
var validatedOwner = PdfPassword.Create(ownerPassword);
173+
174+
_options.UserPassword = validatedUser;
175+
_options.OwnerPassword = validatedOwner;
172176

173177
return this;
174178
}

src/Gotenberg.Sharp.Api.Client/Domain/Requests/Facets/FacetBase.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
using System.Globalization;
1717

18+
using Gotenberg.Sharp.API.Client.Domain.ValueObjects;
19+
1820
namespace Gotenberg.Sharp.API.Client.Domain.Requests.Facets;
1921

2022
public abstract class FacetBase : IConvertToHttpContent
@@ -77,6 +79,7 @@ public virtual IEnumerable<HttpContent> ToHttpContent()
7779
PdfFormat format => format.ToFormDataValue(),
7880
LibrePdfFormats format => format.ToFormDataValue(),
7981
ConversionPdfFormats format => format.ToFormDataValue(),
82+
PdfPassword password => password.Value,
8083
List<Cookie> cookies => JsonConvert.SerializeObject(cookies),
8184
float f => f.ToString(cultureInfo),
8285
double d => d.ToString(cultureInfo),

src/Gotenberg.Sharp.Api.Client/Domain/ValueObjects/PdfPassword.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,14 @@ public static PdfPassword Create(string password)
4141
return new PdfPassword(password);
4242
}
4343

44-
public override string ToString() => Value;
44+
public override string ToString() => "****";
4545

4646
public bool Equals(PdfPassword? other) => other is not null && Value == other.Value;
4747

4848
public override bool Equals(object? obj) => Equals(obj as PdfPassword);
4949

5050
public override int GetHashCode() => Value.GetHashCode();
5151

52-
public static implicit operator string(PdfPassword password) => password.Value;
53-
5452
public static bool operator ==(PdfPassword? left, PdfPassword? right) => Equals(left, right);
5553

5654
public static bool operator !=(PdfPassword? left, PdfPassword? right) => !Equals(left, right);

test/GotenbergSharpClient.Tests/EncryptionOptionsTests.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Gotenberg.Sharp.API.Client.Domain.Settings;
55
using Gotenberg.Sharp.API.Client.Domain.ValueObjects;
66
using Gotenberg.Sharp.API.Client.Extensions;
7+
using Gotenberg.Sharp.API.Client.Infrastructure;
78
using Microsoft.Extensions.DependencyInjection;
89

910
namespace GotenbergSharpClient.Tests;
@@ -19,7 +20,6 @@ public void PdfPassword_Create_WithValidPassword_ReturnsInstance()
1920
var password = PdfPassword.Create("secret123");
2021

2122
password.Value.Should().Be("secret123");
22-
password.ToString().Should().Be("secret123");
2323
}
2424

2525
[TestCase(null)]
@@ -43,12 +43,11 @@ public void PdfPassword_Equality_WithSameValue_ReturnsTrue()
4343
}
4444

4545
[Test]
46-
public void PdfPassword_ImplicitConversion_ToStringReturnsValue()
46+
public void PdfPassword_ToString_ReturnsRedactedValue()
4747
{
48-
PdfPassword password = PdfPassword.Create("secret");
49-
string result = password;
48+
var password = PdfPassword.Create("secret");
5049

51-
result.Should().Be("secret");
50+
password.ToString().Should().Be("****");
5251
}
5352

5453
#endregion
@@ -115,7 +114,7 @@ public async Task UserPassword_SerializesToCorrectHttpContent()
115114

116115
var httpContents = options.ToHttpContent().ToList();
117116
var content = httpContents.FirstOrDefault(c =>
118-
c.Headers.ContentDisposition?.Name == "userPassword");
117+
c.Headers.ContentDisposition?.Name == Constants.Gotenberg.PdfOutput.UserPassword);
119118

120119
content.Should().NotBeNull();
121120
(await content!.ReadAsStringAsync()).Should().Be("openme");
@@ -131,7 +130,7 @@ public async Task OwnerPassword_SerializesToCorrectHttpContent()
131130

132131
var httpContents = options.ToHttpContent().ToList();
133132
var content = httpContents.FirstOrDefault(c =>
134-
c.Headers.ContentDisposition?.Name == "ownerPassword");
133+
c.Headers.ContentDisposition?.Name == Constants.Gotenberg.PdfOutput.OwnerPassword);
135134

136135
content.Should().NotBeNull();
137136
(await content!.ReadAsStringAsync()).Should().Be("editme");
@@ -145,9 +144,9 @@ public void NullPasswords_NotIncludedInHttpContent()
145144
var httpContents = options.ToHttpContent().ToList();
146145

147146
httpContents.FirstOrDefault(c =>
148-
c.Headers.ContentDisposition?.Name == "userPassword").Should().BeNull();
147+
c.Headers.ContentDisposition?.Name == Constants.Gotenberg.PdfOutput.UserPassword).Should().BeNull();
149148
httpContents.FirstOrDefault(c =>
150-
c.Headers.ContentDisposition?.Name == "ownerPassword").Should().BeNull();
149+
c.Headers.ContentDisposition?.Name == Constants.Gotenberg.PdfOutput.OwnerPassword).Should().BeNull();
151150
}
152151

153152
#endregion

0 commit comments

Comments
 (0)