View source
<?php
namespace Drupal\Tests\farm_api\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Url;
use Drupal\user\Entity\Role;
class ConsumerConfigTest extends OauthTestBase {
protected $tokenDebugUrl;
protected function setUp() : void {
parent::setUp();
$this->tokenDebugUrl = Url::fromRoute('oauth2_token.user_debug');
$this->additionalRoles = [];
for ($i = 0; $i < 4; $i++) {
$role = Role::create([
'id' => 'scope_' . $i,
'label' => 'Scope: ' . $i,
'is_admin' => FALSE,
]);
$role
->save();
$this->additionalRoles[] = $role;
}
}
public function testGrantUserAccess() {
$this->client
->set('grant_user_access', FALSE);
$this->client
->set('limit_requested_access', FALSE);
$this->client
->set('limit_user_access', FALSE);
$this->client
->save();
$this->user
->addRole('scope_1');
$this->user
->addRole('scope_2');
$this->user
->save();
$expected_roles = array_merge($this
->getClientRoleIds(), [
'authenticated',
]);
$access_token = $this
->getAccessToken();
$token_info = $this
->getTokenInfo($access_token);
$this
->assertEquals($this->user
->id(), $token_info['id']);
$this
->assertEqualsCanonicalizing($expected_roles, $token_info['roles']);
$this->client
->set('grant_user_access', TRUE);
$this->client
->save();
$expected_roles = array_merge($expected_roles, [
'scope_1',
'scope_2',
]);
$access_token = $this
->getAccessToken();
$token_info = $this
->getTokenInfo($access_token);
$this
->assertEquals($this->user
->id(), $token_info['id']);
$this
->assertEqualsCanonicalizing($expected_roles, $token_info['roles']);
$access_token = $this
->getAccessToken([
'scope_3',
]);
$token_info = $this
->getTokenInfo($access_token);
$this
->assertEquals($this->user
->id(), $token_info['id']);
$this
->assertEqualsCanonicalizing($expected_roles, $token_info['roles']);
}
public function testLimitRequestedAccess() {
$this->client
->set('grant_user_access', FALSE);
$this->client
->set('limit_requested_access', FALSE);
$this->client
->set('limit_user_access', FALSE);
$this->client
->save();
$this->user
->addRole('scope_1');
$this->user
->addRole('scope_2');
$this->user
->save();
$client_roles = array_merge($this
->getClientRoleIds(), [
'scope_3',
]);
$this
->grantClientRoles($client_roles);
$expected_roles = array_merge($client_roles, [
'authenticated',
]);
$access_token = $this
->getAccessToken();
$token_info = $this
->getTokenInfo($access_token);
$this
->assertEquals($this->user
->id(), $token_info['id']);
$this
->assertEqualsCanonicalizing($expected_roles, $token_info['roles']);
$this->client
->set('limit_requested_access', TRUE);
$this->client
->save();
$requested_roles = [
'scope_3',
];
$expected_roles = array_merge($requested_roles, [
'authenticated',
]);
$access_token = $this
->getAccessToken($requested_roles);
$token_info = $this
->getTokenInfo($access_token);
$this
->assertEquals($this->user
->id(), $token_info['id']);
$this
->assertEqualsCanonicalizing($expected_roles, $token_info['roles']);
$this->client
->set('limit_requested_access', TRUE);
$this->client
->set('grant_user_access', TRUE);
$this->client
->save();
$requested_roles = [
'scope_1',
'scope_3',
];
$expected_roles = array_merge($requested_roles, [
'authenticated',
]);
$access_token = $this
->getAccessToken($requested_roles);
$token_info = $this
->getTokenInfo($access_token);
$this
->assertEquals($this->user
->id(), $token_info['id']);
$this
->assertEqualsCanonicalizing($expected_roles, $token_info['roles']);
}
public function testLimitUserAccess() {
$this->client
->set('grant_user_access', FALSE);
$this->client
->set('limit_requested_access', FALSE);
$this->client
->set('limit_user_access', FALSE);
$this->client
->save();
$this->user
->addRole('scope_1');
$this->user
->save();
$client_roles = array_merge($this
->getClientRoleIds(), [
'scope_1',
'scope_2',
'scope_3',
]);
$this
->grantClientRoles($client_roles);
$expected_roles = array_merge($client_roles, [
'authenticated',
]);
$access_token = $this
->getAccessToken();
$token_info = $this
->getTokenInfo($access_token);
$this
->assertEquals($this->user
->id(), $token_info['id']);
$this
->assertEqualsCanonicalizing($expected_roles, $token_info['roles']);
$this->client
->set('limit_user_access', TRUE);
$this->client
->save();
$requested_roles = [
'scope_1',
'scope_3',
];
$expected_roles = [
'scope_1',
'authenticated',
];
$access_token = $this
->getAccessToken($requested_roles);
$token_info = $this
->getTokenInfo($access_token);
$this
->assertEquals($this->user
->id(), $token_info['id']);
$this
->assertEqualsCanonicalizing($expected_roles, $token_info['roles']);
$this->client
->set('grant_user_access', TRUE);
$this->client
->set('limit_user_access', TRUE);
$this->client
->save();
$requested_roles = [];
$expected_roles = [
'scope_1',
'authenticated',
];
$access_token = $this
->getAccessToken($requested_roles);
$token_info = $this
->getTokenInfo($access_token);
$this
->assertEquals($this->user
->id(), $token_info['id']);
$this
->assertEqualsCanonicalizing($expected_roles, $token_info['roles']);
}
protected function getTokenInfo($access_token) {
$response = $this
->get($this->tokenDebugUrl, [
'query' => [
'_format' => 'json',
],
'headers' => [
'Authorization' => 'Bearer ' . $access_token,
],
]);
return Json::decode((string) $response
->getBody());
}
protected function getClientRoleIds() {
return array_map(function ($role) {
return $role['target_id'];
}, $this->client
->get('roles')
->getValue());
}
protected function grantClientRoles(array $role_ids) {
$roles = [];
foreach ($role_ids as $id) {
$roles[] = [
'target_id' => $id,
];
}
$this->client
->set('roles', $roles);
$this->client
->save();
}
protected function getAccessToken(array $scopes = []) {
$valid_payload = [
'grant_type' => 'password',
'client_id' => $this->client
->get('client_id')->value,
'username' => $this->user
->getAccountName(),
'password' => $this->user->pass_raw,
];
if (!empty($scopes)) {
$valid_payload['scope'] = implode(' ', $scopes);
}
$response = $this
->post($this->url, $valid_payload);
$parsed_response = Json::decode((string) $response
->getBody());
return isset($parsed_response['access_token']) ? $parsed_response['access_token'] : NULL;
}
}