diff --git a/app/Http/Controllers/Import/IndexController.php b/app/Http/Controllers/Import/IndexController.php index 3b3c21e48f..b79bf1ed8e 100644 --- a/app/Http/Controllers/Import/IndexController.php +++ b/app/Http/Controllers/Import/IndexController.php @@ -160,6 +160,8 @@ class IndexController extends Controller Preferences::delete('spectre_client_id'); Preferences::delete('spectre_app_secret'); Preferences::delete('spectre_service_secret'); + Preferences::delete('spectre_app_id'); + Preferences::delete('spectre_secret'); Preferences::delete('spectre_private_key'); Preferences::delete('spectre_public_key'); Preferences::delete('spectre_customer'); diff --git a/app/Import/Prerequisites/SpectrePrerequisites.php b/app/Import/Prerequisites/SpectrePrerequisites.php index a662ed3de9..93319c7fdd 100644 --- a/app/Import/Prerequisites/SpectrePrerequisites.php +++ b/app/Import/Prerequisites/SpectrePrerequisites.php @@ -71,9 +71,8 @@ class SpectrePrerequisites implements PrerequisitesInterface 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), + Preferences::getForUser($this->user, 'spectre_app_id', false), + Preferences::getForUser($this->user, 'spectre_secret', false), ]; /** @var Preference $value */ foreach ($values as $value) { @@ -111,9 +110,8 @@ class SpectrePrerequisites implements PrerequisitesInterface 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')); + 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; diff --git a/app/Services/Spectre/Exception/WrongRequestFormatException.php b/app/Services/Spectre/Exception/WrongRequestFormatException.php new file mode 100644 index 0000000000..ac0469f068 --- /dev/null +++ b/app/Services/Spectre/Exception/WrongRequestFormatException.php @@ -0,0 +1,32 @@ +. + */ + +declare(strict_types=1); + +namespace FireflyIII\Services\Spectre\Exception; + +/** + * Class WrongRequestFormatException + */ +class WrongRequestFormatException extends SpectreException +{ + +} \ No newline at end of file diff --git a/app/Services/Spectre/Object/Account.php b/app/Services/Spectre/Object/Account.php index 4f74746fa3..ccab07722c 100644 --- a/app/Services/Spectre/Object/Account.php +++ b/app/Services/Spectre/Object/Account.php @@ -56,7 +56,7 @@ class Account extends SpectreObject */ public function __construct(array $data) { - $this->id = $data['id']; + $this->id = (int)$data['id']; $this->loginId = $data['login_id']; $this->currencyCode = $data['currency_code']; $this->balance = $data['balance']; diff --git a/app/Services/Spectre/Object/Attempt.php b/app/Services/Spectre/Object/Attempt.php index 8bfd94b52d..1bc5c6f882 100644 --- a/app/Services/Spectre/Object/Attempt.php +++ b/app/Services/Spectre/Object/Attempt.php @@ -58,8 +58,6 @@ class Attempt extends SpectreObject private $failErrorClass; /** @var string */ private $failMessage; - /** @var string */ - private $fetchType; /** @var bool */ private $finished; /** @var bool */ @@ -90,6 +88,8 @@ class Attempt extends SpectreObject private $updatedAt; /** @var string */ private $userAgent; // undocumented + /** @var array */ + private $fetchScopes = []; /** * Attempt constructor. @@ -114,7 +114,7 @@ class Attempt extends SpectreObject $this->failAt = new Carbon($data['fail_at']); $this->failErrorClass = $data['fail_error_class']; $this->failMessage = $data['fail_message']; - $this->fetchType = $data['fetch_type']; + $this->fetchScopes = $data['fetch_scopes']; $this->finished = $data['finished']; $this->finishedRecent = $data['finished_recent']; $this->fromDate = new Carbon($data['from_date']); @@ -185,7 +185,7 @@ class Attempt extends SpectreObject 'fail_at' => $this->failAt->toIso8601String(), 'fail_error_class' => $this->failErrorClass, 'fail_message' => $this->failMessage, - 'fetch_type' => $this->fetchType, + 'fetch_scopes' => $this->fetchScopes, 'finished' => $this->finished, 'finished_recent' => $this->finishedRecent, 'from_date' => $this->fromDate->toIso8601String(), diff --git a/app/Services/Spectre/Object/Login.php b/app/Services/Spectre/Object/Login.php index 5566a27026..0db4fe52b9 100644 --- a/app/Services/Spectre/Object/Login.php +++ b/app/Services/Spectre/Object/Login.php @@ -83,7 +83,7 @@ class Login extends SpectreObject $this->customerId = $data['customer_id']; $this->dailyRefresh = $data['daily_refresh']; $this->holderInfo = new Holder($data['holder_info']); - $this->id = $data['id']; + $this->id = (int)$data['id']; $this->lastAttempt = new Attempt($data['last_attempt']); $this->lastSuccessAt = new Carbon($data['last_success_at']); $this->nextRefreshPossibleAt = new Carbon($data['next_refresh_possible_at']); diff --git a/app/Services/Spectre/Object/Transaction.php b/app/Services/Spectre/Object/Transaction.php index 296631915f..4109f95c68 100644 --- a/app/Services/Spectre/Object/Transaction.php +++ b/app/Services/Spectre/Object/Transaction.php @@ -64,7 +64,7 @@ class Transaction extends SpectreObject */ public function __construct(array $data) { - $this->id = $data['id']; + $this->id = (int)$data['id']; $this->mode = $data['mode']; $this->status = $data['status']; $this->madeOn = new Carbon($data['made_on']); diff --git a/app/Services/Spectre/Request/CreateTokenRequest.php b/app/Services/Spectre/Request/CreateTokenRequest.php index 88fe1e6184..60ab35cd39 100644 --- a/app/Services/Spectre/Request/CreateTokenRequest.php +++ b/app/Services/Spectre/Request/CreateTokenRequest.php @@ -52,7 +52,7 @@ class CreateTokenRequest extends SpectreRequest $data = [ 'data' => [ 'customer_id' => $this->customer->getId(), - 'fetch_type' => 'recent', + 'fetch_scopes' => ['accounts', 'transactions'], 'daily_refresh' => true, 'include_fake_providers' => true, 'show_consent_confirmation' => true, diff --git a/app/Services/Spectre/Request/ListLoginsRequest.php b/app/Services/Spectre/Request/ListLoginsRequest.php index 897269cae5..99f1d16b9f 100644 --- a/app/Services/Spectre/Request/ListLoginsRequest.php +++ b/app/Services/Spectre/Request/ListLoginsRequest.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace FireflyIII\Services\Spectre\Request; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Services\Spectre\Object\Customer; use FireflyIII\Services\Spectre\Object\Login; use Log; @@ -39,9 +40,7 @@ class ListLoginsRequest extends SpectreRequest private $logins = []; /** - * - * @throws \FireflyIII\Services\Spectre\Exception\SpectreException - * @throws \FireflyIII\Exceptions\FireflyException + * @throws FireflyException */ public function call(): void { @@ -54,7 +53,7 @@ class ListLoginsRequest extends SpectreRequest $response = $this->sendSignedSpectreGet($uri, []); // count entries: - Log::debug(sprintf('Found %d entries in data-array', count($response['data']))); + Log::debug(sprintf('Found %d entries in data-array', \count($response['data']))); // extract next ID $hasNextPage = false; diff --git a/app/Services/Spectre/Request/SpectreRequest.php b/app/Services/Spectre/Request/SpectreRequest.php index 93eb8a5ebd..8698ea12f8 100644 --- a/app/Services/Spectre/Request/SpectreRequest.php +++ b/app/Services/Spectre/Request/SpectreRequest.php @@ -34,17 +34,15 @@ use Requests_Response; */ abstract class SpectreRequest { - /** @var string */ - protected $clientId = ''; - /** - * @var int - */ + /** @var int */ protected $expiresAt = 0; /** @var string */ - protected $serviceSecret = ''; + private $appId; /** @var string */ private $privateKey; /** @var string */ + private $secret; + /** @var string */ private $server; /** @var User */ private $user; @@ -67,12 +65,12 @@ abstract class SpectreRequest $this->privateKey = $privateKey->data; // set client ID - $clientId = app('preferences')->get('spectre_client_id', null); - $this->clientId = $clientId->data; + $appId = app('preferences')->get('spectre_app_id', null); + $this->appId = $appId->data; // set service secret - $serviceSecret = app('preferences')->get('spectre_service_secret', null); - $this->serviceSecret = $serviceSecret->data; + $secret = app('preferences')->get('spectre_secret', null); + $this->secret = $secret->data; } /** @@ -83,17 +81,33 @@ abstract class SpectreRequest /** * @return string */ - public function getClientId(): string + public function getAppId(): string { - return $this->clientId; + return $this->appId; } /** - * @param string $clientId + * @param string $appId */ - public function setClientId(string $clientId): void + public function setAppId(string $appId): void { - $this->clientId = $clientId; + $this->appId = $appId; + } + + /** + * @return string + */ + public function getSecret(): string + { + return $this->secret; + } + + /** + * @param string $secret + */ + public function setSecret(string $secret): void + { + $this->secret = $secret; } /** @@ -104,26 +118,10 @@ abstract class SpectreRequest return $this->server; } - /** - * @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) + public function setPrivateKey(string $privateKey): void { $this->privateKey = $privateKey; } @@ -139,10 +137,11 @@ abstract class SpectreRequest */ protected function generateSignature(string $method, string $uri, string $data): string { - if (0 === strlen($this->privateKey)) { + if ('' === $this->privateKey) { throw new FireflyException('No private key present.'); } - if ('get' === strtolower($method) || 'delete' === strtolower($method)) { + $method = strtolower($method); + if ('get' === $method || 'delete' === $method) { $data = ''; } $toSign = $this->expiresAt . '|' . strtoupper($method) . '|' . $uri . '|' . $data . ''; // no file so no content there. @@ -150,7 +149,7 @@ abstract class SpectreRequest $signature = ''; // Sign the data - openssl_sign($toSign, $signature, $this->privateKey, OPENSSL_ALGO_SHA1); + openssl_sign($toSign, $signature, $this->privateKey, OPENSSL_ALGO_SHA256); $signature = base64_encode($signature); return $signature; @@ -164,13 +163,13 @@ abstract class SpectreRequest $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, + 'App-id' => $this->getAppId(), + 'Secret' => $this->getSecret(), + 'Accept' => 'application/json', + 'Content-type' => 'application/json', + 'Cache-Control' => 'no-cache', + 'User-Agent' => $userAgent, + 'Expires-at' => $this->expiresAt, ]; } @@ -184,7 +183,7 @@ abstract class SpectreRequest */ protected function sendSignedSpectreGet(string $uri, array $data): array { - if (0 === strlen($this->server)) { + if ('' === $this->server) { throw new FireflyException('No Spectre server defined'); } @@ -227,7 +226,7 @@ abstract class SpectreRequest */ protected function sendSignedSpectrePost(string $uri, array $data): array { - if (0 === strlen($this->server)) { + if ('' === $this->server) { throw new FireflyException('No Spectre server defined'); } @@ -277,7 +276,5 @@ abstract class SpectreRequest if (200 !== $statusCode) { throw new FireflyException(sprintf('Status code %d: %s', $statusCode, $response->body)); } - - return; } } diff --git a/resources/views/import/spectre/accounts.twig b/resources/views/import/spectre/accounts.twig index e2c13db3e6..a9179c4241 100644 --- a/resources/views/import/spectre/accounts.twig +++ b/resources/views/import/spectre/accounts.twig @@ -47,20 +47,27 @@ {% endfor %} + {% if account.options|length > 0 %} + {% else %} + + {{ trans('import.spectre_no_supported_accounts') }} + + {% endif %} + {% if account.options|length > 0 %}
- + {% endif %} diff --git a/resources/views/import/spectre/prerequisites.twig b/resources/views/import/spectre/prerequisites.twig index 0bda9b1823..b93dfe60ea 100644 --- a/resources/views/import/spectre/prerequisites.twig +++ b/resources/views/import/spectre/prerequisites.twig @@ -23,9 +23,8 @@
- {{ ExpandedForm.text('client_id') }} - {{ ExpandedForm.text('service_secret') }} - {{ ExpandedForm.text('app_secret') }} + {{ ExpandedForm.text('app_id') }} + {{ ExpandedForm.text('secret') }}