Skip to content

Commit 6017411

Browse files
committed
added support for all authentication methods of composer
resolves #9093
1 parent 987b17f commit 6017411

3 files changed

Lines changed: 137 additions & 31 deletions

File tree

composer/helpers/v2/src/UpdateChecker.php

Lines changed: 69 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,59 @@ public static function getLatestResolvableVersion(array $args): ?string
1717
[$workingDirectory, $dependencyName, $gitCredentials, $registryCredentials] = $args;
1818

1919
$httpBasicCredentials = [];
20+
$bearerCredentials = [];
21+
$githubOauthCredentials = [];
22+
$gitlabOauthCredentials = [];
23+
$gitlabTokenCredentials = [];
24+
$bitbucketOauthCredentials = [];
2025

2126
foreach ($gitCredentials as $credentials) {
22-
$httpBasicCredentials[$credentials['host']] = [
23-
'username' => $credentials['username'],
24-
'password' => $credentials['password'],
25-
];
27+
if (isset($credentials['host']) && isset($credentials['username']) && isset($credentials['password'])) {
28+
$httpBasicCredentials[$credentials['host']] = [
29+
'username' => $credentials['username'],
30+
'password' => $credentials['password'],
31+
];
32+
}
2633
}
2734

2835
foreach ($registryCredentials as $credentials) {
29-
$httpBasicCredentials[$credentials['registry']] = [
30-
'username' => $credentials['username'],
31-
'password' => $credentials['password'],
32-
];
36+
$host = $credentials['registry'] ?? null;
37+
if (!$host) {
38+
continue;
39+
}
40+
41+
// http-basic
42+
if (isset($credentials['username']) && isset($credentials['password'])) {
43+
$httpBasicCredentials[$host] = [
44+
'username' => $credentials['username'],
45+
'password' => $credentials['password'],
46+
];
47+
}
48+
49+
$authType = $credentials['auth_type'] ?? null;
50+
// bearer
51+
if ($authType === 'bearer' && isset($credentials['token'])) {
52+
$bearerCredentials[$host] = $credentials['token'];
53+
}
54+
// github-oauth
55+
if ($authType === 'github-oauth' && isset($credentials['token'])) {
56+
$githubOauthCredentials[$host] = $credentials['token'];
57+
}
58+
// gitlab-oauth
59+
if ($authType === 'gitlab-oauth' && isset($credentials['token'])) {
60+
$gitlabOauthCredentials[$host] = $credentials['token'];
61+
}
62+
// gitlab-token
63+
if ($authType === 'gitlab-token' && isset($credentials['token'])) {
64+
$gitlabTokenCredentials[$host] = $credentials['token'];
65+
}
66+
// bitbucket-oauth
67+
if ($authType === 'bitbucket-oauth' && (isset($credentials['key']) || isset($credentials['consumer-key']) || isset($credentials['username'])) && (isset($credentials['secret']) || isset($credentials['consumer-secret']) || isset($credentials['password']))) {
68+
$bitbucketOauthCredentials[$host] = [
69+
'consumer-key' => $credentials['key'] ?? $credentials['consumer-key'] ?? $credentials['username'] ?? '',
70+
'consumer-secret' => $credentials['secret'] ?? $credentials['consumer-secret'] ?? $credentials['password'] ?? '',
71+
];
72+
}
3373
}
3474

3575
$io = new ExceptionIO();
@@ -38,13 +78,28 @@ public static function getLatestResolvableVersion(array $args): ?string
3878

3979
$config = $composer->getConfig();
4080

41-
if (0 < count($httpBasicCredentials)) {
42-
$config->merge([
43-
'config' => [
44-
'http-basic' => $httpBasicCredentials,
45-
],
46-
]);
81+
$configToMerge = ['config' => []];
82+
if (!empty($httpBasicCredentials)) {
83+
$configToMerge['config']['http-basic'] = $httpBasicCredentials;
84+
}
85+
if (!empty($bearerCredentials)) {
86+
$configToMerge['config']['bearer'] = $bearerCredentials;
87+
}
88+
if (!empty($githubOauthCredentials)) {
89+
$configToMerge['config']['github-oauth'] = $githubOauthCredentials;
90+
}
91+
if (!empty($gitlabOauthCredentials)) {
92+
$configToMerge['config']['gitlab-oauth'] = $gitlabOauthCredentials;
93+
}
94+
if (!empty($gitlabTokenCredentials)) {
95+
$configToMerge['config']['gitlab-token'] = $gitlabTokenCredentials;
96+
}
97+
if (!empty($bitbucketOauthCredentials)) {
98+
$configToMerge['config']['bitbucket-oauth'] = $bitbucketOauthCredentials;
99+
}
47100

101+
if (!empty($configToMerge['config'])) {
102+
$config->merge($configToMerge);
48103
$io->loadConfiguration($config);
49104
}
50105

composer/helpers/v2/src/Updater.php

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,81 @@ public static function update(array $args): array
3333
$composer = Factory::create($io);
3434
$config = $composer->getConfig();
3535
$httpBasicCredentials = [];
36+
$bearerCredentials = [];
37+
$githubOauthCredentials = [];
38+
$gitlabOauthCredentials = [];
39+
$gitlabTokenCredentials = [];
40+
$bitbucketOauthCredentials = [];
3641

3742
$pm = new DependabotPluginManager($io, $composer, null, false);
3843
$composer->setPluginManager($pm);
3944
$pm->loadInstalledPlugins();
4045

4146
foreach ($gitCredentials as &$cred) {
42-
$httpBasicCredentials[$cred['host']] = [
43-
'username' => $cred['username'],
44-
'password' => $cred['password'],
45-
];
47+
if (isset($cred['host']) && isset($cred['username']) && isset($cred['password'])) {
48+
$httpBasicCredentials[$cred['host']] = [
49+
'username' => $cred['username'],
50+
'password' => $cred['password'],
51+
];
52+
}
4653
}
4754

4855
foreach ($registryCredentials as &$cred) {
49-
$httpBasicCredentials[$cred['registry']] = [
50-
'username' => $cred['username'],
51-
'password' => $cred['password'],
52-
];
56+
$host = $cred['registry'] ?? null;
57+
if (!$host) {
58+
continue;
59+
}
60+
61+
if (isset($cred['username']) && isset($cred['password'])) {
62+
$httpBasicCredentials[$host] = [
63+
'username' => $cred['username'],
64+
'password' => $cred['password'],
65+
];
66+
}
67+
68+
$authType = $cred['auth_type'] ?? null;
69+
if ($authType === 'bearer' && isset($cred['token'])) {
70+
$bearerCredentials[$host] = $cred['token'];
71+
}
72+
if ($authType === 'github-oauth' && isset($cred['token'])) {
73+
$githubOauthCredentials[$host] = $cred['token'];
74+
}
75+
if ($authType === 'gitlab-oauth' && isset($cred['token'])) {
76+
$gitlabOauthCredentials[$host] = $cred['token'];
77+
}
78+
if ($authType === 'gitlab-token' && isset($cred['token'])) {
79+
$gitlabTokenCredentials[$host] = $cred['token'];
80+
}
81+
if ($authType === 'bitbucket-oauth' && (isset($cred['key']) || isset($cred['consumer-key']) || isset($cred['username'])) && (isset($cred['secret']) || isset($cred['consumer-secret']) || isset($cred['password']))) {
82+
$bitbucketOauthCredentials[$host] = [
83+
'consumer-key' => $cred['key'] ?? $cred['consumer-key'] ?? $cred['username'] ?? '',
84+
'consumer-secret' => $cred['secret'] ?? $cred['consumer-secret'] ?? $cred['password'] ?? '',
85+
];
86+
}
5387
}
5488

55-
if ($httpBasicCredentials) {
56-
$config->merge(
57-
[
58-
'config' => [
59-
'http-basic' => $httpBasicCredentials,
60-
],
61-
]
62-
);
89+
$mergeConfig = ['config' => []];
90+
if (!empty($httpBasicCredentials)) {
91+
$mergeConfig['config']['http-basic'] = $httpBasicCredentials;
92+
}
93+
if (!empty($bearerCredentials)) {
94+
$mergeConfig['config']['bearer'] = $bearerCredentials;
95+
}
96+
if (!empty($githubOauthCredentials)) {
97+
$mergeConfig['config']['github-oauth'] = $githubOauthCredentials;
98+
}
99+
if (!empty($gitlabOauthCredentials)) {
100+
$mergeConfig['config']['gitlab-oauth'] = $gitlabOauthCredentials;
101+
}
102+
if (!empty($gitlabTokenCredentials)) {
103+
$mergeConfig['config']['gitlab-token'] = $gitlabTokenCredentials;
104+
}
105+
if (!empty($bitbucketOauthCredentials)) {
106+
$mergeConfig['config']['bitbucket-oauth'] = $bitbucketOauthCredentials;
107+
}
108+
109+
if (!empty($mergeConfig['config'])) {
110+
$config->merge($mergeConfig);
63111
$io->loadConfiguration($config);
64112
}
65113

composer/lib/dependabot/composer/update_checker/version_resolver.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,16 +622,19 @@ def requirement_valid?(req_string)
622622

623623
sig { returns(T::Array[Dependabot::Credential]) }
624624
def git_credentials
625+
# Pass git_source credentials if they have basic auth available. Composer expects http-basic for git hosts.
625626
credentials
626627
.select { |cred| cred["type"] == "git_source" }
627628
.select { |cred| cred["password"] }
628629
end
629630

630631
sig { returns(T::Array[Dependabot::Credential]) }
631632
def registry_credentials
633+
# Include composer repository credentials that use either basic auth (username/password)
634+
# or token-based auth (token-based methods are handled in the PHP helper layer).
632635
credentials
633636
.select { |cred| cred["type"] == PackageManager::REPOSITORY_KEY }
634-
.select { |cred| cred["password"] }
637+
.select { |cred| cred["password"] || cred["token"] || cred["auth_type"] }
635638
end
636639
end
637640
end

0 commit comments

Comments
 (0)