parent
d52ff44cb2
commit
f254bcc407
@ -1,287 +0,0 @@ |
|||||||
<?php |
|
||||||
/* For licensing terms, see /license.txt */ |
|
||||||
|
|
||||||
namespace Chamilo\PluginBundle\WhispeakAuth\Controller; |
|
||||||
|
|
||||||
use Chamilo\PluginBundle\Entity\WhispeakAuth\LogEvent; |
|
||||||
use GuzzleHttp\Exception\RequestException; |
|
||||||
|
|
||||||
/** |
|
||||||
* Class AuthenticationRequestController. |
|
||||||
* |
|
||||||
* @package Chamilo\PluginBundle\WhispeakAuth\Controller |
|
||||||
*/ |
|
||||||
class AuthenticationRequestController extends BaseRequestController |
|
||||||
{ |
|
||||||
/** |
|
||||||
* @var int |
|
||||||
*/ |
|
||||||
private $user2fa; |
|
||||||
|
|
||||||
protected function setUser() |
|
||||||
{ |
|
||||||
if (!empty($this->user2fa)) { |
|
||||||
$this->user = api_get_user_entity($this->user2fa); |
|
||||||
} elseif (isset($_POST['username'])) { |
|
||||||
$this->user = \UserManager::getRepository()->findOneBy(['username' => $_POST['username']]); |
|
||||||
} else { |
|
||||||
$this->user = api_get_user_entity(api_get_user_id()); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @return bool |
|
||||||
*/ |
|
||||||
protected function userIsAllowed() |
|
||||||
{ |
|
||||||
$userId = api_get_user_id(); |
|
||||||
$this->user2fa = \ChamiloSession::read(\WhispeakAuthPlugin::SESSION_2FA_USER, 0); |
|
||||||
|
|
||||||
if (!empty($this->user2fa) || !empty($userId)) { |
|
||||||
return !empty($_FILES['audio']); |
|
||||||
} |
|
||||||
|
|
||||||
return !empty($_POST['username']) && !empty($_FILES['audio']); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @throws \Exception |
|
||||||
* |
|
||||||
* @return string |
|
||||||
*/ |
|
||||||
protected function doApiRequest() |
|
||||||
{ |
|
||||||
$failedLogins = \ChamiloSession::read(\WhispeakAuthPlugin::SESSION_FAILED_LOGINS, 0); |
|
||||||
$maxAttempts = $this->plugin->getMaxAttempts(); |
|
||||||
|
|
||||||
if ($maxAttempts && $failedLogins >= $maxAttempts) { |
|
||||||
return \Display::return_message($this->plugin->get_lang('MaxAttemptsReached'), 'warning'); |
|
||||||
} |
|
||||||
|
|
||||||
$wsId = \WhispeakAuthPlugin::getAuthUidValue($this->user->getId()); |
|
||||||
|
|
||||||
if (empty($wsId)) { |
|
||||||
return \Display::return_message($this->plugin->get_lang('SpeechAuthNotEnrolled'), 'warning'); |
|
||||||
} |
|
||||||
|
|
||||||
$token = $this->createSessionToken(); |
|
||||||
$success = $this->performAuthentication($token, $wsId->getValue()); |
|
||||||
|
|
||||||
/** @var array $lpItemInfo */ |
|
||||||
$lpItemInfo = \ChamiloSession::read(\WhispeakAuthPlugin::SESSION_LP_ITEM, []); |
|
||||||
/** @var array $quizQuestionInfo */ |
|
||||||
$quizQuestionInfo = \ChamiloSession::read(\WhispeakAuthPlugin::SESSION_QUIZ_QUESTION, []); |
|
||||||
|
|
||||||
$return = ''; |
|
||||||
|
|
||||||
$message = $this->plugin->get_lang('AuthentifySuccess'); |
|
||||||
|
|
||||||
if (!$success) { |
|
||||||
if (!empty($lpItemInfo)) { |
|
||||||
$this->plugin->addAttemptInLearningPath( |
|
||||||
LogEvent::STATUS_FAILED, |
|
||||||
$this->user->getId(), |
|
||||||
$lpItemInfo['lp_item'], |
|
||||||
$lpItemInfo['lp'] |
|
||||||
); |
|
||||||
} |
|
||||||
|
|
||||||
if (!empty($quizQuestionInfo)) { |
|
||||||
$this->plugin->addAttemptInQuiz( |
|
||||||
LogEvent::STATUS_FAILED, |
|
||||||
$this->user->getId(), |
|
||||||
$quizQuestionInfo['question'], |
|
||||||
$quizQuestionInfo['quiz'] |
|
||||||
); |
|
||||||
} |
|
||||||
|
|
||||||
$message = $this->plugin->get_lang('AuthentifyFailed'); |
|
||||||
|
|
||||||
\ChamiloSession::write(\WhispeakAuthPlugin::SESSION_FAILED_LOGINS, ++$failedLogins); |
|
||||||
|
|
||||||
if ($maxAttempts && $failedLogins >= $maxAttempts) { |
|
||||||
$message .= PHP_EOL |
|
||||||
.'<span data-reach-attempts="true">'.$this->plugin->get_lang('MaxAttemptsReached').'</span>' |
|
||||||
.PHP_EOL |
|
||||||
.'<br><strong>' |
|
||||||
.$this->plugin->get_lang('LoginWithUsernameAndPassword') |
|
||||||
.'</strong>'; |
|
||||||
|
|
||||||
if (!empty($user2fa)) { |
|
||||||
\Display::addFlash(\Display::return_message($message, 'warning', false)); |
|
||||||
} |
|
||||||
} else { |
|
||||||
$message .= PHP_EOL.$this->plugin->get_lang('TryAgain'); |
|
||||||
|
|
||||||
if ('true' === api_get_setting('allow_lostpassword')) { |
|
||||||
$message .= '<br>' |
|
||||||
.\Display::url( |
|
||||||
get_lang('LostPassword'), |
|
||||||
api_get_path(WEB_CODE_PATH).'auth/lostPassword.php', |
|
||||||
['target' => $lpItemInfo ? '_top' : '_self'] |
|
||||||
); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
$return .= \Display::return_message( |
|
||||||
$message, |
|
||||||
$success ? 'success' : 'warning', |
|
||||||
false |
|
||||||
); |
|
||||||
|
|
||||||
if (!$success && $maxAttempts && $failedLogins >= $maxAttempts) { |
|
||||||
\ChamiloSession::erase(\WhispeakAuthPlugin::SESSION_FAILED_LOGINS); |
|
||||||
|
|
||||||
if (!empty($lpItemInfo)) { |
|
||||||
$return .= '<script>window.location.href = "' |
|
||||||
.api_get_path(WEB_PLUGIN_PATH) |
|
||||||
.'whispeakauth/authentify_password.php";</script>'; |
|
||||||
|
|
||||||
return $return; |
|
||||||
} |
|
||||||
|
|
||||||
if (!empty($quizQuestionInfo)) { |
|
||||||
$url = api_get_path(WEB_CODE_PATH).'exercise/exercise_submit.php?'.$quizQuestionInfo['url_params']; |
|
||||||
|
|
||||||
\ChamiloSession::write(\WhispeakAuthPlugin::SESSION_AUTH_PASSWORD, true); |
|
||||||
|
|
||||||
$return .= "<script>window.location.href = '".$url."';</script>"; |
|
||||||
|
|
||||||
exit; |
|
||||||
} |
|
||||||
|
|
||||||
$return .= '<script>window.location.href = "'.api_get_path(WEB_PATH).'";</script>'; |
|
||||||
|
|
||||||
return $return; |
|
||||||
} |
|
||||||
|
|
||||||
if ($success) { |
|
||||||
\ChamiloSession::erase(\WhispeakAuthPlugin::SESSION_SENTENCE_TEXT); |
|
||||||
\ChamiloSession::erase(\WhispeakAuthPlugin::SESSION_FAILED_LOGINS); |
|
||||||
|
|
||||||
if (!empty($lpItemInfo)) { |
|
||||||
\ChamiloSession::erase(\WhispeakAuthPlugin::SESSION_LP_ITEM); |
|
||||||
\ChamiloSession::erase(\WhispeakAuthPlugin::SESSION_2FA_USER); |
|
||||||
|
|
||||||
$this->plugin->addAttemptInLearningPath( |
|
||||||
LogEvent::STATUS_SUCCESS, |
|
||||||
$this->user->getId(), |
|
||||||
$lpItemInfo['lp_item'], |
|
||||||
$lpItemInfo['lp'] |
|
||||||
); |
|
||||||
|
|
||||||
$return .= '<script>window.location.href = "'.$lpItemInfo['src'].'";</script>'; |
|
||||||
|
|
||||||
return $return; |
|
||||||
} |
|
||||||
|
|
||||||
if (!empty($quizQuestionInfo)) { |
|
||||||
$quizQuestionInfo['passed'] = true; |
|
||||||
$url = api_get_path(WEB_CODE_PATH).'exercise/exercise_submit.php?'.$quizQuestionInfo['url_params']; |
|
||||||
|
|
||||||
\ChamiloSession::write(\WhispeakAuthPlugin::SESSION_QUIZ_QUESTION, $quizQuestionInfo); |
|
||||||
|
|
||||||
$this->plugin->addAttemptInQuiz( |
|
||||||
LogEvent::STATUS_SUCCESS, |
|
||||||
$this->user->getId(), |
|
||||||
$quizQuestionInfo['question'], |
|
||||||
$quizQuestionInfo['quiz'] |
|
||||||
); |
|
||||||
|
|
||||||
$return .= '<script>window.location.href = "'.$url.'";</script>'; |
|
||||||
|
|
||||||
return $return; |
|
||||||
} |
|
||||||
|
|
||||||
$loggedUser = [ |
|
||||||
'user_id' => $this->user->getId(), |
|
||||||
'status' => $this->user->getStatus(), |
|
||||||
'uidReset' => true, |
|
||||||
]; |
|
||||||
|
|
||||||
if (empty($user2fa)) { |
|
||||||
\ChamiloSession::write(\WhispeakAuthPlugin::SESSION_2FA_USER, $this->user->getId()); |
|
||||||
} |
|
||||||
|
|
||||||
\ChamiloSession::erase(\WhispeakAuthPlugin::SESSION_FAILED_LOGINS); |
|
||||||
\ChamiloSession::write('_user', $loggedUser); |
|
||||||
\Login::init_user($this->user->getId(), true); |
|
||||||
|
|
||||||
$return .= '<script>window.location.href = "'.api_get_path(WEB_PATH).'";</script>'; |
|
||||||
} |
|
||||||
|
|
||||||
return $return; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @throws \Exception |
|
||||||
* |
|
||||||
* @return string |
|
||||||
*/ |
|
||||||
private function createSessionToken() |
|
||||||
{ |
|
||||||
try { |
|
||||||
$response = $this->httpClient->post( |
|
||||||
'auth', |
|
||||||
[ |
|
||||||
'headers' => [ |
|
||||||
'Authorization' => "Bearer {$this->apiKey}", |
|
||||||
], |
|
||||||
'json' => [], |
|
||||||
'query' => [ |
|
||||||
'lang' => api_get_language_isocode($this->user->getLanguage()), |
|
||||||
], |
|
||||||
] |
|
||||||
); |
|
||||||
$json = json_decode((string) $response->getBody(), true); |
|
||||||
|
|
||||||
return $json['token']; |
|
||||||
} catch (RequestException $requestException) { |
|
||||||
$this->throwRequestException( |
|
||||||
$requestException, |
|
||||||
$this->plugin->get_lang('AuthentifyFailed') |
|
||||||
); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @param string $token |
|
||||||
* @param string $wsId |
|
||||||
* |
|
||||||
* @throws \Exception |
|
||||||
* |
|
||||||
* @return bool |
|
||||||
*/ |
|
||||||
private function performAuthentication($token, $wsId) |
|
||||||
{ |
|
||||||
try { |
|
||||||
$this->httpClient->post( |
|
||||||
'auth', |
|
||||||
[ |
|
||||||
'headers' => [ |
|
||||||
'Authorization' => "Bearer $token", |
|
||||||
], |
|
||||||
'multipart' => [ |
|
||||||
[ |
|
||||||
'name' => 'speaker', |
|
||||||
'contents' => $wsId, |
|
||||||
], |
|
||||||
[ |
|
||||||
'name' => 'file', |
|
||||||
'contents' => fopen($this->audioFilePath, 'r'), |
|
||||||
'filename' => basename($this->audioFilePath), |
|
||||||
], |
|
||||||
], |
|
||||||
] |
|
||||||
); |
|
||||||
|
|
||||||
return true; |
|
||||||
} catch (RequestException $requestException) { |
|
||||||
$this->throwRequestException( |
|
||||||
$requestException, |
|
||||||
$this->plugin->get_lang('AuthentifyFailed') |
|
||||||
); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
@ -1,142 +0,0 @@ |
|||||||
<?php |
|
||||||
/* For licensing terms, see /license.txt */ |
|
||||||
|
|
||||||
namespace Chamilo\PluginBundle\WhispeakAuth\Controller; |
|
||||||
|
|
||||||
use FFMpeg\FFMpeg; |
|
||||||
use FFMpeg\Format\Audio\Wav; |
|
||||||
use GuzzleHttp\Client; |
|
||||||
use GuzzleHttp\Exception\ClientException; |
|
||||||
use GuzzleHttp\Exception\RequestException; |
|
||||||
|
|
||||||
/** |
|
||||||
* Class BaseRequestController. |
|
||||||
* |
|
||||||
* @package Chamilo\PluginBundle\WhispeakAuth\Controller |
|
||||||
*/ |
|
||||||
abstract class BaseRequestController |
|
||||||
{ |
|
||||||
/** |
|
||||||
* @var \WhispeakAuthPlugin |
|
||||||
*/ |
|
||||||
protected $plugin; |
|
||||||
/** |
|
||||||
* @var \GuzzleHttp\Client |
|
||||||
*/ |
|
||||||
protected $httpClient; |
|
||||||
/** |
|
||||||
* @var string |
|
||||||
*/ |
|
||||||
protected $apiKey; |
|
||||||
/** |
|
||||||
* @var \Chamilo\UserBundle\Entity\User |
|
||||||
*/ |
|
||||||
protected $user; |
|
||||||
/** |
|
||||||
* @var string |
|
||||||
*/ |
|
||||||
protected $audioFilePath; |
|
||||||
|
|
||||||
/** |
|
||||||
* BaseController constructor. |
|
||||||
*/ |
|
||||||
public function __construct() |
|
||||||
{ |
|
||||||
$this->plugin = \WhispeakAuthPlugin::create(); |
|
||||||
$this->httpClient = new Client(['base_uri' => $this->plugin->getApiUrl(),]); |
|
||||||
$this->apiKey = $this->plugin->get(\WhispeakAuthPlugin::SETTING_TOKEN); |
|
||||||
} |
|
||||||
|
|
||||||
abstract protected function setUser(); |
|
||||||
|
|
||||||
/** |
|
||||||
* @return bool |
|
||||||
*/ |
|
||||||
abstract protected function userIsAllowed(); |
|
||||||
|
|
||||||
/** |
|
||||||
* @throws \Exception |
|
||||||
*/ |
|
||||||
protected function protect() |
|
||||||
{ |
|
||||||
if (false === $this->userIsAllowed()) { |
|
||||||
throw new \Exception(get_lang('NotAllowed')); |
|
||||||
} |
|
||||||
|
|
||||||
$this->plugin->protectTool(false); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @throws \Exception |
|
||||||
*/ |
|
||||||
private function uploadAudioFile() |
|
||||||
{ |
|
||||||
$pluginName = $this->plugin->get_name(); |
|
||||||
|
|
||||||
$path = api_upload_file($pluginName, $_FILES['audio'], $this->user->getId()); |
|
||||||
|
|
||||||
if (false === $path) { |
|
||||||
throw new \Exception(get_lang('UploadError')); |
|
||||||
} |
|
||||||
|
|
||||||
$fullPath = api_get_path(SYS_UPLOAD_PATH).$pluginName.$path['path_to_save']; |
|
||||||
$mimeType = mime_content_type($fullPath); |
|
||||||
|
|
||||||
if ('wav' !== substr($mimeType, -3)) { |
|
||||||
$ffmeg = FFMpeg::create(); |
|
||||||
|
|
||||||
$audioFile = $ffmeg->open($fullPath); |
|
||||||
|
|
||||||
$fullPath = dirname($fullPath).'/audio.wav'; |
|
||||||
|
|
||||||
$audioFile->save(new Wav(), $fullPath); |
|
||||||
} |
|
||||||
|
|
||||||
$this->audioFilePath = $fullPath; |
|
||||||
} |
|
||||||
|
|
||||||
public function process() |
|
||||||
{ |
|
||||||
try { |
|
||||||
$this->protect(); |
|
||||||
$this->setUser(); |
|
||||||
|
|
||||||
if (empty($this->user)) { |
|
||||||
throw new \Exception(get_lang('NoUser')); |
|
||||||
} |
|
||||||
|
|
||||||
$this->uploadAudioFile(); |
|
||||||
|
|
||||||
$response = $this->doApiRequest(); |
|
||||||
|
|
||||||
echo $response; |
|
||||||
} catch (\Exception $exception) { |
|
||||||
echo \Display::return_message($exception->getMessage(), 'error'); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @throws \Exception |
|
||||||
* |
|
||||||
* @return mixed |
|
||||||
*/ |
|
||||||
abstract protected function doApiRequest(); |
|
||||||
|
|
||||||
/** |
|
||||||
* @param \GuzzleHttp\Exception\RequestException $requestException |
|
||||||
* @param string $defaultMessage |
|
||||||
* |
|
||||||
* @throws \Exception |
|
||||||
*/ |
|
||||||
protected function throwRequestException(RequestException $requestException, $defaultMessage) |
|
||||||
{ |
|
||||||
$message = $defaultMessage; |
|
||||||
|
|
||||||
if ($requestException->hasResponse()) { |
|
||||||
$json = json_decode((string) $requestException->getResponse()->getBody(), true); |
|
||||||
$message = is_array($json['message']) ? implode("\n", $json['message']) : $json['message']; |
|
||||||
} |
|
||||||
|
|
||||||
throw new \Exception($message); |
|
||||||
} |
|
||||||
} |
|
||||||
Loading…
Reference in new issue