Oauth2ClientService.php in OAuth2 Client 8.2
File
src/Service/Oauth2ClientService.php
View source
<?php
namespace Drupal\oauth2_client\Service;
use Drupal\Core\State\StateInterface;
use Drupal\oauth2_client\PluginManager\Oauth2ClientPluginManagerInterface;
use Drupal\oauth2_client\Service\Grant\Oauth2ClientGrantServiceInterface;
class Oauth2ClientService extends Oauth2ClientServiceBase {
protected $oauth2ClientPluginManager;
protected $state;
protected $grantServices = [];
public function __construct(Oauth2ClientPluginManagerInterface $oauth2ClientPluginManager, StateInterface $state, Oauth2ClientGrantServiceInterface $authorizationCodeGrantService, Oauth2ClientGrantServiceInterface $clientCredentialsGrantService, Oauth2ClientGrantServiceInterface $refreshTokenGrantService, Oauth2ClientGrantServiceInterface $resourceOwnersCredentialsGrantService) {
$this->oauth2ClientPluginManager = $oauth2ClientPluginManager;
$this->state = $state;
$this->grantServices = [
'authorization_code' => $authorizationCodeGrantService,
'client_credentials' => $clientCredentialsGrantService,
'refresh_token' => $refreshTokenGrantService,
'resource_owner' => $resourceOwnersCredentialsGrantService,
];
}
public function getAccessToken($clientId) {
$access_token = $this
->retrieveAccessToken($clientId);
if (!$access_token || $access_token
->getExpires() && $access_token
->hasExpired()) {
$client = $this
->getClient($clientId);
switch ($client
->getGrantType()) {
case 'authorization_code':
$access_token = $this
->getAuthorizationCodeAccessToken($clientId);
break;
case 'client_credentials':
$access_token = $this
->getClientCredentialsAccessToken($clientId);
break;
case 'resource_owner':
$access_token = $this
->getResourceOwnersCredentialsAccessToken($clientId);
break;
}
}
return $access_token;
}
public function getProvider($clientId) {
$client = $this
->getClient($clientId);
switch ($client
->getGrantType()) {
case 'client_credentials':
$provider = $this
->getClientCredentialsProvider($clientId);
break;
case 'resource_owner':
$provider = $this
->getResourceOwnersCredentialsProvider($clientId);
break;
case 'authorization_code':
default:
$provider = $this
->getAuthorizationCodeProvider($clientId);
break;
}
return $provider;
}
private function getAuthorizationCodeAccessToken($clientId) {
$stored_token = $this
->retrieveAccessToken($clientId);
if ($stored_token) {
if ($stored_token
->getExpires() && $stored_token
->hasExpired()) {
if (empty($stored_token
->getRefreshToken())) {
$access_token = NULL;
}
else {
$access_token = $this->grantServices['refresh_token']
->getAccessToken($clientId);
}
}
else {
$access_token = $stored_token;
}
}
if (empty($access_token)) {
$access_token = $this->grantServices['authorization_code']
->getAccessToken($clientId);
}
return $access_token;
}
private function getAuthorizationCodeProvider($clientId) {
return $this->grantServices['authorization_code']
->getGrantProvider($clientId);
}
private function getClientCredentialsAccessToken($clientId) {
$access_token = $this
->retrieveAccessToken($clientId);
if (!$access_token) {
$access_token = $this->grantServices['client_credentials']
->getAccessToken($clientId);
}
return $access_token;
}
private function getClientCredentialsProvider($clientId) {
return $this->grantServices['client_credentials']
->getGrantProvider($clientId);
}
private function getResourceOwnersCredentialsAccessToken($clientId) {
$access_token = $this
->retrieveAccessToken($clientId);
if (!$access_token) {
$access_token = $this->grantServices['resource_owner']
->getAccessToken($clientId);
}
return $access_token;
}
private function getResourceOwnersCredentialsProvider($clientId) {
return $this->grantServices['resource_owner']
->getGrantProvider($clientId);
}
}