summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKifah Meeran2018-02-25 15:24:00 (GMT)
committerKifah Meeran2018-02-25 15:24:00 (GMT)
commit07c418b042fb82e957adb833197b05086ebe09d8 (patch)
tree83f076ffab57baeebc72ae7480a0a54006e86d2b
parentb9bd7540377ecdaf5cd0442b6b46367ac4773768 (diff)
Issue #2947620: The Release 8.x-2.x contains only LICENSE.txt8.x-2.x-dev
-rw-r--r--README.txt98
-rw-r--r--composer.json26
-rw-r--r--config/schema/social_auth_digitalocean.schema.yml10
-rw-r--r--img/digitalocean_logo.svg33
-rw-r--r--social_auth_digitalocean.info.yml8
-rw-r--r--social_auth_digitalocean.install45
-rw-r--r--social_auth_digitalocean.links.task.yml4
-rw-r--r--social_auth_digitalocean.module27
-rw-r--r--social_auth_digitalocean.routing.yml25
-rw-r--r--social_auth_digitalocean.services.yml9
-rw-r--r--src/Controller/DigitaloceanAuthController.php187
-rw-r--r--src/DigitaloceanAuthManager.php160
-rw-r--r--src/Form/DigitaloceanAuthSettingsForm.php126
-rw-r--r--src/Plugin/Network/DigitaloceanAuth.php187
-rw-r--r--src/Plugin/Network/DigitaloceanAuthInterface.php10
-rw-r--r--src/Settings/DigitaloceanAuthSettings.php46
-rw-r--r--src/Settings/DigitaloceanAuthSettingsInterface.php26
17 files changed, 1027 insertions, 0 deletions
diff --git a/README.txt b/README.txt
new file mode 100644
index 0000000..a21da70
--- /dev/null
+++ b/README.txt
@@ -0,0 +1,98 @@
+SOCIAL AUTH GOOGLE MODULE
+
+CONTENTS OF THIS FILE
+---------------------
+
+ * Introduction
+ * Requirements
+ * Recommended modules
+ * Installation
+ * Configuration
+ * How it works
+ * Support requests
+
+INTRODUCTION
+------------
+
+Social Auth Digitalocean Module is a Digitalocean Authentication integration for Drupal.
+
+REQUIREMENTS
+------------
+
+This module requires the following modules:
+
+ * Social Auth (https://drupal.org/project/social_auth)
+ * Social API (https://drupal.org/project/social_api)
+
+
+RECOMMENDED MODULES
+-------------------
+
+ * Composer Manager (https://www.drupal.org/project/composer_manager):
+ This module will help to install the League PHP library and Digitalocean base library for the league,
+ which are the libraries required to make user authentication.
+
+INSTALLATION
+------------
+
+ * Download Digitalocean Base Library for TheLeague OAuth2 PHP library
+ (https://github.com/thephpleague/oauth2-digitalocean). We recommend to use
+ Composer Manager module to install the library.
+
+ * Install the dependencies: Social API and Social Auth.
+
+ * Install as you would normally install a contributed Drupal module. See:
+ https://drupal.org/documentation/install/modules-themes/modules-8
+ for further information.
+
+CONFIGURATION
+-------------
+
+ * Add your Digitalocean project OAuth information in
+ Configuration » User Authentication » Digitalocean.
+
+ * Place a Social Auth Digitalocean block in Structure » Block Layout.
+
+ * If you already have a Social Auth Login block in the site, rebuild the cache.
+
+
+HOW IT WORKS
+------------
+
+User can click on the Digitalocean logo on the Social Auth Login block
+You can also add a button or link anywhere on the site that points
+to /user/login/digitalocean, so theming and customizing the button or link
+is very flexible.
+
+When the user opens the /user/login/digitalocean link, it automatically takes
+user to Digitalocean Accounts for authentication. Digitalocean then returns the user to
+Drupal site. If we have an existing Drupal user with the same email address
+provided by Digitalocean, that user is logged in. Otherwise a new Drupal user is
+created.
+
+SUPPORT REQUESTS
+----------------
+
+Before posting a support request, carefully read the installation
+instructions provided in module documentation page.
+
+Before posting a support request, check Composer Manager status report at
+admin/reports/composer-manager. This status page will show the Digitalocean The League OAuth2 Base
+PHP library version if Drupal can detect it.
+
+Before posting a support request, check Recent log entries at
+admin/reports/dblog
+
+Once you have done this, you can post a support request at module issue queue:
+https://www.drupal.org/node/2841076
+
+When posting a support request, please inform what does the status report say
+at admin/reports/composer-manager and if you were able to see any errors in
+Recent log entries.
+
+MAINTAINERS
+-----------
+
+Current maintainers:
+ * Getulio Sánchez (gvso) - https://www.drupal.org/u/gvso
+ * Himanshu Dixit (himanshu-dixit) - https://www.drupal.org/u/himanshu-dixit
diff --git a/composer.json b/composer.json
new file mode 100644
index 0000000..f489b13
--- /dev/null
+++ b/composer.json
@@ -0,0 +1,26 @@
+{
+ "name": "drupal/social_auth_digitalocean",
+ "type": "drupal-module",
+ "description": "Social Auth integration for Digitalocean.",
+ "keywords": ["Drupal"],
+ "license": "GPL-2.0+",
+ "homepage": "https://www.drupal.org/project/social_auth_digitalocean",
+ "minimum-stability": "dev",
+ "support": {
+ "issues": "https://www.drupal.org/project/issues/social_auth_digitalocean",
+ "source": "http://cgit.drupalcode.org/social_auth_digitalocean"
+ },
+ "require": {
+ "chrishemmings/oauth2-digitalocean": "^2.0"
+ },
+ "authors": [
+ {
+ "name": "Getulio Valentin Sánchez (gvso)",
+ "email": "valentin2507@gmail.com"
+ },
+ {
+ "name": "Himanshu Dixit (himanshu-dixit)",
+ "email": "hudixt@gmail.com"
+ }
+ ]
+}
diff --git a/config/schema/social_auth_digitalocean.schema.yml b/config/schema/social_auth_digitalocean.schema.yml
new file mode 100644
index 0000000..fea19ce
--- /dev/null
+++ b/config/schema/social_auth_digitalocean.schema.yml
@@ -0,0 +1,10 @@
+social_auth_digitalocean.settings:
+ type: config_object
+ label: 'Social Auth Digitalocean settings'
+ mapping:
+ client_id:
+ type: string
+ label: 'Client ID'
+ client_secret:
+ type: string
+ label: 'Client Secret'
diff --git a/img/digitalocean_logo.svg b/img/digitalocean_logo.svg
new file mode 100644
index 0000000..9e94ed5
--- /dev/null
+++ b/img/digitalocean_logo.svg
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<svg
+ xmlns:dc="http://purl.org/dc/elements/1.1/"
+ xmlns:cc="http://creativecommons.org/ns#"
+ xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+ xmlns:svg="http://www.w3.org/2000/svg"
+ xmlns="http://www.w3.org/2000/svg"
+ viewBox="0 0 235.17332 235.30667"
+ height="235.30667"
+ width="235.17332"
+ xml:space="preserve"
+ id="svg2"
+ version="1.1"><metadata
+ id="metadata8"><rdf:RDF><cc:Work
+ rdf:about=""><dc:format>image/svg+xml</dc:format><dc:type
+ rdf:resource="http://purl.org/dc/dcmitype/StillImage" /></cc:Work></rdf:RDF></metadata><defs
+ id="defs6" /><g
+ transform="matrix(1.3333333,0,0,-1.3333333,0,235.30667)"
+ id="g10"><g
+ transform="scale(0.1)"
+ id="g12"><path
+ id="path14"
+ style="fill:#4b73c2;fill-opacity:1;fill-rule:nonzero;stroke:none"
+ d="m 882,0 v 342 c 362,0 643,359 504,740 -51,141 -164,254 -305,305 C 700,1525 341,1245 341,883 v 0 H 0 c 0,577 558,1027 1163,838 264,-83 475,-293 557,-557 C 1909,558 1460,0 882,0" /><path
+ id="path16"
+ style="fill:#4b73c2;fill-opacity:1;fill-rule:nonzero;stroke:none"
+ d="M 883,341 H 543 v 340 h 340 v 0 -340" /><path
+ id="path18"
+ style="fill:#4b73c2;fill-opacity:1;fill-rule:nonzero;stroke:none"
+ d="M 543,79 H 281 v 0 262 H 543 V 79" /><path
+ id="path20"
+ style="fill:#4b73c2;fill-opacity:1;fill-rule:nonzero;stroke:none"
+ d="M 281,341 H 62 v 0 219 h 219 v 0 -219" /></g></g></svg> \ No newline at end of file
diff --git a/social_auth_digitalocean.info.yml b/social_auth_digitalocean.info.yml
new file mode 100644
index 0000000..802ddf1
--- /dev/null
+++ b/social_auth_digitalocean.info.yml
@@ -0,0 +1,8 @@
+name: Social Auth Digitalocean
+type: module
+description: 'Social Auth integration for Digitalocean.'
+package: Social
+core: 8.x
+configure: social_auth_digitalocean.settings_form
+dependencies:
+ - social_auth
diff --git a/social_auth_digitalocean.install b/social_auth_digitalocean.install
new file mode 100644
index 0000000..9569bb9
--- /dev/null
+++ b/social_auth_digitalocean.install
@@ -0,0 +1,45 @@
+<?php
+
+/**
+ * @file
+ * Install, update, and uninstall functions for the Social Auth Digitalocean module.
+ */
+
+use Drupal\social_api\Utility\SocialApiImplementerInstaller;
+use Drupal\social_auth\Controller\SocialAuthController;
+
+/**
+ * Implements hook_requirements().
+ *
+ * Checks that a compatible version of oAuth2 Digitalocean library for the
+ * league OAuth2 client with Composer. Check installation instructions
+ * from the README.txt.
+ */
+function social_auth_digitalocean_requirements($phase) {
+
+ $requirements = [];
+
+ // Social API should be installed at this point in order to check library.
+ \Drupal::service('module_installer')->install(['social_api']);
+
+ if ($phase == 'install') {
+ $requirements = SocialApiImplementerInstaller::checkLibrary('social_auth_digitalocean', 'Social Auth Digitalocean', 'chrishemmings/oauth2-digitalocean', 2.0, 3.0);
+ }
+
+ return $requirements;
+
+}
+
+/**
+ * Implements hook_install().
+ */
+function social_auth_digitalocean_install() {
+ SocialAuthController::setLoginButtonSettings('social_auth_digitalocean', 'social_auth_digitalocean.redirect_to_digitalocean', 'img/digitalocean_logo.svg');
+}
+
+/**
+ * Implements hook_uninstall().
+ */
+function social_auth_digitalocean_uninstall() {
+ SocialAuthController::deleteLoginButtonSettings('social_auth_digitalocean');
+}
diff --git a/social_auth_digitalocean.links.task.yml b/social_auth_digitalocean.links.task.yml
new file mode 100644
index 0000000..b7e4259
--- /dev/null
+++ b/social_auth_digitalocean.links.task.yml
@@ -0,0 +1,4 @@
+social_auth_digitalocean.settings.tab:
+ title: 'Digitalocean'
+ route_name: social_auth_digitalocean.settings_form
+ base_route: social_auth.integrations
diff --git a/social_auth_digitalocean.module b/social_auth_digitalocean.module
new file mode 100644
index 0000000..7690d0f
--- /dev/null
+++ b/social_auth_digitalocean.module
@@ -0,0 +1,27 @@
+<?php
+
+/**
+ * @file
+ * Hook implementations for Social Auth Digitalocean module.
+ */
+
+use Drupal\Core\Routing\RouteMatchInterface;
+
+/**
+ * Implements hook_help().
+ */
+function social_auth_digitalocean_help($route_name, RouteMatchInterface $route_match) {
+ $output = '';
+
+ switch ($route_name) {
+ case 'simple_digitalocean_connect.settings_form':
+ $output = '<h3>' . t('Configuration instructions') . '</h3>';
+ $output .= '<p>';
+ $output .= t('Configuration instructions and other useful documentation can be found from <a href="@handbook-url">Social Auth Digitalocean Handbook</a>.', ['@handbook-url' => 'https://www.drupal.org/node/2764227']);
+ $output .= '</p>';
+ break;
+
+ }
+
+ return $output;
+}
diff --git a/social_auth_digitalocean.routing.yml b/social_auth_digitalocean.routing.yml
new file mode 100644
index 0000000..5273508
--- /dev/null
+++ b/social_auth_digitalocean.routing.yml
@@ -0,0 +1,25 @@
+social_auth_digitalocean.redirect_to_digitalocean:
+ path: 'user/login/digitalocean'
+ defaults:
+ _controller: '\Drupal\social_auth_digitalocean\Controller\DigitaloceanAuthController::redirectToDigitalocean'
+ requirements:
+ _role: 'anonymous'
+ options:
+ no_cache: TRUE
+
+social_auth_digitalocean.callback:
+ path: 'user/login/digitalocean/callback'
+ defaults:
+ _controller: '\Drupal\social_auth_digitalocean\Controller\DigitaloceanAuthController::callback'
+ requirements:
+ _role: 'anonymous'
+ options:
+ no_cache: TRUE
+
+social_auth_digitalocean.settings_form:
+ path: 'admin/config/social-api/social-auth/digitalocean'
+ defaults:
+ _title: 'Social Auth Digitalocean settings'
+ _form: '\Drupal\social_auth_digitalocean\Form\DigitaloceanAuthSettingsForm'
+ requirements:
+ _permission: 'administer social api authentication'
diff --git a/social_auth_digitalocean.services.yml b/social_auth_digitalocean.services.yml
new file mode 100644
index 0000000..398c418
--- /dev/null
+++ b/social_auth_digitalocean.services.yml
@@ -0,0 +1,9 @@
+services:
+ social_auth_digitalocean.manager:
+ class: Drupal\social_auth_digitalocean\DigitaloceanAuthManager
+ arguments:
+ - '@logger.factory'
+ - '@event_dispatcher'
+ - '@entity_field.manager'
+ - '@url_generator.non_bubbling'
+ - '@config.factory'
diff --git a/src/Controller/DigitaloceanAuthController.php b/src/Controller/DigitaloceanAuthController.php
new file mode 100644
index 0000000..672872c
--- /dev/null
+++ b/src/Controller/DigitaloceanAuthController.php
@@ -0,0 +1,187 @@
+<?php
+
+namespace Drupal\social_auth_digitalocean\Controller;
+
+use Drupal\Core\Controller\ControllerBase;
+use Drupal\social_api\Plugin\NetworkManager;
+use Drupal\social_auth\SocialAuthDataHandler;
+use Drupal\social_auth\SocialAuthUserManager;
+use Drupal\social_auth_digitalocean\DigitaloceanAuthManager;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+use Drupal\Core\Routing\TrustedRedirectResponse;
+use Symfony\Component\HttpFoundation\RequestStack;
+use Drupal\Core\Logger\LoggerChannelFactoryInterface;
+
+/**
+ * Returns responses for Simple Digitalocean Connect module routes.
+ */
+class DigitaloceanAuthController extends ControllerBase {
+
+ /**
+ * The network plugin manager.
+ *
+ * @var \Drupal\social_api\Plugin\NetworkManager
+ */
+ private $networkManager;
+
+ /**
+ * The user manager.
+ *
+ * @var \Drupal\social_auth\SocialAuthUserManager
+ */
+ private $userManager;
+
+ /**
+ * The digitalocean authentication manager.
+ *
+ * @var \Drupal\social_auth_digitalocean\DigitaloceanAuthManager
+ */
+ private $digitaloceanManager;
+
+ /**
+ * Used to access GET parameters.
+ *
+ * @var \Symfony\Component\HttpFoundation\RequestStack
+ */
+ private $request;
+
+ /**
+ * The Social Auth Data Handler.
+ *
+ * @var \Drupal\social_auth\SocialAuthDataHandler
+ */
+ private $dataHandler;
+
+
+ /**
+ * The logger channel.
+ *
+ * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
+ */
+ protected $loggerFactory;
+
+ /**
+ * DigitaloceanAuthController constructor.
+ *
+ * @param \Drupal\social_api\Plugin\NetworkManager $network_manager
+ * Used to get an instance of social_auth_digitalocean network plugin.
+ * @param \Drupal\social_auth\SocialAuthUserManager $user_manager
+ * Manages user login/registration.
+ * @param \Drupal\social_auth_digitalocean\DigitaloceanAuthManager $digitalocean_manager
+ * Used to manage authentication methods.
+ * @param \Symfony\Component\HttpFoundation\RequestStack $request
+ * Used to access GET parameters.
+ * @param \Drupal\social_auth\SocialAuthDataHandler $social_auth_data_handler
+ * SocialAuthDataHandler object.
+ * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
+ * Used for logging errors.
+ */
+ public function __construct(NetworkManager $network_manager, SocialAuthUserManager $user_manager, DigitaloceanAuthManager $digitalocean_manager, RequestStack $request, SocialAuthDataHandler $social_auth_data_handler, LoggerChannelFactoryInterface $logger_factory) {
+
+ $this->networkManager = $network_manager;
+ $this->userManager = $user_manager;
+ $this->digitaloceanManager = $digitalocean_manager;
+ $this->request = $request;
+ $this->dataHandler = $social_auth_data_handler;
+ $this->loggerFactory = $logger_factory;
+
+ // Sets the plugin id.
+ $this->userManager->setPluginId('social_auth_digitalocean');
+
+ // Sets the session keys to nullify if user could not logged in.
+ $this->userManager->setSessionKeysToNullify(['access_token', 'oauth2state']);
+ $this->setting = $this->config('social_auth_digitalocean.settings');
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function create(ContainerInterface $container) {
+ return new static(
+ $container->get('plugin.network.manager'),
+ $container->get('social_auth.user_manager'),
+ $container->get('social_auth_digitalocean.manager'),
+ $container->get('request_stack'),
+ $container->get('social_auth.social_auth_data_handler'),
+ $container->get('logger.factory')
+ );
+ }
+
+ /**
+ * Response for path 'user/login/digitalocean'.
+ *
+ * Redirects the user to Digitalocean for authentication.
+ */
+ public function redirectToDigitalocean() {
+ /* @var \League\OAuth2\Client\Provider\Digitalocean false $digitalocean */
+ $digitalocean = $this->networkManager->createInstance('social_auth_digitalocean')->getSdk();
+
+ // If digitalocean client could not be obtained.
+ if (!$digitalocean) {
+ drupal_set_message($this->t('Social Auth Digitalocean not configured properly. Contact site administrator.'), 'error');
+ return $this->redirect('user.login');
+ }
+
+ // Digitalocean service was returned, inject it to $digitaloceanManager.
+ $this->digitaloceanManager->setClient($digitalocean);
+
+ // Generates the URL where the user will be redirected for Digitalocean login.
+ // If the user did not have email permission granted on previous attempt,
+ // we use the re-request URL requesting only the email address.
+ $digitalocean_login_url = $this->digitaloceanManager->getDigitaloceanLoginUrl();
+
+ $state = $this->digitaloceanManager->getState();
+
+ $this->dataHandler->set('oauth2state', $state);
+
+ return new TrustedRedirectResponse($digitalocean_login_url);
+ }
+
+ /**
+ * Response for path 'user/login/digitalocean/callback'.
+ *
+ * Digitalocean returns the user here after user has authenticated in Digitalocean.
+ */
+ public function callback() {
+ // Checks if user cancel login via Digitalocean.
+ $error = $this->request->getCurrentRequest()->get('error');
+ if ($error == 'access_denied') {
+ drupal_set_message($this->t('You could not be authenticated.'), 'error');
+ return $this->redirect('user.login');
+ }
+
+ /* @var \League\OAuth2\Client\Provider\Digitalocean false $digitalocean */
+ $digitalocean = $this->networkManager->createInstance('social_auth_digitalocean')->getSdk();
+
+ // If Digitalocean client could not be obtained.
+ if (!$digitalocean) {
+ drupal_set_message($this->t('Social Auth Digitalocean not configured properly. Contact site administrator.'), 'error');
+ return $this->redirect('user.login');
+ }
+
+ $state = $this->dataHandler->get('oauth2state');
+
+ // Retreives $_GET['state'].
+ $retrievedState = $this->request->getCurrentRequest()->query->get('state');
+ if (empty($retrievedState) || ($retrievedState !== $state)) {
+ $this->userManager->nullifySessionKeys();
+ drupal_set_message($this->t('Digitalocean login failed. Unvalid oAuth2 State.'), 'error');
+ return $this->redirect('user.login');
+ }
+
+ // Saves access token to session.
+ $this->dataHandler->set('access_token', $this->digitaloceanManager->getAccessToken());
+
+ $this->digitaloceanManager->setClient($digitalocean)->authenticate();
+
+ // Gets user's info from Digitalocean API.
+ if (!$digitalocean_profile = $this->digitaloceanManager->getUserInfo()) {
+ drupal_set_message($this->t('Digitalocean login failed, could not load Digitalocean profile. Contact site administrator.'), 'error');
+ return $this->redirect('user.login');
+ }
+
+ // If user information could be retrieved.
+ return $this->userManager->authenticateUser('drupal_user', $digitalocean_profile->getEmail(), $digitalocean_profile->getId(), $this->digitaloceanManager->getAccessToken()->getToken(), '', ' ');
+ }
+
+}
diff --git a/src/DigitaloceanAuthManager.php b/src/DigitaloceanAuthManager.php
new file mode 100644
index 0000000..09b13f1
--- /dev/null
+++ b/src/DigitaloceanAuthManager.php
@@ -0,0 +1,160 @@
+<?php
+
+namespace Drupal\social_auth_digitalocean;
+
+use Drupal\social_auth\AuthManager\OAuth2Manager;
+use Drupal\Core\Config\ConfigFactory;
+use Drupal\Core\Logger\LoggerChannelFactoryInterface;
+use Drupal\Core\Entity\EntityFieldManagerInterface;
+use Drupal\Core\Routing\UrlGeneratorInterface;
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
+
+/**
+ * Contains all the logic for Digitalocean login integration.
+ */
+class DigitaloceanAuthManager extends OAuth2Manager {
+
+ /**
+ * The logger channel.
+ *
+ * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
+ */
+ protected $loggerFactory;
+
+ /**
+ * The event dispatcher.
+ *
+ * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
+ */
+ protected $eventDispatcher;
+
+ /**
+ * The entity field manager.
+ *
+ * @var \Drupal\Core\Entity\EntityFieldManagerInterface
+ */
+ protected $entityFieldManager;
+
+ /**
+ * The url generator.
+ *
+ * @var \Drupal\Core\Routing\UrlGeneratorInterface
+ */
+ protected $urlGenerator;
+
+ /**
+ * The Digitalocean client object.
+ *
+ * @var \League\OAuth2\Client\Provider\Digitalocean
+ */
+ protected $client;
+ /**
+ * The Digitalocean access token.
+ *
+ * @var \League\OAuth2\Client\Token\AccessToken
+ */
+ protected $token;
+
+ /**
+ * The Digitalocean user.
+ *
+ * @var \League\OAuth2\Client\Provider\DigitaloceanUser
+ */
+ protected $user;
+
+ /**
+ * The data point to be collected.
+ *
+ * @var string
+ */
+ protected $scopes;
+
+ /**
+ * Social Auth Digitalocean Settings.
+ *
+ * @var array
+ */
+ protected $settings;
+
+
+ /**
+ * Constructor.
+ *
+ * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
+ * Used for logging errors.
+ * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
+ * Used for dispatching events to other modules.
+ * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
+ * Used for accessing Drupal user picture preferences.
+ * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
+ * Used for generating absoulute URLs.
+ * @param \Drupal\Core\Config\ConfigFactory $configFactory
+ * Used for accessing configuration object factory.
+ */
+ public function __construct(LoggerChannelFactoryInterface $logger_factory, EventDispatcherInterface $event_dispatcher, EntityFieldManagerInterface $entity_field_manager, UrlGeneratorInterface $url_generator, ConfigFactory $configFactory) {
+ $this->loggerFactory = $logger_factory;
+ $this->eventDispatcher = $event_dispatcher;
+ $this->entityFieldManager = $entity_field_manager;
+ $this->urlGenerator = $url_generator;
+ $this->config = $configFactory->getEditable('social_auth_digitalocean.settings');
+ }
+
+ /**
+ * Authenticates the users by using the access token.
+ *
+ * @return $this
+ * The current object.
+ */
+ public function authenticate() {
+ $this->token = $this->client->getAccessToken('authorization_code',
+ ['code' => $_GET['code']]);
+ }
+
+ /**
+ * Gets the data by using the access token returned.
+ *
+ * @return League\OAuth2\Client\Provider\DigitaloceanUser
+ * User info returned by the Digitalocean.
+ */
+ public function getUserInfo() {
+ $this->user = $this->client->getResourceOwner($this->token);
+ return $this->user;
+ }
+
+ /**
+ * Returns token generated after authorization.
+ *
+ * @return string
+ * Used for making API calls.
+ */
+ public function getAccessToken() {
+ return $this->token;
+ }
+
+ /**
+ * Returns the Digitalocean login URL where user will be redirected.
+ *
+ * @return string
+ * Absolute Digitalocean login URL where user will be redirected
+ */
+ public function getDigitaloceanLoginUrl() {
+ $login_url = $this->client->getAuthorizationUrl();
+
+ // Generate and return the URL where we should redirect the user.
+ return $login_url;
+ }
+
+ /**
+ * Returns the Digitalocean login URL where user will be redirected.
+ *
+ * @return string
+ * Absolute Digitalocean login URL where user will be redirected
+ */
+ public function getState() {
+ $state = $this->client->getState();
+
+ // Generate and return the URL where we should redirect the user.
+ return $state;
+ }
+
+}
diff --git a/src/Form/DigitaloceanAuthSettingsForm.php b/src/Form/DigitaloceanAuthSettingsForm.php
new file mode 100644
index 0000000..0c8b148
--- /dev/null
+++ b/src/Form/DigitaloceanAuthSettingsForm.php
@@ -0,0 +1,126 @@
+<?php
+
+namespace Drupal\social_auth_digitalocean\Form;
+
+use Drupal\Core\Config\ConfigFactoryInterface;
+use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Path\PathValidatorInterface;
+use Drupal\Core\Routing\RequestContext;
+use Drupal\Core\Routing\RouteProviderInterface;
+use Drupal\social_auth\Form\SocialAuthSettingsForm;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+/**
+ * Settings form for Social Auth Digitalocean.
+ */
+class DigitaloceanAuthSettingsForm extends SocialAuthSettingsForm {
+
+ /**
+ * The request context.
+ *
+ * @var \Drupal\Core\Routing\RequestContext
+ */
+ protected $requestContext;
+
+ /**
+ * Constructor.
+ *
+ * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
+ * The configuration factory.
+ * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
+ * Used to check if route exists.
+ * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
+ * Used to check if path is valid and exists.
+ * @param \Drupal\Core\Routing\RequestContext $request_context
+ * Holds information about the current request.
+ */
+ public function __construct(ConfigFactoryInterface $config_factory, RouteProviderInterface $route_provider, PathValidatorInterface $path_validator, RequestContext $request_context) {
+ parent::__construct($config_factory, $route_provider, $path_validator);
+ $this->requestContext = $request_context;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function create(ContainerInterface $container) {
+ // Instantiates this class.
+ return new static(
+ // Load the services required to construct this class.
+ $container->get('config.factory'),
+ $container->get('router.route_provider'),
+ $container->get('path.validator'),
+ $container->get('router.request_context')
+ );
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getFormId() {
+ return 'social_auth_digitalocean_settings';
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function getEditableConfigNames() {
+ return array_merge(
+ parent::getEditableConfigNames(),
+ ['social_auth_digitalocean.settings']
+ );
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function buildForm(array $form, FormStateInterface $form_state) {
+ $config = $this->config('social_auth_digitalocean.settings');
+
+ $form['digitalocean_settings'] = [
+ '#type' => 'details',
+ '#title' => $this->t('Digitalocean Client settings'),
+ '#open' => TRUE,
+ '#description' => $this->t('You need to first create a Digitalocean App at <a href="@digitalocean-dev">@digitalocean-dev</a>', ['@digitalocean-dev' => 'https://cloud.digitalocean.com/settings/api/applications']),
+ ];
+
+ $form['digitalocean_settings']['client_id'] = [
+ '#type' => 'textfield',
+ '#required' => TRUE,
+ '#title' => $this->t('Client ID'),
+ '#default_value' => $config->get('client_id'),
+ '#description' => $this->t('Copy the Client ID here.'),
+ ];
+
+ $form['digitalocean_settings']['client_secret'] = [
+ '#type' => 'textfield',
+ '#required' => TRUE,
+ '#title' => $this->t('Client Secret'),
+ '#default_value' => $config->get('client_secret'),
+ '#description' => $this->t('Copy the Client Secret here.'),
+ ];
+
+ $form['digitalocean_settings']['authorized_redirect_url'] = [
+ '#type' => 'textfield',
+ '#disabled' => TRUE,
+ '#title' => $this->t('Application Callback URL'),
+ '#description' => $this->t('Copy this value to <em>Application Callback URL</em> field of your Digitalocean App settings.'),
+ '#default_value' => $GLOBALS['base_url'] . '/user/login/digitalocean/callback',
+ ];
+
+ return parent::buildForm($form, $form_state);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function submitForm(array &$form, FormStateInterface $form_state) {
+ $values = $form_state->getValues();
+ $this->config('social_auth_digitalocean.settings')
+ ->set('client_id', $values['client_id'])
+ ->set('client_secret', $values['client_secret'])
+ ->save();
+
+ parent::submitForm($form, $form_state);
+ }
+
+}
diff --git a/src/Plugin/Network/DigitaloceanAuth.php b/src/Plugin/Network/DigitaloceanAuth.php
new file mode 100644
index 0000000..2003f4c
--- /dev/null
+++ b/src/Plugin/Network/DigitaloceanAuth.php
@@ -0,0 +1,187 @@
+<?php
+
+namespace Drupal\social_auth_digitalocean\Plugin\Network;
+
+use Drupal\Core\Config\ConfigFactoryInterface;
+use Drupal\Core\Entity\EntityTypeManagerInterface;
+use Drupal\Core\Logger\LoggerChannelFactoryInterface;
+use Drupal\Core\Routing\RequestContext;
+use Drupal\social_auth\SocialAuthDataHandler;
+use Drupal\social_api\Plugin\NetworkBase;
+use Drupal\social_api\SocialApiException;
+use Drupal\social_auth_digitalocean\Settings\DigitaloceanAuthSettings;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+use League\OAuth2\Client\Provider\Digitalocean;
+use Drupal\Core\Site\Settings;
+
+/**
+ * Defines a Network Plugin for Social Auth Digitalocean.
+ *
+ * @package Drupal\simple_digitalocean_connect\Plugin\Network
+ *
+ * @Network(
+ * id = "social_auth_digitalocean",
+ * social_network = "Digitalocean",
+ * type = "social_auth",
+ * handlers = {
+ * "settings": {
+ * "class": "\Drupal\social_auth_digitalocean\Settings\DigitaloceanAuthSettings",
+ * "config_id": "social_auth_digitalocean.settings"
+ * }
+ * }
+ * )
+ */
+class DigitaloceanAuth extends NetworkBase implements DigitaloceanAuthInterface {
+
+ /**
+ * The Social Auth Data Handler.
+ *
+ * @var \Drupal\social_auth\SocialAuthDataHandler
+ */
+ protected $dataHandler;
+
+ /**
+ * The logger factory.
+ *
+ * @var \Drupal\Core\Logger\LoggerChannelFactory
+ */
+ protected $loggerFactory;
+
+ /**
+ * The request context object.
+ *
+ * @var \Drupal\Core\Routing\RequestContext
+ */
+ protected $requestContext;
+
+ /**
+ * The site settings.
+ *
+ * @var \Drupal\Core\Site\Settings
+ */
+ protected $siteSettings;
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
+ return new static(
+ $container->get('social_auth.social_auth_data_handler'),
+ $configuration,
+ $plugin_id,
+ $plugin_definition,
+ $container->get('entity_type.manager'),
+ $container->get('config.factory'),
+ $container->get('logger.factory'),
+ $container->get('router.request_context'),
+ $container->get('settings')
+ );
+ }
+
+ /**
+ * DigitaloceanAuth constructor.
+ *
+ * @param \Drupal\social_auth\SocialAuthDataHandler $data_handler
+ * The data handler.
+ * @param array $configuration
+ * A configuration array containing information about the plugin instance.
+ * @param string $plugin_id
+ * The plugin_id for the plugin instance.
+ * @param array $plugin_definition
+ * The plugin implementation definition.
+ * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
+ * The entity type manager.
+ * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
+ * The configuration factory object.
+ * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
+ * The logger factory.
+ * @param \Drupal\Core\Routing\RequestContext $requestContext
+ * The Request Context Object.
+ * @param \Drupal\Core\Site\Settings $settings
+ * The settings factory.
+ */
+ public function __construct(SocialAuthDataHandler $data_handler,
+ array $configuration,
+ $plugin_id,
+ array $plugin_definition,
+ EntityTypeManagerInterface $entity_type_manager,
+ ConfigFactoryInterface $config_factory,
+ LoggerChannelFactoryInterface $logger_factory,
+ RequestContext $requestContext,
+ Settings $settings
+ ) {
+
+ parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $config_factory);
+
+ $this->dataHandler = $data_handler;
+ $this->loggerFactory = $logger_factory;
+ $this->requestContext = $requestContext;
+ $this->siteSettings = $settings;
+ }
+
+ /**
+ * Sets the underlying SDK library.
+ *
+ * @return \League\OAuth2\Client\Provider\Digitalocean
+ * The initialized 3rd party library instance.
+ *
+ * @throws SocialApiException
+ * If the SDK library does not exist.
+ */
+ protected function initSdk() {
+
+ $class_name = 'ChrisHemmings\OAuth2\Client\Provider\DigitalOcean';
+ if (!class_exists($class_name)) {
+ throw new SocialApiException(sprintf('The Digitalocean Library for the league oAuth not found. Class: %s.', $class_name));
+ }
+ /* @var \Drupal\social_auth_digitalocean\Settings\DigitaloceanAuthSettings $settings */
+ $settings = $this->settings;
+ // Proxy configuration data for outward proxy.
+ $proxyUrl = $this->siteSettings->get("http_client_config")["proxy"]["http"];
+ if ($this->validateConfig($settings)) {
+ // All these settings are mandatory.
+ if ($proxyUrl) {
+ $league_settings = [
+ 'clientId' => $settings->getClientId(),
+ 'clientSecret' => $settings->getClientSecret(),
+ 'redirectUri' => $this->requestContext->getCompleteBaseUrl() . '/user/login/digitalocean/callback',
+ 'proxy' => $proxyUrl,
+ ];
+ }
+ else {
+ $league_settings = [
+ 'clientId' => $settings->getClientId(),
+ 'clientSecret' => $settings->getClientSecret(),
+ 'redirectUri' => $this->requestContext->getCompleteBaseUrl() . '/user/login/digitalocean/callback',
+ ];
+ }
+
+ return new \ChrisHemmings\OAuth2\Client\Provider\DigitalOcean($league_settings);
+ }
+ return FALSE;
+ }
+
+ /**
+ * Checks that module is configured.
+ *
+ * @param \Drupal\social_auth_digitalocean\Settings\DigitaloceanAuthSettings $settings
+ * The Digitalocean auth settings.
+ *
+ * @return bool
+ * True if module is configured.
+ * False otherwise.
+ */
+ protected function validateConfig(DigitaloceanAuthSettings $settings) {
+ $client_id = $settings->getClientId();
+ $client_secret = $settings->getClientSecret();
+ if (!$client_id || !$client_secret) {
+ $this->loggerFactory
+ ->get('social_auth_digitalocean')
+ ->error('Define Client ID and Client Secret on module settings.');
+ return FALSE;
+ }
+
+ return TRUE;
+ }
+
+}
diff --git a/src/Plugin/Network/DigitaloceanAuthInterface.php b/src/Plugin/Network/DigitaloceanAuthInterface.php
new file mode 100644
index 0000000..7eae68d
--- /dev/null
+++ b/src/Plugin/Network/DigitaloceanAuthInterface.php
@@ -0,0 +1,10 @@
+<?php
+
+namespace Drupal\social_auth_digitalocean\Plugin\Network;
+
+use Drupal\social_api\Plugin\NetworkInterface;
+
+/**
+ * Defines the Digitalocean Auth interface.
+ */
+interface DigitaloceanAuthInterface extends NetworkInterface {}
diff --git a/src/Settings/DigitaloceanAuthSettings.php b/src/Settings/DigitaloceanAuthSettings.php
new file mode 100644
index 0000000..7bb60de
--- /dev/null
+++ b/src/Settings/DigitaloceanAuthSettings.php
@@ -0,0 +1,46 @@
+<?php
+
+namespace Drupal\social_auth_digitalocean\Settings;
+
+use Drupal\social_api\Settings\SettingsBase;
+
+/**
+ * Defines methods to get Social Auth Digitalocean settings.
+ */
+class DigitaloceanAuthSettings extends SettingsBase implements DigitaloceanAuthSettingsInterface {
+
+ /**
+ * Client ID.
+ *
+ * @var string
+ */
+ protected $clientId;
+
+ /**
+ * Client secret.
+ *
+ * @var string
+ */
+ protected $clientSecret;
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getClientId() {
+ if (!$this->clientId) {
+ $this->clientId = $this->config->get('client_id');
+ }
+ return $this->clientId;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getClientSecret() {
+ if (!$this->clientSecret) {
+ $this->clientSecret = $this->config->get('client_secret');
+ }
+ return $this->clientSecret;
+ }
+
+}
diff --git a/src/Settings/DigitaloceanAuthSettingsInterface.php b/src/Settings/DigitaloceanAuthSettingsInterface.php
new file mode 100644
index 0000000..a01316e
--- /dev/null
+++ b/src/Settings/DigitaloceanAuthSettingsInterface.php
@@ -0,0 +1,26 @@
+<?php
+
+namespace Drupal\social_auth_digitalocean\Settings;
+
+/**
+ * Defines an interface for Social Auth Digitalocean settings.
+ */
+interface DigitaloceanAuthSettingsInterface {
+
+ /**
+ * Gets the client ID.
+ *
+ * @return string
+ * The client ID.
+ */
+ public function getClientId();
+
+ /**
+ * Gets the client secret.
+ *
+ * @return string
+ * The client secret.
+ */
+ public function getClientSecret();
+
+}