Skip to content

Commit

Permalink
Update PbsAuthManager for OAuth2Manager changes (Social Auth Issue #2…
Browse files Browse the repository at this point in the history
…982765).
  • Loading branch information
cdubz committed Feb 7, 2019
1 parent 8f33a65 commit 80baeaf
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
8 changes: 8 additions & 0 deletions social_auth_pbs.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@ services:
class: Drupal\social_auth_pbs\PbsAuthManager
arguments:
- '@config.factory'
- '@logger.factory'

# Logger.

logger.channel.social_auth_pbs:
class: Drupal\Core\Logger\LoggerChannel
factory: logger.factory:get
arguments: ['social_auth_pbs']
55 changes: 43 additions & 12 deletions src/PbsAuthManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Drupal\social_auth_pbs;

use Drupal\social_auth\AuthManager\OAuth2Manager;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\social_auth\AuthManager\OAuth2Manager;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;

/**
* Contains all the logic for PBS login integration.
Expand All @@ -15,24 +17,41 @@ class PbsAuthManager extends OAuth2Manager {
*
* @param \Drupal\Core\Config\ConfigFactory $configFactory
* Used for accessing configuration object factory.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* The logger factory.
*/
public function __construct(ConfigFactory $configFactory) {
parent::__construct($configFactory->get('social_auth_pbs.settings'));
public function __construct(ConfigFactory $configFactory,
LoggerChannelFactoryInterface $logger_factory) {
parent::__construct(
$configFactory->get('social_auth_pbs.settings'),
$logger_factory
);
}

/**
* {@inheritdoc}
*/
public function authenticate() {
$this->setAccessToken($this->client->getAccessToken('authorization_code',
['code' => $_GET['code']]));
try {
$this->setAccessToken($this->client->getAccessToken('authorization_code',
['code' => $_GET['code']]));
}
catch (IdentityProviderException $e) {
$this->loggerFactory->get('social_auth_pbs')
->error('There was an error during authentication. Exception: '
. $e->getMessage()
);
}
}

/**
* {@inheritdoc}
*/
public function getUserInfo() {
$this->user = $this->client->getResourceOwner($this->getAccessToken());
if (!$this->user) {
$this->user = $this->client->getResourceOwner($this->getAccessToken());
}

return $this->user;
}

Expand All @@ -51,18 +70,30 @@ public function getAuthorizationUrl() {
/**
* {@inheritdoc}
*/
public function requestEndPoint($path) {
$url = 'https://account.pbs.org' . $path;
public function requestEndPoint($method, $path, $domain = NULL, array $options = []) {
if (!$domain) {
$domain = 'https://account.pbs.org';
}

$url = $domain . $path;

$request = $this->client->getAuthenticatedRequest(
'GET',
$method,
$url,
$this->getAccessToken()
$this->getAccessToken(),
$options
);

$response = $this->client->getResponse($request);
try {
return $this->client->getParsedResponse($request);
}
catch (IdentityProviderException $e) {
$this->loggerFactory->get('social_auth_pbs')
->error('There was an error when requesting ' . $url . '. Exception: '
. $e->getMessage());
}

return $response->getBody()->getContents();
return NULL;
}

/**
Expand Down

0 comments on commit 80baeaf

Please sign in to comment.