Skip to content
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: PHP Composer

on:
push:
branches: [ master ]
branches: [ v4.x ]
pull_request:
branches: [ master ]
branches: [ v4.x ]

jobs:
build:
Expand Down
34 changes: 34 additions & 0 deletions src/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
28 changes: 28 additions & 0 deletions tests/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading