Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
First code for Spectre import.
James Cole69019d521519
diff --git a/app/Console/Commands/CreateImport.php b/app/Console/Commands/CreateImport.phpindex 0241976e6e..8c23105c0a 100644--- a/app/Console/Commands/CreateImport.php+++ b/app/Console/Commands/CreateImport.php@@ -116,7 +116,7 @@ class CreateImport extends Command// make prerequisites thing.$class = (string)config(sprintf('import.prerequisites.%s', $provider));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 */$object = app($class);diff --git a/app/Http/Controllers/Import/IndexController.php b/app/Http/Controllers/Import/IndexController.phpindex 8ddf08856d..567c386911 100644--- a/app/Http/Controllers/Import/IndexController.php+++ b/app/Http/Controllers/Import/IndexController.php@@ -85,14 +85,13 @@ class IndexController extends ControllerLog::debug(sprintf('Created job #%d for provider %s', $importJob->id, $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 ($hasPreReq === false) {Log::debug('Provider has no prerequisites. Continue.');- // @codeCoverageIgnoreStart// if job provider also has no configuration: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');Log::debug('Redirect to status-page.');@@ -106,13 +105,12 @@ class IndexController extends ControllerLog::debug('Redirect to configuration.');return redirect(route('import.job.configuration.index', [$importJob->key]));- // @codeCoverageIgnoreEnd}Log::debug('Job provider has prerequisites.');// if need to set prerequisites, do that first.$class = (string)config(sprintf('import.prerequisites.%s', $importProvider));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 */$providerPre = app($class);@@ -189,7 +187,6 @@ class IndexController extends Controller$providers[$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));$result = false;diff --git a/app/Http/Controllers/Import/JobConfigurationController.php b/app/Http/Controllers/Import/JobConfigurationController.phpindex 2273cf0cbc..4bd705396f 100644--- a/app/Http/Controllers/Import/JobConfigurationController.php+++ b/app/Http/Controllers/Import/JobConfigurationController.php@@ -83,7 +83,7 @@ class JobConfigurationController extends Controller// if provider has no config, just push it through:$importProvider = $importJob->provider;- if (!(bool)config(sprintf('import.has_config.%s', $importProvider))) {+ if (!(bool)config(sprintf('import.has_job_config.%s', $importProvider))) {// @codeCoverageIgnoreStartLog::debug('Job needs no config, is ready to run!');$this->repository->updateStatus($importJob, 'ready_to_run');diff --git a/app/Http/Controllers/Import/PrerequisitesController.php b/app/Http/Controllers/Import/PrerequisitesController.phpindex b576e5780c..7a6668d708 100644--- a/app/Http/Controllers/Import/PrerequisitesController.php+++ b/app/Http/Controllers/Import/PrerequisitesController.php@@ -83,7 +83,7 @@ class PrerequisitesController extends Controllerapp('view')->share('subTitle', trans('import.prerequisites_breadcrumb_' . $importProvider));$class = (string)config(sprintf('import.prerequisites.%s', $importProvider));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 */$object = app($class);diff --git a/app/Import/Prerequisites/SpectrePrerequisites.php b/app/Import/Prerequisites/SpectrePrerequisites.phpindex 4dd96c6809..719e176603 100644--- a/app/Import/Prerequisites/SpectrePrerequisites.php+++ b/app/Import/Prerequisites/SpectrePrerequisites.php@@ -24,169 +24,166 @@ namespace FireflyIII\Import\Prerequisites;use FireflyIII\Models\Preference;use FireflyIII\User;-use Illuminate\Http\Request;use Illuminate\Support\MessageBag;-use Log;-use Preferences;/*** This class contains all the routines necessary to connect to Spectre.*/class SpectrePrerequisites implements PrerequisitesInterface{-// /** @var User */-// private $user;-//-// /**-// * Returns view name that allows user to fill in prerequisites. Currently asks for the API key.-// *-// * @return string-// */-// public function getView(): string-// {-// return 'import.spectre.prerequisites';-// }-//-// /**-// * Returns any values required for the prerequisites-view.-// *-// * @return array-// */-// public function getViewParameters(): array-// {-// $publicKey = $this->getPublicKey();-// $subTitle = (string)trans('import.spectre_title');-// $subTitleIcon = 'fa-archive';-//-// return compact('publicKey', 'subTitle', 'subTitleIcon');-// }-//-// /**-// * 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-// * tumbles into place: no installation token? Will be requested. No device server? Will be created. Etc.-// *-// * @return bool-// */-// public function hasPrerequisites(): bool-// {-// $values = [-// Preferences::getForUser($this->user, 'spectre_app_id', false),-// Preferences::getForUser($this->user, 'spectre_secret', false),-// ];-// /** @var Preference $value */-// foreach ($values as $value) {-// if (false === $value->data || null === $value->data) {-// Log::info(sprintf('Config var "%s" is missing.', $value->name));-//-// return true;-// }-// }-// Log::debug('All prerequisites are here!');-//-// return false;-// }-//-// /**-// * Indicate if all prerequisites have been met.-// *-// * @return bool-// */-// public function isComplete(): bool-// {-// // return true when user has set the App Id and the Spectre Secret.-// $values = [-// Preferences::getForUser($this->user, 'spectre_app_id', false),-// Preferences::getForUser($this->user, 'spectre_secret', false),-// ];-// $result = true;-// /** @var Preference $value */-// foreach ($values as $value) {-// if (false === $value->data || null === $value->data) {-// $result = false;-// }-// }-//-// return $result;-// }-//-// /**-// * Set the user for this Prerequisites-routine. Class is expected to implement and save this.-// *-// * @param User $user-// */-// public function setUser(User $user): void-// {-// $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.-// * If this fails, the error is returned in a message bag and the user is notified (this is fairly friendly).-// *-// * @param Request $request-// *-// * @return MessageBag-// */-// public function storePrerequisites(Request $request): MessageBag-// {-// Log::debug('Storing Spectre API keys..');-// Preferences::setForUser($this->user, 'spectre_app_id', $request->get('app_id'));-// Preferences::setForUser($this->user, 'spectre_secret', $request->get('secret'));-// 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);-//-// Preferences::setForUser($this->user, 'spectre_private_key', $privKey);-// 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 = Preferences::getForUser($this->user, 'spectre_public_key', null);-// if (null === $preference) {-// Log::debug('public key is null');-// // create key pair-// $this->createKeyPair();-// }-// $preference = Preferences::getForUser($this->user, 'spectre_public_key', null);-// Log::debug('Return public key for user');-//-// return $preference->data;-// }+ /** @var User */+ private $user;+ //+ // /**+ // * Returns view name that allows user to fill in prerequisites. Currently asks for the API key.+ // *+ // * @return string+ // */+ // public function getView(): string+ // {+ // return 'import.spectre.prerequisites';+ // }+ //+ // /**+ // * Returns any values required for the prerequisites-view.+ // *+ // * @return array+ // */+ // public function getViewParameters(): array+ // {+ // $publicKey = $this->getPublicKey();+ // $subTitle = (string)trans('import.spectre_title');+ // $subTitleIcon = 'fa-archive';+ //+ // return compact('publicKey', 'subTitle', 'subTitleIcon');+ // }+ //+ // /**+ // * 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+ // * tumbles into place: no installation token? Will be requested. No device server? Will be created. Etc.+ // *+ // * @return bool+ // */+ // public function hasPrerequisites(): bool+ // {+ // $values = [+ // Preferences::getForUser($this->user, 'spectre_app_id', false),+ // Preferences::getForUser($this->user, 'spectre_secret', false),+ // ];+ // /** @var Preference $value */+ // foreach ($values as $value) {+ // if (false === $value->data || null === $value->data) {+ // Log::info(sprintf('Config var "%s" is missing.', $value->name));+ //+ // return true;+ // }+ // }+ // Log::debug('All prerequisites are here!');+ //+ // return false;+ // }+ //+ // /**+ // * Indicate if all prerequisites have been met.+ // *+ // * @return bool+ // */+ // public function isComplete(): bool+ // {+ // // return true when user has set the App Id and the Spectre Secret.+ // $values = [+ // Preferences::getForUser($this->user, 'spectre_app_id', false),+ // Preferences::getForUser($this->user, 'spectre_secret', false),+ // ];+ // $result = true;+ // /** @var Preference $value */+ // foreach ($values as $value) {+ // if (false === $value->data || null === $value->data) {+ // $result = false;+ // }+ // }+ //+ // return $result;+ // }+ //+ // /**+ // * Set the user for this Prerequisites-routine. Class is expected to implement and save this.+ // *+ // * @param User $user+ // */+ // public function setUser(User $user): void+ // {+ // $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.+ // * If this fails, the error is returned in a message bag and the user is notified (this is fairly friendly).+ // *+ // * @param Request $request+ // *+ // * @return MessageBag+ // */+ // public function storePrerequisites(Request $request): MessageBag+ // {+ // Log::debug('Storing Spectre API keys..');+ // Preferences::setForUser($this->user, 'spectre_app_id', $request->get('app_id'));+ // Preferences::setForUser($this->user, 'spectre_secret', $request->get('secret'));+ // 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);+ //+ // Preferences::setForUser($this->user, 'spectre_private_key', $privKey);+ // 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 = Preferences::getForUser($this->user, 'spectre_public_key', null);+ // if (null === $preference) {+ // Log::debug('public key is null');+ // // create key pair+ // $this->createKeyPair();+ // }+ // $preference = Preferences::getForUser($this->user, 'spectre_public_key', null);+ // Log::debug('Return public key for user');+ //+ // return $preference->data;+ // }/*** Returns view name that allows user to fill in prerequisites.*@@ -204,7 +201,20 @@ class SpectrePrerequisites implements PrerequisitesInterface*/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{+ $this->user = $user;}/**@@ -239,4 +250,20 @@ class SpectrePrerequisites implements PrerequisitesInterface{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;+ }}diff --git a/config/import.php b/config/import.phpindex 958fbabf5e..1f1dd6e8d0 100644--- a/config/import.php+++ b/config/import.php@@ -36,7 +36,7 @@ return ['fake' => true,'file' => true,'bunq' => false,- 'spectre' => false,+ 'spectre' => true,'plaid' => false,'quovo' => false,'yodlee' => false,@@ -75,21 +75,21 @@ return ['prerequisites' => ['fake' => FakePrerequisites::class,'file' => false,- 'bunq' => SpectrePrerequisites::class,- 'spectre' => false,+ 'bunq' => false,+ 'spectre' => SpectrePrerequisites::class,'plaid' => false,'quovo' => false,'yodlee' => false,],- // some providers may have extra configuration per job.- 'has_config' => [+ // some providers may need extra configuration per job+ 'has_job_config' => ['fake' => true,'file' => true,- 'bunq' => true,- 'spectre' => true,- 'plaid' => true,- 'quovo' => true,- 'yodlee' => true,+ 'bunq' => false,+ 'spectre' => false,+ 'plaid' => false,+ 'quovo' => false,+ 'yodlee' => false,],// if so, this is the class that handles it.'configuration' => [diff --git a/resources/lang/en_US/import.php b/resources/lang/en_US/import.phpindex bf8bcc9c81..de5e397c0c 100644--- a/resources/lang/en_US/import.php+++ b/resources/lang/en_US/import.php@@ -68,6 +68,9 @@ return [// prerequisites:'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_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_saved_for_fake' => 'Fake API key stored successfully!',diff --git a/resources/views/import/spectre/prerequisites.twig b/resources/views/import/spectre/prerequisites.twigindex b93dfe60ea..391b53b27a 100644--- a/resources/views/import/spectre/prerequisites.twig+++ b/resources/views/import/spectre/prerequisites.twig@@ -10,13 +10,13 @@<div class="col-lg-12 col-md-12 col-sm-12"><div class="box box-default"><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 class="box-body"><div class="row"><div class="col-lg-8"><p>- {{ trans('import.spectre_prerequisites_text')|raw }}+ {{ trans('import.prereq_spectre_text')|raw }}</p></div></div>@@ -29,7 +29,7 @@</div><div class="row"><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"><label for="ffInput_pub_key_holder" class="col-sm-4 control-label">{{ trans('form.public_key') }}</label>