Skip to content

Commit f4875f2

Browse files
Jabenclaude
andcommitted
Add validation and null-forgiving operators to BasicAuth handler registration
- Added null-forgiving operators (!) to BasicAuthUsername and BasicAuthPassword - Added validation to ensure both username and password are provided together - Throws InvalidOperationException if only one credential is configured - Prevents silently falling back to PassThroughHandler on misconfiguration - Uses XOR operator (^) to detect partial configuration This ensures users get clear error messages when BasicAuth is misconfigured instead of silently failing to authenticate. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 54cda1d commit f4875f2

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

src/Gotenberg.Sharp.Api.Client/Extensions/TypedClientServiceCollectionExtensions.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,20 @@ public static IHttpClientBuilder AddGotenbergSharpClient(
8787
{
8888
var ops = GetOptions(sp);
8989

90+
var hasUsername = !string.IsNullOrWhiteSpace(ops.BasicAuthUsername);
91+
var hasPassword = !string.IsNullOrWhiteSpace(ops.BasicAuthPassword);
92+
93+
// Validate that both username and password are provided together
94+
if (hasUsername ^ hasPassword)
95+
{
96+
throw new InvalidOperationException(
97+
"BasicAuth configuration is incomplete. Both BasicAuthUsername and BasicAuthPassword must be set, or neither should be set.");
98+
}
99+
90100
// Add basic auth handler if credentials are configured
91-
if (!string.IsNullOrWhiteSpace(ops.BasicAuthUsername) &&
92-
!string.IsNullOrWhiteSpace(ops.BasicAuthPassword))
101+
if (hasUsername && hasPassword)
93102
{
94-
return new BasicAuthHandler(ops.BasicAuthUsername, ops.BasicAuthPassword);
103+
return new BasicAuthHandler(ops.BasicAuthUsername!, ops.BasicAuthPassword!);
95104
}
96105

97106
// Return a pass-through handler if no auth is configured

0 commit comments

Comments
 (0)