2016-02-23 03:19:46 +05:30
|
|
|
<?php
|
2019-08-23 13:58:04 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-02-21 01:28:52 +05:30
|
|
|
namespace api\tests\functional\_steps;
|
2016-02-23 03:19:46 +05:30
|
|
|
|
2019-02-21 01:28:52 +05:30
|
|
|
use api\tests\FunctionalTester;
|
2024-12-06 06:04:09 +05:30
|
|
|
use common\components\OAuth2\Repositories\PublicScopeRepository;
|
2016-02-23 03:19:46 +05:30
|
|
|
|
2016-12-18 04:50:53 +05:30
|
|
|
class OauthSteps extends FunctionalTester {
|
2016-02-23 03:19:46 +05:30
|
|
|
|
2019-09-18 04:44:05 +05:30
|
|
|
public function obtainAuthCode(array $permissions = []): string {
|
2017-01-24 04:30:08 +05:30
|
|
|
$this->amAuthenticated();
|
2019-09-18 04:44:05 +05:30
|
|
|
$this->sendPOST('/api/oauth2/v1/complete?' . http_build_query([
|
2016-02-23 03:19:46 +05:30
|
|
|
'client_id' => 'ely',
|
|
|
|
'redirect_uri' => 'http://ely.by',
|
|
|
|
'response_type' => 'code',
|
2019-09-18 04:44:05 +05:30
|
|
|
'scope' => implode(' ', $permissions),
|
|
|
|
]), ['accept' => true]);
|
2016-02-23 03:19:46 +05:30
|
|
|
$this->canSeeResponseJsonMatchesJsonPath('$.redirectUri');
|
2019-09-18 04:44:05 +05:30
|
|
|
[$redirectUri] = $this->grabDataFromResponseByJsonPath('$.redirectUri');
|
2024-12-02 15:40:55 +05:30
|
|
|
preg_match('/code=([^&$]+)/', (string)$redirectUri, $matches);
|
2016-02-23 03:19:46 +05:30
|
|
|
|
|
|
|
return $matches[1];
|
|
|
|
}
|
|
|
|
|
2019-08-23 13:58:04 +05:30
|
|
|
public function getAccessToken(array $permissions = []): string {
|
2019-09-18 04:44:05 +05:30
|
|
|
$authCode = $this->obtainAuthCode($permissions);
|
2016-08-06 19:06:24 +05:30
|
|
|
$response = $this->issueToken($authCode);
|
|
|
|
|
|
|
|
return $response['access_token'];
|
|
|
|
}
|
|
|
|
|
2019-08-23 13:58:04 +05:30
|
|
|
public function getRefreshToken(array $permissions = []): string {
|
2019-09-22 02:47:21 +05:30
|
|
|
$authCode = $this->obtainAuthCode(array_merge([PublicScopeRepository::OFFLINE_ACCESS], $permissions));
|
2016-08-06 19:06:24 +05:30
|
|
|
$response = $this->issueToken($authCode);
|
|
|
|
|
|
|
|
return $response['refresh_token'];
|
|
|
|
}
|
|
|
|
|
2019-09-22 02:47:21 +05:30
|
|
|
public function issueToken(string $authCode): array {
|
|
|
|
$this->sendPOST('/api/oauth2/v1/token', [
|
|
|
|
'grant_type' => 'authorization_code',
|
2016-02-23 03:19:46 +05:30
|
|
|
'code' => $authCode,
|
|
|
|
'client_id' => 'ely',
|
|
|
|
'client_secret' => 'ZuM1vGchJz-9_UZ5HC3H3Z9Hg5PzdbkM',
|
|
|
|
'redirect_uri' => 'http://ely.by',
|
|
|
|
]);
|
|
|
|
|
2016-08-06 19:06:24 +05:30
|
|
|
return json_decode($this->grabResponse(), true);
|
2016-02-23 03:19:46 +05:30
|
|
|
}
|
|
|
|
|
2019-09-22 02:47:21 +05:30
|
|
|
public function getAccessTokenByClientCredentialsGrant(array $permissions = [], bool $useTrusted = true): string {
|
|
|
|
$this->sendPOST('/api/oauth2/v1/token', [
|
|
|
|
'grant_type' => 'client_credentials',
|
2016-12-18 04:50:53 +05:30
|
|
|
'client_id' => $useTrusted ? 'trusted-client' : 'default-client',
|
|
|
|
'client_secret' => $useTrusted ? 'tXBbyvMcyaOgHMOAXBpN2EC7uFoJAaL9' : 'AzWRy7ZjS1yRQUk2vRBDic8fprOKDB1W',
|
2019-09-24 04:26:32 +05:30
|
|
|
'scope' => implode(' ', $permissions),
|
2016-12-18 04:50:53 +05:30
|
|
|
]);
|
|
|
|
|
|
|
|
$response = json_decode($this->grabResponse(), true);
|
|
|
|
|
|
|
|
return $response['access_token'];
|
|
|
|
}
|
|
|
|
|
2016-02-23 03:19:46 +05:30
|
|
|
}
|