This commit is contained in:
Christoph Wurst 2016-08-26 11:21:03 +02:00
Родитель 63c1287761
Коммит c9d5910c72
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: CC42AC2A7F0E56D8
2 изменённых файлов: 106 добавлений и 55 удалений

Просмотреть файл

@ -14,40 +14,18 @@ namespace OCA\TwoFactor_U2F\Controller;
require_once(__DIR__ . '/../../vendor/yubico/u2flib-server/src/u2flib_server/U2F.php');
use OC;
use OCA\TwoFactor_U2F\Service\U2FManager;
use OCP\AppFramework\Controller;
use OCP\ILogger;
use OCP\IRequest;
use OCP\ISession;
use u2flib_server\U2F;
class SettingsController extends Controller {
/** @var ISession */
private $session;
/** @var U2FManager */
private $manager;
/** @var ILogger */
private $logger;
public function __construct($appName, IRequest $request, ISession $session, ILogger $logger) {
public function __construct($appName, IRequest $request, U2FManager $manager) {
parent::__construct($appName, $request);
$this->session = $session;
$this->logger = $logger;
}
private function getU2f() {
return new U2F(OC::$server->getURLGenerator()->getAbsoluteURL('/'));
}
private function getRegs() {
if (!file_exists('/tmp/yubi')) {
return [];
}
return [json_decode(file_get_contents('/tmp/yubi'))];
}
private function setReg($data) {
file_put_contents('/tmp/yubi', json_encode($data));
$this->manager = $manager;
}
/**
@ -55,20 +33,7 @@ class SettingsController extends Controller {
* @UseSession
*/
public function startRegister() {
$u2f = $this->getU2f();
$data = $u2f->getRegisterData($this->getRegs());
list($req, $sigs) = $data;
$this->logger->debug(json_encode($req));
$this->logger->debug(json_encode($sigs));
$this->session->set('twofactor_u2f_regReq', json_encode($req));
return [
'req' => $req,
'sigs' => $sigs,
'username' => 'user', // TODO
];
return $this->manager->startRegistration($user);
}
/**
@ -78,20 +43,7 @@ class SettingsController extends Controller {
* @param string $clientData
*/
public function finishRegister($registrationData, $clientData) {
$this->logger->debug($registrationData);
$this->logger->debug($clientData);
$u2f = $this->getU2f();
$regReq = json_decode($this->session->get('twofactor_u2f_regReq'));
$regResp = [
'registrationData' => $registrationData,
'clientData' => $clientData,
];
$reg = $u2f->doRegister($regReq, (object) $regResp);
$this->setReg($reg);
$this->logger->debug(json_encode($reg));
$this->manager->finishRegistration($registrationData, $clientData);
}
}

Просмотреть файл

@ -0,0 +1,99 @@
<?php
/**
* Nextcloud - U2F 2FA
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @copyright Christoph Wurst 2016
*/
namespace OCA\TwoFactor_U2F\Service;
require_once(__DIR__ . '/../../vendor/yubico/u2flib-server/src/u2flib_server/U2F.php');
use OC;
use OCP\ILogger;
use OCP\ISession;
use OCP\IUser;
use u2flib_server\U2F;
class U2FManager {
/** @var ISession */
private $session;
/** @var ILogger */
private $logger;
public function __construct(ISession $session, ILogger $logger) {
$this->session = $session;
$this->logger = $logger;
}
private function getU2f() {
return new U2F(OC::$server->getURLGenerator()->getAbsoluteURL('/'));
}
public function isEnabled(IUser $user) {
// TODO: save in DB
return file_exists('/tmp/yubi');
}
private function getRegs() {
if (!file_exists('/tmp/yubi')) {
return [];
}
return [json_decode(file_get_contents('/tmp/yubi'))];
}
private function setReg($data) {
file_put_contents('/tmp/yubi', json_encode($data));
}
public function startRegistration(IUser $user = null) {
$u2f = $this->getU2f();
$data = $u2f->getRegisterData($this->getRegs());
list($req, $sigs) = $data;
$this->logger->debug(json_encode($req));
$this->logger->debug(json_encode($sigs));
$this->session->set('twofactor_u2f_regReq', json_encode($req));
return [
'req' => $req,
'sigs' => $sigs,
'username' => 'user', // TODO
];
}
public function finishRegistration($registrationData, $clientData) {
$this->logger->debug($registrationData);
$this->logger->debug($clientData);
$u2f = $this->getU2f();
$regReq = json_decode($this->session->get('twofactor_u2f_regReq'));
$regResp = [
'registrationData' => $registrationData,
'clientData' => $clientData,
];
$reg = $u2f->doRegister($regReq, (object) $regResp);
$this->setReg($reg);
$this->logger->debug(json_encode($reg));
}
public function startAuthenticate() {
$u2f = $this->getU2f();
$u2f->getAuthenticateData($registrations);
}
public function finishAuthenticate() {
}
}