Skip to content

Commit 3c1e2e5

Browse files
committed
feat(libstore/filetransfer): add username/password authentication support
Add a `UsernameAuth` struct and optional `usernameAuth` field to `FileTransferRequest` to support programmatic username/password authentication. This uses curl's `CURLOPT_USERNAME`/`CURLOPT_PASSWORD` options, which works with multiple protocols (HTTP, FTP, etc.) and is not specific to any particular authentication scheme. The primary motivation is to enable S3 authentication refactoring where AWS credentials (access key ID and secret access key) can be passed through this general-purpose mechanism, reducing the amount of S3-specific code behind `#if NIX_WITH_CURL_S3` guards.
1 parent 090f7fb commit 3c1e2e5

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/libstore/filetransfer.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,14 @@ struct curlFileTransfer : public FileTransfer
426426
curl_easy_setopt(req, CURLOPT_ERRORBUFFER, errbuf);
427427
errbuf[0] = 0;
428428

429+
// Set up username/password authentication if provided
430+
if (request.usernameAuth) {
431+
curl_easy_setopt(req, CURLOPT_USERNAME, request.usernameAuth->username.c_str());
432+
if (request.usernameAuth->password) {
433+
curl_easy_setopt(req, CURLOPT_PASSWORD, request.usernameAuth->password->c_str());
434+
}
435+
}
436+
429437
result.data.clear();
430438
result.bodySize = 0;
431439
}

src/libstore/include/nix/store/filetransfer.hh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ extern FileTransferSettings fileTransferSettings;
7777

7878
extern const unsigned int RETRY_TIME_MS_DEFAULT;
7979

80+
/**
81+
* Username and optional password for HTTP basic authentication.
82+
* These are used with curl's CURLOPT_USERNAME and CURLOPT_PASSWORD options
83+
* for various protocols including HTTP, FTP, and others.
84+
*/
85+
struct UsernameAuth
86+
{
87+
std::string username;
88+
std::optional<std::string> password;
89+
};
90+
8091
struct FileTransferRequest
8192
{
8293
ValidURL uri;
@@ -92,6 +103,11 @@ struct FileTransferRequest
92103
std::optional<std::string> data;
93104
std::string mimeType;
94105
std::function<void(std::string_view data)> dataCallback;
106+
/**
107+
* Optional username and password for HTTP basic authentication.
108+
* When provided, these credentials will be used with curl's CURLOPT_USERNAME/PASSWORD option.
109+
*/
110+
std::optional<UsernameAuth> usernameAuth;
95111

96112
FileTransferRequest(ValidURL uri)
97113
: uri(std::move(uri))

0 commit comments

Comments
 (0)