entityTypeManager = $entityTypeManager; $this->entity_query = $entity_query; $this->connection = $connection; $this->logger = $loggerChannel->get('casost'); } public static function create(ContainerInterface $container) { return new static( $container->get('entity.manager'), $container->get('entity.query'), $container->get('database'), $container->get('logger.factory') ); } public function loginGo(Request $request) { try { $CASOSTConfigs = $this->entityTypeManager->getStorage('casost_config')->loadByProperties(array('name' => 'casost_sch_sso_config')); $CASOSTConfig = reset($CASOSTConfigs); if ($CASOSTConfig) { $this->serverVersion = $CASOSTConfig->serverversion->value; $this->serverHostname = $CASOSTConfig->serverhostname->value; $this->serverPort = $CASOSTConfig->serverport->value; $this->serverUri = $CASOSTConfig->serveruri->value === null ? '' : $CASOSTConfig->serveruri->value; $this->changeSessionId = $CASOSTConfig->changesessionid->value; $this->CASServerCACert = $CASOSTConfig->casservercacert->value; $this->CASServerCNValidate = $CASOSTConfig->casservercnvalidate->value; $this->noCASServerValidation = $CASOSTConfig->nocasservervalidation->value; $this->proxy = $CASOSTConfig->proxy->value; $this->handleLogoutRequests = $CASOSTConfig->handlelogoutrequests->value; $this->CASLang = $CASOSTConfig->caslang->value; $this->allowed1 = $CASOSTConfig->allowed1->value; $this->allowed1Value = $CASOSTConfig->allowed1value->value; $this->allowed2 = $CASOSTConfig->allowed2->value; $this->allowed2Value = $CASOSTConfig->allowed2value->value; } // phpCAS::setDebug("/home/haris/devel/eepal/drupal/modules/casost/phpcas.log"); // Enable verbose error messages. Disable in production! //phpCAS::setVerbose(true); phpCAS::client($this->serverVersion, $this->serverHostname, intval($this->serverPort), $this->serverUri, boolval($this->changeSessionId)); // \phpCAS::setServerLoginURL('http://sso-test.sch.gr/login'); // \phpCAS::setServerServiceValidateURL('http://sso-test.sch.gr/cas/samlValidate'); if ($this->CASServerCACert) { if ($this->CASServerCNValidate) { phpCAS::setCasServerCACert($this->CASServerCACert, true); } else { phpCAS::setCasServerCACert($this->CASServerCACert, false); } } if ($this->noCASServerValidation) { phpCAS::setNoCasServerValidation(); } phpCAS::handleLogoutRequests(); if (!phpCAS::forceAuthentication()) { $response = new Response(); $response->setContent('forbidden. cannot force authentication'); $response->setStatusCode(Response::HTTP_FORBIDDEN); $response->headers->set('Content-Type', 'application/json'); return $response; } $attributes = phpCAS::getAttributes(); /* $isAllowed = true; $att1 = $attributes[$this->allowed1]; $att2 = $attributes[$this->allowed2]; if (!isset($att1) || !isset($att2)) { $isAllowed = false; } if (!is_array($attributes[$this->allowed1])) { $attributes[$this->allowed1] = [$attributes[$this->allowed1]]; } if (!is_array($attributes[$this->allowed2])) { $attributes[$this->allowed2] = [$attributes[$this->allowed2]]; } $found1 = false; foreach ($attributes[$this->allowed1] as $value) { if (1 === preg_match($this->allowed1Value, $value)) { $found1 = true; } } $found2 = false; foreach ($attributes[$this->allowed2] as $value) { if (1 === preg_match($this->allowed2Value, $value)) { $found2 = true; } } if (!$found1 || !$found2) { $isAllowed = false; } */ /* if (!$isAllowed) { $response = new Response(); $response->setContent(t('Access is allowed only to official school accounts')); $response->setStatusCode(Response::HTTP_FORBIDDEN); $response->headers->set('Content-Type', 'application/json;charset=UTF-8'); return $response; } */ $CASUser = phpCAS::getUser(); $this->logger->warning($CASUser); $filterAttribute = function ($attribute) use ($attributes) { if (!isset($attributes[$attribute])) { return; } if (is_array($attributes[$attribute])) { return $attributes[$attribute]; } return $attributes[$attribute]; }; // $this->logger->warning('cn=' . $filterAttribute('cn')); $epalToken = $this->authenticatePhase2($request, $CASUser, $filterAttribute('cn')); if ($epalToken) { return new RedirectResponse('/angular/eepal-front/dist/#/school?auth_token=' . $epalToken.'&auth_role=director', 302, []); } else { $response = new Response(); $response->setContent('forbidden'); $response->setStatusCode(Response::HTTP_FORBIDDEN); $response->headers->set('Content-Type', 'application/json'); return $response; } } catch (\Exception $e) { $this->logger->warning($e->getMessage()); $response = new Response(); $response->setContent('forbidden'); $response->setStatusCode(Response::HTTP_FORBIDDEN); $response->headers->set('Content-Type', 'application/json'); return $response; } } public function authenticatePhase2($request, $CASUser, $cn) { $trx = $this->connection->startTransaction(); try { $currentTime = time(); $epalToken = md5(uniqid(mt_rand(), true)); $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('mail' => $CASUser)); $user = reset($users); if ($user) { $user->setPassword($epalToken); $user->setUsername($epalToken); $user->save(); } if ($user === null || !$user) { //Create a User $user = User::create(); //Mandatory settings $unique_id = uniqid('####'); $user->setPassword($epalToken); $user->enforceIsNew(); $user->setEmail($CASUser); $user->setUsername($epalToken); //This username must be unique and accept only a-Z,0-9, - _ @ . $user->activate(); $user->set('init', $cn); //Set Language $language_interface = \Drupal::languageManager()->getCurrentLanguage(); $user->set('langcode', $language_interface->getId()); $user->set('preferred_langcode', $language_interface->getId()); $user->set('preferred_admin_langcode', $language_interface->getId()); //Adding default user role $user->addRole('epal'); $user->save(); } return $epalToken; } catch (OAuthException $e) { $this->logger->warning($e->getMessage()); $trx->rollback(); return false; } catch (\Exception $ee) { $this->logger->warning($ee->getMessage()); $trx->rollback(); return false; } return false; } }