export webauthn data

This commit is contained in:
Roland Gruber 2020-07-08 20:46:56 +02:00
parent 5e770d8920
commit 21e7e4a95d
3 changed files with 93 additions and 6 deletions

View File

@ -1 +1 @@
7.3.PRE1
7.3.DEV

View File

@ -1,5 +1,6 @@
<?php
namespace LAM\PERSISTENCE;
use LAM\LOGIN\WEBAUTHN\WebauthnManager;
use LAM\PDF\PDFStructure;
use LAM\PDF\PDFStructureReader;
use LAM\PDF\PDFStructureWriter;
@ -76,11 +77,7 @@ class ConfigDataExporter {
$jsonData['pdfProfiles'] = $this->_getPdfProfiles($serverProfiles);
$jsonData['pdfProfileTemplates'] = $this->_getPdfProfileTemplates();
$jsonData['selfServiceProfiles'] = $this->_getSelfServiceProfiles();
/**
* TODO
*
* webauthn
*/
$jsonData['webauthn'] = $this->_getWebauthn();
return json_encode($jsonData);
}
@ -238,6 +235,24 @@ class ConfigDataExporter {
return $data;
}
/**
* Returns the content of the webauthn database.
*
* @return array data
*/
public function _getWebauthn() {
$data = array();
if ((version_compare(phpversion(), '7.2.0') >= 0)
&& extension_loaded('PDO')
&& in_array('sqlite', \PDO::getAvailableDrivers())) {
include_once __DIR__ . '/webauthn.inc';
$webauthnManager = new WebauthnManager();
$webauthnDatabase = $webauthnManager->getDatabase();
$data = $webauthnDatabase->export();
}
return $data;
}
}
/**
@ -296,6 +311,13 @@ class ConfigDataImporter {
case 'selfServiceProfiles':
$steps[] = new ImporterStep(_('Self service profiles'), 'selfServiceProfiles', $value);
break;
case 'webauthn':
if ((version_compare(phpversion(), '7.2.0') >= 0)
&& extension_loaded('PDO')
&& in_array('sqlite', \PDO::getAvailableDrivers())) {
$steps[] = new ImporterStep(_('WebAuthn devices'), 'webauthn', $value);
}
break;
default:
logNewMessage(LOG_WARNING, 'Unknown import type: ' . $key);
}
@ -343,6 +365,9 @@ class ConfigDataImporter {
case 'selfServiceProfiles':
$this->importSelfServiceProfiles($step);
break;
case 'webauthn':
$this->importWebauthn($step);
break;
default:
logNewMessage(LOG_WARNING, 'Unknown import type: ' . $key);
}
@ -560,6 +585,21 @@ class ConfigDataImporter {
}
}
/**
* Imports the webauthn data.
*
* @param ImporterStep $step importer step
* @throws LAMException error saving profiles
*/
private function importWebauthn($step) {
$failedNames = array();
$data = $step->getValue();
include_once __DIR__ . '/webauthn.inc';
$webauthnManager = new WebauthnManager();
$webauthnDatabase = $webauthnManager->getDatabase();
$webauthnDatabase->import($data);
}
}
/**

View File

@ -650,6 +650,53 @@ class PublicKeyCredentialSourceRepositorySQLite implements PublicKeyCredentialSo
$pdo->exec($sql);
}
/**
* Exports all entries.
*
* @return array data
*/
public function export() {
$pdo = $this->getPDO();
$statement = $pdo->prepare('select * from ' . self::TABLE_NAME);
$statement->execute();
$dbData = $statement->fetchAll();
$data = array();
foreach ($dbData as $dbRow) {
$data[] = array(
'userId' => $dbRow['userId'],
'credentialId' => $dbRow['credentialId'],
'credentialSource' => $dbRow['credentialSource'],
'registrationTime' => $dbRow['registrationTime'],
'lastUseTime' => $dbRow['lastUseTime'],
);
}
return $data;
}
/**
* Imports entries from export data.
*
* @param array $data export data
*/
public function import($data) {
$pdo = $this->getPDO();
$statement = $pdo->prepare('delete from ' . self::TABLE_NAME);
$statement->execute();
if (empty($data)) {
return;
}
foreach ($data as $dbRow) {
$statement = $pdo->prepare('insert into ' . self::TABLE_NAME . ' (userId, credentialId, credentialSource, registrationTime, lastUseTime) VALUES (?, ?, ?, ?, ?)');
$statement->execute(array(
$dbRow['userId'],
$dbRow['credentialId'],
$dbRow['credentialSource'],
$dbRow['registrationTime'],
$dbRow['lastUseTime']
));
}
}
}