Skip to content

Commit a80f570

Browse files
committed
Add Authentication::createApiGatewayClientCredentials
1 parent 072a0ec commit a80f570

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/Authentication.php

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

141+
/**
142+
* Creates a new instance of Authentication for 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+
* @param string $tokenResource The access token resource of the API
148+
*
149+
* @return Authentication
150+
*/
151+
public static function createApiGatewayClientCredentials(
152+
string $clientId,
153+
string $clientSecret,
154+
string $authUrl,
155+
string $tokenResource = 'token'
156+
) : Authentication {
157+
$getTokenRequestFunc = function (
158+
string $unusedBaseUrl,
159+
string $unusedRefreshToken = null
160+
) use (
161+
$clientId,
162+
$clientSecret,
163+
$authUrl,
164+
$tokenResource
165+
) {
166+
$data = ['client_id' => $clientId, 'client_secret' => $clientSecret, 'grant_type' => 'client_credentials'];
167+
return new Request(
168+
'POST',
169+
"{$authUrl}/oauth2/{$tokenResource}",
170+
['Content-Type' => 'application/x-www-form-urlencoded'],
171+
Http::buildQueryString($data)
172+
);
173+
};
174+
175+
return new self($getTokenRequestFunc);
176+
}
177+
141178
/**
142179
* Extracts an access token from the given API response
143180
*

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('\TraderInteractive\Api\Authentication', $auth);
178+
}
179+
180+
/**
181+
* @test
182+
* @covers ::createApiGatewayClientCredentials
183+
* @covers ::getTokenRequest
184+
*/
185+
public function getTokenRequestApiGatewayClientCredentials()
186+
{
187+
$auth = Authentication::createApiGatewayClientCredentials('id', 'secret', 'authUrl');
188+
$request = $auth->getTokenRequest('baseUrl');
189+
$this->assertSame('authUrl/oauth2/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)