Whispeak: Update token and phrase when failing request - refs BT#17415

pull/3662/head
Angel Fernando Quiroz Campos 5 years ago
parent fc763c80c8
commit 157ab182e9
  1. 42
      plugin/whispeakauth/Controller/AuthenticationController.php
  2. 21
      plugin/whispeakauth/Controller/EnrollmentController.php
  3. 20
      plugin/whispeakauth/ajax/record_audio.php
  4. 6
      plugin/whispeakauth/assets/js/RecordAudio.js
  5. 2
      plugin/whispeakauth/view/record_audio.html.twig

@ -120,6 +120,8 @@ class AuthenticationController extends BaseController
$userId = api_get_user_id();
$user2fa = ChamiloSession::read(WhispeakAuthPlugin::SESSION_2FA_USER, 0);
$result = [];
if (!empty($user2fa) || !empty($userId)) {
$isAllowed = !empty($_FILES['audio']);
} else {
@ -191,6 +193,18 @@ class AuthenticationController extends BaseController
$message = $this->plugin->get_lang('AuthentifyFailed');
$authTokenRequest = new ApiRequest();
$authTokenResponse = $authTokenRequest->createAuthenticationSessionToken($user);
if (empty($authTokenResponse['text'])) {
$varNumber = mt_rand(1, 6);
$authTokenResponse['text'] = $this->plugin->get_lang("AuthentifySampleText$varNumber");
}
$result['text'] = $authTokenResponse['text'];
ChamiloSession::write(WhispeakAuthPlugin::SESSION_SENTENCE_TEXT, $authTokenResponse['token']);
ChamiloSession::write(WhispeakAuthPlugin::SESSION_FAILED_LOGINS, ++$failedLogins);
if ($maxAttempts && $failedLogins >= $maxAttempts) {
@ -220,7 +234,7 @@ class AuthenticationController extends BaseController
}
}
echo Display::return_message(
$result['resultHtml'] = Display::return_message(
$message,
$success ? 'success' : 'warning',
false
@ -230,11 +244,11 @@ class AuthenticationController extends BaseController
ChamiloSession::erase(WhispeakAuthPlugin::SESSION_FAILED_LOGINS);
if (!empty($lpItemInfo)) {
echo '<script>window.location.href = "'
$result['resultHtml'] .= '<script>window.location.href = "'
.api_get_path(WEB_PLUGIN_PATH)
.'whispeakauth/authentify_password.php";</script>';
exit;
return $result;
}
if (!empty($quizQuestionInfo)) {
@ -242,14 +256,14 @@ class AuthenticationController extends BaseController
ChamiloSession::write(WhispeakAuthPlugin::SESSION_AUTH_PASSWORD, true);
echo "<script>window.location.href = '".$url."';</script>";
$result['resultHtml'] .= "<script>window.location.href = '".$url."';</script>";
exit;
return $result;
}
echo '<script>window.location.href = "'.api_get_path(WEB_PATH).'";</script>';
$result['resultHtml'] .= '<script>window.location.href = "'.api_get_path(WEB_PATH).'";</script>';
exit;
return $result;
}
if ($success) {
@ -267,9 +281,9 @@ class AuthenticationController extends BaseController
$lpItemInfo['lp']
);
echo '<script>window.location.href = "'.$lpItemInfo['src'].'";</script>';
$result['resultHtml'] .= '<script>window.location.href = "'.$lpItemInfo['src'].'";</script>';
exit;
return $result;
}
if (!empty($quizQuestionInfo)) {
@ -285,9 +299,9 @@ class AuthenticationController extends BaseController
$quizQuestionInfo['quiz']
);
echo '<script>window.location.href = "'.$url.'";</script>';
$result['resultHtml'] .= '<script>window.location.href = "'.$url.'";</script>';
exit;
return $result;
}
if (empty($lpItemInfo) && empty($quizQuestionInfo)) {
@ -308,8 +322,12 @@ class AuthenticationController extends BaseController
ChamiloSession::write('_user', $loggedUser);
Login::init_user($user->getId(), true);
echo '<script>window.location.href = "'.api_get_path(WEB_PATH).'";</script>';
$result['resultHtml'] .= '<script>window.location.href = "'.api_get_path(WEB_PATH).'";</script>';
return $result;
}
return $result;
}
/**

@ -48,6 +48,8 @@ class EnrollmentController extends BaseController
*/
public function ajax()
{
$result = ['resultHtml' => ''];
if (!$this->plugin->toolIsEnabled() || empty($_FILES['audio'])) {
throw new \Exception(get_lang('NotAllowed'));
}
@ -63,13 +65,28 @@ class EnrollmentController extends BaseController
}
$request = new ApiRequest();
$response = $request->createEnrollment($token, $audioFilePath, $user);
try {
$response = $request->createEnrollment($token, $audioFilePath, $user);
} catch (\Exception $exception) {
$enrollTokenRequest = new ApiRequest();
$enrollTokenResponse = $enrollTokenRequest->createEnrollmentSessionToken($user);
\ChamiloSession::write(\WhispeakAuthPlugin::SESSION_SENTENCE_TEXT, $enrollTokenResponse['token']);
return [
'resultHtml' => \Display::return_message($exception->getMessage(), 'error'),
'text' => $enrollTokenResponse['text'],
];
}
\ChamiloSession::erase(\WhispeakAuthPlugin::SESSION_SENTENCE_TEXT);
$this->plugin->saveEnrollment($user, $response['speaker']);
echo \Display::return_message($this->plugin->get_lang('EnrollmentSuccess'), 'success');
$result['resultHtml'] .= \Display::return_message($this->plugin->get_lang('EnrollmentSuccess'), 'success');
return $result;
}
/**

@ -17,11 +17,15 @@ if ($isEnrollment) {
$controller = new EnrollmentController();
header('Content-Type: application/json');
try {
$controller->ajax();
echo json_encode($controller->ajax());
} catch (Exception $exception) {
WhispeakAuthPlugin::displayNotAllowedMessage(
$exception->getMessage()
echo json_encode(
[
'resultHtml' => Display::return_message($exception->getMessage(), 'error', false),
]
);
}
die;
@ -30,9 +34,15 @@ if ($isEnrollment) {
if ($isAuthentify) {
$controller = new AuthenticationController();
header('Content-Type: application/json');
try {
$controller->ajax();
echo json_encode($controller->ajax());
} catch (Exception $exception) {
echo Display::return_message($exception->getMessage(), 'error', false);
echo json_encode(
[
'resultHtml' => Display::return_message($exception->getMessage(), 'error', false),
]
);
}
}

@ -43,7 +43,11 @@ window.RecordAudio = (function () {
btnStop.prop('disabled', true).text(btnStop.data('loadingtext'));
}
}).done(function (response) {
$('#messages-deck').html(response);
if (response.text) {
$('#txt-sample-text').text(response.text);
}
$('#messages-deck').html(response.resultHtml);
if ($('#messages-deck > .alert.alert-success').length > 0) {
tagAudio.parents('#audio-wrapper').addClass('hidden').removeClass('show');

@ -8,7 +8,7 @@
<span class="fa fa-microphone fa-5x fa-fw" aria-hidden="true"></span>
</div>
<div class="col-sm-9 text-center">
<p class="lead">{{ sample_text }}</p>
<p class="lead" id="txt-sample-text">{{ sample_text }}</p>
</div>
</div>
</div>

Loading…
Cancel
Save