Skip to content

Commit 6d5b7e4

Browse files
Copilotswissspidy
andcommitted
Add support for additional git providers in set_composer_auth_env_var()
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 2fbba68 commit 6d5b7e4

3 files changed

Lines changed: 381 additions & 0 deletions

File tree

‎features/package-auth.feature‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Feature: Composer authentication for various git providers
2+
3+
Scenario: GitHub OAuth token is set in COMPOSER_AUTH
4+
Given an empty directory
5+
When I run `GITHUB_TOKEN=ghp_test123456789 wp package path`
6+
Then STDOUT should not be empty
7+
And the return code should be 0
8+
9+
Scenario: GitLab OAuth token is set in COMPOSER_AUTH
10+
Given an empty directory
11+
When I run `GITLAB_OAUTH_TOKEN=glpat_test123456789 wp package path`
12+
Then STDOUT should not be empty
13+
And the return code should be 0
14+
15+
Scenario: GitLab personal access token is set in COMPOSER_AUTH
16+
Given an empty directory
17+
When I run `GITLAB_TOKEN=glpat_test123456789 wp package path`
18+
Then STDOUT should not be empty
19+
And the return code should be 0
20+
21+
Scenario: Bitbucket OAuth consumer is set in COMPOSER_AUTH
22+
Given an empty directory
23+
When I run `BITBUCKET_CONSUMER_KEY=test_key BITBUCKET_CONSUMER_SECRET=test_secret wp package path`
24+
Then STDOUT should not be empty
25+
And the return code should be 0
26+
27+
Scenario: HTTP Basic Auth is set in COMPOSER_AUTH
28+
Given an empty directory
29+
When I run `HTTP_BASIC_AUTH='{"repo.example.com":{"username":"user","password":"pass"}}' wp package path`
30+
Then STDOUT should not be empty
31+
And the return code should be 0
32+
33+
Scenario: Multiple auth providers can be used together
34+
Given an empty directory
35+
When I run `GITHUB_TOKEN=ghp_test123 GITLAB_TOKEN=glpat_test456 wp package path`
36+
Then STDOUT should not be empty
37+
And the return code should be 0
38+
39+
Scenario: Invalid HTTP_BASIC_AUTH JSON is ignored
40+
Given an empty directory
41+
When I run `HTTP_BASIC_AUTH='not-valid-json' wp package path`
42+
Then STDOUT should not be empty
43+
And the return code should be 0

‎src/Package_Command.php‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,11 +1409,51 @@ private function set_composer_auth_env_var() {
14091409
if ( empty( $composer_auth ) || ! is_array( $composer_auth ) ) {
14101410
$composer_auth = [];
14111411
}
1412+
1413+
// GitHub OAuth token.
14121414
$github_token = getenv( 'GITHUB_TOKEN' );
14131415
if ( ! isset( $composer_auth['github-oauth'] ) && is_string( $github_token ) ) {
14141416
$composer_auth['github-oauth'] = [ 'github.com' => $github_token ];
14151417
$changed = true;
14161418
}
1419+
1420+
// GitLab OAuth token.
1421+
$gitlab_oauth_token = getenv( 'GITLAB_OAUTH_TOKEN' );
1422+
if ( ! isset( $composer_auth['gitlab-oauth'] ) && is_string( $gitlab_oauth_token ) ) {
1423+
$composer_auth['gitlab-oauth'] = [ 'gitlab.com' => $gitlab_oauth_token ];
1424+
$changed = true;
1425+
}
1426+
1427+
// GitLab personal access token.
1428+
$gitlab_token = getenv( 'GITLAB_TOKEN' );
1429+
if ( ! isset( $composer_auth['gitlab-token'] ) && is_string( $gitlab_token ) ) {
1430+
$composer_auth['gitlab-token'] = [ 'gitlab.com' => $gitlab_token ];
1431+
$changed = true;
1432+
}
1433+
1434+
// Bitbucket OAuth consumer.
1435+
$bitbucket_key = getenv( 'BITBUCKET_CONSUMER_KEY' );
1436+
$bitbucket_secret = getenv( 'BITBUCKET_CONSUMER_SECRET' );
1437+
if ( ! isset( $composer_auth['bitbucket-oauth'] ) && is_string( $bitbucket_key ) && is_string( $bitbucket_secret ) ) {
1438+
$composer_auth['bitbucket-oauth'] = [
1439+
'bitbucket.org' => [
1440+
'consumer-key' => $bitbucket_key,
1441+
'consumer-secret' => $bitbucket_secret,
1442+
],
1443+
];
1444+
$changed = true;
1445+
}
1446+
1447+
// HTTP Basic Authentication.
1448+
$http_basic_auth = getenv( 'HTTP_BASIC_AUTH' );
1449+
if ( ! isset( $composer_auth['http-basic'] ) && is_string( $http_basic_auth ) ) {
1450+
$http_basic_auth_decoded = json_decode( $http_basic_auth, true /*assoc*/ );
1451+
if ( is_array( $http_basic_auth_decoded ) ) {
1452+
$composer_auth['http-basic'] = $http_basic_auth_decoded;
1453+
$changed = true;
1454+
}
1455+
}
1456+
14171457
if ( $changed ) {
14181458
putenv( 'COMPOSER_AUTH=' . json_encode( $composer_auth ) );
14191459
}

‎tests/PackageAuthTest.php‎

Lines changed: 298 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,298 @@
1+
<?php
2+
3+
use WP_CLI\Tests\TestCase;
4+
5+
class PackageAuthTest extends TestCase {
6+
7+
private $env_vars_to_restore = [];
8+
9+
public function set_up() {
10+
parent::set_up();
11+
12+
// Save current environment variables
13+
$this->env_vars_to_restore = [
14+
'COMPOSER_AUTH' => getenv( 'COMPOSER_AUTH' ),
15+
'GITHUB_TOKEN' => getenv( 'GITHUB_TOKEN' ),
16+
'GITLAB_OAUTH_TOKEN' => getenv( 'GITLAB_OAUTH_TOKEN' ),
17+
'GITLAB_TOKEN' => getenv( 'GITLAB_TOKEN' ),
18+
'BITBUCKET_CONSUMER_KEY' => getenv( 'BITBUCKET_CONSUMER_KEY' ),
19+
'BITBUCKET_CONSUMER_SECRET' => getenv( 'BITBUCKET_CONSUMER_SECRET' ),
20+
'HTTP_BASIC_AUTH' => getenv( 'HTTP_BASIC_AUTH' ),
21+
];
22+
23+
// Clear all auth-related environment variables
24+
foreach ( array_keys( $this->env_vars_to_restore ) as $var ) {
25+
putenv( $var );
26+
}
27+
}
28+
29+
public function tear_down() {
30+
// Restore environment variables
31+
foreach ( $this->env_vars_to_restore as $var => $value ) {
32+
if ( false !== $value ) {
33+
putenv( "$var=$value" );
34+
} else {
35+
putenv( $var );
36+
}
37+
}
38+
39+
parent::tear_down();
40+
}
41+
42+
/**
43+
* Test that GITHUB_TOKEN is added to COMPOSER_AUTH.
44+
*/
45+
public function test_github_token_added_to_composer_auth() {
46+
putenv( 'GITHUB_TOKEN=ghp_test123456789' );
47+
48+
$package = new Package_Command();
49+
$method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' );
50+
if ( PHP_VERSION_ID < 80100 ) {
51+
$method->setAccessible( true );
52+
}
53+
$method->invoke( $package );
54+
55+
$composer_auth = getenv( 'COMPOSER_AUTH' );
56+
$this->assertNotFalse( $composer_auth );
57+
58+
$auth_array = json_decode( $composer_auth, true );
59+
$this->assertIsArray( $auth_array );
60+
$this->assertArrayHasKey( 'github-oauth', $auth_array );
61+
$this->assertArrayHasKey( 'github.com', $auth_array['github-oauth'] );
62+
$this->assertSame( 'ghp_test123456789', $auth_array['github-oauth']['github.com'] );
63+
}
64+
65+
/**
66+
* Test that GITLAB_OAUTH_TOKEN is added to COMPOSER_AUTH.
67+
*/
68+
public function test_gitlab_oauth_token_added_to_composer_auth() {
69+
putenv( 'GITLAB_OAUTH_TOKEN=glpat_test123456789' );
70+
71+
$package = new Package_Command();
72+
$method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' );
73+
if ( PHP_VERSION_ID < 80100 ) {
74+
$method->setAccessible( true );
75+
}
76+
$method->invoke( $package );
77+
78+
$composer_auth = getenv( 'COMPOSER_AUTH' );
79+
$this->assertNotFalse( $composer_auth );
80+
81+
$auth_array = json_decode( $composer_auth, true );
82+
$this->assertIsArray( $auth_array );
83+
$this->assertArrayHasKey( 'gitlab-oauth', $auth_array );
84+
$this->assertArrayHasKey( 'gitlab.com', $auth_array['gitlab-oauth'] );
85+
$this->assertSame( 'glpat_test123456789', $auth_array['gitlab-oauth']['gitlab.com'] );
86+
}
87+
88+
/**
89+
* Test that GITLAB_TOKEN is added to COMPOSER_AUTH.
90+
*/
91+
public function test_gitlab_token_added_to_composer_auth() {
92+
putenv( 'GITLAB_TOKEN=glpat_test123456789' );
93+
94+
$package = new Package_Command();
95+
$method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' );
96+
if ( PHP_VERSION_ID < 80100 ) {
97+
$method->setAccessible( true );
98+
}
99+
$method->invoke( $package );
100+
101+
$composer_auth = getenv( 'COMPOSER_AUTH' );
102+
$this->assertNotFalse( $composer_auth );
103+
104+
$auth_array = json_decode( $composer_auth, true );
105+
$this->assertIsArray( $auth_array );
106+
$this->assertArrayHasKey( 'gitlab-token', $auth_array );
107+
$this->assertArrayHasKey( 'gitlab.com', $auth_array['gitlab-token'] );
108+
$this->assertSame( 'glpat_test123456789', $auth_array['gitlab-token']['gitlab.com'] );
109+
}
110+
111+
/**
112+
* Test that BITBUCKET_CONSUMER_KEY and BITBUCKET_CONSUMER_SECRET are added to COMPOSER_AUTH.
113+
*/
114+
public function test_bitbucket_oauth_added_to_composer_auth() {
115+
putenv( 'BITBUCKET_CONSUMER_KEY=test_key' );
116+
putenv( 'BITBUCKET_CONSUMER_SECRET=test_secret' );
117+
118+
$package = new Package_Command();
119+
$method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' );
120+
if ( PHP_VERSION_ID < 80100 ) {
121+
$method->setAccessible( true );
122+
}
123+
$method->invoke( $package );
124+
125+
$composer_auth = getenv( 'COMPOSER_AUTH' );
126+
$this->assertNotFalse( $composer_auth );
127+
128+
$auth_array = json_decode( $composer_auth, true );
129+
$this->assertIsArray( $auth_array );
130+
$this->assertArrayHasKey( 'bitbucket-oauth', $auth_array );
131+
$this->assertArrayHasKey( 'bitbucket.org', $auth_array['bitbucket-oauth'] );
132+
$this->assertArrayHasKey( 'consumer-key', $auth_array['bitbucket-oauth']['bitbucket.org'] );
133+
$this->assertArrayHasKey( 'consumer-secret', $auth_array['bitbucket-oauth']['bitbucket.org'] );
134+
$this->assertSame( 'test_key', $auth_array['bitbucket-oauth']['bitbucket.org']['consumer-key'] );
135+
$this->assertSame( 'test_secret', $auth_array['bitbucket-oauth']['bitbucket.org']['consumer-secret'] );
136+
}
137+
138+
/**
139+
* Test that Bitbucket OAuth is not added if only one credential is provided.
140+
*/
141+
public function test_bitbucket_oauth_requires_both_credentials() {
142+
putenv( 'BITBUCKET_CONSUMER_KEY=test_key' );
143+
// BITBUCKET_CONSUMER_SECRET is not set
144+
145+
$package = new Package_Command();
146+
$method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' );
147+
if ( PHP_VERSION_ID < 80100 ) {
148+
$method->setAccessible( true );
149+
}
150+
$method->invoke( $package );
151+
152+
$composer_auth = getenv( 'COMPOSER_AUTH' );
153+
// No auth should be set because only one credential was provided
154+
$this->assertFalse( $composer_auth );
155+
}
156+
157+
/**
158+
* Test that HTTP_BASIC_AUTH is added to COMPOSER_AUTH.
159+
*/
160+
public function test_http_basic_auth_added_to_composer_auth() {
161+
$http_basic = json_encode(
162+
[
163+
'repo.example.com' => [
164+
'username' => 'user',
165+
'password' => 'pass',
166+
],
167+
]
168+
);
169+
putenv( "HTTP_BASIC_AUTH=$http_basic" );
170+
171+
$package = new Package_Command();
172+
$method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' );
173+
if ( PHP_VERSION_ID < 80100 ) {
174+
$method->setAccessible( true );
175+
}
176+
$method->invoke( $package );
177+
178+
$composer_auth = getenv( 'COMPOSER_AUTH' );
179+
$this->assertNotFalse( $composer_auth );
180+
181+
$auth_array = json_decode( $composer_auth, true );
182+
$this->assertIsArray( $auth_array );
183+
$this->assertArrayHasKey( 'http-basic', $auth_array );
184+
$this->assertArrayHasKey( 'repo.example.com', $auth_array['http-basic'] );
185+
$this->assertArrayHasKey( 'username', $auth_array['http-basic']['repo.example.com'] );
186+
$this->assertArrayHasKey( 'password', $auth_array['http-basic']['repo.example.com'] );
187+
$this->assertSame( 'user', $auth_array['http-basic']['repo.example.com']['username'] );
188+
$this->assertSame( 'pass', $auth_array['http-basic']['repo.example.com']['password'] );
189+
}
190+
191+
/**
192+
* Test that invalid HTTP_BASIC_AUTH JSON is ignored.
193+
*/
194+
public function test_invalid_http_basic_auth_json_ignored() {
195+
putenv( 'HTTP_BASIC_AUTH=not-valid-json' );
196+
197+
$package = new Package_Command();
198+
$method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' );
199+
if ( PHP_VERSION_ID < 80100 ) {
200+
$method->setAccessible( true );
201+
}
202+
$method->invoke( $package );
203+
204+
$composer_auth = getenv( 'COMPOSER_AUTH' );
205+
// No auth should be set because the JSON was invalid
206+
$this->assertFalse( $composer_auth );
207+
}
208+
209+
/**
210+
* Test that multiple auth providers can be used together.
211+
*/
212+
public function test_multiple_auth_providers() {
213+
putenv( 'GITHUB_TOKEN=ghp_test123' );
214+
putenv( 'GITLAB_TOKEN=glpat_test456' );
215+
216+
$package = new Package_Command();
217+
$method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' );
218+
if ( PHP_VERSION_ID < 80100 ) {
219+
$method->setAccessible( true );
220+
}
221+
$method->invoke( $package );
222+
223+
$composer_auth = getenv( 'COMPOSER_AUTH' );
224+
$this->assertNotFalse( $composer_auth );
225+
226+
$auth_array = json_decode( $composer_auth, true );
227+
$this->assertIsArray( $auth_array );
228+
$this->assertArrayHasKey( 'github-oauth', $auth_array );
229+
$this->assertArrayHasKey( 'gitlab-token', $auth_array );
230+
$this->assertSame( 'ghp_test123', $auth_array['github-oauth']['github.com'] );
231+
$this->assertSame( 'glpat_test456', $auth_array['gitlab-token']['gitlab.com'] );
232+
}
233+
234+
/**
235+
* Test that existing COMPOSER_AUTH is preserved.
236+
*/
237+
public function test_existing_composer_auth_preserved() {
238+
$existing_auth = json_encode(
239+
[
240+
'github-oauth' => [
241+
'github.com' => 'existing_token',
242+
],
243+
]
244+
);
245+
putenv( "COMPOSER_AUTH=$existing_auth" );
246+
putenv( 'GITLAB_TOKEN=glpat_new_token' );
247+
248+
$package = new Package_Command();
249+
$method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' );
250+
if ( PHP_VERSION_ID < 80100 ) {
251+
$method->setAccessible( true );
252+
}
253+
$method->invoke( $package );
254+
255+
$composer_auth = getenv( 'COMPOSER_AUTH' );
256+
$this->assertNotFalse( $composer_auth );
257+
258+
$auth_array = json_decode( $composer_auth, true );
259+
$this->assertIsArray( $auth_array );
260+
// Existing github-oauth should be preserved
261+
$this->assertArrayHasKey( 'github-oauth', $auth_array );
262+
$this->assertSame( 'existing_token', $auth_array['github-oauth']['github.com'] );
263+
// New gitlab-token should be added
264+
$this->assertArrayHasKey( 'gitlab-token', $auth_array );
265+
$this->assertSame( 'glpat_new_token', $auth_array['gitlab-token']['gitlab.com'] );
266+
}
267+
268+
/**
269+
* Test that environment variable tokens don't override existing COMPOSER_AUTH values.
270+
*/
271+
public function test_env_tokens_dont_override_composer_auth() {
272+
$existing_auth = json_encode(
273+
[
274+
'github-oauth' => [
275+
'github.com' => 'existing_token',
276+
],
277+
]
278+
);
279+
putenv( "COMPOSER_AUTH=$existing_auth" );
280+
putenv( 'GITHUB_TOKEN=new_token' );
281+
282+
$package = new Package_Command();
283+
$method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' );
284+
if ( PHP_VERSION_ID < 80100 ) {
285+
$method->setAccessible( true );
286+
}
287+
$method->invoke( $package );
288+
289+
$composer_auth = getenv( 'COMPOSER_AUTH' );
290+
$this->assertNotFalse( $composer_auth );
291+
292+
$auth_array = json_decode( $composer_auth, true );
293+
$this->assertIsArray( $auth_array );
294+
// Existing github-oauth should be preserved, not overridden by GITHUB_TOKEN
295+
$this->assertArrayHasKey( 'github-oauth', $auth_array );
296+
$this->assertSame( 'existing_token', $auth_array['github-oauth']['github.com'] );
297+
}
298+
}

0 commit comments

Comments
 (0)