Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions features/package-auth.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Feature: Composer authentication for various git providers

Scenario: GitHub OAuth token is set in COMPOSER_AUTH
Given an empty directory
When I run `GITHUB_TOKEN=ghp_test123456789 wp package path`
Then STDOUT should not be empty
And the return code should be 0

Scenario: GitLab OAuth token is set in COMPOSER_AUTH
Given an empty directory
When I run `GITLAB_OAUTH_TOKEN=glpat_test123456789 wp package path`
Then STDOUT should not be empty
And the return code should be 0

Scenario: GitLab personal access token is set in COMPOSER_AUTH
Given an empty directory
When I run `GITLAB_TOKEN=glpat_test123456789 wp package path`
Then STDOUT should not be empty
And the return code should be 0

Scenario: Bitbucket OAuth consumer is set in COMPOSER_AUTH
Given an empty directory
When I run `BITBUCKET_CONSUMER_KEY=test_key BITBUCKET_CONSUMER_SECRET=test_secret wp package path`
Then STDOUT should not be empty
And the return code should be 0

Scenario: HTTP Basic Auth is set in COMPOSER_AUTH
Given an empty directory
When I run `HTTP_BASIC_AUTH='{"repo.example.com":{"username":"user","password":"pass"}}' wp package path`
Then STDOUT should not be empty
And the return code should be 0

Scenario: Multiple auth providers can be used together
Given an empty directory
When I run `GITHUB_TOKEN=ghp_test123 GITLAB_TOKEN=glpat_test456 wp package path`
Then STDOUT should not be empty
And the return code should be 0

Scenario: Invalid HTTP_BASIC_AUTH JSON is ignored
Given an empty directory
When I run `HTTP_BASIC_AUTH='not-valid-json' wp package path`
Then STDOUT should not be empty
And the return code should be 0
45 changes: 44 additions & 1 deletion src/Package_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -1409,11 +1409,54 @@ private function set_composer_auth_env_var() {
if ( empty( $composer_auth ) || ! is_array( $composer_auth ) ) {
$composer_auth = [];
}

// GitHub OAuth token.
$github_token = getenv( 'GITHUB_TOKEN' );
if ( ! isset( $composer_auth['github-oauth'] ) && is_string( $github_token ) ) {
if ( ! isset( $composer_auth['github-oauth'] ) && is_string( $github_token ) && '' !== $github_token ) {
$composer_auth['github-oauth'] = [ 'github.com' => $github_token ];
$changed = true;
}

// GitLab OAuth token.
$gitlab_oauth_token = getenv( 'GITLAB_OAUTH_TOKEN' );
if ( ! isset( $composer_auth['gitlab-oauth'] ) && is_string( $gitlab_oauth_token ) && '' !== $gitlab_oauth_token ) {
$composer_auth['gitlab-oauth'] = [ 'gitlab.com' => $gitlab_oauth_token ];
$changed = true;
}

// GitLab personal access token.
$gitlab_token = getenv( 'GITLAB_TOKEN' );
if ( ! isset( $composer_auth['gitlab-token'] ) && is_string( $gitlab_token ) && '' !== $gitlab_token ) {
$composer_auth['gitlab-token'] = [ 'gitlab.com' => $gitlab_token ];
$changed = true;
}

// Bitbucket OAuth consumer.
$bitbucket_key = getenv( 'BITBUCKET_CONSUMER_KEY' );
$bitbucket_secret = getenv( 'BITBUCKET_CONSUMER_SECRET' );
if ( ! isset( $composer_auth['bitbucket-oauth'] )
&& is_string( $bitbucket_key ) && '' !== $bitbucket_key
&& is_string( $bitbucket_secret ) && '' !== $bitbucket_secret
) {
$composer_auth['bitbucket-oauth'] = [
'bitbucket.org' => [
'consumer-key' => $bitbucket_key,
'consumer-secret' => $bitbucket_secret,
],
];
$changed = true;
}

// HTTP Basic Authentication.
$http_basic_auth = getenv( 'HTTP_BASIC_AUTH' );
if ( ! isset( $composer_auth['http-basic'] ) && is_string( $http_basic_auth ) && '' !== $http_basic_auth ) {
$http_basic_auth_decoded = json_decode( $http_basic_auth, true /*assoc*/ );
if ( is_array( $http_basic_auth_decoded ) ) {
$composer_auth['http-basic'] = $http_basic_auth_decoded;
$changed = true;
}
}

if ( $changed ) {
putenv( 'COMPOSER_AUTH=' . json_encode( $composer_auth ) );
}
Expand Down
Loading