diff --git a/cas.install b/cas.install index af33aba..6d822a5 100644 --- a/cas.install +++ b/cas.install @@ -128,3 +128,13 @@ function cas_update_8003() { $config->set('user_accounts.prevent_normal_login', FALSE); $config->save(); } + +/** + * Set new 'protocol' config option to HTTPS. + */ +function cas_update_8004() { + $config_factory = \Drupal::configFactory(); + $config = $config_factory->getEditable('cas.settings'); + $config->set('server.protocol', 'https'); + $config->save(); +} diff --git a/src/Form/CasSettings.php b/src/Form/CasSettings.php index 47884f2..c8ad8e1 100755 --- a/src/Form/CasSettings.php +++ b/src/Form/CasSettings.php @@ -79,7 +79,7 @@ class CasSettings extends ConfigFormBase { ); $form['server']['version'] = array( '#type' => 'radios', - '#title' => $this->t('Protocol version'), + '#title' => $this->t('CAS Protocol version'), '#options' => array( '1.0' => $this->t('1.0'), '2.0' => $this->t('2.0'), @@ -89,14 +89,14 @@ class CasSettings extends ConfigFormBase { '#description' => $this->t('The CAS protocol version your CAS server supports.'), ); $form['server']['protocol'] = array( - '#type' => 'select', - '#title' => $this->t('Protocol'), + '#type' => 'radios', + '#title' => $this->t('HTTP Protocol'), '#options' => array( - 'http' => $this->t('Http'), - 'https' => $this->t('Https'), + 'http' => $this->t('HTTP (non-secure)'), + 'https' => $this->t('HTTPS (secure)'), ), '#default_value' => $config->get('server.protocol'), - '#description' => $this->t('Protocol of the CAS server. WARNING: Do not use HTTP on production environments as it can lead to security problems.'), + '#description' => $this->t('HTTP protocol of the CAS server. WARNING: Do not use HTTP on production environments!'), ); $form['server']['hostname'] = array( '#type' => 'textfield', diff --git a/src/Service/CasHelper.php b/src/Service/CasHelper.php index 47cbb0e..9d2fbfd 100644 --- a/src/Service/CasHelper.php +++ b/src/Service/CasHelper.php @@ -122,11 +122,15 @@ class CasHelper { * The base URL. */ public function getServerBaseUrl() { - $url = $this->settings->get('server.protocol') . '://' . $this->settings->get('server.hostname'); + $protocol = $this->settings->get('server.protocol'); + $url = $protocol . '://' . $this->settings->get('server.hostname'); + + // Only append port if it's non standard. $port = $this->settings->get('server.port'); - if (!empty($port) && $port != 443) { + if (($protocol == 'http' && $port != 80) || ($protocol == 'https' && $port != 443)) { $url .= ':' . $this->settings->get('server.port'); } + $url .= $this->settings->get('server.path'); $url = rtrim($url, '/') . '/'; diff --git a/tests/src/Unit/Service/CasHelperTest.php b/tests/src/Unit/Service/CasHelperTest.php index bc4ba66..3e9d588 100644 --- a/tests/src/Unit/Service/CasHelperTest.php +++ b/tests/src/Unit/Service/CasHelperTest.php @@ -126,13 +126,13 @@ class CasHelperTest extends UnitTestCase { * @covers ::getServerBaseUrl * @covers ::__construct */ - public function testGetServerBaseUrlNonStandardProtocol() { + public function testGetServerBaseUrlNonStandardHttpProtocol() { /** @var \Drupal\Core\Config\ConfigFactory $config_factory */ $config_factory = $this->getConfigFactoryStub(array( 'cas.settings' => array( 'server.protocol' => 'http', 'server.hostname' => 'example.com', - 'server.port' => 443, + 'server.port' => 80, 'server.path' => '/cas', ), ));