You are here

protected function SamlauthConfigureForm::formatKeyOrCert in SAML Authentication 4.x

Same name and namespace in other branches
  1. 8.3 src/Form/SamlauthConfigureForm.php \Drupal\samlauth\Form\SamlauthConfigureForm::formatKeyOrCert()

Format a long string in PEM format, or remove PEM format.

Our configuration stores unformatted key/cert values, which is what we would get from SAML metadata and what the SAML toolkit expects. But displaying them formatted in a textbox is better for humans, and also allows us to paste PEM-formatted values (as well as unformatted) into the textbox and not have to remove all the newlines manually, if we got them delivered this way.

The side effect is that certificates/keys are re- and un-formatted on every save operation, but that should be OK.

Parameters

string|null $value: A certificate or private key, either with or without head/footer.

bool $heads: True to format and include head and footer; False to remove them and return one string without spaces / line breaks.

bool $key: (optional) True if this is a private key rather than a certificate.

Return value

string (Un)formatted key or cert.

2 calls to SamlauthConfigureForm::formatKeyOrCert()
SamlauthConfigureForm::buildForm in src/Form/SamlauthConfigureForm.php
Form constructor.
SamlauthConfigureForm::submitForm in src/Form/SamlauthConfigureForm.php
Form submission handler.

File

src/Form/SamlauthConfigureForm.php, line 1618

Class

SamlauthConfigureForm
Provides a configuration form for samlauth module settings and IdP/SP info.

Namespace

Drupal\samlauth\Form

Code

protected function formatKeyOrCert($value, $heads, $key = FALSE) {

  // If the string contains a colon, it's probably a "key:" config value
  // that we placed in the certificate element because we have no other
  // place for it. Leave it alone (and if it fails validation, so be it).
  if (is_string($value) && strpos($value, ':') === FALSE) {
    $value = $key ? SamlUtils::formatPrivateKey($value, $heads) : SamlUtils::formatCert($value, $heads);
  }
  return $value;
}