First code for Spectre login and import routine.

This commit is contained in:
James Cole
2018-05-14 20:21:00 +02:00
parent a9c8c8384d
commit 9f26757e8a
8 changed files with 286 additions and 35 deletions

View File

@@ -25,6 +25,7 @@ namespace FireflyIII\Import\Prerequisites;
use FireflyIII\Models\Preference;
use FireflyIII\User;
use Illuminate\Support\MessageBag;
use Log;
/**
* This class contains all the routines necessary to connect to Spectre.
@@ -204,16 +205,15 @@ class SpectrePrerequisites implements PrerequisitesInterface
/** @var Preference $appIdPreference */
$appIdPreference = app('preferences')->getForUser($this->user, 'spectre_app_id', null);
$appId = null === $appIdPreference ? '' : $appIdPreference->data;
/** @var Preference $secretPreference */
$secretPreference = app('preferences')->getForUser($this->user, 'spectre_secret', null);
$secret = null === $secretPreference ? '' : $secretPreference->data;
$publicKey = $this->getPublicKey();
return [
'app_id' => $appId,
'secret' => $secret,
'app_id' => $appId,
'secret' => $secret,
'public_key' => $publicKey,
];
}
@@ -224,7 +224,7 @@ class SpectrePrerequisites implements PrerequisitesInterface
*/
public function isComplete(): bool
{
return false;
return $this->hasAppId() && $this->hasSecret();
}
/**
@@ -248,9 +248,63 @@ class SpectrePrerequisites implements PrerequisitesInterface
*/
public function storePrerequisites(array $data): MessageBag
{
Log::debug('Storing Spectre API keys..');
app('preferences')->setForUser($this->user, 'spectre_app_id',$data['app_id'] ?? null);
app('preferences')->setForUser($this->user, 'spectre_secret', $data['secret'] ?? null);
Log::debug('Done!');
return new MessageBag;
}
/**
* This method creates a new public/private keypair for the user. This isn't really secure, since the key is generated on the fly with
* no regards for HSM's, smart cards or other things. It would require some low level programming to get this right. But the private key
* is stored encrypted in the database so it's something.
*/
private function createKeyPair(): void
{
Log::debug('Generate new Spectre key pair for user.');
$keyConfig = [
'digest_alg' => 'sha512',
'private_key_bits' => 2048,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
];
// Create the private and public key
$res = openssl_pkey_new($keyConfig);
// Extract the private key from $res to $privKey
$privKey = '';
openssl_pkey_export($res, $privKey);
// Extract the public key from $res to $pubKey
$pubKey = openssl_pkey_get_details($res);
app('preferences')->setForUser($this->user, 'spectre_private_key', $privKey);
app('preferences')->setForUser($this->user, 'spectre_public_key', $pubKey['key']);
Log::debug('Created key pair');
}
/**
* Get a public key from the users preferences.
*
* @return string
*/
private function getPublicKey(): string
{
Log::debug('get public key');
$preference = app('preferences')->getForUser($this->user, 'spectre_public_key', null);
if (null === $preference) {
Log::debug('public key is null');
// create key pair
$this->createKeyPair();
}
$preference = app('preferences')->getForUser($this->user, 'spectre_public_key', null);
Log::debug('Return public key for user');
return $preference->data;
}
/**
* @return bool
*/
@@ -266,4 +320,20 @@ class SpectrePrerequisites implements PrerequisitesInterface
return true;
}
/**
* @return bool
*/
private function hasSecret(): bool
{
$secret = app('preferences')->getForUser($this->user, 'spectre_secret', null);
if (null === $secret) {
return false;
}
if ('' === (string)$secret->data) {
return false;
}
return true;
}
}

View File

@@ -24,14 +24,68 @@ namespace FireflyIII\Import\Routine;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use FireflyIII\Support\Import\Routine\Spectre\StageNewHandler;
/**
* @deprecated
* @codeCoverageIgnore
* Class FileRoutine
*/
class SpectreRoutine implements RoutineInterface
{
/** @var ImportJob */
private $importJob;
/** @var ImportJobRepositoryInterface */
private $repository;
/**
* At the end of each run(), the import routine must set the job to the expected status.
*
* The final status of the routine must be "provider_finished".
*
* Spectre:
* Stage new:
* - StageNewHandler
*
* @return bool
* @throws FireflyException
*/
public function run(): void
{
if ($this->importJob->status === 'ready_to_run') {
switch ($this->importJob->stage) {
default:
throw new FireflyException(sprintf('SpectreRoutine cannot handle stage "%s".', $this->importJob->stage));
case 'new':
/** @var StageNewHandler $handler */
$handler = app(StageNewHandler::class);
$handler->setImportJob($this->importJob);
$handler->run();
$this->repository->setStage($this->importJob, 'authenticate');
break;
}
}
}
/**
* @param ImportJob $importJob
*
* @return void
*/
public function setImportJob(ImportJob $importJob): void
{
$this->importJob = $importJob;
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($importJob->user);
}
// /** @var Collection */
// public $errors;
// /** @var Collection */
@@ -570,28 +624,5 @@ class SpectreRoutine implements RoutineInterface
// {
// $this->repository->setStatus($this->job, $status);
// }
/**
* At the end of each run(), the import routine must set the job to the expected status.
*
* The final status of the routine must be "provider_finished".
*
* @return bool
* @throws FireflyException
*/
public function run(): void
{
// TODO: Implement run() method.
throw new NotImplementedException;
}
/**
* @param ImportJob $importJob
*
* @return void
*/
public function setImportJob(ImportJob $importJob): void
{
// TODO: Implement setImportJob() method.
throw new NotImplementedException;
}
}