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'); 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\AppFramework\Controller;
use OCP\ILogger;
use OCP\IRequest; use OCP\IRequest;
use OCP\ISession;
use u2flib_server\U2F;
class SettingsController extends Controller { class SettingsController extends Controller {
/** @var ISession */ /** @var U2FManager */
private $session; private $manager;
/** @var ILogger */ public function __construct($appName, IRequest $request, U2FManager $manager) {
private $logger;
public function __construct($appName, IRequest $request, ISession $session, ILogger $logger) {
parent::__construct($appName, $request); parent::__construct($appName, $request);
$this->session = $session; $this->manager = $manager;
$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));
} }
/** /**
@ -55,20 +33,7 @@ class SettingsController extends Controller {
* @UseSession * @UseSession
*/ */
public function startRegister() { public function startRegister() {
$u2f = $this->getU2f(); return $this->manager->startRegistration($user);
$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
];
} }
/** /**
@ -78,20 +43,7 @@ class SettingsController extends Controller {
* @param string $clientData * @param string $clientData
*/ */
public function finishRegister($registrationData, $clientData) { public function finishRegister($registrationData, $clientData) {
$this->logger->debug($registrationData); $this->manager->finishRegistration($registrationData, $clientData);
$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));
} }
} }

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

@ -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() {
}
}