View source
<?php
namespace Drupal\media_acquiadam;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Access\CsrfTokenGenerator;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Url;
use GuzzleHttp\ClientInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Oauth implements OauthInterface, ContainerInjectionInterface {
protected $damApiBase = "https://apiv2.webdamdb.com";
protected $config;
protected $csrfTokenGenerator;
protected $urlGenerator;
protected $httpClient;
protected $authFinishRedirect;
protected $loggerChannel;
protected $currentUser;
public function __construct(ConfigFactoryInterface $config_factory, CsrfTokenGenerator $csrfTokenGenerator, UrlGeneratorInterface $urlGenerator, ClientInterface $httpClient, LoggerChannelFactoryInterface $loggerChannelFactory, AccountProxyInterface $account) {
$this->config = $config_factory
->get('media_acquiadam.settings');
$this->csrfTokenGenerator = $csrfTokenGenerator;
$this->urlGenerator = $urlGenerator;
$this->httpClient = $httpClient;
$this->loggerChannel = $loggerChannelFactory
->get('media_acquiadam');
$this->currentUser = $account;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('csrf_token'), $container
->get('url_generator.non_bubbling'), $container
->get('http_client'), $container
->get('logger.factory'), $container
->get('current_user'));
}
public function authRequestStateIsValid($token) {
return $this->csrfTokenGenerator
->validate($token, 'media_acquiadam.oauth');
}
public function getAccessToken($auth_code) {
$this->loggerChannel
->debug('Getting new access token for @username.', [
'@username' => $this->currentUser
->getAccountName(),
]);
$response = $this->httpClient
->post("{$this->damApiBase}/oauth2/token", [
'form_params' => [
'grant_type' => 'authorization_code',
'code' => $auth_code,
'redirect_uri' => $this->urlGenerator
->generateFromRoute('media_acquiadam.auth_finish', [
'auth_finish_redirect' => $this->authFinishRedirect,
], [
'absolute' => TRUE,
]),
'client_id' => $this->config
->get('client_id'),
'client_secret' => $this->config
->get('secret'),
],
]);
$body = (string) $response
->getBody();
$body = json_decode($body);
return [
'access_token' => $body->access_token,
'expire_time' => time() + $body->expires_in,
'refresh_token' => $body->refresh_token,
];
}
public function getAuthLink() {
$client_id = $this->config
->get('client_id');
$token = $this->csrfTokenGenerator
->get('media_acquiadam.oauth');
$redirect_uri = $this->urlGenerator
->generateFromRoute('media_acquiadam.auth_finish', [
'auth_finish_redirect' => $this->authFinishRedirect,
], [
'absolute' => TRUE,
]);
return "{$this->damApiBase}/oauth2/authorize?response_type=code&state={$token}&redirect_uri={$redirect_uri}&client_id={$client_id}";
}
public function refreshAccess($refresh_token) {
$this->loggerChannel
->debug('Refreshing access token for @username.', [
'@username' => $this->currentUser
->getAccountName(),
]);
$response = $this->httpClient
->post("{$this->damApiBase}/oauth2/token", [
'form_params' => [
'grant_type' => 'refresh_token',
'refresh_token' => $refresh_token,
'client_id' => $this->config
->get('client_id'),
'client_secret' => $this->config
->get('secret'),
'redirect_uri' => $this->urlGenerator
->generateFromRoute('media_acquiadam.auth_finish', [
'auth_finish_redirect' => $this->authFinishRedirect,
], [
'absolute' => TRUE,
]),
],
]);
$body = (string) $response
->getBody();
$body = json_decode($body);
return [
'access_token' => $body->access_token,
'expire_time' => time() + $body->expires_in,
'refresh_token' => $body->refresh_token,
];
}
public function getAuthFinishRedirect() {
if (isset($this->authFinishRedirect)) {
return $this->authFinishRedirect;
}
else {
return NULL;
}
}
public function setAuthFinishRedirect($authFinishRedirect) {
$parsed_url = UrlHelper::parse($authFinishRedirect);
$filterable_keys = $this->config
->get('oauth.excluded_redirect_keys');
if (empty($filterable_keys) || !is_array($filterable_keys)) {
$filterable_keys = [
'original_path',
];
}
$this->authFinishRedirect = Url::fromUri('base:' . $parsed_url['path'], [
'query' => UrlHelper::filterQueryParameters($parsed_url['query'], $filterable_keys),
'fragment' => $parsed_url['fragment'],
])
->toString();
}
}