Merge branch 'spectre' into develop

* spectre:
  Fix encryption.
  Can handle some mandatory fields (not all).
  More code for Spectre import.
  Initial code to get providers from Spectre.
  Exceptions when class does not exist.
This commit is contained in:
James Cole 2017-12-11 14:53:01 +01:00
commit fd7a293f4b
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
26 changed files with 1999 additions and 85 deletions

View File

@ -22,8 +22,9 @@ declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Import;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Support\Import\Information\InformationInterface;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use FireflyIII\Support\Import\Prerequisites\PrerequisitesInterface;
use Illuminate\Http\Request;
use Log;
@ -31,66 +32,27 @@ use Session;
class BankController extends Controller
{
/**
* This method must ask the user all parameters necessary to start importing data. This may not be enough
* to finish the import itself (ie. mapping) but it should be enough to begin: accounts to import from,
* accounts to import into, data ranges, etc.
*
* @param string $bank
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
*/
public function form(string $bank)
{
$class = config(sprintf('firefly.import_pre.%s', $bank));
/** @var PrerequisitesInterface $object */
$object = app($class);
$object->setUser(auth()->user());
if ($object->hasPrerequisites()) {
return redirect(route('import.bank.prerequisites', [$bank]));
}
$class = config(sprintf('firefly.import_info.%s', $bank));
/** @var InformationInterface $object */
$object = app($class);
$object->setUser(auth()->user());
$remoteAccounts = $object->getAccounts();
return view('import.bank.form', compact('remoteAccounts', 'bank'));
}
/**
* With the information given in the submitted form Firefly III will call upon the bank's classes to return transaction
* information as requested. The user will be able to map unknown data and continue. Or maybe, it's put into some kind of
* fake CSV file and forwarded to the import routine.
* Once there are no prerequisites, this method will create an importjob object and
* redirect the user to a view where this object can be used by a bank specific
* class to process.
*
* @param Request $request
* @param string $bank
* @param ImportJobRepositoryInterface $repository
* @param string $bank
*
* @return \Illuminate\Http\RedirectResponse|null
* @throws FireflyException
*/
public function postForm(Request $request, string $bank)
public function createJob(ImportJobRepositoryInterface $repository, string $bank)
{
$class = config(sprintf('firefly.import_pre.%s', $bank));
/** @var PrerequisitesInterface $object */
$object = app($class);
$object->setUser(auth()->user());
if ($object->hasPrerequisites()) {
return redirect(route('import.bank.prerequisites', [$bank]));
if (!class_exists($class)) {
throw new FireflyException(sprintf('Cannot find class %s', $class));
}
$remoteAccounts = $request->get('do_import');
if (!is_array($remoteAccounts) || 0 === count($remoteAccounts)) {
Session::flash('error', 'Must select accounts');
$importJob = $repository->create($bank);
return redirect(route('import.bank.form', [$bank]));
}
$remoteAccounts = array_keys($remoteAccounts);
$class = config(sprintf('firefly.import_pre.%s', $bank));
// get import file
unset($remoteAccounts, $class);
// get import config
return redirect(route('import.file.configure', [$importJob->key]));
}
/**
@ -105,18 +67,22 @@ class BankController extends Controller
* @param string $bank
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @throws FireflyException
*/
public function postPrerequisites(Request $request, string $bank)
{
Log::debug(sprintf('Now in postPrerequisites for %s', $bank));
$class = config(sprintf('firefly.import_pre.%s', $bank));
if (!class_exists($class)) {
throw new FireflyException(sprintf('Cannot find class %s', $class));
}
/** @var PrerequisitesInterface $object */
$object = app($class);
$object->setUser(auth()->user());
if (!$object->hasPrerequisites()) {
Log::debug(sprintf('No more prerequisites for %s, move to form.', $bank));
return redirect(route('import.bank.form', [$bank]));
return redirect(route('import.bank.create-job', [$bank]));
}
Log::debug('Going to store entered preprerequisites.');
// store post data
@ -128,7 +94,7 @@ class BankController extends Controller
return redirect(route('import.bank.prerequisites', [$bank]));
}
return redirect(route('import.bank.form', [$bank]));
return redirect(route('import.bank.create-job', [$bank]));
}
/**
@ -138,21 +104,26 @@ class BankController extends Controller
* @param string $bank
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
* @throws FireflyException
*/
public function prerequisites(string $bank)
{
$class = config(sprintf('firefly.import_pre.%s', $bank));
if (!class_exists($class)) {
throw new FireflyException(sprintf('Cannot find class %s', $class));
}
/** @var PrerequisitesInterface $object */
$object = app($class);
$object->setUser(auth()->user());
if ($object->hasPrerequisites()) {
$view = $object->getView();
$parameters = $object->getViewParameters();
$parameters = ['title' => strval(trans('firefly.import_index_title')), 'mainTitleIcon' => 'fa-archive'];
$parameters = $object->getViewParameters() + $parameters;
return view($view, $parameters);
}
return redirect(route('import.bank.form', [$bank]));
return redirect(route('import.bank.create-job', [$bank]));
}
}

View File

@ -0,0 +1,185 @@
<?php
/**
* SpectreConfigurator.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Import\Configurator;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\ImportJob;
use FireflyIII\Support\Import\Configuration\ConfigurationInterface;
use FireflyIII\Support\Import\Configuration\Spectre\SelectProvider;
use FireflyIII\Support\Import\Configuration\Spectre\InputMandatory;
use FireflyIII\Support\Import\Configuration\Spectre\SelectCountry;
use Log;
/**
* Class SpectreConfigurator.
*/
class SpectreConfigurator implements ConfiguratorInterface
{
/** @var ImportJob */
private $job;
/** @var string */
private $warning = '';
/**
* ConfiguratorInterface constructor.
*/
public function __construct()
{
}
/**
* Store any data from the $data array into the job.
*
* @param array $data
*
* @return bool
*
* @throws FireflyException
*/
public function configureJob(array $data): bool
{
$class = $this->getConfigurationClass();
$job = $this->job;
/** @var ConfigurationInterface $object */
$object = new $class($this->job);
$object->setJob($job);
$result = $object->storeConfiguration($data);
$this->warning = $object->getWarningMessage();
return $result;
}
/**
* Return the data required for the next step in the job configuration.
*
* @return array
*
* @throws FireflyException
*/
public function getNextData(): array
{
$class = $this->getConfigurationClass();
$job = $this->job;
/** @var ConfigurationInterface $object */
$object = app($class);
$object->setJob($job);
return $object->getData();
}
/**
* @return string
*
* @throws FireflyException
*/
public function getNextView(): string
{
if (!$this->job->configuration['selected-country']) {
return 'import.spectre.select-country';
}
if (!$this->job->configuration['selected-provider']) {
return 'import.spectre.select-provider';
}
if (!$this->job->configuration['has-input-mandatory']) {
return 'import.spectre.input-fields';
}
throw new FireflyException('No view for state');
}
/**
* Return possible warning to user.
*
* @return string
*/
public function getWarningMessage(): string
{
return $this->warning;
}
/**
* @return bool
*/
public function isJobConfigured(): bool
{
$config = $this->job->configuration;
$config['selected-country'] = $config['selected-country'] ?? false;
$config['selected-provider'] = $config['selected-provider'] ?? false;
$config['has-input-mandatory'] = $config['has-input-mandatory'] ?? false;
$this->job->configuration = $config;
$this->job->save();
if ($config['selected-country'] && $config['selected-provider'] && $config['has-input-mandatory'] && false) {
return true;
}
return false;
}
/**
* @param ImportJob $job
*/
public function setJob(ImportJob $job)
{
$this->job = $job;
if (null === $this->job->configuration || 0 === count($this->job->configuration)) {
Log::debug(sprintf('Gave import job %s initial configuration.', $this->job->key));
$this->job->configuration = [
'selected-country' => false,
];
$this->job->save();
}
}
/**
* @return string
*
* @throws FireflyException
*/
private function getConfigurationClass(): string
{
$class = false;
switch (true) {
case !$this->job->configuration['selected-country']:
$class = SelectCountry::class;
break;
case !$this->job->configuration['selected-provider']:
$class = SelectProvider::class;
break;
case !$this->job->configuration['has-input-mandatory']:
$class = InputMandatory::class;
default:
break;
}
if (false === $class || 0 === strlen($class)) {
throw new FireflyException('Cannot handle current job state in getConfigurationClass().');
}
if (!class_exists($class)) {
throw new FireflyException(sprintf('Class %s does not exist in getConfigurationClass().', $class));
}
return $class;
}
}

View File

@ -0,0 +1,77 @@
<?php
namespace FireflyIII\Jobs;
use FireflyIII\Models\Configuration;
use FireflyIII\Models\SpectreProvider;
use FireflyIII\Services\Spectre\Request\ListProvidersRequest;
use FireflyIII\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Log;
class GetSpectreProviders implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $user;
/**
* Create a new job instance.
*/
public function __construct(User $user)
{
$this->user = $user;
Log::debug('Constructed job GetSpectreProviders');
}
/**
* Execute the job.
*/
public function handle()
{
/** @var Configuration $configValue */
$configValue = app('fireflyconfig')->get('spectre_provider_download', 0);
$now = time();
if ($now - intval($configValue->data) < 86400) {
Log::debug(sprintf('Difference is %d, so will NOT execute job.', ($now - intval($configValue->data))));
return;
}
Log::debug(sprintf('Difference is %d, so will execute job.', ($now - intval($configValue->data))));
// get user
// fire away!
$request = new ListProvidersRequest($this->user);
$request->call();
// store all providers:
$providers = $request->getProviders();
foreach ($providers as $provider) {
// find provider?
$dbProvider = SpectreProvider::where('spectre_id', $provider['id'])->first();
if (is_null($dbProvider)) {
$dbProvider = new SpectreProvider;
}
// update fields:
$dbProvider->spectre_id = $provider['id'];
$dbProvider->code = $provider['code'];
$dbProvider->mode = $provider['mode'];
$dbProvider->status = $provider['status'];
$dbProvider->interactive = $provider['interactive'] === 1;
$dbProvider->automatic_fetch = $provider['automatic_fetch'] === 1;
$dbProvider->country_code = $provider['country_code'];
$dbProvider->data = $provider;
$dbProvider->save();
Log::debug(sprintf('Stored provider #%d under ID #%d', $provider['id'], $dbProvider->id));
}
app('fireflyconfig')->set('spectre_provider_download', time());
return;
}
}

View File

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace FireflyIII\Models;
use Illuminate\Database\Eloquent\Model;
/**
* Class SpectreProvider
*/
class SpectreProvider extends Model
{
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts
= [
'spectre_id' => 'int',
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime',
'interactive' => 'boolean',
'automatic_fetch' => 'boolean',
'data' => 'array',
];
protected $fillable = ['spectre_id', 'code', 'mode', 'name', 'status', 'interactive', 'automatic_fetch', 'country_code', 'data'];
}

View File

@ -160,6 +160,9 @@ abstract class BunqRequest
return $result;
}
/**
* @return array
*/
protected function getDefaultHeaders(): array
{
$userAgent = sprintf('FireflyIII v%s', config('firefly.version'));

View File

@ -0,0 +1,80 @@
<?php
/**
* ListUserRequest.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Services\Spectre\Request;
use Log;
/**
* Class ListUserRequest.
*/
class ListProvidersRequest extends SpectreRequest
{
protected $providers = [];
/**
*
*/
public function call(): void
{
$hasNextPage = true;
$nextId = 0;
while ($hasNextPage) {
Log::debug(sprintf('Now calling for next_id %d', $nextId));
$parameters = ['include_fake_providers' => 'true', 'include_provider_fields' => 'true', 'from_id' => $nextId];
$uri = '/api/v3/providers?' . http_build_query($parameters);
$response = $this->sendSignedSpectreGet($uri, []);
// count entries:
Log::debug(sprintf('Found %d entries in data-array', count($response['data'])));
// extract next ID
$hasNextPage = false;
if (isset($response['meta']['next_id']) && intval($response['meta']['next_id']) > $nextId) {
$hasNextPage = true;
$nextId = $response['meta']['next_id'];
Log::debug(sprintf('Next ID is now %d.', $nextId));
} else {
Log::debug('No next page.');
}
// store providers:
foreach ($response['data'] as $providerArray) {
$providerId = $providerArray['id'];
$this->providers[$providerId] = $providerArray;
Log::debug(sprintf('Stored provider #%d', $providerId));
}
}
return;
}
/**
* @return array
*/
public function getProviders(): array
{
return $this->providers;
}
}

View File

@ -0,0 +1,378 @@
<?php
/**
* BunqRequest.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Services\Spectre\Request;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\User;
use Log;
use Requests;
use Requests_Exception;
//use FireflyIII\Services\Bunq\Object\ServerPublicKey;
/**
* Class BunqRequest.
*/
abstract class SpectreRequest
{
/** @var string */
protected $clientId = '';
protected $expiresAt = 0;
/** @var ServerPublicKey */
protected $serverPublicKey;
/** @var string */
protected $serviceSecret = '';
/** @var string */
private $privateKey = '';
/** @var string */
private $server = '';
/** @var User */
private $user;
/**
* SpectreRequest constructor.
*/
public function __construct(User $user)
{
$this->user = $user;
$this->server = config('firefly.spectre.server');
$this->expiresAt = time() + 180;
$privateKey = app('preferences')->get('spectre_private_key', null);
$this->privateKey = $privateKey->data;
// set client ID
$clientId = app('preferences')->get('spectre_client_id', null);
$this->clientId = $clientId->data;
// set service secret
$serviceSecret = app('preferences')->get('spectre_service_secret', null);
$this->serviceSecret = $serviceSecret->data;
}
/**
*
*/
abstract public function call(): void;
/**
* @return string
*/
public function getClientId(): string
{
return $this->clientId;
}
/**
* @param string $clientId
*/
public function setClientId(string $clientId): void
{
$this->clientId = $clientId;
}
/**
* @return string
*/
public function getServer(): string
{
return $this->server;
}
/**
* @return ServerPublicKey
*/
public function getServerPublicKey(): ServerPublicKey
{
return $this->serverPublicKey;
}
/**
* @param ServerPublicKey $serverPublicKey
*/
public function setServerPublicKey(ServerPublicKey $serverPublicKey)
{
$this->serverPublicKey = $serverPublicKey;
}
/**
* @return string
*/
public function getServiceSecret(): string
{
return $this->serviceSecret;
}
/**
* @param string $serviceSecret
*/
public function setServiceSecret(string $serviceSecret): void
{
$this->serviceSecret = $serviceSecret;
}
/**
* @param string $privateKey
*/
public function setPrivateKey(string $privateKey)
{
$this->privateKey = $privateKey;
}
/**
* @param string $secret
*/
public function setSecret(string $secret)
{
$this->secret = $secret;
}
/**
* @param string $method
* @param string $uri
* @param string $data
*
* @return string
*
* @throws FireflyException
*/
protected function generateSignature(string $method, string $uri, string $data): string
{
if (0 === strlen($this->privateKey)) {
throw new FireflyException('No private key present.');
}
if ('get' === strtolower($method) || 'delete' === strtolower($method)) {
$data = '';
}
// base64(sha1_signature(private_key, "Expires-at|request_method|original_url|post_body|md5_of_uploaded_file|")))
// Prepare the signature
$toSign = $this->expiresAt . '|' . strtoupper($method) . '|' . $uri . '|' . $data . ''; // no file so no content there.
Log::debug(sprintf('String to sign: %s', $toSign));
$signature = '';
// Sign the data
openssl_sign($toSign, $signature, $this->privateKey, OPENSSL_ALGO_SHA1);
$signature = base64_encode($signature);
return $signature;
}
/**
* @return array
*/
protected function getDefaultHeaders(): array
{
$userAgent = sprintf('FireflyIII v%s', config('firefly.version'));
return [
'Client-Id' => $this->getClientId(),
'Service-Secret' => $this->getServiceSecret(),
'Accept' => 'application/json',
'Content-type' => 'application/json',
'Cache-Control' => 'no-cache',
'User-Agent' => $userAgent,
'Expires-at' => $this->expiresAt,
];
}
/**
* @param string $uri
* @param array $headers
*
* @return array
*
* @throws Exception
*/
protected function sendSignedBunqDelete(string $uri, array $headers): array
{
if (0 === strlen($this->server)) {
throw new FireflyException('No bunq server defined');
}
$fullUri = $this->server . $uri;
$signature = $this->generateSignature('delete', $uri, $headers, '');
$headers['X-Bunq-Client-Signature'] = $signature;
try {
$response = Requests::delete($fullUri, $headers);
} catch (Requests_Exception $e) {
return ['Error' => [0 => ['error_description' => $e->getMessage(), 'error_description_translated' => $e->getMessage()]]];
}
$body = $response->body;
$array = json_decode($body, true);
$responseHeaders = $response->headers->getAll();
$statusCode = intval($response->status_code);
$array['ResponseHeaders'] = $responseHeaders;
$array['ResponseStatusCode'] = $statusCode;
Log::debug(sprintf('Response to DELETE %s is %s', $fullUri, $body));
if ($this->isErrorResponse($array)) {
$this->throwResponseError($array);
}
if (!$this->verifyServerSignature($body, $responseHeaders, $statusCode)) {
throw new FireflyException(sprintf('Could not verify signature for request to "%s"', $uri));
}
return $array;
}
/**
* @param string $uri
* @param array $data
* @param array $headers
*
* @return array
*
* @throws Exception
*/
protected function sendSignedBunqPost(string $uri, array $data, array $headers): array
{
$body = json_encode($data);
$fullUri = $this->server . $uri;
$signature = $this->generateSignature('post', $uri, $headers, $body);
$headers['X-Bunq-Client-Signature'] = $signature;
try {
$response = Requests::post($fullUri, $headers, $body);
} catch (Requests_Exception $e) {
return ['Error' => [0 => ['error_description' => $e->getMessage(), 'error_description_translated' => $e->getMessage()]]];
}
$body = $response->body;
$array = json_decode($body, true);
$responseHeaders = $response->headers->getAll();
$statusCode = intval($response->status_code);
$array['ResponseHeaders'] = $responseHeaders;
$array['ResponseStatusCode'] = $statusCode;
if ($this->isErrorResponse($array)) {
$this->throwResponseError($array);
}
if (!$this->verifyServerSignature($body, $responseHeaders, $statusCode)) {
throw new FireflyException(sprintf('Could not verify signature for request to "%s"', $uri));
}
return $array;
}
/**
* @param string $uri
* @param array $data
* @param array $headers
*
* @return array
*
* @throws Exception
*/
protected function sendSignedSpectreGet(string $uri, array $data): array
{
if (0 === strlen($this->server)) {
throw new FireflyException('No Spectre server defined');
}
$headers = $this->getDefaultHeaders();
$body = json_encode($data);
$fullUri = $this->server . $uri;
$signature = $this->generateSignature('get', $fullUri, $body);
$headers['Signature'] = $signature;
Log::debug('Final headers for spectre signed get request:', $headers);
try {
$response = Requests::get($fullUri, $headers);
} catch (Requests_Exception $e) {
throw new FireflyException(sprintf('Request Exception: %s', $e->getMessage()));
}
$statusCode = intval($response->status_code);
if ($statusCode !== 200) {
throw new FireflyException(sprintf('Status code %d: %s', $statusCode, $response->body));
}
$body = $response->body;
$array = json_decode($body, true);
$responseHeaders = $response->headers->getAll();
$array['ResponseHeaders'] = $responseHeaders;
$array['ResponseStatusCode'] = $statusCode;
return $array;
}
/**
* @param string $uri
* @param array $headers
*
* @return array
*/
protected function sendUnsignedBunqDelete(string $uri, array $headers): array
{
$fullUri = $this->server . $uri;
try {
$response = Requests::delete($fullUri, $headers);
} catch (Requests_Exception $e) {
return ['Error' => [0 => ['error_description' => $e->getMessage(), 'error_description_translated' => $e->getMessage()]]];
}
$body = $response->body;
$array = json_decode($body, true);
$responseHeaders = $response->headers->getAll();
$statusCode = $response->status_code;
$array['ResponseHeaders'] = $responseHeaders;
$array['ResponseStatusCode'] = $statusCode;
if ($this->isErrorResponse($array)) {
$this->throwResponseError($array);
}
return $array;
}
/**
* @param string $uri
* @param array $data
* @param array $headers
*
* @return array
*/
protected function sendUnsignedBunqPost(string $uri, array $data, array $headers): array
{
$body = json_encode($data);
$fullUri = $this->server . $uri;
try {
$response = Requests::post($fullUri, $headers, $body);
} catch (Requests_Exception $e) {
return ['Error' => [0 => ['error_description' => $e->getMessage(), 'error_description_translated' => $e->getMessage()]]];
}
$body = $response->body;
$array = json_decode($body, true);
$responseHeaders = $response->headers->getAll();
$statusCode = $response->status_code;
$array['ResponseHeaders'] = $responseHeaders;
$array['ResponseStatusCode'] = $statusCode;
if ($this->isErrorResponse($array)) {
$this->throwResponseError($array);
}
return $array;
}
}

View File

@ -0,0 +1,101 @@
<?php
declare(strict_types=1);
namespace FireflyIII\Support\Import\Configuration\Spectre;
use Crypt;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\ImportJob;
use FireflyIII\Models\SpectreProvider;
use FireflyIII\Support\Import\Configuration\ConfigurationInterface;
/**
* Class InputMandatory
*/
class InputMandatory implements ConfigurationInterface
{
/** @var ImportJob */
private $job;
/**
* Get the data necessary to show the configuration screen.
*
* @return array
*/
public function getData(): array
{
$config = $this->job->configuration;
$providerId = $config['provider'];
$provider = SpectreProvider::where('spectre_id', $providerId)->first();
if (is_null($provider)) {
throw new FireflyException(sprintf('Cannot find Spectre provider with ID #%d', $providerId));
}
$fields = $provider->data['required_fields'] ?? [];
$positions = [];
// Obtain a list of columns
foreach ($fields as $key => $row) {
$positions[$key] = $row['position'];
}
array_multisort($positions, SORT_ASC, $fields);
$country = SelectCountry::$allCountries[$config['country']] ?? $config['country'];
return compact('provider', 'country', 'fields');
}
/**
* Return possible warning to user.
*
* @return string
*/
public function getWarningMessage(): string
{
return '';
}
/**
* @param ImportJob $job
*
* @return ConfigurationInterface
*/
public function setJob(ImportJob $job)
{
$this->job = $job;
}
/**
* Store the result.
*
* @param array $data
*
* @return bool
*/
public function storeConfiguration(array $data): bool
{
$config = $this->job->configuration;
$providerId = $config['provider'];
$provider = SpectreProvider::where('spectre_id', $providerId)->first();
if (is_null($provider)) {
throw new FireflyException(sprintf('Cannot find Spectre provider with ID #%d', $providerId));
}
$mandatory = [];
$fields = $provider->data['required_fields'] ?? [];
foreach ($fields as $field) {
$name = $field['name'];
$mandatory[$name] = Crypt::encrypt($data[$name]) ?? null;
}
// store in config of job:
$config['mandatory-fields'] = $mandatory;
$config['has-input-mandatory'] = true;
$this->job->configuration = $config;
$this->job->save();
// try to grab login for this job. See what happens?
// fire job that creates login object. user is redirected to "wait here" page (status page). Page should
// refresh and go back to interactive when user is supposed to enter SMS code or something.
// otherwise start downloading stuff
return true;
}
}

View File

@ -0,0 +1,329 @@
<?php
declare(strict_types=1);
namespace FireflyIII\Support\Import\Configuration\Spectre;
use FireflyIII\Models\ImportJob;
use FireflyIII\Models\SpectreProvider;
use FireflyIII\Support\Import\Configuration\ConfigurationInterface;
/**
* Class SelectCountry
*/
class SelectCountry implements ConfigurationInterface
{
public static $allCountries
= [
'AF' => 'Afghanistan',
'AX' => 'Aland Islands',
'AL' => 'Albania',
'DZ' => 'Algeria',
'AS' => 'American Samoa',
'AD' => 'Andorra',
'AO' => 'Angola',
'AI' => 'Anguilla',
'AQ' => 'Antarctica',
'AG' => 'Antigua and Barbuda',
'AR' => 'Argentina',
'AM' => 'Armenia',
'AW' => 'Aruba',
'AU' => 'Australia',
'AT' => 'Austria',
'AZ' => 'Azerbaijan',
'BS' => 'Bahamas',
'BH' => 'Bahrain',
'BD' => 'Bangladesh',
'BB' => 'Barbados',
'BY' => 'Belarus',
'BE' => 'Belgium',
'BZ' => 'Belize',
'BJ' => 'Benin',
'BM' => 'Bermuda',
'BT' => 'Bhutan',
'BO' => 'Bolivia',
'BQ' => 'Bonaire, Saint Eustatius and Saba',
'BA' => 'Bosnia and Herzegovina',
'BW' => 'Botswana',
'BV' => 'Bouvet Island',
'BR' => 'Brazil',
'IO' => 'British Indian Ocean Territory',
'VG' => 'British Virgin Islands',
'BN' => 'Brunei',
'BG' => 'Bulgaria',
'BF' => 'Burkina Faso',
'BI' => 'Burundi',
'KH' => 'Cambodia',
'CM' => 'Cameroon',
'CA' => 'Canada',
'CV' => 'Cape Verde',
'KY' => 'Cayman Islands',
'CF' => 'Central African Republic',
'TD' => 'Chad',
'CL' => 'Chile',
'CN' => 'China',
'CX' => 'Christmas Island',
'CC' => 'Cocos Islands',
'CO' => 'Colombia',
'KM' => 'Comoros',
'CK' => 'Cook Islands',
'CR' => 'Costa Rica',
'HR' => 'Croatia',
'CU' => 'Cuba',
'CW' => 'Curacao',
'CY' => 'Cyprus',
'CZ' => 'Czech Republic',
'CD' => 'Democratic Republic of the Congo',
'DK' => 'Denmark',
'DJ' => 'Djibouti',
'DM' => 'Dominica',
'DO' => 'Dominican Republic',
'TL' => 'East Timor',
'EC' => 'Ecuador',
'EG' => 'Egypt',
'SV' => 'El Salvador',
'GQ' => 'Equatorial Guinea',
'ER' => 'Eritrea',
'EE' => 'Estonia',
'ET' => 'Ethiopia',
'FK' => 'Falkland Islands',
'FO' => 'Faroe Islands',
'FJ' => 'Fiji',
'FI' => 'Finland',
'FR' => 'France',
'GF' => 'French Guiana',
'PF' => 'French Polynesia',
'TF' => 'French Southern Territories',
'GA' => 'Gabon',
'GM' => 'Gambia',
'GE' => 'Georgia',
'DE' => 'Germany',
'GH' => 'Ghana',
'GI' => 'Gibraltar',
'GR' => 'Greece',
'GL' => 'Greenland',
'GD' => 'Grenada',
'GP' => 'Guadeloupe',
'GU' => 'Guam',
'GT' => 'Guatemala',
'GG' => 'Guernsey',
'GN' => 'Guinea',
'GW' => 'Guinea-Bissau',
'GY' => 'Guyana',
'HT' => 'Haiti',
'HM' => 'Heard Island and McDonald Islands',
'HN' => 'Honduras',
'HK' => 'Hong Kong',
'HU' => 'Hungary',
'IS' => 'Iceland',
'IN' => 'India',
'ID' => 'Indonesia',
'IR' => 'Iran',
'IQ' => 'Iraq',
'IE' => 'Ireland',
'IM' => 'Isle of Man',
'IL' => 'Israel',
'IT' => 'Italy',
'CI' => 'Ivory Coast',
'JM' => 'Jamaica',
'JP' => 'Japan',
'JE' => 'Jersey',
'JO' => 'Jordan',
'KZ' => 'Kazakhstan',
'KE' => 'Kenya',
'KI' => 'Kiribati',
'XK' => 'Kosovo',
'KW' => 'Kuwait',
'KG' => 'Kyrgyzstan',
'LA' => 'Laos',
'LV' => 'Latvia',
'LB' => 'Lebanon',
'LS' => 'Lesotho',
'LR' => 'Liberia',
'LY' => 'Libya',
'LI' => 'Liechtenstein',
'LT' => 'Lithuania',
'LU' => 'Luxembourg',
'MO' => 'Macao',
'MK' => 'Macedonia',
'MG' => 'Madagascar',
'MW' => 'Malawi',
'MY' => 'Malaysia',
'MV' => 'Maldives',
'ML' => 'Mali',
'MT' => 'Malta',
'MH' => 'Marshall Islands',
'MQ' => 'Martinique',
'MR' => 'Mauritania',
'MU' => 'Mauritius',
'YT' => 'Mayotte',
'MX' => 'Mexico',
'FM' => 'Micronesia',
'MD' => 'Moldova',
'MC' => 'Monaco',
'MN' => 'Mongolia',
'ME' => 'Montenegro',
'MS' => 'Montserrat',
'MA' => 'Morocco',
'MZ' => 'Mozambique',
'MM' => 'Myanmar',
'NA' => 'Namibia',
'NR' => 'Nauru',
'NP' => 'Nepal',
'NL' => 'Netherlands',
'NC' => 'New Caledonia',
'NZ' => 'New Zealand',
'NI' => 'Nicaragua',
'NE' => 'Niger',
'NG' => 'Nigeria',
'NU' => 'Niue',
'NF' => 'Norfolk Island',
'KP' => 'North Korea',
'MP' => 'Northern Mariana Islands',
'NO' => 'Norway',
'OM' => 'Oman',
'PK' => 'Pakistan',
'PW' => 'Palau',
'PS' => 'Palestinian Territory',
'PA' => 'Panama',
'PG' => 'Papua New Guinea',
'PY' => 'Paraguay',
'PE' => 'Peru',
'PH' => 'Philippines',
'PN' => 'Pitcairn',
'PL' => 'Poland',
'PT' => 'Portugal',
'PR' => 'Puerto Rico',
'QA' => 'Qatar',
'CG' => 'Republic of the Congo',
'RE' => 'Reunion',
'RO' => 'Romania',
'RU' => 'Russia',
'RW' => 'Rwanda',
'BL' => 'Saint Barthelemy',
'SH' => 'Saint Helena',
'KN' => 'Saint Kitts and Nevis',
'LC' => 'Saint Lucia',
'MF' => 'Saint Martin',
'PM' => 'Saint Pierre and Miquelon',
'VC' => 'Saint Vincent and the Grenadines',
'WS' => 'Samoa',
'SM' => 'San Marino',
'ST' => 'Sao Tome and Principe',
'SA' => 'Saudi Arabia',
'SN' => 'Senegal',
'RS' => 'Serbia',
'SC' => 'Seychelles',
'SL' => 'Sierra Leone',
'SG' => 'Singapore',
'SX' => 'Sint Maarten',
'SK' => 'Slovakia',
'SI' => 'Slovenia',
'SB' => 'Solomon Islands',
'SO' => 'Somalia',
'ZA' => 'South Africa',
'GS' => 'South Georgia and the South Sandwich Islands',
'KR' => 'South Korea',
'SS' => 'South Sudan',
'ES' => 'Spain',
'LK' => 'Sri Lanka',
'SD' => 'Sudan',
'SR' => 'Suriname',
'SJ' => 'Svalbard and Jan Mayen',
'SZ' => 'Swaziland',
'SE' => 'Sweden',
'CH' => 'Switzerland',
'SY' => 'Syria',
'TW' => 'Taiwan',
'TJ' => 'Tajikistan',
'TZ' => 'Tanzania',
'TH' => 'Thailand',
'TG' => 'Togo',
'TK' => 'Tokelau',
'TO' => 'Tonga',
'TT' => 'Trinidad and Tobago',
'TN' => 'Tunisia',
'TR' => 'Turkey',
'TM' => 'Turkmenistan',
'TC' => 'Turks and Caicos Islands',
'TV' => 'Tuvalu',
'VI' => 'U.S. Virgin Islands',
'UG' => 'Uganda',
'UA' => 'Ukraine',
'AE' => 'United Arab Emirates',
'GB' => 'United Kingdom',
'US' => 'United States',
'UM' => 'United States Minor Outlying Islands',
'UY' => 'Uruguay',
'UZ' => 'Uzbekistan',
'VU' => 'Vanuatu',
'VA' => 'Vatican',
'VE' => 'Venezuela',
'VN' => 'Vietnam',
'WF' => 'Wallis and Futuna',
'EH' => 'Western Sahara',
'YE' => 'Yemen',
'ZM' => 'Zambia',
'ZW' => 'Zimbabwe',
'XF' => 'Fake Country (for testing)',
'XO' => 'Other financial applications',
];
/** @var ImportJob */
private $job;
/**
* Get the data necessary to show the configuration screen.
*
* @return array
*/
public function getData(): array
{
$providers = SpectreProvider::get();
$countries = [];
/** @var SpectreProvider $provider */
foreach ($providers as $provider) {
$countries[$provider->country_code] = self::$allCountries[$provider->country_code] ?? $provider->country_code;
}
asort($countries);
return compact('countries');
}
/**
* Return possible warning to user.
*
* @return string
*/
public function getWarningMessage(): string
{
return '';
}
/**
* @param ImportJob $job
*
* @return ConfigurationInterface
*/
public function setJob(ImportJob $job)
{
$this->job = $job;
}
/**
* Store the result.
*
* @param array $data
*
* @return bool
*/
public function storeConfiguration(array $data): bool
{
$config = $this->job->configuration;
$config['country'] = $data['country_code'] ?? 'XF'; // default to fake country.
$config['selected-country'] = true;
$this->job->configuration = $config;
$this->job->save();
return true;
}
}

View File

@ -0,0 +1,77 @@
<?php
declare(strict_types=1);
namespace FireflyIII\Support\Import\Configuration\Spectre;
use FireflyIII\Models\ImportJob;
use FireflyIII\Models\SpectreProvider;
use FireflyIII\Support\Import\Configuration\ConfigurationInterface;
/**
* Class SelectProvider
*/
class SelectProvider implements ConfigurationInterface
{
/** @var ImportJob */
private $job;
/**
* Get the data necessary to show the configuration screen.
*
* @return array
*/
public function getData(): array
{
$config = $this->job->configuration;
$selection = SpectreProvider::where('country_code', $config['country'])->where('status', 'active')->get();
$providers = [];
/** @var SpectreProvider $provider */
foreach ($selection as $provider) {
$providerId = $provider->spectre_id;
$name = $provider->data['name'];
$providers[$providerId] = $name;
}
$country = SelectCountry::$allCountries[$config['country']] ?? $config['country'];
return compact('providers', 'country');
}
/**
* Return possible warning to user.
*
* @return string
*/
public function getWarningMessage(): string
{
return '';
}
/**
* @param ImportJob $job
*
* @return ConfigurationInterface
*/
public function setJob(ImportJob $job)
{
$this->job = $job;
}
/**
* Store the result.
*
* @param array $data
*
* @return bool
*/
public function storeConfiguration(array $data): bool
{
$config = $this->job->configuration;
$config['provider'] = intval($data['provider_code']) ?? 0; // default to fake country.
$config['selected-provider'] = true;
$this->job->configuration = $config;
$this->job->save();
return true;
}
}

View File

@ -0,0 +1,207 @@
<?php
/**
* SpectreInformation.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Import\Information;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Services\Bunq\Object\Alias;
use FireflyIII\Services\Bunq\Object\MonetaryAccountBank;
use FireflyIII\Services\Bunq\Request\DeleteDeviceSessionRequest;
use FireflyIII\Services\Bunq\Request\DeviceSessionRequest;
use FireflyIII\Services\Bunq\Request\ListMonetaryAccountRequest;
use FireflyIII\Services\Bunq\Request\ListUserRequest;
use FireflyIII\Services\Bunq\Token\SessionToken;
use FireflyIII\Support\CacheProperties;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Log;
use Preferences;
/**
* Class SpectreInformation
*/
class SpectreInformation implements InformationInterface
{
/** @var User */
private $user;
/**
* Returns a collection of accounts. Preferrably, these follow a uniform Firefly III format so they can be managed over banks.
*
* The format for these bank accounts is basically this:
*
* id: bank specific id
* name: bank appointed name
* number: account number (usually IBAN)
* currency: ISO code of currency
* balance: current balance
*
*
* any other fields are optional but can be useful:
* image: logo or account specific thing
* color: any associated color.
*
* @return array
*/
public function getAccounts(): array
{
// cache for an hour:
$cache = new CacheProperties;
$cache->addProperty('bunq.get-accounts');
$cache->addProperty(date('dmy h'));
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
Log::debug('Now in getAccounts()');
$sessionToken = $this->startSession();
$userId = $this->getUserInformation($sessionToken);
// get list of Bunq accounts:
$accounts = $this->getMonetaryAccounts($sessionToken, $userId);
$return = [];
/** @var MonetaryAccountBank $account */
foreach ($accounts as $account) {
$current = [
'id' => $account->getId(),
'name' => $account->getDescription(),
'currency' => $account->getCurrency(),
'balance' => $account->getBalance()->getValue(),
'color' => $account->getSetting()->getColor(),
];
/** @var Alias $alias */
foreach ($account->getAliases() as $alias) {
if ('IBAN' === $alias->getType()) {
$current['number'] = $alias->getValue();
}
}
$return[] = $current;
}
$cache->store($return);
$this->closeSession($sessionToken);
return $return;
}
/**
* 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;
}
/**
* @param SessionToken $sessionToken
*/
private function closeSession(SessionToken $sessionToken): void
{
Log::debug('Going to close session');
$apiKey = Preferences::getForUser($this->user, 'bunq_api_key')->data;
$serverPublicKey = Preferences::getForUser($this->user, 'bunq_server_public_key')->data;
$privateKey = Preferences::getForUser($this->user, 'bunq_private_key')->data;
$request = new DeleteDeviceSessionRequest();
$request->setSecret($apiKey);
$request->setPrivateKey($privateKey);
$request->setServerPublicKey($serverPublicKey);
$request->setSessionToken($sessionToken);
$request->call();
return;
}
/**
* @param SessionToken $sessionToken
* @param int $userId
*
* @return Collection
*/
private function getMonetaryAccounts(SessionToken $sessionToken, int $userId): Collection
{
$apiKey = Preferences::getForUser($this->user, 'bunq_api_key')->data;
$serverPublicKey = Preferences::getForUser($this->user, 'bunq_server_public_key')->data;
$privateKey = Preferences::getForUser($this->user, 'bunq_private_key')->data;
$request = new ListMonetaryAccountRequest;
$request->setSessionToken($sessionToken);
$request->setSecret($apiKey);
$request->setServerPublicKey($serverPublicKey);
$request->setPrivateKey($privateKey);
$request->setUserId($userId);
$request->call();
return $request->getMonetaryAccounts();
}
/**
* @param SessionToken $sessionToken
*
* @return int
*
* @throws FireflyException
*/
private function getUserInformation(SessionToken $sessionToken): int
{
$apiKey = Preferences::getForUser($this->user, 'bunq_api_key')->data;
$serverPublicKey = Preferences::getForUser($this->user, 'bunq_server_public_key')->data;
$privateKey = Preferences::getForUser($this->user, 'bunq_private_key')->data;
$request = new ListUserRequest;
$request->setSessionToken($sessionToken);
$request->setSecret($apiKey);
$request->setServerPublicKey($serverPublicKey);
$request->setPrivateKey($privateKey);
$request->call();
// return the first that isn't null?
$company = $request->getUserCompany();
if ($company->getId() > 0) {
return $company->getId();
}
$user = $request->getUserPerson();
if ($user->getId() > 0) {
return $user->getId();
}
throw new FireflyException('Expected user or company from Bunq, but got neither.');
}
/**
* @return SessionToken
*/
private function startSession(): SessionToken
{
Log::debug('Now in startSession.');
$apiKey = Preferences::getForUser($this->user, 'bunq_api_key')->data;
$serverPublicKey = Preferences::getForUser($this->user, 'bunq_server_public_key')->data;
$privateKey = Preferences::getForUser($this->user, 'bunq_private_key')->data;
$installationToken = Preferences::getForUser($this->user, 'bunq_installation_token')->data;
$request = new DeviceSessionRequest();
$request->setSecret($apiKey);
$request->setServerPublicKey($serverPublicKey);
$request->setPrivateKey($privateKey);
$request->setInstallationToken($installationToken);
$request->call();
$sessionToken = $request->getSessionToken();
Log::debug(sprintf('Now have got session token: %s', serialize($sessionToken)));
return $sessionToken;
}
}

View File

@ -0,0 +1,176 @@
<?php
/**
* SpectrePrerequisites.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Import\Prerequisites;
use FireflyIII\Jobs\GetSpectreProviders;
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 = strval(trans('bank.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_client_id', false),
Preferences::getForUser($this->user, 'spectre_app_secret', false),
Preferences::getForUser($this->user, 'spectre_service_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!');
// at this point, check if all providers are present. Providers are shared amongst
// users in a multi-user environment.
GetSpectreProviders::dispatch($this->user);
return false;
}
/**
* 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;
return;
}
/**
* 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_client_id', $request->get('client_id'));
Preferences::setForUser($this->user, 'spectre_app_secret', $request->get('app_secret'));
Preferences::setForUser($this->user, 'spectre_service_secret', $request->get('service_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');
return;
}
/**
* 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;
}
}

View File

@ -28,39 +28,51 @@ declare(strict_types=1);
*/
return [
'configuration' => [
'configuration' => [
'single_user_mode' => true,
'is_demo_site' => false,
],
'encryption' => (is_null(env('USE_ENCRYPTION')) || env('USE_ENCRYPTION') === true),
'version' => '4.6.11.1',
'maxUploadSize' => 15242880,
'allowedMimes' => ['image/png', 'image/jpeg', 'application/pdf','text/plain'],
'list_length' => 10,
'export_formats' => [
'encryption' => (is_null(env('USE_ENCRYPTION')) || env('USE_ENCRYPTION') === true),
'version' => '4.6.11.1',
'maxUploadSize' => 15242880,
'allowedMimes' => ['image/png', 'image/jpeg', 'application/pdf', 'text/plain'],
'list_length' => 10,
'export_formats' => [
'csv' => 'FireflyIII\Export\Exporter\CsvExporter',
],
'import_formats' => [
'csv' => 'FireflyIII\Import\Configurator\CsvConfigurator',
'import_formats' => [
'csv' => 'FireflyIII\Import\Configurator\CsvConfigurator',
'spectre' => '',
],
'import_configurators' => [
'import_configurators' => [
'csv' => 'FireflyIII\Import\Configurator\CsvConfigurator',
'spectre' => 'FireflyIII\Import\Configurator\SpectreConfigurator',
],
'import_processors' => [
'import_processors' => [
'csv' => 'FireflyIII\Import\FileProcessor\CsvProcessor',
],
'import_pre' => [
'bunq' => 'FireflyIII\Support\Import\Prerequisites\BunqPrerequisites',
'import_pre' => [
'bunq' => 'FireflyIII\Support\Import\Prerequisites\BunqPrerequisites',
'spectre' => 'FireflyIII\Support\Import\Prerequisites\SpectrePrerequisites',
'plaid' => 'FireflyIII\Support\Import\Prerequisites\PlairPrerequisites',
],
'import_info' => [
'bunq' => 'FireflyIII\Support\Import\Information\BunqInformation',
'import_info' => [
'bunq' => 'FireflyIII\Support\Import\Information\BunqInformation',
'spectre' => 'FireflyIII\Support\Import\Information\SpectreInformation',
'plaid' => 'FireflyIII\Support\Import\Information\PlaidInformation',
],
'import_transactions' => [
'bunq' => 'FireflyIII\Support\Import\Transactions\BunqTransactions',
'import_transactions' => [
'bunq' => 'FireflyIII\Support\Import\Transactions\BunqTransactions',
'spectre' => 'FireflyIII\Support\Import\Transactions\SpectreTransactions',
'plaid' => 'FireflyIII\Support\Import\Transactions\PlaidTransactions',
],
'bunq' => [
'bunq' => [
'server' => 'https://sandbox.public.api.bunq.com',
],
'spectre' => [
'server' => 'https://www.saltedge.com',
],
'default_export_format' => 'csv',
'default_import_format' => 'csv',
'bill_periods' => ['weekly', 'monthly', 'quarterly', 'half-year', 'yearly'],

View File

@ -0,0 +1,47 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class ChangesForSpectre extends Migration
{
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// create provider table:
if (!Schema::hasTable('spectre_providers')) {
Schema::create(
'spectre_providers',
function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->softDeletes();
//'spectre_id', 'code', 'mode', 'name', 'status', 'interactive', 'automatic_fetch', 'country_code', 'data'
$table->integer('spectre_id', false, true);
$table->string('code', 100);
$table->string('mode', 20);
$table->string('status', 20);
$table->boolean('interactive')->default(0);
$table->boolean('automatic_fetch')->default(0);
$table->string('country_code', 3);
$table->text('data');
}
);
}
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.6 KiB

After

Width:  |  Height:  |  Size: 3.2 KiB

BIN
public/images/logos/csv.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

View File

@ -23,6 +23,19 @@ declare(strict_types=1);
return [
'bunq_prerequisites_title' => 'Prerequisites for an import from bunq',
'bunq_prerequisites_text' => 'In order to import from bunq, you need to obtain an API key. You can do this through the app.',
'bunq_prerequisites_title' => 'Prerequisites for an import from bunq',
'bunq_prerequisites_text' => 'In order to import from bunq, you need to obtain an API key. You can do this through the app.',
// Spectre:
'spectre_title' => 'Import using Spectre',
'spectre_prerequisites_title' => 'Prerequisites for an import using Spectre',
'spectre_prerequisites_text' => 'In order to import data using the Spectre API, you need to prove some secrets. They can be found on the <a href="https://www.saltedge.com/clients/profile/secrets">secrets page</a>.',
'spectre_enter_pub_key' => 'The import will only work when you enter this public key on your <a href="https://www.saltedge.com/clients/security/edit">security page</a>.',
'spectre_select_country_title' => 'Select a country',
'spectre_select_country_text' => 'Firefly III has a large selection of banks and sites from which Spectre can download transactional data. These banks are sorted by country. Please not that there is a "Fake Country" for when you wish to test something. If you wish to import from other financial tools, please use the imaginary country called "Other financial applications". By default, Spectre only allows you to download data from fake banks. Make sure your status is "Live" on your <a href="https://www.saltedge.com/clients/dashboard">Dashboard</a> if you wish to download from real banks.',
'spectre_select_provider_title' => 'Select a bank',
'spectre_select_provider_text' => 'Spectre supports the following banks or financial services grouped under <em>:country</em>. Please pick the one you wish to import from.',
'spectre_input_fields_title' => 'Input mandatory fields',
'spectre_input_fields_text' => 'The following fields are mandated by ":provider" (from :country).',
'spectre_instructions_english' => 'These instructions are provided by Spectre for your convencience. They are in English:',
];

View File

@ -195,6 +195,11 @@ return [
'csv_delimiter' => 'CSV field delimiter',
'csv_import_account' => 'Default import account',
'csv_config' => 'CSV import configuration',
'client_id' => 'Client ID',
'service_secret' => 'Service secret',
'app_secret' => 'App secret',
'public_key' => 'Public key',
'country_code' => 'Country code',
'due_date' => 'Due date',

View File

@ -20,21 +20,34 @@
</div>
<div class="row">
<div class="col-lg-8">
<p>
<div class="col-lg-1 text-center">
{# file import #}
<a href="{{ route('import.file.index') }}" class="btn btn-app">
<i class="fa fa-file-text-o"></i>
<a href="{{ route('import.file.index') }}">
<img src="images/logos/csv.png" alt="bunq"/><br />
{{ 'import_general_index_csv_file'|_ }}
</a>
</div>
<div class="col-lg-1 text-center">
{# bunq import #}
{#
<a href="{{ route('import.bank.prerequisites', ['bunq']) }}" class="btn btn-app">
<a href="{{ route('import.bank.prerequisites', ['bunq']) }}">
<img src="images/logos/bunq.png" alt="bunq"/><br />
Import from bunq
</a>
#}
</p>
</div>
<div class="col-lg-1 text-center">
{# import from Spectre #}
<a href="{{ route('import.bank.prerequisites', ['spectre']) }}">
<img src="images/logos/spectre.png" alt="Spectre"/><br />
Import using Spectre
</a>
</div>
<div class="col-lg-1 text-center">
{# import from Plaid #}
<a href="{{ route('import.bank.prerequisites', ['plaid']) }}">
<img src="images/logos/plaid.png" alt="Plaid"/><br />
Import using Plaid
</a>
</div>
</div>
</div>
</div>

View File

@ -0,0 +1,65 @@
{% extends "./layout/default" %}
{% block breadcrumbs %}
{{ Breadcrumbs.renderIfExists }}
{% endblock %}
{% block content %}
<div class="row">
<form class="form-horizontal" action="{{ route('import.file.process-configuration', job.key) }}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
<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('bank.spectre_input_fields_title') }}</h3>
</div>
<div class="box-body">
<div class="row">
<div class="col-lg-8">
<p>
{{ trans('bank.spectre_input_fields_text',{provider: data.provider.data.name, country: data.country})|raw }}
</p>
<p>
{{ trans('bank.spectre_instructions_english') }}
</p>
<p>
{{ data.provider.data.instruction|nl2br }}
</p>
</div>
</div>
<div class="row">
<div class="col-lg-8">
{% for field in data.fields %}
{# text, password, select, file #}
{% if field.nature == 'text' %}
{{ ExpandedForm.text(field.name,null, {label: field.english_name ~ ' ('~field.localized_name~')'}) }}
{% endif %}
{% if field.nature == 'password' %}
{{ ExpandedForm.password(field.name, {label: field.english_name ~ ' ('~field.localized_name~')'}) }}
{% endif %}
{% if field.nature == 'select' %}
DO NOT SUPPORT
{{ dump(field) }}
{% endif %}
{% if field.narture == 'file' %}
DO NOT SUPPORT
{{ dump(field) }}
{% endif %}
{% endfor %}
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn pull-right btn-success">
{{ ('submit')|_ }}
</button>
</div>
</div>
</div>
</form>
</div>
{% endblock %}
{% block scripts %}
{% endblock %}
{% block styles %}
{% endblock %}

View File

@ -0,0 +1,58 @@
{% extends "./layout/default" %}
{% block breadcrumbs %}
{{ Breadcrumbs.renderIfExists }}
{% endblock %}
{% block content %}
<div class="row">
<form class="form-horizontal" action="{{ route('import.bank.prerequisites.post',['spectre']) }}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
<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('bank.spectre_prerequisites_title') }}</h3>
</div>
<div class="box-body">
<div class="row">
<div class="col-lg-8">
<p>
{{ trans('bank.spectre_prerequisites_text')|raw }}
</p>
</div>
</div>
<div class="row">
<div class="col-lg-8">
{{ ExpandedForm.text('client_id') }}
{{ ExpandedForm.text('service_secret') }}
{{ ExpandedForm.text('app_secret') }}
</div>
</div>
<div class="row">
<div class="col-lg-8">
<p>{{ trans('bank.spectre_enter_pub_key')|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>
<div class="col-sm-8">
<textarea class="form-control"
rows="10"
id="ffInput_pub_key_holder" name="pub_key_holder" contenteditable="false">{{ publicKey }}</textarea>
</div>
</div>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn pull-right btn-success">
{{ ('submit')|_ }}
</button>
</div>
</div>
</div>
</form>
</div>
{% endblock %}
{% block scripts %}
{% endblock %}
{% block styles %}
{% endblock %}

View File

@ -0,0 +1,42 @@
{% extends "./layout/default" %}
{% block breadcrumbs %}
{{ Breadcrumbs.renderIfExists }}
{% endblock %}
{% block content %}
<div class="row">
<form class="form-horizontal" action="{{ route('import.file.process-configuration', job.key) }}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
<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('bank.spectre_select_country_title') }}</h3>
</div>
<div class="box-body">
<div class="row">
<div class="col-lg-8">
<p>
{{ trans('bank.spectre_select_country_text')|raw }}
</p>
</div>
</div>
<div class="row">
<div class="col-lg-8">
{{ ExpandedForm.select('country_code', data.countries, null)|raw }}
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn pull-right btn-success">
{{ ('submit')|_ }}
</button>
</div>
</div>
</div>
</form>
</div>
{% endblock %}
{% block scripts %}
{% endblock %}
{% block styles %}
{% endblock %}

View File

@ -0,0 +1,42 @@
{% extends "./layout/default" %}
{% block breadcrumbs %}
{{ Breadcrumbs.renderIfExists }}
{% endblock %}
{% block content %}
<div class="row">
<form class="form-horizontal" action="{{ route('import.file.process-configuration', job.key) }}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
<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('bank.spectre_select_bank_title') }}</h3>
</div>
<div class="box-body">
<div class="row">
<div class="col-lg-8">
<p>
{{ trans('bank.spectre_select_provider_text',{country: data.country})|raw }}
</p>
</div>
</div>
<div class="row">
<div class="col-lg-8">
{{ ExpandedForm.select('provider_code', data.providers, null)|raw }}
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn pull-right btn-success">
{{ ('submit')|_ }}
</button>
</div>
</div>
</div>
</form>
</div>
{% endblock %}
{% block scripts %}
{% endblock %}
{% block styles %}
{% endblock %}

View File

@ -446,8 +446,9 @@ Route::group(
Route::get('bank/{bank}/prerequisites', ['uses' => 'Import\BankController@prerequisites', 'as' => 'bank.prerequisites']);
Route::post('bank/{bank}/prerequisites', ['uses' => 'Import\BankController@postPrerequisites', 'as' => 'bank.prerequisites.post']);
Route::get('bank/{bank}/form', ['uses' => 'Import\BankController@form', 'as' => 'bank.form']);
Route::post('bank/{bank}/form', ['uses' => 'Import\BankController@postForm', 'as' => 'bank.form.post']);
Route::get('bank/{bank}/create', ['uses' => 'Import\BankController@createJob', 'as' => 'bank.create-job']);
Route:: get('bank/{bank}/configure/{importJob}', ['uses' => 'Import\BankController@configure', 'as' => 'bank.configure']);
Route::post('bank/{bank}/configure/{importJob}', ['uses' => 'Import\BankController@postConfigure', 'as' => 'bank.configure.post']);
}
);