diff --git a/CHANGELOG.md b/CHANGELOG.md index 7746fc8..a61c0df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,11 +5,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Changed +- Revert support for token auth, added in v6.1.0. +- Label username and password for clientId and clientSecret. No param changes. ## [6.1.0] - 2025-06-25 ### Added -- Support for API token authentication. Username and password can still be used - as a fallback. +- ~~Support for API token authentication. Username and password can still be used + as a fallback.~~ Reverted in v6.1.1. ### Changed - Deprecate `getConfig()` method. Packages can maintain their config internally. @@ -21,10 +24,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - **BC break**: Removed support for PHP versions <= v8.0 as they are no longer [actively supported](https://php.net/supported-versions.php) by the PHP project. +## [5.2.1] - 2025-07-20 +### Changed +- Revert support for token auth, added in v5.2.0. +- Label username and password for clientId and clientSecret. No param changes. + ## [5.2.0] - 2025-06-25 ### Added -- Support for API token authentication. Username and password can still be used - as a fallback. +- ~~Support for API token authentication. Username and password can still be used + as a fallback.~~ Reverted in v5.2.1. ### Changed - Deprecate `getConfig()` method. Packages can maintain their config internally. diff --git a/README.md b/README.md index 485f8af..2f9cb7c 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,8 @@ $ composer require maxemail/api-php ```php // Instantiate Client: $config = [ - 'token' => 'apitoken', + 'username' => 'client ID', + 'password' => 'client secret' ]; $api = new \Maxemail\Api\Client($config); diff --git a/phpunit.xml.dist b/phpunit.xml.dist index c313011..2853b4a 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -12,7 +12,8 @@ - + + diff --git a/src/Client.php b/src/Client.php index 9a716b5..d0856a0 100644 --- a/src/Client.php +++ b/src/Client.php @@ -64,8 +64,6 @@ class Client implements LoggerAwareInterface private string $uri = 'https://mxm.xtremepush.com/'; - private readonly string $token; - private readonly string $username; private readonly string $password; @@ -90,26 +88,20 @@ class Client implements LoggerAwareInterface /** * @param array{ - * token: string, // Required, or username & password - * username: string, // Required, if no token - * password: string, // Required, if no token + * username: string, // Required; API client ID + * password: string, // Required; API client secret * uri: string, // Optional. Default https://mxm.xtremepush.com/ * debugLogging: bool, // Optional. Enable logging of request/response. Default false * } $config */ public function __construct(array $config) { - // Must have API token - if (!isset($config['token'])) { - // Must have user/pass - if (!isset($config['username']) || !isset($config['password'])) { - throw new Exception\InvalidArgumentException('API config requires token OR username & password'); - } - $this->username = $config['username']; - $this->password = $config['password']; - } else { - $this->token = $config['token']; + // Must have user/pass + if (!isset($config['username']) || !isset($config['password'])) { + throw new Exception\InvalidArgumentException('API config requires username & password'); } + $this->username = $config['username']; + $this->password = $config['password']; if (isset($config['uri'])) { $parsed = parse_url($config['uri']); @@ -153,6 +145,10 @@ private function getClient(): GuzzleClientInterface $clientConfig = [ 'base_uri' => $this->uri . 'api/json/', + 'auth' => [ + $this->username, + $this->password, + ], 'headers' => [ 'User-Agent' => 'MxmApiClient/' . self::VERSION . ' PHP/' . PHP_VERSION, 'Content-Type' => 'application/x-www-form-urlencoded', @@ -161,15 +157,6 @@ private function getClient(): GuzzleClientInterface 'handler' => $stack, ]; - if (isset($this->token)) { - $clientConfig['headers']['Authorization'] = 'Bearer ' . $this->token; - } else { - $clientConfig['auth'] = [ - $this->username, - $this->password, - ]; - } - if (!isset($this->httpClientFactory)) { $this->httpClient = new GuzzleClient($clientConfig); } else { diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 4c3dbe0..1cbeb1c 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -19,7 +19,8 @@ class ClientTest extends TestCase { private array $testConfig = [ 'uri' => 'https://maxemail.example.com/', - 'token' => 'apitoken', + 'username' => 'clientId', + 'password' => 'clientSecret', ]; public function testConfigValid(): void @@ -30,11 +31,16 @@ public function testConfigValid(): void $expectedUri = $this->testConfig['uri'] . 'api/json/'; static::assertSame($expectedUri, $actual['base_uri']); + $expectedAuth = [ + $this->testConfig['username'], + $this->testConfig['password'], + ]; + static::assertSame($expectedAuth, $actual['auth']); + $expectedHeaders = [ 'User-Agent' => 'MxmApiClient/' . Client::VERSION . ' PHP/' . PHP_VERSION, 'Content-Type' => 'application/x-www-form-urlencoded', 'Accept' => 'application/json', - 'Authorization' => 'Bearer ' . $this->testConfig['token'], ]; static::assertSame($expectedHeaders, $actual['headers']); @@ -50,7 +56,8 @@ public function testConfigValid(): void public function testConfigDefaultHost(): void { $config = [ - 'token' => 'apitoken', + 'username' => 'clientId', + 'password' => 'clientSecret', ]; $api = new Client($config); @@ -72,7 +79,8 @@ public function testConfigStripsUriPath(): void { $config = [ 'uri' => 'https://maxemail.example.com/some/extra/path', - 'token' => 'apitoken', + 'username' => 'clientId', + 'password' => 'clientSecret', ]; $api = new Client($config); @@ -97,7 +105,8 @@ public function testConfigInvalidUri(): void $config = [ 'uri' => '//', - 'token' => 'apitoken', + 'username' => 'clientId', + 'password' => 'clientSecret', ]; new Client($config); @@ -110,52 +119,20 @@ public function testConfigMissingUriProtocol(): void $config = [ 'uri' => 'maxemail.example.com', - 'token' => 'apitoken', + 'username' => 'clientId', + 'password' => 'clientSecret', ]; new Client($config); } - public function testConfigLegacyAuthentication(): void - { - $config = [ - 'username' => 'api@user.com', - 'password' => 'apipass', - ]; - - $api = new Client($config); - - $factory = function (array $actual) use ($config): GuzzleClient { - $expectedAuth = [ - $config['username'], - $config['password'], - ]; - static::assertSame($expectedAuth, $actual['auth']); - - return $this->createMock(GuzzleClient::class); - }; - - $api->setHttpClientFactory($factory); - - // Get a service, to trigger the HTTP Client factory - $api->folder; - } - - public function testConfigMissingToken(): void - { - $this->expectException(Exception\InvalidArgumentException::class); - $this->expectExceptionMessage('API config requires token OR username & password'); - - new Client([]); - } - public function testConfigMissingUsername(): void { $this->expectException(Exception\InvalidArgumentException::class); - $this->expectExceptionMessage('API config requires token OR username & password'); + $this->expectExceptionMessage('API config requires username & password'); $config = [ - 'password' => 'apipass', + 'password' => 'clientSecret', ]; new Client($config); @@ -164,39 +141,20 @@ public function testConfigMissingUsername(): void public function testConfigMissingPassword(): void { $this->expectException(Exception\InvalidArgumentException::class); - $this->expectExceptionMessage('API config requires token OR username & password'); + $this->expectExceptionMessage('API config requires username & password'); $config = [ - 'username' => 'api@user.com', + 'username' => 'clientId', ]; new Client($config); } - public function testGetConfigWithToken(): void + public function testGetConfig(): void { $api = new Client($this->testConfig); - $expected = [ - 'uri' => $this->testConfig['uri'], - 'username' => null, - 'password' => null, - ]; - - static::assertSame($expected, $api->getConfig()); - } - - public function testGetConfigWithLegacyAuthentication(): void - { - $config = [ - 'uri' => 'https://maxemail.example.com/', - 'username' => 'api@user.com', - 'password' => 'apipass', - ]; - - $api = new Client($config); - - static::assertSame($config, $api->getConfig()); + static::assertSame($this->testConfig, $api->getConfig()); } public function testSetGetLogger(): void diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index ccb23d9..d11fa51 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -27,7 +27,8 @@ protected function setUp(): void $config = [ 'uri' => getenv('FUNC_API_URI'), - 'token' => getenv('FUNC_API_TOKEN'), + 'username' => getenv('FUNC_API_USERNAME'), + 'password' => getenv('FUNC_API_PASSWORD'), ]; $this->client = new Client($config); }