@@ -172,20 +172,53 @@ struct GitArchiveInputScheme : InputScheme
172172 return input;
173173 }
174174
175- std::optional<std::string> getAccessToken (const fetchers::Settings & settings, const std::string & host) const
175+ // Search for the longest possible match starting from the begining and ending at either the end or a path segment.
176+ std::optional<std::string> getAccessToken (const fetchers::Settings & settings, const std::string & host, const std::string & url) const override
176177 {
177178 auto tokens = settings.accessTokens .get ();
179+ std::string answer;
180+ size_t answer_match_len = 0 ;
181+ if (! url.empty ()) {
182+ for (auto & token : tokens) {
183+ auto first = url.find (token.first );
184+ if (
185+ first != std::string::npos
186+ && token.first .length () > answer_match_len
187+ && first == 0
188+ && url.substr (0 ,token.first .length ()) == token.first
189+ && (url.length () == token.first .length () || url[token.first .length ()] == ' /' )
190+ )
191+ {
192+ answer = token.second ;
193+ answer_match_len = token.first .length ();
194+ }
195+ }
196+ if (!answer.empty ())
197+ return answer;
198+ }
178199 if (auto token = get (tokens, host))
179200 return *token;
180201 return {};
181202 }
182203
183204 Headers makeHeadersWithAuthTokens (
184205 const fetchers::Settings & settings,
185- const std::string & host) const
206+ const std::string & host,
207+ const Input & input) const
208+ {
209+ auto owner = getStrAttr (input.attrs , " owner" );
210+ auto repo = getStrAttr (input.attrs , " repo" );
211+ auto hostAndPath = fmt ( " %s/%s/%s" , host, owner, repo);
212+ return makeHeadersWithAuthTokens (settings, host, hostAndPath);
213+ }
214+
215+ Headers makeHeadersWithAuthTokens (
216+ const fetchers::Settings & settings,
217+ const std::string & host,
218+ const std::string & hostAndPath) const
186219 {
187220 Headers headers;
188- auto accessToken = getAccessToken (settings, host);
221+ auto accessToken = getAccessToken (settings, host, hostAndPath );
189222 if (accessToken) {
190223 auto hdr = accessHeaderFromToken (*accessToken);
191224 if (hdr)
@@ -366,7 +399,7 @@ struct GitHubInputScheme : GitArchiveInputScheme
366399 : " https://%s/api/v3/repos/%s/%s/commits/%s" ,
367400 host, getOwner (input), getRepo (input), *input.getRef ());
368401
369- Headers headers = makeHeadersWithAuthTokens (*input.settings , host);
402+ Headers headers = makeHeadersWithAuthTokens (*input.settings , host, input );
370403
371404 auto json = nlohmann::json::parse (
372405 readFile (
@@ -383,7 +416,7 @@ struct GitHubInputScheme : GitArchiveInputScheme
383416 {
384417 auto host = getHost (input);
385418
386- Headers headers = makeHeadersWithAuthTokens (*input.settings , host);
419+ Headers headers = makeHeadersWithAuthTokens (*input.settings , host, input );
387420
388421 // If we have no auth headers then we default to the public archive
389422 // urls so we do not run into rate limits.
@@ -440,7 +473,7 @@ struct GitLabInputScheme : GitArchiveInputScheme
440473 auto url = fmt (" https://%s/api/v4/projects/%s%%2F%s/repository/commits?ref_name=%s" ,
441474 host, getStrAttr (input.attrs , " owner" ), getStrAttr (input.attrs , " repo" ), *input.getRef ());
442475
443- Headers headers = makeHeadersWithAuthTokens (*input.settings , host);
476+ Headers headers = makeHeadersWithAuthTokens (*input.settings , host, input );
444477
445478 auto json = nlohmann::json::parse (
446479 readFile (
@@ -470,7 +503,7 @@ struct GitLabInputScheme : GitArchiveInputScheme
470503 host, getStrAttr (input.attrs , " owner" ), getStrAttr (input.attrs , " repo" ),
471504 input.getRev ()->to_string (HashFormat::Base16, false ));
472505
473- Headers headers = makeHeadersWithAuthTokens (*input.settings , host);
506+ Headers headers = makeHeadersWithAuthTokens (*input.settings , host, input );
474507 return DownloadUrl { url, headers };
475508 }
476509
@@ -510,7 +543,7 @@ struct SourceHutInputScheme : GitArchiveInputScheme
510543 auto base_url = fmt (" https://%s/%s/%s" ,
511544 host, getStrAttr (input.attrs , " owner" ), getStrAttr (input.attrs , " repo" ));
512545
513- Headers headers = makeHeadersWithAuthTokens (*input.settings , host);
546+ Headers headers = makeHeadersWithAuthTokens (*input.settings , host, input );
514547
515548 std::string refUri;
516549 if (ref == " HEAD" ) {
@@ -557,7 +590,7 @@ struct SourceHutInputScheme : GitArchiveInputScheme
557590 host, getStrAttr (input.attrs , " owner" ), getStrAttr (input.attrs , " repo" ),
558591 input.getRev ()->to_string (HashFormat::Base16, false ));
559592
560- Headers headers = makeHeadersWithAuthTokens (*input.settings , host);
593+ Headers headers = makeHeadersWithAuthTokens (*input.settings , host, input );
561594 return DownloadUrl { url, headers };
562595 }
563596
0 commit comments