mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-15 00:27:30 +00:00
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:
101
app/Support/Import/Configuration/Spectre/InputMandatory.php
Normal file
101
app/Support/Import/Configuration/Spectre/InputMandatory.php
Normal 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;
|
||||
}
|
||||
}
|
329
app/Support/Import/Configuration/Spectre/SelectCountry.php
Normal file
329
app/Support/Import/Configuration/Spectre/SelectCountry.php
Normal 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;
|
||||
}
|
||||
}
|
77
app/Support/Import/Configuration/Spectre/SelectProvider.php
Normal file
77
app/Support/Import/Configuration/Spectre/SelectProvider.php
Normal 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;
|
||||
}
|
||||
}
|
207
app/Support/Import/Information/SpectreInformation.php
Normal file
207
app/Support/Import/Information/SpectreInformation.php
Normal 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;
|
||||
}
|
||||
}
|
176
app/Support/Import/Prerequisites/SpectrePrerequisites.php
Normal file
176
app/Support/Import/Prerequisites/SpectrePrerequisites.php
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user