diff --git a/composer.json b/composer.json index 1ee4e57..51d476e 100644 --- a/composer.json +++ b/composer.json @@ -6,6 +6,9 @@ "conflict": { "drupal/jsonapi": "1.*" }, + "require": { + "league/oauth2-client": "^2.4" + }, "require-dev": { "drupal/block_field": "~1.0", "drupal/dynamic_entity_reference": "~2.0", @@ -14,7 +17,6 @@ "drupal/metatag": "~1.0", "drupal/paragraphs": "~1.0", "drupal/pathauto": "~1.0", - "fzaninotto/faker": "^1.7", - "league/oauth2-client": "^2.4" + "fzaninotto/faker": "^1.7" } } diff --git a/modules/entity_share_client/config/schema/remote.schema.yml b/modules/entity_share_client/config/schema/remote.schema.yml index f397bdf..5935973 100644 --- a/modules/entity_share_client/config/schema/remote.schema.yml +++ b/modules/entity_share_client/config/schema/remote.schema.yml @@ -20,9 +20,6 @@ entity_share_client.remote.*: uuid: label: 'UUID' type: string - verified: - label: 'Verified' - type: boolean data: type: mapping label: 'Credential data' diff --git a/modules/entity_share_client/src/Annotation/ClientAuthorization.php b/modules/entity_share_client/src/Annotation/ClientAuthorization.php index 8a8a5b2..e202771 100644 --- a/modules/entity_share_client/src/Annotation/ClientAuthorization.php +++ b/modules/entity_share_client/src/Annotation/ClientAuthorization.php @@ -1,5 +1,7 @@ httpClientFactory->fromOptions([ + 'base_uri' => $url . '/', + 'cookies' => TRUE, + 'allow_redirects' => TRUE, + ]); + } + + /** + * {@inheritdoc} + */ + public function getJsonApiClient($url) { + return $this->httpClientFactory->fromOptions([ + 'base_uri' => $url . '/', + 'headers' => [ + 'Content-type' => 'application/vnd.api+json', + ], + ]); + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + return $form + [ + 'credential_provider' => [], + 'entity_share' => [], + ]; + } + + /** + * {@inheritdoc} + */ + public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {} + +} diff --git a/modules/entity_share_client/src/Plugin/ClientAuthorization/Oauth.php b/modules/entity_share_client/src/Plugin/ClientAuthorization/Oauth.php index 019189e..4b5a536 100644 --- a/modules/entity_share_client/src/Plugin/ClientAuthorization/Oauth.php +++ b/modules/entity_share_client/src/Plugin/ClientAuthorization/Oauth.php @@ -110,13 +110,13 @@ class Oauth extends ClientAuthorizationBase { '%error' => $e->getMessage() ] ); - throw $e; + return ''; } return $newAccessToken->getToken(); } return $accessToken->getToken(); } - throw new \UnexpectedValueException('Access Token object not found in storage'); + return ''; } /** diff --git a/modules/entity_share_client/src/Plugin/ClientAuthorizationBase.php b/modules/entity_share_client/src/Plugin/ClientAuthorizationBase.php index 3e51127..8e69cae 100644 --- a/modules/entity_share_client/src/Plugin/ClientAuthorizationBase.php +++ b/modules/entity_share_client/src/Plugin/ClientAuthorizationBase.php @@ -91,7 +91,6 @@ abstract class ClientAuthorizationBase extends PluginBase implements ClientAutho public function defaultConfiguration() { return [ 'uuid' => $this->uuid->generate(), - 'verified' => FALSE, 'data' => [], ]; } diff --git a/modules/entity_share_client/src/Service/RemoteManager.php b/modules/entity_share_client/src/Service/RemoteManager.php index 2c913e0..aee270a 100644 --- a/modules/entity_share_client/src/Service/RemoteManager.php +++ b/modules/entity_share_client/src/Service/RemoteManager.php @@ -126,7 +126,13 @@ class RemoteManager implements RemoteManagerInterface { } /** - * {@inheritdoc} + * Prepares a client object from the auth plugin. + * + * @param \Drupal\entity_share_client\Entity\RemoteInterface $remote + * The remote website on which to perform the request. + * + * @return \GuzzleHttp\Client + * The configured client. */ protected function getHttpClient(RemoteInterface $remote) { $remote_id = $remote->id(); @@ -138,7 +144,13 @@ class RemoteManager implements RemoteManagerInterface { } /** - * {@inheritdoc} + * Prepares a client object from the auth plugin. + * + * @param \Drupal\entity_share_client\Entity\RemoteInterface $remote + * The remote website on which to perform the request. + * + * @return \GuzzleHttp\Client + * The configured client. */ protected function getJsonApiHttpClient(RemoteInterface $remote) { $remote_id = $remote->id(); diff --git a/modules/entity_share_client/tests/src/Functional/AuthenticationImportTest.php b/modules/entity_share_client/tests/src/Functional/AuthenticationImportTest.php index ff58663..005c4b0 100644 --- a/modules/entity_share_client/tests/src/Functional/AuthenticationImportTest.php +++ b/modules/entity_share_client/tests/src/Functional/AuthenticationImportTest.php @@ -130,8 +130,14 @@ class AuthenticationImportTest extends EntityShareClientFunctionalTestBase { // Now we alter the remote by removing the basic auth, thus we simulate // being an anonymous user. - $this->remote->set('basic_auth_username', ''); - $this->remote->set('basic_auth_password', ''); + $plugin = $this->authPluginManager->createInstance('anonymous'); + $configuration = $plugin->getConfiguration(); + $configuration['data'] = [ + 'credential_provider' => 'entity_share', + 'storage_key' => $configuration['uuid'], + ]; + $plugin->setConfiguration($configuration); + $this->remote->mergePluginConfig($plugin); $this->remote->save(); // Since the remote ID remains the same, we need to reset some of // remote manager's cached values. diff --git a/modules/entity_share_client/tests/src/Kernel/RemoteUrlTest.php b/modules/entity_share_client/tests/src/Kernel/RemoteUrlTest.php index 0204b3d..64a5f8a 100755 --- a/modules/entity_share_client/tests/src/Kernel/RemoteUrlTest.php +++ b/modules/entity_share_client/tests/src/Kernel/RemoteUrlTest.php @@ -14,6 +14,13 @@ use Drupal\KernelTests\Core\Entity\EntityKernelTestBase; */ class RemoteUrlTest extends EntityKernelTestBase { + /** + * Injected plugin service. + * + * @var \Drupal\entity_share_client\Plugin\ClientAuthorizationManager + */ + protected $authPluginManager; + /** * {@inheritdoc} */ @@ -28,6 +35,7 @@ class RemoteUrlTest extends EntityKernelTestBase { */ protected function setUp() { parent::setUp(); + $this->authPluginManager = $this->container->get('plugin.manager.entity_share_auth'); $this->installEntitySchema('remote'); } @@ -36,14 +44,26 @@ class RemoteUrlTest extends EntityKernelTestBase { */ public function testRemotePreSave() { $remote_storage = $this->entityTypeManager->getStorage('remote'); + $plugin = $this->authPluginManager->createInstance('basic_auth'); + $configuration = $plugin->getConfiguration(); $remote = $remote_storage->create([ 'id' => $this->randomMachineName(), 'label' => $this->randomString(), 'url' => 'http://example.com', - 'basic_auth_username' => 'test', - 'basic_auth_password' => 'test', ]); + $credentials = []; + $credentials['username'] = 'test'; + $credentials['password'] = 'test'; + $this->state->set($configuration['uuid'], $credentials); + $key = $configuration['uuid']; + $configuration['data'] = [ + 'credential_provider' => 'entity_share', + 'storage_key' => $key, + ]; + $plugin->setConfiguration($configuration); + $remote->mergePluginConfig($plugin); $remote->save(); + $this->assertEqual($remote->get('url'), 'http://example.com'); $remote->set('url', 'http://example.com/');