Skip to content

Commit 7fa8e6d

Browse files
Mic92edolstra
andcommitted
nix: add 'nix auth fill' command
Expose the authentication framework via a git-credential 'fill' compatible command, so credentials can be inspected and credential helpers tested. With --require it prompts interactively when no source provides the credential. Assisted-by: Claude:unspecified Co-authored-by: Eelco Dolstra <edolstra@gmail.com>
1 parent bb5276c commit 7fa8e6d

3 files changed

Lines changed: 92 additions & 0 deletions

File tree

src/nix/auth-fill.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
R""(
2+
3+
# Examples
4+
5+
* Get the credentials for a host:
6+
7+
```console
8+
# printf 'protocol=https\nhost=cache.example.org\n' | nix auth fill
9+
protocol=https
10+
host=cache.example.org
11+
username=alice
12+
password=foobar
13+
```
14+
15+
# Description
16+
17+
Read a [`git-credential`](https://git-scm.com/docs/git-credential)
18+
request from standard input, resolve it against
19+
[`auth-sources`](@docroot@/command-ref/conf-file.md#conf-auth-sources),
20+
and write the resulting fields to standard output.
21+
22+
With `--require`, prompt via `$SSH_ASKPASS` for any field no source
23+
provides.
24+
25+
)""

src/nix/auth.cc

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "nix/util/auth.hh"
2+
#include "nix/cmd/command.hh"
3+
4+
namespace nix {
5+
6+
struct CmdAuthFill : Command
7+
{
8+
bool require = false;
9+
10+
CmdAuthFill()
11+
{
12+
addFlag({
13+
.longName = "require",
14+
.description = "Prompt for credentials if no authentication source provides them.",
15+
.handler = {&require, true},
16+
});
17+
}
18+
19+
std::string description() override
20+
{
21+
return "obtain a user name and password from the configured authentication sources";
22+
}
23+
24+
std::string doc() override
25+
{
26+
return
27+
#include "auth-fill.md"
28+
;
29+
}
30+
31+
Category category() override
32+
{
33+
return catUtility;
34+
}
35+
36+
void run() override
37+
{
38+
logger->pause();
39+
auto request = auth::AuthData::parseGitAuthData(drainFD(STDIN_FILENO));
40+
if (auto authData = auth::getAuthenticator()->fill(request, require))
41+
writeFull(STDOUT_FILENO, authData->toGitAuthData());
42+
}
43+
};
44+
45+
struct CmdAuth : NixMultiCommand
46+
{
47+
CmdAuth()
48+
: NixMultiCommand("auth", RegisterCommand::getCommandsFor({"auth"}))
49+
{
50+
}
51+
52+
std::string description() override
53+
{
54+
return "authentication-related commands";
55+
}
56+
57+
Category category() override
58+
{
59+
return catUtility;
60+
}
61+
};
62+
63+
static auto rCmdAuth = registerCommand<CmdAuth>("auth");
64+
static auto rCmdAuthFill = registerCommand2<CmdAuthFill>({"auth", "fill"});
65+
66+
} // namespace nix

src/nix/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ subdir('nix-meson-build-support/generate-header')
7777
nix_sources = [ config_priv_h ] + files(
7878
'add-to-store.cc',
7979
'app.cc',
80+
'auth.cc',
8081
'build-trace.cc',
8182
'build.cc',
8283
'bundle.cc',

0 commit comments

Comments
 (0)