Skip to content

Commit

Permalink
Display server response when a OTP test fails
Browse files Browse the repository at this point in the history
  • Loading branch information
jaark committed Apr 16, 2019
1 parent a92ad8e commit 52678b8
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 105 deletions.
4 changes: 2 additions & 2 deletions js/settings-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function testYubiKeyOTP(otp)
OC.msg.finishedSaving('#twofactor_yubikey-settings-otpresults', {
'status': 'success',
'data': {
'message':'Success! OTP Verified! Configuration is good.'
'message': OC.L10N.translate('twofactor_yubikey', 'Success! OTP Verified! Configuration is good.')
}
});
}
Expand All @@ -52,7 +52,7 @@ function testYubiKeyOTP(otp)
OC.msg.finishedSaving('#twofactor_yubikey-settings-otpresults', {
'status': 'failure',
'data': {
'message':'OTP Failed Validation! Verify Configuration and try again.'
'message': OC.L10N.translate('twofactor_yubikey', 'OTP Failed Validation! ') + OC.L10N.translate('twofactor_yubikey', data.message)
}
});
}
Expand Down
5 changes: 2 additions & 3 deletions js/settings-personal.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var twofactor_yubikeyid = {
OC.msg.finishedSaving('#twofactor_yubikey-settings-msg', {
'status': 'failure',
'data': {
'message': OC.L10N.translate('twofactor_yubikey', 'Key Registration failed. Try again or contact your administrator.')
'message': OC.L10N.translate('twofactor_yubikey', 'Key Registration failed. ') + OC.L10N.translate('twofactor_yubikey', data.message)
}
});
}
Expand Down Expand Up @@ -140,8 +140,7 @@ $(document).ready(function() {

}
}).focusout(function(e) {
twofactor_yubikeyid.save($('#twofactor_yubikey-yubikey-name').val(), $(this).val());

twofactor_yubikeyid.save($('#twofactor_yubikey-yubikey-name').val(), $(this).val());
});

});
177 changes: 86 additions & 91 deletions lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,96 +19,91 @@
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;



class SettingsController extends Controller {
/** @var IYubiotp */
private $yubiotp;

/** @var IUserSession */
private $userSession;

/** @var Defaults */
private $defaults;

/**
* @param string $appName
* @param IRequest $request
* @param IUserSession $userSession
* @param IYubiotp $yubiotp
* @param Defaults $defaults
*/
public function __construct($appName, IRequest $request, IUserSession $userSession, IYubiotp $yubiotp, Defaults $defaults) {
parent::__construct($appName, $request);
$this->userSession = $userSession;
$this->yubiotp = $yubiotp;
$this->defaults = $defaults;
}

/**
* @NoAdminRequired
* @param string $otp
* @param string $name
* @return JSONResponse
*/
public function addkey($otp, $name) {
$user = $this->userSession->getUser();
if( $this->yubiotp->addKey($user, $otp, $name) )
{
return ['success' => true ];
}
else
{
return ['success' => false];
}
}

/**
* @NoAdminRequired
* @param string $keyId Ybikey ID
* @return JSONResponse
*/
public function deletekey($keyId) {
$user = $this->userSession->getUser();
if( $this->yubiotp->deleteKeyId($user, $keyId) ){
return ['success' => true ];
}
else {
return ['success' => false ];
}

}

/**
* @NoAdminRequired
* @return JSONResponse
*/
public function getkeys() {
$user = $this->userSession->getUser();
$keys = $this->yubiotp->getYubikeys($user);
$out = array();

foreach ($keys as $key) {
$out[] = $key->outputArray();
}

return array('keys' => $out);
}


/**
* @NoAdminRequired
* @param string $otp
* @return JSONResponse
*/
public function testotp($otp) {

if( $this->yubiotp->validateTestOTP($otp) ){
return ['success' => true ];
}
else {
return ['success' => false ];
}

}
/** @var IYubiotp */
private $yubiotp;

/** @var IUserSession */
private $userSession;

/** @var Defaults */
private $defaults;

/**
* @param string $appName
* @param IRequest $request
* @param IUserSession $userSession
* @param IYubiotp $yubiotp
* @param Defaults $defaults
*/
public function __construct($appName, IRequest $request, IUserSession $userSession, IYubiotp $yubiotp, Defaults $defaults) {
parent::__construct($appName, $request);
$this->userSession = $userSession;
$this->yubiotp = $yubiotp;
$this->defaults = $defaults;
}

/**
* @NoAdminRequired
* @param string $otp
* @param string $name
* @return JSONResponse
*/
public function addkey($otp, $name) {
$user = $this->userSession->getUser();
$result = $this->yubiotp->addKey($user, $otp, $name);

if (\PEAR::isError($result)) {
return ['success' => false, 'message' => $result->message ];
} else {
return ['success' => true];
}
}

/**
* @NoAdminRequired
* @param string $keyId Ybikey ID
* @return JSONResponse
*/
public function deletekey($keyId) {
$user = $this->userSession->getUser();
if( $this->yubiotp->deleteKeyId($user, $keyId) ){
return ['success' => true ];
}
else {
return ['success' => false ];
}

}

/**
* @NoAdminRequired
* @return JSONResponse
*/
public function getkeys() {
$user = $this->userSession->getUser();
$keys = $this->yubiotp->getYubikeys($user);
$out = array();

foreach ($keys as $key) {
$out[] = $key->outputArray();
}

return array('keys' => $out);
}


/**
* @NoAdminRequired
* @param string $otp
* @return JSONResponse
*/
public function testotp($otp) {
$result = $this->yubiotp->validateTestOTP($otp);
if (\PEAR::isError($result)) {
return ['success' => false, 'message' => $result->message ];
} else {
return ['success' => true];
}
}
}
25 changes: 16 additions & 9 deletions lib/Service/Yubiotp.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use OCA\TwoFactorYubikey\Db\YubiKey;
use OCA\TwoFactorYubikey\Db\YubiKeyMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\Authentication\TwoFactorAuth\TwoFactorException;

use OCP\Activity\IManager;
use OCP\ILogger;
Expand Down Expand Up @@ -99,8 +100,9 @@ public function addKey(IUser $user, $otp, $name = "") {
$keyID = substr($otp, 0, 12);

//First, Let's validate the otp (ensures that configuration is good)
if (!$this->validateTestOTP($otp)) {
return false;
$testAuth = $this->validateTestOTP($otp);
if (\PEAR::isError($testAuth)) {
return $testAuth;
}

//Second, let's make sure we're not adding duplicates
Expand Down Expand Up @@ -154,8 +156,13 @@ private function publishEvent(IUser $user, string $event) {
}

/**
* Attempt to delete the pecified key for the user.
* Returns true on success, false otherwise.
*
* @param IUser $user
* @param string $kkeyID
* @param string $keyID
*
* @return boolean
*/
public function deleteKeyId(IUser $user, $keyID) {

Expand Down Expand Up @@ -192,8 +199,12 @@ public function deleteKeyId(IUser $user, $keyID) {
}

/**
* @param IUser $user
* Tests if an OTP can be authenticated against the current server.
* Used when registering a key or testing the server configuration.
*
* @param string $otp
*
* @return mixed PEAR error on error, true otherwise
*/
public function validateTestOTP($otp) {
$config = new \OCA\TwoFactorYubikey\TwoFactorYubikeyConfig(\OC::$server->getConfig());
Expand All @@ -209,11 +220,7 @@ public function validateTestOTP($otp) {
}
$auth = $yubi->verify($otp);

if (\PEAR::isError($auth)) {
return false;
} else {
return true;
}
return $auth;

}

Expand Down

1 comment on commit 52678b8

@jaark
Copy link
Owner Author

@jaark jaark commented on 52678b8 Apr 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provides information for #9

Please sign in to comment.