Skip to content

Commit e55b7cc

Browse files
authored
Merge pull request #60 from chadicus/v4.x
Allow API Gateway Authentication
2 parents 5a0e236 + 4115a22 commit e55b7cc

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

.github/workflows/php.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: PHP Composer
22

33
on:
44
push:
5-
branches: [ master ]
5+
branches: [ v4.x ]
66
pull_request:
7-
branches: [ master ]
7+
branches: [ v4.x ]
88

99
jobs:
1010
build:

src/Authentication.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,40 @@ public static function createOwnerCredentials(
138138
return new self($getTokenRequestFunc);
139139
}
140140

141+
/**
142+
* Creates a new instance of Authentication for API Gateway Client Credentials grant type
143+
*
144+
* @param string $clientId The oauth client id
145+
* @param string $clientSecret The oauth client secret
146+
* @param string $authUrl The oauth auth url
147+
*
148+
* @return Authentication
149+
*/
150+
public static function createApiGatewayClientCredentials(
151+
string $clientId,
152+
string $clientSecret,
153+
string $authUrl
154+
) : Authentication {
155+
$getTokenRequestFunc = function (
156+
string $unusedBaseUrl,
157+
string $unusedRefreshToken = null
158+
) use (
159+
$clientId,
160+
$clientSecret,
161+
$authUrl
162+
) {
163+
$data = ['client_id' => $clientId, 'client_secret' => $clientSecret, 'grant_type' => 'client_credentials'];
164+
return new Request(
165+
'POST',
166+
$authUrl,
167+
['Content-Type' => 'application/x-www-form-urlencoded'],
168+
Http::buildQueryString($data)
169+
);
170+
};
171+
172+
return new self($getTokenRequestFunc);
173+
}
174+
141175
/**
142176
* Extracts an access token from the given API response
143177
*

tests/AuthenticationTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,34 @@ public function getTokenRequestClientCredentialsCustomTokenResource()
166166
);
167167
$this->assertSame(['Content-Type' => ['application/x-www-form-urlencoded']], $request->getHeaders());
168168
}
169+
170+
/**
171+
* @test
172+
* @covers ::createApiGatewayClientCredentials
173+
*/
174+
public function createApiGatewayClientCredentials()
175+
{
176+
$auth = Authentication::createApiGatewayClientCredentials('not under test', 'not under test', 'http://auth');
177+
$this->assertInstanceOf(Authentication::class, $auth);
178+
}
179+
180+
/**
181+
* @test
182+
* @covers ::createApiGatewayClientCredentials
183+
* @covers ::getTokenRequest
184+
*/
185+
public function getTokenRequestApiGatewayClientCredentials()
186+
{
187+
$auth = Authentication::createApiGatewayClientCredentials('id', 'secret', 'example.com/token');
188+
$request = $auth->getTokenRequest('baseUrl');
189+
$this->assertSame('example.com/token', (string)$request->getUri());
190+
$this->assertSame('POST', $request->getMethod());
191+
$this->assertSame(
192+
'client_id=id&client_secret=secret&grant_type=client_credentials',
193+
(string)$request->getBody()
194+
);
195+
$this->assertSame(['Content-Type' => ['application/x-www-form-urlencoded']], $request->getHeaders());
196+
}
169197
}
170198

171199
function time()

0 commit comments

Comments
 (0)