SimplesamlphpAuthManager.php in simpleSAMLphp Authentication 8.3
File
src/Service/SimplesamlphpAuthManager.php
View source
<?php
namespace Drupal\simplesamlphp_auth\Service;
use Drupal\Core\Config\ConfigFactoryInterface;
use SimpleSAML\Auth\Simple;
use SimpleSAML\Configuration;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\simplesamlphp_auth\Exception\SimplesamlphpAttributeException;
use Drupal\Core\Site\Settings;
use SimpleSAML\Error\CriticalConfigurationError;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Routing\AdminContext;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\Messenger\MessengerInterface;
class SimplesamlphpAuthManager {
use StringTranslationTrait;
protected $config;
protected $simplesamlConfig;
protected $instance;
protected $attributes;
protected $currentUser;
protected $adminContext;
protected $moduleHandler;
protected $requestStack;
protected $messenger;
public function __construct(ConfigFactoryInterface $config_factory, AccountInterface $current_user, AdminContext $admin_context, ModuleHandlerInterface $module_handler, RequestStack $request_stack, MessengerInterface $messenger, Simple $instance = NULL, Configuration $config = NULL) {
$this->config = $config_factory
->get('simplesamlphp_auth.settings');
$this->currentUser = $current_user;
$this->adminContext = $admin_context;
$this->moduleHandler = $module_handler;
$this->requestStack = $request_stack;
$this->messenger = $messenger;
$this->instance = $instance;
$this->simplesamlConfig = $config;
}
public function externalAuthenticate() {
$uri = $this->requestStack
->getCurrentRequest()
->getUri();
$instance = $this
->getSimpleSamlInstance();
if (empty($instance)) {
return FALSE;
}
$instance
->requireAuth([
'ReturnTo' => $uri,
]);
}
protected function getSimpleSamlInstance() {
if (!empty($this->instance)) {
return $this->instance;
}
else {
$this
->checkLibrary();
$auth_source = $this->config
->get('auth_source');
try {
$this->instance = new Simple($auth_source);
return $this->instance;
} catch (CriticalConfigurationError $e) {
if ($this->currentUser
->hasPermission('administer simplesamlphp authentication') && $this->adminContext
->isAdminRoute()) {
$this->messenger
->addError($this
->t('There is a Simplesamlphp configuration problem. @message', [
'@message' => $e
->getMessage(),
]), 'error');
}
return NULL;
}
}
}
protected function getSimpleSamlConfiguration() {
if (!empty($this->simplesamlConfig)) {
return $this->simplesamlConfig;
}
else {
$this
->checkLibrary();
try {
$this->simplesamlConfig = Configuration::getInstance();
return $this->simplesamlConfig;
} catch (CriticalConfigurationError $e) {
if ($this->currentUser
->hasPermission('administer simplesamlphp authentication') && $this->currentUser
->isAdminRoute()) {
$this->messenger
->addError($this
->t('There is a Simplesamlphp configuration problem. @message', [
'@message' => $e
->getMessage(),
]), 'error');
}
return NULL;
}
}
}
public function getStorage() {
$config = $this
->getSimpleSamlConfiguration();
if (!empty($config) && !empty($config
->getValue('store.type'))) {
return $config
->getValue('store.type');
}
return NULL;
}
public function isAuthenticated() {
if ($instance = $this
->getSimpleSamlInstance()) {
return $instance
->isAuthenticated();
}
return FALSE;
}
public function getAuthname() {
return $this
->getAttribute($this->config
->get('unique_id'));
}
public function getDefaultName() {
return $this
->getAttribute($this->config
->get('user_name'));
}
public function getDefaultEmail() {
return $this
->getAttribute($this->config
->get('mail_attr'));
}
public function getAttributes() {
if (!$this->attributes) {
$this->attributes = $this
->getSimpleSamlInstance()
->getAttributes();
}
return $this->attributes;
}
public function getAttribute($attribute) {
$attributes = $this
->getAttributes();
if (isset($attributes)) {
if (!empty($attributes[$attribute][0])) {
return $attributes[$attribute][0];
}
}
throw new SimplesamlphpAttributeException(sprintf('Error in simplesamlphp_auth.module: no valid "%s" attribute set.', $attribute));
}
public function allowUserByAttribute() {
$attributes = $this
->getAttributes();
foreach ($this->moduleHandler
->getImplementations('simplesamlphp_auth_allow_login') as $module) {
if ($this->moduleHandler
->invoke($module, 'simplesamlphp_auth_allow_login', [
$attributes,
]) === FALSE) {
return FALSE;
}
}
return TRUE;
}
public function isActivated() {
if ($this->config
->get('activate') == 1) {
return TRUE;
}
return FALSE;
}
public function logout($redirect_path = NULL) {
if (!$redirect_path) {
$redirect_path = base_path();
}
if ($instance = $this
->getSimpleSamlInstance()) {
$instance
->logout($redirect_path);
}
}
protected function checkLibrary() {
if ($dir = Settings::get('simplesamlphp_dir')) {
include_once $dir . '/lib/_autoload.php';
}
if (!class_exists('SimpleSAML\\Configuration')) {
$this->messenger
->addError($this
->t('The SimpleSAMLphp library cannot be found.'));
}
}
}