First code for Spectre import.

This commit is contained in:
James Cole
2018-05-14 17:59:43 +02:00
parent 96411b17e9
commit 69019d5215
8 changed files with 206 additions and 179 deletions

View File

@@ -116,7 +116,7 @@ class CreateImport extends Command
// make prerequisites thing. // make prerequisites thing.
$class = (string)config(sprintf('import.prerequisites.%s', $provider)); $class = (string)config(sprintf('import.prerequisites.%s', $provider));
if (!class_exists($class)) { if (!class_exists($class)) {
throw new FireflyException(sprintf('No class to handle configuration for "%s".', $provider)); // @codeCoverageIgnore throw new FireflyException(sprintf('No class to handle prerequisites for "%s".', $provider)); // @codeCoverageIgnore
} }
/** @var PrerequisitesInterface $object */ /** @var PrerequisitesInterface $object */
$object = app($class); $object = app($class);

View File

@@ -85,14 +85,13 @@ class IndexController extends Controller
Log::debug(sprintf('Created job #%d for provider %s', $importJob->id, $importProvider)); Log::debug(sprintf('Created job #%d for provider %s', $importJob->id, $importProvider));
$hasPreReq = (bool)config(sprintf('import.has_prereq.%s', $importProvider)); $hasPreReq = (bool)config(sprintf('import.has_prereq.%s', $importProvider));
$hasConfig = (bool)config(sprintf('import.has_config.%s', $importProvider)); $hasConfig = (bool)config(sprintf('import.has_job_config.%s', $importProvider));
// if job provider has no prerequisites: // if job provider has no prerequisites:
if ($hasPreReq === false) { if ($hasPreReq === false) {
Log::debug('Provider has no prerequisites. Continue.'); Log::debug('Provider has no prerequisites. Continue.');
// @codeCoverageIgnoreStart
// if job provider also has no configuration: // if job provider also has no configuration:
if ($hasConfig === false) { if ($hasConfig === false) {
Log::debug('Provider has no configuration. Job is ready to start.'); Log::debug('Provider needs no configuration for job. Job is ready to start.');
$this->repository->updateStatus($importJob, 'ready_to_run'); $this->repository->updateStatus($importJob, 'ready_to_run');
Log::debug('Redirect to status-page.'); Log::debug('Redirect to status-page.');
@@ -106,13 +105,12 @@ class IndexController extends Controller
Log::debug('Redirect to configuration.'); Log::debug('Redirect to configuration.');
return redirect(route('import.job.configuration.index', [$importJob->key])); return redirect(route('import.job.configuration.index', [$importJob->key]));
// @codeCoverageIgnoreEnd
} }
Log::debug('Job provider has prerequisites.'); Log::debug('Job provider has prerequisites.');
// if need to set prerequisites, do that first. // if need to set prerequisites, do that first.
$class = (string)config(sprintf('import.prerequisites.%s', $importProvider)); $class = (string)config(sprintf('import.prerequisites.%s', $importProvider));
if (!class_exists($class)) { if (!class_exists($class)) {
throw new FireflyException(sprintf('No class to handle configuration for "%s".', $importProvider)); // @codeCoverageIgnore throw new FireflyException(sprintf('No class to handle prerequisites for "%s".', $importProvider)); // @codeCoverageIgnore
} }
/** @var PrerequisitesInterface $providerPre */ /** @var PrerequisitesInterface $providerPre */
$providerPre = app($class); $providerPre = app($class);
@@ -189,7 +187,6 @@ class IndexController extends Controller
$providers[$providerName] = [ $providers[$providerName] = [
'has_prereq' => (bool)config('import.has_prereq.' . $providerName), 'has_prereq' => (bool)config('import.has_prereq.' . $providerName),
'has_config' => (bool)config('import.has_config.' . $providerName),
]; ];
$class = (string)config(sprintf('import.prerequisites.%s', $providerName)); $class = (string)config(sprintf('import.prerequisites.%s', $providerName));
$result = false; $result = false;

View File

@@ -83,7 +83,7 @@ class JobConfigurationController extends Controller
// if provider has no config, just push it through: // if provider has no config, just push it through:
$importProvider = $importJob->provider; $importProvider = $importJob->provider;
if (!(bool)config(sprintf('import.has_config.%s', $importProvider))) { if (!(bool)config(sprintf('import.has_job_config.%s', $importProvider))) {
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
Log::debug('Job needs no config, is ready to run!'); Log::debug('Job needs no config, is ready to run!');
$this->repository->updateStatus($importJob, 'ready_to_run'); $this->repository->updateStatus($importJob, 'ready_to_run');

View File

@@ -83,7 +83,7 @@ class PrerequisitesController extends Controller
app('view')->share('subTitle', trans('import.prerequisites_breadcrumb_' . $importProvider)); app('view')->share('subTitle', trans('import.prerequisites_breadcrumb_' . $importProvider));
$class = (string)config(sprintf('import.prerequisites.%s', $importProvider)); $class = (string)config(sprintf('import.prerequisites.%s', $importProvider));
if (!class_exists($class)) { if (!class_exists($class)) {
throw new FireflyException(sprintf('No class to handle configuration for "%s".', $importProvider)); // @codeCoverageIgnore throw new FireflyException(sprintf('No class to handle prerequisites for "%s".', $importProvider)); // @codeCoverageIgnore
} }
/** @var PrerequisitesInterface $object */ /** @var PrerequisitesInterface $object */
$object = app($class); $object = app($class);

View File

@@ -24,169 +24,166 @@ namespace FireflyIII\Import\Prerequisites;
use FireflyIII\Models\Preference; use FireflyIII\Models\Preference;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag; use Illuminate\Support\MessageBag;
use Log;
use Preferences;
/** /**
* This class contains all the routines necessary to connect to Spectre. * This class contains all the routines necessary to connect to Spectre.
*/ */
class SpectrePrerequisites implements PrerequisitesInterface class SpectrePrerequisites implements PrerequisitesInterface
{ {
// /** @var User */ /** @var User */
// private $user; private $user;
// //
// /** // /**
// * Returns view name that allows user to fill in prerequisites. Currently asks for the API key. // * Returns view name that allows user to fill in prerequisites. Currently asks for the API key.
// * // *
// * @return string // * @return string
// */ // */
// public function getView(): string // public function getView(): string
// { // {
// return 'import.spectre.prerequisites'; // return 'import.spectre.prerequisites';
// } // }
// //
// /** // /**
// * Returns any values required for the prerequisites-view. // * Returns any values required for the prerequisites-view.
// * // *
// * @return array // * @return array
// */ // */
// public function getViewParameters(): array // public function getViewParameters(): array
// { // {
// $publicKey = $this->getPublicKey(); // $publicKey = $this->getPublicKey();
// $subTitle = (string)trans('import.spectre_title'); // $subTitle = (string)trans('import.spectre_title');
// $subTitleIcon = 'fa-archive'; // $subTitleIcon = 'fa-archive';
// //
// return compact('publicKey', 'subTitle', 'subTitleIcon'); // return compact('publicKey', 'subTitle', 'subTitleIcon');
// } // }
// //
// /** // /**
// * Returns if this import method has any special prerequisites such as config // * Returns if this import method has any special prerequisites such as config
// * variables or other things. The only thing we verify is the presence of the API key. Everything else // * variables or other things. The only thing we verify is the presence of the API key. Everything else
// * tumbles into place: no installation token? Will be requested. No device server? Will be created. Etc. // * tumbles into place: no installation token? Will be requested. No device server? Will be created. Etc.
// * // *
// * @return bool // * @return bool
// */ // */
// public function hasPrerequisites(): bool // public function hasPrerequisites(): bool
// { // {
// $values = [ // $values = [
// Preferences::getForUser($this->user, 'spectre_app_id', false), // Preferences::getForUser($this->user, 'spectre_app_id', false),
// Preferences::getForUser($this->user, 'spectre_secret', false), // Preferences::getForUser($this->user, 'spectre_secret', false),
// ]; // ];
// /** @var Preference $value */ // /** @var Preference $value */
// foreach ($values as $value) { // foreach ($values as $value) {
// if (false === $value->data || null === $value->data) { // if (false === $value->data || null === $value->data) {
// Log::info(sprintf('Config var "%s" is missing.', $value->name)); // Log::info(sprintf('Config var "%s" is missing.', $value->name));
// //
// return true; // return true;
// } // }
// } // }
// Log::debug('All prerequisites are here!'); // Log::debug('All prerequisites are here!');
// //
// return false; // return false;
// } // }
// //
// /** // /**
// * Indicate if all prerequisites have been met. // * Indicate if all prerequisites have been met.
// * // *
// * @return bool // * @return bool
// */ // */
// public function isComplete(): bool // public function isComplete(): bool
// { // {
// // return true when user has set the App Id and the Spectre Secret. // // return true when user has set the App Id and the Spectre Secret.
// $values = [ // $values = [
// Preferences::getForUser($this->user, 'spectre_app_id', false), // Preferences::getForUser($this->user, 'spectre_app_id', false),
// Preferences::getForUser($this->user, 'spectre_secret', false), // Preferences::getForUser($this->user, 'spectre_secret', false),
// ]; // ];
// $result = true; // $result = true;
// /** @var Preference $value */ // /** @var Preference $value */
// foreach ($values as $value) { // foreach ($values as $value) {
// if (false === $value->data || null === $value->data) { // if (false === $value->data || null === $value->data) {
// $result = false; // $result = false;
// } // }
// } // }
// //
// return $result; // return $result;
// } // }
// //
// /** // /**
// * Set the user for this Prerequisites-routine. Class is expected to implement and save this. // * Set the user for this Prerequisites-routine. Class is expected to implement and save this.
// * // *
// * @param User $user // * @param User $user
// */ // */
// public function setUser(User $user): void // public function setUser(User $user): void
// { // {
// $this->user = $user; // $this->user = $user;
// //
// } // }
// //
// /** // /**
// * This method responds to the user's submission of an API key. It tries to register this instance as a new Firefly III device. // * This method responds to the user's submission of an API key. It tries to register this instance as a new Firefly III device.
// * If this fails, the error is returned in a message bag and the user is notified (this is fairly friendly). // * If this fails, the error is returned in a message bag and the user is notified (this is fairly friendly).
// * // *
// * @param Request $request // * @param Request $request
// * // *
// * @return MessageBag // * @return MessageBag
// */ // */
// public function storePrerequisites(Request $request): MessageBag // public function storePrerequisites(Request $request): MessageBag
// { // {
// Log::debug('Storing Spectre API keys..'); // Log::debug('Storing Spectre API keys..');
// Preferences::setForUser($this->user, 'spectre_app_id', $request->get('app_id')); // Preferences::setForUser($this->user, 'spectre_app_id', $request->get('app_id'));
// Preferences::setForUser($this->user, 'spectre_secret', $request->get('secret')); // Preferences::setForUser($this->user, 'spectre_secret', $request->get('secret'));
// Log::debug('Done!'); // Log::debug('Done!');
// //
// return new MessageBag; // 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 // * 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 // * 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. // * is stored encrypted in the database so it's something.
// */ // */
// private function createKeyPair(): void // private function createKeyPair(): void
// { // {
// Log::debug('Generate new Spectre key pair for user.'); // Log::debug('Generate new Spectre key pair for user.');
// $keyConfig = [ // $keyConfig = [
// 'digest_alg' => 'sha512', // 'digest_alg' => 'sha512',
// 'private_key_bits' => 2048, // 'private_key_bits' => 2048,
// 'private_key_type' => OPENSSL_KEYTYPE_RSA, // 'private_key_type' => OPENSSL_KEYTYPE_RSA,
// ]; // ];
// // Create the private and public key // // Create the private and public key
// $res = openssl_pkey_new($keyConfig); // $res = openssl_pkey_new($keyConfig);
// //
// // Extract the private key from $res to $privKey // // Extract the private key from $res to $privKey
// $privKey = ''; // $privKey = '';
// openssl_pkey_export($res, $privKey); // openssl_pkey_export($res, $privKey);
// //
// // Extract the public key from $res to $pubKey // // Extract the public key from $res to $pubKey
// $pubKey = openssl_pkey_get_details($res); // $pubKey = openssl_pkey_get_details($res);
// //
// Preferences::setForUser($this->user, 'spectre_private_key', $privKey); // Preferences::setForUser($this->user, 'spectre_private_key', $privKey);
// Preferences::setForUser($this->user, 'spectre_public_key', $pubKey['key']); // Preferences::setForUser($this->user, 'spectre_public_key', $pubKey['key']);
// Log::debug('Created key pair'); // Log::debug('Created key pair');
// //
// } // }
// //
// /** // /**
// * Get a public key from the users preferences. // * Get a public key from the users preferences.
// * // *
// * @return string // * @return string
// */ // */
// private function getPublicKey(): string // private function getPublicKey(): string
// { // {
// Log::debug('get public key'); // Log::debug('get public key');
// $preference = Preferences::getForUser($this->user, 'spectre_public_key', null); // $preference = Preferences::getForUser($this->user, 'spectre_public_key', null);
// if (null === $preference) { // if (null === $preference) {
// Log::debug('public key is null'); // Log::debug('public key is null');
// // create key pair // // create key pair
// $this->createKeyPair(); // $this->createKeyPair();
// } // }
// $preference = Preferences::getForUser($this->user, 'spectre_public_key', null); // $preference = Preferences::getForUser($this->user, 'spectre_public_key', null);
// Log::debug('Return public key for user'); // Log::debug('Return public key for user');
// //
// return $preference->data; // return $preference->data;
// } // }
/** /**
* Returns view name that allows user to fill in prerequisites. * Returns view name that allows user to fill in prerequisites.
* *
@@ -204,7 +201,20 @@ class SpectrePrerequisites implements PrerequisitesInterface
*/ */
public function getViewParameters(): array public function getViewParameters(): array
{ {
return []; /** @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;
return [
'app_id' => $appId,
'secret' => $secret,
];
} }
/** /**
@@ -224,6 +234,7 @@ class SpectrePrerequisites implements PrerequisitesInterface
*/ */
public function setUser(User $user): void public function setUser(User $user): void
{ {
$this->user = $user;
} }
/** /**
@@ -239,4 +250,20 @@ class SpectrePrerequisites implements PrerequisitesInterface
{ {
return new MessageBag; return new MessageBag;
} }
/**
* @return bool
*/
private function hasAppId(): bool
{
$appId = app('preferences')->getForUser($this->user, 'spectre_app_id', null);
if (null === $appId) {
return false;
}
if ('' === (string)$appId->data) {
return false;
}
return true;
}
} }

View File

@@ -36,7 +36,7 @@ return [
'fake' => true, 'fake' => true,
'file' => true, 'file' => true,
'bunq' => false, 'bunq' => false,
'spectre' => false, 'spectre' => true,
'plaid' => false, 'plaid' => false,
'quovo' => false, 'quovo' => false,
'yodlee' => false, 'yodlee' => false,
@@ -75,21 +75,21 @@ return [
'prerequisites' => [ 'prerequisites' => [
'fake' => FakePrerequisites::class, 'fake' => FakePrerequisites::class,
'file' => false, 'file' => false,
'bunq' => SpectrePrerequisites::class, 'bunq' => false,
'spectre' => false, 'spectre' => SpectrePrerequisites::class,
'plaid' => false, 'plaid' => false,
'quovo' => false, 'quovo' => false,
'yodlee' => false, 'yodlee' => false,
], ],
// some providers may have extra configuration per job. // some providers may need extra configuration per job
'has_config' => [ 'has_job_config' => [
'fake' => true, 'fake' => true,
'file' => true, 'file' => true,
'bunq' => true, 'bunq' => false,
'spectre' => true, 'spectre' => false,
'plaid' => true, 'plaid' => false,
'quovo' => true, 'quovo' => false,
'yodlee' => true, 'yodlee' => false,
], ],
// if so, this is the class that handles it. // if so, this is the class that handles it.
'configuration' => [ 'configuration' => [

View File

@@ -68,6 +68,9 @@ return [
// prerequisites: // prerequisites:
'prereq_fake_title' => 'Prerequisites for an import from the fake import provider', 'prereq_fake_title' => 'Prerequisites for an import from the fake import provider',
'prereq_fake_text' => 'This fake provider requires a fake API key. It must be 32 characters long. You can use this one: 123456789012345678901234567890AA', 'prereq_fake_text' => 'This fake provider requires a fake API key. It must be 32 characters long. You can use this one: 123456789012345678901234567890AA',
'prereq_spectre_title' => 'Prerequisites for an import using the Spectre API',
'prereq_spectre_text' => 'In order to import data using the Spectre API (v4), you must provide Firefly III with two secret values. They can be found on the <a href="https://www.saltedge.com/clients/profile/secrets">secrets page</a>.',
'prereq_spectre_pub' => 'Likewise, the Spectre API needs to know the public key you see below. Without it, it will not recognize you. Please enter this public key on your <a href="https://www.saltedge.com/clients/profile/secrets">secrets page</a>.',
// prerequisites success messages: // prerequisites success messages:
'prerequisites_saved_for_fake' => 'Fake API key stored successfully!', 'prerequisites_saved_for_fake' => 'Fake API key stored successfully!',

View File

@@ -10,13 +10,13 @@
<div class="col-lg-12 col-md-12 col-sm-12"> <div class="col-lg-12 col-md-12 col-sm-12">
<div class="box box-default"> <div class="box box-default">
<div class="box-header with-border"> <div class="box-header with-border">
<h3 class="box-title">{{ trans('import.spectre_prerequisites_title') }}</h3> <h3 class="box-title">{{ trans('import.prereq_spectre_title') }}</h3>
</div> </div>
<div class="box-body"> <div class="box-body">
<div class="row"> <div class="row">
<div class="col-lg-8"> <div class="col-lg-8">
<p> <p>
{{ trans('import.spectre_prerequisites_text')|raw }} {{ trans('import.prereq_spectre_text')|raw }}
</p> </p>
</div> </div>
</div> </div>
@@ -29,7 +29,7 @@
</div> </div>
<div class="row"> <div class="row">
<div class="col-lg-8"> <div class="col-lg-8">
<p>{{ trans('import.spectre_enter_pub_key')|raw }}</p> <p>{{ trans('import.prereq_spectre_pub')|raw }}</p>
<div class="form-group" id="pub_key_holder"> <div class="form-group" id="pub_key_holder">
<label for="ffInput_pub_key_holder" class="col-sm-4 control-label">{{ trans('form.public_key') }}</label> <label for="ffInput_pub_key_holder" class="col-sm-4 control-label">{{ trans('form.public_key') }}</label>