diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b2cf46..7d7a67d 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 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 f8d47c4..335a696 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 8a738f1..0d42423 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -12,7 +12,8 @@ - + + diff --git a/src/Client.php b/src/Client.php index 828fc6b..780776f 100644 --- a/src/Client.php +++ b/src/Client.php @@ -66,11 +66,6 @@ class Client implements \Psr\Log\LoggerAwareInterface */ private $uri = 'https://mxm.xtremepush.com/'; - /** - * @var string - */ - private $token; - /** * @var string */ @@ -113,9 +108,8 @@ class Client implements \Psr\Log\LoggerAwareInterface /** * @param array $config { - * @var string $token Required, or username & password - * @var string $username Required, if no token - * @var string $password Required, if no token + * @var string $username Required; API client ID + * @var string $password Required; API client secret * @var string $uri Optional. Default https://mxm.xtremepush.com/ * @var string $user @deprecated See username * @var string $pass @deprecated See password @@ -124,25 +118,20 @@ class Client implements \Psr\Log\LoggerAwareInterface */ public function __construct(array $config) { - // Must have API token - if (!isset($config['token'])) { - // Support deprecated key names from v3 - if (!isset($config['username']) && isset($config['user'])) { - $config['username'] = $config['user']; - } - if (!isset($config['password']) && isset($config['pass'])) { - $config['password'] = $config['pass']; - } + // Support deprecated key names from v3 + if (!isset($config['username']) && isset($config['user'])) { + $config['username'] = $config['user']; + } + if (!isset($config['password']) && isset($config['pass'])) { + $config['password'] = $config['pass']; + } - // 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']); @@ -186,6 +175,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', @@ -194,15 +187,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 370a511..ce9346a 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -19,7 +19,8 @@ class ClientTest extends TestCase { private $testConfig = [ 'uri' => 'https://maxemail.example.com/', - 'token' => 'apitoken', + 'username' => 'clientId', + 'password' => 'clientSecret', ]; public function testConfigValid() @@ -30,11 +31,16 @@ public function testConfigValid() $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']); @@ -75,7 +81,8 @@ public function testConfigSupportDeprecatedUserPass() public function testConfigDefaultHost() { $config = [ - 'token' => 'apitoken', + 'username' => 'clientId', + 'password' => 'clientSecret', ]; $api = new Client($config); @@ -97,7 +104,8 @@ public function testConfigStripsUriPath() { $config = [ 'uri' => 'https://maxemail.example.com/some/extra/path', - 'token' => 'apitoken', + 'username' => 'clientId', + 'password' => 'clientSecret', ]; $api = new Client($config); @@ -122,7 +130,8 @@ public function testConfigInvalidUri() $config = [ 'uri' => '//', - 'token' => 'apitoken', + 'username' => 'clientId', + 'password' => 'clientSecret', ]; new Client($config); @@ -135,52 +144,20 @@ public function testConfigMissingUriProtocol() $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); @@ -189,39 +166,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() diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index 21567bd..5df4ca7 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -30,7 +30,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); }