id4me-2.0.x-dev/src/Id4meService.php

src/Id4meService.php
<?php

namespace Drupal\id4me;

use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\Core\Url;
use GuzzleHttp\Client;
use Id4me\RP\Model\ClaimRequest;
use Id4me\RP\Model\ClaimRequestList;
use Id4me\RP\Service;

/**
 * Id4me service.
 */
class Id4meService {

  /**
   * The Id4me service facade.
   *
   * @var \Id4me\RP\Service
   */
  protected $id4Me;

  /**
   * The cache service.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cache;

  /**
   * The config service.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * The user's identifier.
   *
   * @var string
   */
  protected $identifier;

  /**
   * The OpenID config data.
   *
   * @var \Id4me\RP\Model\OpenIdConfig
   */
  protected $openidConfig;

  /**
   * The authority name.
   *
   * @var string
   */
  protected $authorityName;

  /**
   * The OpenId client.
   *
   * @var \Id4me\RP\Model\Client
   */
  protected $client;

  /**
   * The state token.
   *
   * @var string
   */
  protected $state;

  /**
   * The authorization tokens.
   *
   * @var \Id4me\RP\Model\AuthorizationTokens
   */
  protected $authorizationTokens;

  /**
   * Class constructor.
   *
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache
   *   The cache service.
   * @param \Drupal\Core\Config\ConfigFactory $config_factory
   *   The config factory service.
   */
  public function __construct(
      CacheBackendInterface $cache,
      ConfigFactory $config_factory
  ) {
    $this->id4Me = new Service(
      new HttpClient(new Client())
    );
    $this->cache = $cache;
    $this->config = $config_factory->get('system.site');
  }

  /**
   * Set identifier.
   *
   * @param string $identifier
   *   The user's identifier.
   *
   * @return $this
   */
  public function setIdentifier($identifier) {
    $this->identifier = $identifier;
    return $this;
  }

  /**
   * Get identifier.
   *
   * @return string
   *   The user's identifier.
   */
  public function getIdentifier() {
    return $this->identifier;
  }

  /**
   * Set state.
   *
   * @param string $state
   *   The state identifier.
   *
   * @return $this
   */
  public function setState($state) {
    $this->state = $state;
    return $this;
  }

  /**
   * Get state.
   *
   * @return string
   *   The state identifier.
   */
  public function getState() {
    return $this->state;
  }

  /**
   * Discover the Id4me service.
   *
   * @return $this
   *
   * @throws \Id4me\RP\Exception\InvalidOpenIdDomainException
   *   An InvalidOpenIdDomainException exception.
   * @throws \Id4me\RP\Exception\OpenIdDnsRecordNotFoundException
   *   An invalid OpenIdDnsRecordNotFoundException exception.
   */
  public function discover() {
    $this->authorityName = $this->id4Me->discover($this->identifier);
    return $this;
  }

  /**
   * Register with the Id4me service.
   *
   * @return $this
   *
   * @throws \Id4me\RP\Exception\InvalidAuthorityIssuerException
   *   An invalid InvalidAuthorityIssuerException exception.
   */
  public function register() {
    $this->openidConfig = $this->id4Me->getOpenIdConfig($this->authorityName);
    if ($cache = $this->cache->get('id4me-' . $this->authorityName)) {
      $this->client = $cache->data;
    }
    else {
      $this->client = $this->id4Me->register(
        $this->openidConfig,
        $this->config->get('name'),
        Url::fromUserInput('/id4me/authorize', ['absolute' => TRUE])->toString()
      );
      $this->cache->set('id4me-' . $this->authorityName, $this->client);
    }
    return $this;
  }

  /**
   * Authorize with the Id4me service.
   *
   * @return \Drupal\Core\Routing\TrustedRedirectResponse
   *   A trusted redirect response.
   */
  public function authorize() {
    $this->state = StateToken::create();
    $_SESSION['id4me_' . $this->state] = [
      'authorityName' => $this->authorityName,
      'client' => serialize($this->client),
      'identifier' => $this->identifier,
      'openidConfig' => serialize($this->openidConfig),
    ];

    $authorizationUrl = $this->id4Me->getAuthorizationUrl(
      $this->openidConfig,
      $this->client->getClientId(),
      $this->identifier,
      $this->client->getActiveRedirectUri(),
      $this->state,
      NULL,
      new ClaimRequestList(
        new ClaimRequest('preferred_username', TRUE, 'To initiate a local account'),
        new ClaimRequest('email', TRUE, 'To initiate a local account')
      )
    );
    return new TrustedRedirectResponse($authorizationUrl);
  }

  /**
   * Get authorization tokens.
   *
   * @param string $code
   *   The authorization code.
   *
   * @return \Id4me\RP\Model\AuthorizationTokens
   *   The authorization tokens.
   *
   * @throws \Id4me\RP\Exception\InvalidAuthorityIssuerException
   *   An invalid InvalidAuthorityIssuerException exception.
   * @throws \Id4me\RP\Exception\InvalidIDTokenException
   *   An invalid InvalidIDTokenException exception.
   */
  public function getAuthorizationTokens($code) {
    $this->openidConfig = unserialize($_SESSION['id4me_' . $this->state]['openidConfig']);
    $this->client = unserialize($_SESSION['id4me_' . $this->state]['client']);
    $this->authorizationTokens = $this->id4Me->getAuthorizationTokens($this->openidConfig, $code, $this->client);
    return $this->authorizationTokens;
  }

  /**
   * Get user info.
   *
   * @return \Id4me\RP\Model\UserInfo
   *   The user info.
   */
  public function getUserInfo() {
    return $this->id4Me->getUserInfo(
      $this->openidConfig,
      $this->client,
      $this->authorizationTokens
    );
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc