You are here

public function MiniorangeSPInformation::upload_metadata in SAML SP 2.0 Single Sign On (SSO) - SAML Service Provider 8

2 calls to MiniorangeSPInformation::upload_metadata()
MiniorangeSPInformation::miniorange_saml_fetch_metadata in src/Form/MiniorangeSPInformation.php
MiniorangeSPInformation::miniorange_saml_upload_file in src/Form/MiniorangeSPInformation.php

File

src/Form/MiniorangeSPInformation.php, line 339
Contains \Drupal\miniorange_saml\Form\MiniorangeSPInformation.

Class

MiniorangeSPInformation

Namespace

Drupal\miniorange_saml\Form

Code

public function upload_metadata($file) {
  global $base_url;
  $idp_name_stored = \Drupal::config('miniorange_saml.settings')
    ->get('miniorange_saml_idp_name');
  if (empty($idp_name_stored)) {
    \Drupal::configFactory()
      ->getEditable('miniorange_saml.settings')
      ->set('miniorange_saml_idp_name', 'Identity Provider')
      ->save();
  }
  $document = new \DOMDocument();
  $document
    ->loadXML($file);
  restore_error_handler();
  $first_child = $document->firstChild;
  if (!empty($first_child)) {
    $metadata = new MetadataReader($document);
    $identity_providers = $metadata
      ->getIdentityProviders();
    if (empty($identity_providers)) {
      \Drupal::messenger()
        ->addMessage(t('Please provide a valid metadata file.'), 'error');
      return;
    }
    foreach ($identity_providers as $key => $idp) {
      $saml_login_url = $idp
        ->getLoginURL('HTTP-Redirect');
      if (empty($saml_login_url)) {
        $saml_login_url = $idp
          ->getLoginURL('HTTP-POST');
      }
      $saml_issuer = $idp
        ->getEntityID();
      $saml_x509_certificate = $idp
        ->getSigningCertificate();
      $sp_issuer = $base_url;
      \Drupal::configFactory()
        ->getEditable('miniorange_saml.settings')
        ->set('miniorange_saml_sp_issuer', $sp_issuer)
        ->save();
      \Drupal::configFactory()
        ->getEditable('miniorange_saml.settings')
        ->set('miniorange_saml_idp_issuer', $saml_issuer)
        ->save();
      \Drupal::configFactory()
        ->getEditable('miniorange_saml.settings')
        ->set('miniorange_saml_idp_login_url', $saml_login_url)
        ->save();
      \Drupal::configFactory()
        ->getEditable('miniorange_saml.settings')
        ->set('miniorange_saml_idp_x509_certificate', $saml_x509_certificate[0])
        ->save();
    }
    \Drupal::messenger()
      ->addStatus(t('Identity Provider Configuration successfully saved.'));
    return;
  }
  else {
    \Drupal::messenger()
      ->addError(t('Please provide a valid metadata file.'));
    return;
  }
}