diff --git a/src/Authentication.php b/src/Authentication.php index 478e76d..9fb3c08 100644 --- a/src/Authentication.php +++ b/src/Authentication.php @@ -138,6 +138,40 @@ public static function createOwnerCredentials( return new self($getTokenRequestFunc); } + /** + * Creates a new instance of Authentication for API Gateway Client Credentials grant type + * + * @param string $clientId The oauth client id + * @param string $clientSecret The oauth client secret + * @param string $authUrl The oauth auth url + * + * @return Authentication + */ + public static function createApiGatewayClientCredentials( + string $clientId, + string $clientSecret, + string $authUrl + ) : Authentication { + $getTokenRequestFunc = function ( + string $unusedBaseUrl, + string $unusedRefreshToken = null + ) use ( + $clientId, + $clientSecret, + $authUrl + ) { + $data = ['client_id' => $clientId, 'client_secret' => $clientSecret, 'grant_type' => 'client_credentials']; + return new Request( + 'POST', + $authUrl, + ['Content-Type' => 'application/x-www-form-urlencoded'], + Http::buildQueryString($data) + ); + }; + + return new self($getTokenRequestFunc); + } + /** * Extracts an access token from the given API response * diff --git a/tests/AuthenticationTest.php b/tests/AuthenticationTest.php index a8f36e3..25024b9 100644 --- a/tests/AuthenticationTest.php +++ b/tests/AuthenticationTest.php @@ -166,6 +166,34 @@ public function getTokenRequestClientCredentialsCustomTokenResource() ); $this->assertSame(['Content-Type' => ['application/x-www-form-urlencoded']], $request->getHeaders()); } + + /** + * @test + * @covers ::createApiGatewayClientCredentials + */ + public function createApiGatewayClientCredentials() + { + $auth = Authentication::createApiGatewayClientCredentials('not under test', 'not under test', 'http://auth'); + $this->assertInstanceOf(Authentication::class, $auth); + } + + /** + * @test + * @covers ::createApiGatewayClientCredentials + * @covers ::getTokenRequest + */ + public function getTokenRequestApiGatewayClientCredentials() + { + $auth = Authentication::createApiGatewayClientCredentials('id', 'secret', 'example.com/token'); + $request = $auth->getTokenRequest('baseUrl'); + $this->assertSame('example.com/token', (string)$request->getUri()); + $this->assertSame('POST', $request->getMethod()); + $this->assertSame( + 'client_id=id&client_secret=secret&grant_type=client_credentials', + (string)$request->getBody() + ); + $this->assertSame(['Content-Type' => ['application/x-www-form-urlencoded']], $request->getHeaders()); + } } function time()