You are here

class SamlService in SAML Authentication 8

Same name and namespace in other branches
  1. 8.3 src/SamlService.php \Drupal\samlauth\SamlService
  2. 8.2 src/SamlService.php \Drupal\samlauth\SamlService
  3. 4.x src/SamlService.php \Drupal\samlauth\SamlService

Class SamlService.

@package Drupal\samlauth

Hierarchy

Expanded class hierarchy of SamlService

1 file declares its use of SamlService
SamlController.php in src/Controller/SamlController.php
Contains Drupal\samlauth\Controller\SamlController.
1 string reference to 'SamlService'
samlauth.services.yml in ./samlauth.services.yml
samlauth.services.yml
1 service uses SamlService
samlauth.saml in ./samlauth.services.yml
Drupal\samlauth\SamlService

File

src/SamlService.php, line 20
Contains Drupal\samlauth\SamlService.

Namespace

Drupal\samlauth
View source
class SamlService implements ContainerInjectionInterface {

  /**
   * @var \OneLogin_Saml2_Auth
   */
  protected $auth;

  /**
   * Constructor for Drupal\samlauth\SamlService.
   *
   * @param \OneLogin_Saml2_Auth $auth
   */
  public function __construct(OneLogin_Saml2_Auth $auth) {
    $this->auth = $auth;
  }

  /**
   * Factory method for dependency injection container.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   * @return static
   */
  public static function create(ContainerInterface $container) {
    $config = samlauth_get_config();
    $auth = new OneLogin_Saml2_Auth($config);
    return new static($auth);
  }

  /**
   * Show metadata about the local sp. Use this to configure your saml2 IDP
   *
   * @return mixed xml string representing metadata
   * @throws InvalidArgumentException
   */
  public function getMetadata() {
    $settings = $this->auth
      ->getSettings();
    $metadata = $settings
      ->getSPMetadata();
    $errors = $settings
      ->validateMetadata($metadata);
    if (empty($errors)) {
      return $metadata;
    }
    else {
      throw new InvalidArgumentException('Invalid SP metadata: ' . implode(', ', $errors), OneLogin_Saml2_Error::METADATA_SP_INVALID);
    }
  }

  /**
   * Initiate a SAML2 authentication flow.
   *
   * @param string $return_to
   *   The path to return the user to after a successful login.
   */
  public function login($return_to = null) {
    $this->auth
      ->login($return_to);
  }

  /**
   * Initiate a SAML2 logout flow.
   *
   * @param null $return_to
   *   The path to return the user to after a successful login.
   */
  public function logout($return_to = null) {
    user_logout();
    $this->auth
      ->logout($return_to, array(
      'referrer' => $return_to,
    ));
  }

  /**
   * Process a SAML response (assertion consumer service)
   *
   * @return array|null
   *   Returns array with error description on error. Null otherwise.
   * @throws \OneLogin_Saml2_Error
   */
  public function acs() {
    $this->auth
      ->processResponse();
    $errors = $this->auth
      ->getErrors();
    if (!empty($errors)) {
      return $errors;
    }
    if (!$this
      ->isAuthenticated()) {
      return array(
        'error' => 'Could not authenticate.',
      );
    }
  }

  // Helper function.
  public function getData() {
    return $this->auth
      ->getAttributes();
  }

  /**
   * @return bool if a valid user was fetched from the saml assertion this request.
   */
  protected function isAuthenticated() {
    return $this->auth
      ->isAuthenticated();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SamlService::$auth protected property
SamlService::acs public function Process a SAML response (assertion consumer service)
SamlService::create public static function Factory method for dependency injection container. Overrides ContainerInjectionInterface::create
SamlService::getData public function
SamlService::getMetadata public function Show metadata about the local sp. Use this to configure your saml2 IDP
SamlService::isAuthenticated protected function
SamlService::login public function Initiate a SAML2 authentication flow.
SamlService::logout public function Initiate a SAML2 logout flow.
SamlService::__construct public function Constructor for Drupal\samlauth\SamlService.