Skip to content

Commit a9f4d73

Browse files
committed
feat: test and document access-token prefix support
1 parent 69c7b42 commit a9f4d73

File tree

5 files changed

+89
-4
lines changed

5 files changed

+89
-4
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <gtest/gtest.h>
2+
#include "fetchers.hh"
3+
#include "fetch-settings.hh"
4+
#include "json-utils.hh"
5+
#include <nlohmann/json.hpp>
6+
#include "tests/characterization.hh"
7+
8+
namespace nix::fetchers {
9+
10+
using nlohmann::json;
11+
12+
class AccessKeysTest : public ::testing::Test
13+
{
14+
protected:
15+
16+
public:
17+
void SetUp() override {
18+
experimentalFeatureSettings.experimentalFeatures.get().insert(Xp::Flakes);
19+
}
20+
void TearDown() override { }
21+
};
22+
23+
TEST_F(AccessKeysTest, singleGitHub)
24+
{
25+
fetchers::Settings fetchSettings = fetchers::Settings{};
26+
fetchSettings.accessTokens.get().insert({"github.com","token"});
27+
auto i = Input::fromURL(fetchSettings, "github:a/b");
28+
29+
auto token = i.scheme->getAccessToken(fetchSettings, "github.com", "github.com/a/b");
30+
ASSERT_EQ(token,"token");
31+
}
32+
33+
TEST_F(AccessKeysTest, repoGitHub)
34+
{
35+
fetchers::Settings fetchSettings = fetchers::Settings{};
36+
fetchSettings.accessTokens.get().insert({"github.com","token"});
37+
fetchSettings.accessTokens.get().insert({"github.com/a/b","another_token"});
38+
fetchSettings.accessTokens.get().insert({"github.com/a/c","yet_another_token"});
39+
auto i = Input::fromURL(fetchSettings, "github:a/a");
40+
41+
auto token = i.scheme->getAccessToken(fetchSettings, "github.com", "github.com/a/a");
42+
ASSERT_EQ(token,"token");
43+
44+
token = i.scheme->getAccessToken(fetchSettings, "github.com", "github.com/a/b");
45+
ASSERT_EQ(token,"another_token");
46+
47+
token = i.scheme->getAccessToken(fetchSettings, "github.com", "github.com/a/c");
48+
ASSERT_EQ(token,"yet_another_token");
49+
}
50+
51+
TEST_F(AccessKeysTest, multipleGitLab)
52+
{
53+
fetchers::Settings fetchSettings = fetchers::Settings{};
54+
fetchSettings.accessTokens.get().insert({"gitlab.com","token"});
55+
fetchSettings.accessTokens.get().insert({"gitlab.com/a/b","another_token"});
56+
auto i = Input::fromURL(fetchSettings, "gitlab:a/b");
57+
58+
auto token = i.scheme->getAccessToken(fetchSettings, "gitlab.com", "gitlab.com/a/b");
59+
ASSERT_EQ(token,"another_token");
60+
61+
token = i.scheme->getAccessToken(fetchSettings, "gitlab.com", "gitlab.com/a/c");
62+
ASSERT_EQ(token,"token");
63+
}
64+
65+
TEST_F(AccessKeysTest, multipleSourceHut)
66+
{
67+
fetchers::Settings fetchSettings = fetchers::Settings{};
68+
fetchSettings.accessTokens.get().insert({"git.sr.ht","token"});
69+
fetchSettings.accessTokens.get().insert({"git.sr.ht/~a/b","another_token"});
70+
auto i = Input::fromURL(fetchSettings, "sourcehut:a/b");
71+
72+
auto token = i.scheme->getAccessToken(fetchSettings, "git.sr.ht", "git.sr.ht/~a/b");
73+
ASSERT_EQ(token,"another_token");
74+
75+
token = i.scheme->getAccessToken(fetchSettings, "git.sr.ht", "git.sr.ht/~a/c");
76+
ASSERT_EQ(token,"token");
77+
}
78+
79+
}

src/libfetchers-tests/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ subdir('nix-meson-build-support/common')
4444

4545
sources = files(
4646
'public-key.cc',
47+
'access-tokens.cc',
4748
)
4849

4950
include_dirs = [include_directories('.')]

src/libfetchers/fetch-settings.hh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ struct Settings : public Config
2323
Access tokens are specified as a string made up of
2424
space-separated `host=token` values. The specific token
2525
used is selected by matching the `host` portion against the
26-
"host" specification of the input. The actual use of the
27-
`token` value is determined by the type of resource being
28-
accessed:
26+
"host" specification of the input. The `host` portion may
27+
contain a path element which will match against the prefix
28+
URL for the input. (eg: `github.com/org=token`). The actual use
29+
of the `token` value is determined by the type of resource
30+
being accessed:
2931
3032
* Github: the token value is the OAUTH-TOKEN string obtained
3133
as the Personal Access Token from the Github server (see

src/libfetchers/fetchers.hh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ struct InputScheme
264264

265265
virtual std::optional<std::string> isRelative(const Input & input) const
266266
{ return std::nullopt; }
267+
268+
virtual std::optional<std::string> getAccessToken(const fetchers::Settings & settings, const std::string & host, const std::string & url) const
269+
{ return {};}
267270
};
268271

269272
void registerInputScheme(std::shared_ptr<InputScheme> && fetcher);

src/libfetchers/github.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ struct GitArchiveInputScheme : InputScheme
172172
return input;
173173
}
174174

175-
std::optional<std::string> getAccessToken(const fetchers::Settings & settings, const std::string & host, const std::string & url) const
175+
std::optional<std::string> getAccessToken(const fetchers::Settings & settings, const std::string & host, const std::string & url) const override
176176
{
177177
auto tokens = settings.accessTokens.get();
178178
std::string answer;

0 commit comments

Comments
 (0)