mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Improve bunq import.
This commit is contained in:
parent
85dc1263ea
commit
0c2b35e542
@ -134,6 +134,6 @@ class PrerequisitesController extends Controller
|
||||
$request->session()->flash('error', $result->first());
|
||||
}
|
||||
|
||||
return redirect(route('import.index'));
|
||||
return redirect(route('import.prerequisites', [$bank]));
|
||||
}
|
||||
}
|
||||
|
@ -206,16 +206,8 @@ class BunqConfigurator implements ConfiguratorInterface
|
||||
|
||||
// set default config:
|
||||
$defaultConfig = [
|
||||
// 'has-token' => false,
|
||||
// 'token' => '',
|
||||
// 'token-expires' => 0,
|
||||
// 'token-url' => '',
|
||||
'is-redirected' => false,
|
||||
// 'customer' => null,
|
||||
// 'login' => null,
|
||||
'stage' => 'initial',
|
||||
// 'accounts' => '',
|
||||
// 'accounts-mapped' => '',
|
||||
'auto-start' => true,
|
||||
'apply-rules' => true,
|
||||
'match-bills' => false,
|
||||
|
@ -22,22 +22,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Import\Prerequisites;
|
||||
|
||||
use Exception;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Services\Bunq\Id\DeviceServerId;
|
||||
use FireflyIII\Services\Bunq\Object\DeviceServer;
|
||||
use FireflyIII\Services\Bunq\Object\ServerPublicKey;
|
||||
use FireflyIII\Services\Bunq\Request\DeviceServerRequest;
|
||||
use FireflyIII\Services\Bunq\Request\InstallationTokenRequest;
|
||||
use FireflyIII\Services\Bunq\Request\ListDeviceServerRequest;
|
||||
use FireflyIII\Services\Bunq\Token\InstallationToken;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use Log;
|
||||
use Preferences;
|
||||
use Requests;
|
||||
use Requests_Exception;
|
||||
|
||||
/**
|
||||
* This class contains all the routines necessary to connect to Bunq.
|
||||
@ -86,11 +75,9 @@ class BunqPrerequisites implements PrerequisitesInterface
|
||||
public function hasPrerequisites(): bool
|
||||
{
|
||||
Log::debug('Now in BunqPrerequisites::hasPrerequisites()');
|
||||
$apiKey = Preferences::getForUser($this->user, 'bunq_api_key', false);
|
||||
$deviceId = Preferences::getForUser($this->user, 'bunq_device_server_id', null);
|
||||
$result = (false === $apiKey->data || null === $apiKey->data) || is_null($deviceId);
|
||||
$apiKey = Preferences::getForUser($this->user, 'bunq_api_key', false);
|
||||
$result = (false === $apiKey->data || null === $apiKey->data);
|
||||
|
||||
Log::debug(sprintf('Is device ID NULL? %s', var_export(null === $deviceId, true)));
|
||||
Log::debug(sprintf('Is apiKey->data false? %s', var_export(false === $apiKey->data, true)));
|
||||
Log::debug(sprintf('Is apiKey->data NULL? %s', var_export(null === $apiKey->data, true)));
|
||||
Log::debug(sprintf('Result is: %s', var_export($result, true)));
|
||||
@ -124,263 +111,8 @@ class BunqPrerequisites implements PrerequisitesInterface
|
||||
$apiKey = $request->get('api_key');
|
||||
Log::debug('Storing bunq API key');
|
||||
Preferences::setForUser($this->user, 'bunq_api_key', $apiKey);
|
||||
// register Firefly III as a new device.
|
||||
$serverId = null;
|
||||
$messages = new MessageBag;
|
||||
try {
|
||||
Log::debug('Going to try and get the device registered.');
|
||||
$serverId = $this->registerDevice();
|
||||
Log::debug(sprintf('Found device server with id %d', $serverId->getId()));
|
||||
} catch (FireflyException $e) {
|
||||
Log::error(sprintf('Could not register device because: %s: %s', $e->getMessage(), $e->getTraceAsString()));
|
||||
$messages->add('error', $e->getMessage());
|
||||
}
|
||||
|
||||
return $messages;
|
||||
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('Now in createKeyPair()');
|
||||
$private = Preferences::getForUser($this->user, 'bunq_private_key', null);
|
||||
$public = Preferences::getForUser($this->user, 'bunq_public_key', null);
|
||||
|
||||
if (!(null === $private && null === $public)) {
|
||||
Log::info('Already have public and private key, return NULL.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Log::debug('Generate new 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, 'bunq_private_key', $privKey);
|
||||
Preferences::setForUser($this->user, 'bunq_public_key', $pubKey['key']);
|
||||
Log::debug('Created and stored key pair');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* When the device server cannot be registered for some reason (when previous attempts failed to be stored) this method can be used
|
||||
* to try and detect the server ID for this firefly instance.
|
||||
*
|
||||
* @return DeviceServerId
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getExistingDevice(): ?DeviceServerId
|
||||
{
|
||||
Log::debug('Now in getExistingDevice()');
|
||||
$installationToken = $this->getInstallationToken();
|
||||
$serverPublicKey = $this->getServerPublicKey();
|
||||
$request = new ListDeviceServerRequest;
|
||||
$remoteIp = $this->getRemoteIp();
|
||||
$request->setInstallationToken($installationToken);
|
||||
$request->setServerPublicKey($serverPublicKey);
|
||||
$request->setPrivateKey($this->getPrivateKey());
|
||||
$request->call();
|
||||
$devices = $request->getDevices();
|
||||
/** @var DeviceServer $device */
|
||||
foreach ($devices as $device) {
|
||||
if ($device->getIp() === $remoteIp) {
|
||||
return $device->getId();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the installation token, either from the users preferences or from Bunq.
|
||||
*
|
||||
* @return InstallationToken
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getInstallationToken(): InstallationToken
|
||||
{
|
||||
Log::debug('Now in getInstallationToken().');
|
||||
$token = Preferences::getForUser($this->user, 'bunq_installation_token', null);
|
||||
if (null !== $token) {
|
||||
Log::debug('Have installation token, return it.');
|
||||
|
||||
return $token->data;
|
||||
}
|
||||
Log::debug('Have no installation token, request one.');
|
||||
|
||||
// verify bunq api code:
|
||||
$publicKey = $this->getPublicKey();
|
||||
$request = new InstallationTokenRequest;
|
||||
$request->setPublicKey($publicKey);
|
||||
$request->call();
|
||||
Log::debug('Sent request for installation token.');
|
||||
|
||||
$installationToken = $request->getInstallationToken();
|
||||
$installationId = $request->getInstallationId();
|
||||
$serverPublicKey = $request->getServerPublicKey();
|
||||
|
||||
Preferences::setForUser($this->user, 'bunq_installation_token', $installationToken);
|
||||
Preferences::setForUser($this->user, 'bunq_installation_id', $installationId);
|
||||
Preferences::setForUser($this->user, 'bunq_server_public_key', $serverPublicKey);
|
||||
|
||||
Log::debug('Stored token, ID and pub key.');
|
||||
|
||||
return $installationToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the private key from the users preferences.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getPrivateKey(): string
|
||||
{
|
||||
Log::debug('get private key');
|
||||
$preference = Preferences::getForUser($this->user, 'bunq_private_key', null);
|
||||
if (null === $preference) {
|
||||
Log::debug('private key is null');
|
||||
// create key pair
|
||||
$this->createKeyPair();
|
||||
}
|
||||
$preference = Preferences::getForUser($this->user, 'bunq_private_key', null);
|
||||
Log::debug('Return private key for user');
|
||||
|
||||
return $preference->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a public key from the users preferences.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getPublicKey(): string
|
||||
{
|
||||
Log::debug('Now in getPublicKey()');
|
||||
$preference = Preferences::getForUser($this->user, 'bunq_public_key', null);
|
||||
if (null === $preference) {
|
||||
Log::debug('public key is NULL.');
|
||||
// create key pair
|
||||
$this->createKeyPair();
|
||||
}
|
||||
$preference = Preferences::getForUser($this->user, 'bunq_public_key', null);
|
||||
Log::debug('Return public key for user');
|
||||
|
||||
return $preference->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request users server remote IP. Let's assume this value will not change any time soon.
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getRemoteIp(): string
|
||||
{
|
||||
$preference = Preferences::getForUser($this->user, 'external_ip', null);
|
||||
if (null === $preference) {
|
||||
try {
|
||||
$response = Requests::get('https://api.ipify.org');
|
||||
} catch (Requests_Exception|Exception $e) {
|
||||
throw new FireflyException(sprintf('Could not retrieve external IP: %s', $e->getMessage()));
|
||||
}
|
||||
if (200 !== $response->status_code) {
|
||||
throw new FireflyException(sprintf('Could not retrieve external IP: %d %s', $response->status_code, $response->body));
|
||||
}
|
||||
$serverIp = $response->body;
|
||||
Preferences::setForUser($this->user, 'external_ip', $serverIp);
|
||||
|
||||
return $serverIp;
|
||||
}
|
||||
|
||||
return $preference->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the public key of the server, necessary to verify server signature.
|
||||
*
|
||||
* @return ServerPublicKey
|
||||
*/
|
||||
private function getServerPublicKey(): ServerPublicKey
|
||||
{
|
||||
return Preferences::getForUser($this->user, 'bunq_server_public_key', null)->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* To install Firefly III as a new device:
|
||||
* - Send an installation token request.
|
||||
* - Use this token to send a device server request
|
||||
* - Store the installation token
|
||||
* - Use the installation token each time we need a session.
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function registerDevice(): DeviceServerId
|
||||
{
|
||||
Log::debug('Now in registerDevice');
|
||||
$deviceServerId = Preferences::getForUser($this->user, 'bunq_device_server_id', null);
|
||||
$serverIp = $this->getRemoteIp();
|
||||
if (null !== $deviceServerId) {
|
||||
Log::debug('Have device server ID.');
|
||||
|
||||
return $deviceServerId->data;
|
||||
}
|
||||
Log::debug('Device server ID is null, we have to find an existing one or register a new one.');
|
||||
$installationToken = $this->getInstallationToken();
|
||||
$serverPublicKey = $this->getServerPublicKey();
|
||||
$apiKey = Preferences::getForUser($this->user, 'bunq_api_key', '');
|
||||
|
||||
// try get the current from a list:
|
||||
$deviceServerId = $this->getExistingDevice();
|
||||
if (null !== $deviceServerId) {
|
||||
Log::debug('Found device server ID in existing devices list.');
|
||||
|
||||
return $deviceServerId;
|
||||
}
|
||||
|
||||
Log::debug('Going to create new DeviceServerRequest() because nothing found in existing list.');
|
||||
$request = new DeviceServerRequest;
|
||||
$request->setPrivateKey($this->getPrivateKey());
|
||||
$request->setDescription('Firefly III v' . config('firefly.version') . ' for ' . $this->user->email);
|
||||
$request->setSecret($apiKey->data);
|
||||
$request->setPermittedIps([$serverIp]);
|
||||
$request->setInstallationToken($installationToken);
|
||||
$request->setServerPublicKey($serverPublicKey);
|
||||
$deviceServerId = null;
|
||||
// try to register device:
|
||||
try {
|
||||
$request->call();
|
||||
$deviceServerId = $request->getDeviceServerId();
|
||||
} catch (FireflyException $e) {
|
||||
Log::error($e->getMessage());
|
||||
// we really have to quit at this point :(
|
||||
//throw new FireflyException($e->getMessage());
|
||||
}
|
||||
if(is_null($deviceServerId)) {
|
||||
throw new FireflyException('Was not able to register server with bunq. Please see the log files.');
|
||||
}
|
||||
|
||||
Preferences::setForUser($this->user, 'bunq_device_server_id', $deviceServerId);
|
||||
Log::debug(sprintf('Server ID: %s', serialize($deviceServerId)));
|
||||
|
||||
return $deviceServerId;
|
||||
}
|
||||
}
|
||||
|
@ -23,14 +23,48 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Import\Routine;
|
||||
|
||||
use Exception;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
||||
use FireflyIII\Services\Bunq\Id\DeviceServerId;
|
||||
use FireflyIII\Services\Bunq\Object\DeviceServer;
|
||||
use FireflyIII\Services\Bunq\Object\ServerPublicKey;
|
||||
use FireflyIII\Services\Bunq\Request\DeviceServerRequest;
|
||||
use FireflyIII\Services\Bunq\Request\DeviceSessionRequest;
|
||||
use FireflyIII\Services\Bunq\Request\InstallationTokenRequest;
|
||||
use FireflyIII\Services\Bunq\Request\ListDeviceServerRequest;
|
||||
use FireflyIII\Services\Bunq\Token\InstallationToken;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
use Preferences;
|
||||
use Requests;
|
||||
|
||||
/**
|
||||
* Class BunqRoutine
|
||||
*
|
||||
* Steps before import:
|
||||
* 1) register device complete.
|
||||
*
|
||||
* Stage: 'initial'.
|
||||
*
|
||||
* 1) Get an installation token (if not present)
|
||||
* 2) Register device (if not found)
|
||||
*
|
||||
* Stage 'registered'
|
||||
*
|
||||
* 1) Get a session token. (new session)
|
||||
* 2) store user person / user company
|
||||
*
|
||||
* Stage 'logged-in'
|
||||
*
|
||||
* Get list of bank accounts
|
||||
*
|
||||
* Stage 'has-accounts'
|
||||
*
|
||||
* Do customer statement export (in CSV?)
|
||||
*
|
||||
*
|
||||
*/
|
||||
class BunqRoutine implements RoutineInterface
|
||||
{
|
||||
@ -93,10 +127,15 @@ class BunqRoutine implements RoutineInterface
|
||||
|
||||
switch ($stage) {
|
||||
case 'initial':
|
||||
// get customer and token:
|
||||
// register device and get tokens.
|
||||
$this->runStageInitial();
|
||||
break;
|
||||
case 'default':
|
||||
case 'registered':
|
||||
// get all bank accounts of user.
|
||||
$this->runStageRegistered();
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new FireflyException(sprintf('No action for stage %s!', $stage));
|
||||
break;
|
||||
// case 'has-token':
|
||||
@ -131,17 +170,56 @@ class BunqRoutine implements RoutineInterface
|
||||
|
||||
/**
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
protected function runStageInitial()
|
||||
{
|
||||
Log::debug('In runStageInitial()');
|
||||
$this->setStatus('running');
|
||||
|
||||
// get session server
|
||||
// register the device at Bunq:
|
||||
$serverId = $this->registerDevice();
|
||||
$this->addStep();
|
||||
Log::debug(sprintf('Found device server with id %d', $serverId->getId()));
|
||||
|
||||
$config = $this->getConfig();
|
||||
$config['stage'] = 'registered';
|
||||
$this->setConfig($config);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a session token + userperson + usercompany. Store it in the job.
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
protected function runStageRegistered(): void
|
||||
{
|
||||
Log::debug('Now in runStageRegistered()');
|
||||
$apiKey = Preferences::getForUser($this->job->user, 'bunq_api_key')->data;
|
||||
$serverPublicKey = Preferences::getForUser($this->job->user, 'bunq_server_public_key')->data;
|
||||
$installationToken = $this->getInstallationToken();
|
||||
$request = new DeviceSessionRequest;
|
||||
$request->setInstallationToken($installationToken);
|
||||
$request->setPrivateKey($this->getPrivateKey());
|
||||
$request->setServerPublicKey($serverPublicKey);
|
||||
$request->setSecret($apiKey);
|
||||
$request->call();
|
||||
|
||||
die(' in run stage initial');
|
||||
// todo store objects in job!
|
||||
$deviceSession = $request->getDeviceSessionId();
|
||||
$userPerson = $request->getUserPerson();
|
||||
$userCompany = $request->getUserCompany();
|
||||
|
||||
$config = $this->getConfig();
|
||||
$config['device_session_id'] = $deviceSession->toArray();
|
||||
$config['user_person'] = $userPerson->toArray();
|
||||
$config['user_company'] = $userCompany->toArray();
|
||||
$config['stage'] = 'logged-in';
|
||||
$this->setConfig($config);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -162,6 +240,46 @@ class BunqRoutine implements RoutineInterface
|
||||
$this->repository->addTotalSteps($this->job, $steps);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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('Now in createKeyPair()');
|
||||
$private = Preferences::getForUser($this->job->user, 'bunq_private_key', null);
|
||||
$public = Preferences::getForUser($this->job->user, 'bunq_public_key', null);
|
||||
|
||||
if (!(null === $private && null === $public)) {
|
||||
Log::info('Already have public and private key, return NULL.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Log::debug('Generate new 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->job->user, 'bunq_private_key', $privKey);
|
||||
Preferences::setForUser($this->job->user, 'bunq_public_key', $pubKey['key']);
|
||||
Log::debug('Created and stored key pair');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
@ -170,6 +288,38 @@ class BunqRoutine implements RoutineInterface
|
||||
return $this->repository->getConfiguration($this->job);
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to detect the current device ID (in case this instance has been registered already.
|
||||
*
|
||||
* @return DeviceServerId
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getExistingDevice(): ?DeviceServerId
|
||||
{
|
||||
Log::debug('Now in getExistingDevice()');
|
||||
$installationToken = $this->getInstallationToken();
|
||||
$serverPublicKey = $this->getServerPublicKey();
|
||||
$request = new ListDeviceServerRequest;
|
||||
$remoteIp = $this->getRemoteIp();
|
||||
$request->setInstallationToken($installationToken);
|
||||
$request->setServerPublicKey($serverPublicKey);
|
||||
$request->setPrivateKey($this->getPrivateKey());
|
||||
$request->call();
|
||||
$devices = $request->getDevices();
|
||||
/** @var DeviceServer $device */
|
||||
foreach ($devices as $device) {
|
||||
if ($device->getIp() === $remoteIp) {
|
||||
Log::debug(sprintf('This instance is registered as device #%s', $device->getId()->getId()));
|
||||
|
||||
return $device->getId();
|
||||
}
|
||||
}
|
||||
Log::info('This instance is not yet registered.');
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shorthand method.
|
||||
*
|
||||
@ -180,6 +330,127 @@ class BunqRoutine implements RoutineInterface
|
||||
return $this->repository->getExtendedStatus($this->job);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the installation token, either from the users preferences or from Bunq.
|
||||
*
|
||||
* @return InstallationToken
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getInstallationToken(): InstallationToken
|
||||
{
|
||||
Log::debug('Now in getInstallationToken().');
|
||||
$token = Preferences::getForUser($this->job->user, 'bunq_installation_token', null);
|
||||
if (null !== $token) {
|
||||
Log::debug('Have installation token, return it.');
|
||||
|
||||
return $token->data;
|
||||
}
|
||||
Log::debug('Have no installation token, request one.');
|
||||
|
||||
// verify bunq api code:
|
||||
$publicKey = $this->getPublicKey();
|
||||
$request = new InstallationTokenRequest;
|
||||
$request->setPublicKey($publicKey);
|
||||
$request->call();
|
||||
Log::debug('Sent request for installation token.');
|
||||
|
||||
$installationToken = $request->getInstallationToken();
|
||||
$installationId = $request->getInstallationId();
|
||||
$serverPublicKey = $request->getServerPublicKey();
|
||||
|
||||
Preferences::setForUser($this->job->user, 'bunq_installation_token', $installationToken);
|
||||
Preferences::setForUser($this->job->user, 'bunq_installation_id', $installationId);
|
||||
Preferences::setForUser($this->job->user, 'bunq_server_public_key', $serverPublicKey);
|
||||
|
||||
Log::debug('Stored token, ID and pub key.');
|
||||
|
||||
return $installationToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the private key from the users preferences.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getPrivateKey(): string
|
||||
{
|
||||
Log::debug('In getPrivateKey()');
|
||||
$preference = Preferences::getForUser($this->job->user, 'bunq_private_key', null);
|
||||
if (null === $preference) {
|
||||
Log::debug('private key is null');
|
||||
// create key pair
|
||||
$this->createKeyPair();
|
||||
}
|
||||
$preference = Preferences::getForUser($this->job->user, 'bunq_private_key', null);
|
||||
Log::debug('Return private key for user');
|
||||
|
||||
return $preference->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a public key from the users preferences.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getPublicKey(): string
|
||||
{
|
||||
Log::debug('Now in getPublicKey()');
|
||||
$preference = Preferences::getForUser($this->job->user, 'bunq_public_key', null);
|
||||
if (null === $preference) {
|
||||
Log::debug('public key is NULL.');
|
||||
// create key pair
|
||||
$this->createKeyPair();
|
||||
}
|
||||
$preference = Preferences::getForUser($this->job->user, 'bunq_public_key', null);
|
||||
Log::debug('Return public key for user');
|
||||
|
||||
return $preference->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request users server remote IP. Let's assume this value will not change any time soon.
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getRemoteIp(): string
|
||||
{
|
||||
$preference = Preferences::getForUser($this->job->user, 'external_ip', null);
|
||||
if (null === $preference) {
|
||||
try {
|
||||
$response = Requests::get('https://api.ipify.org');
|
||||
} catch (Exception $e) {
|
||||
throw new FireflyException(sprintf('Could not retrieve external IP: %s', $e->getMessage()));
|
||||
}
|
||||
if (200 !== $response->status_code) {
|
||||
throw new FireflyException(sprintf('Could not retrieve external IP: %d %s', $response->status_code, $response->body));
|
||||
}
|
||||
$serverIp = $response->body;
|
||||
Preferences::setForUser($this->job->user, 'external_ip', $serverIp);
|
||||
|
||||
return $serverIp;
|
||||
}
|
||||
|
||||
return $preference->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the public key of the server, necessary to verify server signature.
|
||||
*
|
||||
* @return ServerPublicKey
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getServerPublicKey(): ServerPublicKey
|
||||
{
|
||||
$pref = Preferences::getForUser($this->job->user, 'bunq_server_public_key', null)->data;
|
||||
if (is_null($pref)) {
|
||||
throw new FireflyException('Cannot determine bunq server public key, but should have it at this point.');
|
||||
}
|
||||
|
||||
return $pref;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shorthand method.
|
||||
*
|
||||
@ -190,6 +461,69 @@ class BunqRoutine implements RoutineInterface
|
||||
return $this->repository->getStatus($this->job);
|
||||
}
|
||||
|
||||
/**
|
||||
* To install Firefly III as a new device:
|
||||
* - Send an installation token request.
|
||||
* - Use this token to send a device server request
|
||||
* - Store the installation token
|
||||
* - Use the installation token each time we need a session.
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function registerDevice(): DeviceServerId
|
||||
{
|
||||
Log::debug('Now in registerDevice()');
|
||||
$deviceServerId = Preferences::getForUser($this->job->user, 'bunq_device_server_id', null);
|
||||
$serverIp = $this->getRemoteIp();
|
||||
if (null !== $deviceServerId) {
|
||||
Log::debug('Already have device server ID.');
|
||||
|
||||
return $deviceServerId->data;
|
||||
}
|
||||
|
||||
Log::debug('Device server ID is null, we have to find an existing one or register a new one.');
|
||||
$installationToken = $this->getInstallationToken();
|
||||
$serverPublicKey = $this->getServerPublicKey();
|
||||
$apiKey = Preferences::getForUser($this->job->user, 'bunq_api_key', '');
|
||||
$this->addStep();
|
||||
|
||||
// try get the current from a list:
|
||||
$deviceServerId = $this->getExistingDevice();
|
||||
$this->addStep();
|
||||
if (null !== $deviceServerId) {
|
||||
Log::debug('Found device server ID in existing devices list.');
|
||||
|
||||
return $deviceServerId;
|
||||
}
|
||||
|
||||
Log::debug('Going to create new DeviceServerRequest() because nothing found in existing list.');
|
||||
$request = new DeviceServerRequest;
|
||||
$request->setPrivateKey($this->getPrivateKey());
|
||||
$request->setDescription('Firefly III v' . config('firefly.version') . ' for ' . $this->job->user->email);
|
||||
$request->setSecret($apiKey->data);
|
||||
$request->setPermittedIps([$serverIp]);
|
||||
$request->setInstallationToken($installationToken);
|
||||
$request->setServerPublicKey($serverPublicKey);
|
||||
$deviceServerId = null;
|
||||
// try to register device:
|
||||
try {
|
||||
$request->call();
|
||||
$deviceServerId = $request->getDeviceServerId();
|
||||
} catch (FireflyException $e) {
|
||||
Log::error($e->getMessage());
|
||||
// we really have to quit at this point :(
|
||||
throw new FireflyException($e->getMessage());
|
||||
}
|
||||
if (is_null($deviceServerId)) {
|
||||
throw new FireflyException('Was not able to register server with bunq. Please see the log files.');
|
||||
}
|
||||
|
||||
Preferences::setForUser($this->job->user, 'bunq_device_server_id', $deviceServerId);
|
||||
Log::debug(sprintf('Server ID: %s', serialize($deviceServerId)));
|
||||
|
||||
return $deviceServerId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shorthand.
|
||||
*
|
||||
|
@ -24,6 +24,7 @@ namespace FireflyIII\Models;
|
||||
|
||||
use Crypt;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Log;
|
||||
use Storage;
|
||||
@ -31,6 +32,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
/**
|
||||
* Class ImportJob.
|
||||
* @property User $user
|
||||
*/
|
||||
class ImportJob extends Model
|
||||
{
|
||||
|
@ -31,6 +31,7 @@ use Log;
|
||||
|
||||
/**
|
||||
* Class Preference.
|
||||
* @property mixed $data
|
||||
*/
|
||||
class Preference extends Model
|
||||
{
|
||||
|
@ -31,6 +31,18 @@ class BunqId
|
||||
/** @var int */
|
||||
private $id = 0;
|
||||
|
||||
/**
|
||||
* BunqId constructor.
|
||||
*
|
||||
* @param null $data
|
||||
*/
|
||||
public function __construct($data = null)
|
||||
{
|
||||
if (!is_null($data)) {
|
||||
$this->id = $data['id'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
@ -46,4 +58,14 @@ class BunqId
|
||||
{
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -27,4 +27,13 @@ namespace FireflyIII\Services\Bunq\Id;
|
||||
*/
|
||||
class DeviceSessionId extends BunqId
|
||||
{
|
||||
/**
|
||||
* DeviceSessionId constructor.
|
||||
*
|
||||
* @param null $data
|
||||
*/
|
||||
public function __construct($data = null)
|
||||
{
|
||||
parent::__construct($data);
|
||||
}
|
||||
}
|
||||
|
@ -114,6 +114,19 @@ class UserCompany extends BunqObject
|
||||
$this->sectorOfIndustry = $data['sector_of_industry'] ?? '';
|
||||
$this->counterBankIban = $data['counter_bank_iban'];
|
||||
$this->name = $data['name'];
|
||||
|
||||
// TODO alias
|
||||
// TODO avatar
|
||||
// TODO daily_limit_without_confirmation_login
|
||||
// TODO notification_filters
|
||||
// TODO address_main
|
||||
// TODO address_postal
|
||||
// TODO director_alias
|
||||
// TODO ubo
|
||||
// TODO customer
|
||||
// TODO customer_limit
|
||||
// TODO billing_contract
|
||||
// TODO pack_membership
|
||||
}
|
||||
|
||||
/**
|
||||
@ -123,4 +136,32 @@ class UserCompany extends BunqObject
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
$data = [
|
||||
'id' => $this->id,
|
||||
'created' => $this->created->format('Y-m-d H:i:s.u'),
|
||||
'updated' => $this->updated->format('Y-m-d H:i:s.u'),
|
||||
'status' => $this->status,
|
||||
'sub_status' => $this->subStatus,
|
||||
'public_uuid' => $this->publicUuid,
|
||||
'display_name' => $this->displayName,
|
||||
'public_nick_name' => $this->publicNickName,
|
||||
'language' => $this->language,
|
||||
'region' => $this->region,
|
||||
'session_timeout' => $this->sessionTimeout,
|
||||
'version_terms_of_service' => $this->versionTos,
|
||||
'chamber_of_commerce_number' => $this->cocNumber,
|
||||
'type_of_business_entity' => $this->typeOfBusinessEntity,
|
||||
'sector_of_industry' => $this->sectorOfIndustry,
|
||||
'counter_bank_iban' => $this->counterBankIban,
|
||||
'name' => $this->name,
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
@ -120,8 +120,13 @@ class UserPerson extends BunqObject
|
||||
public function __construct(array $data)
|
||||
{
|
||||
if (0 === count($data)) {
|
||||
$this->created = new Carbon;
|
||||
$this->updated = new Carbon;
|
||||
$this->dateOfBirth = new Carbon;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->id = intval($data['id']);
|
||||
$this->created = Carbon::createFromFormat('Y-m-d H:i:s.u', $data['created']);
|
||||
$this->updated = Carbon::createFromFormat('Y-m-d H:i:s.u', $data['updated']);
|
||||
@ -148,14 +153,14 @@ class UserPerson extends BunqObject
|
||||
$this->documentType = $data['document_type'];
|
||||
$this->documentCountry = $data['document_country_of_issuance'];
|
||||
|
||||
// create aliases
|
||||
// create avatar
|
||||
// create daily limit
|
||||
// create notification filters
|
||||
// create address main, postal
|
||||
// document front, back attachment
|
||||
// customer, customer_limit
|
||||
// billing contracts
|
||||
// TODO create aliases
|
||||
// TODO create avatar
|
||||
// TODO create daily limit
|
||||
// TODO create notification filters
|
||||
// TODO create address main, postal
|
||||
// TODO document front, back attachment
|
||||
// TODO customer, customer_limit
|
||||
// TODO billing contracts
|
||||
}
|
||||
|
||||
/**
|
||||
@ -165,4 +170,40 @@ class UserPerson extends BunqObject
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
$data = [
|
||||
'id' => $this->id,
|
||||
'created' => $this->created->format('Y-m-d H:i:s.u'),
|
||||
'updated' => $this->updated->format('Y-m-d H:i:s.u'),
|
||||
'status' => $this->status,
|
||||
'sub_status' => $this->subStatus,
|
||||
'public_uuid' => $this->publicUuid,
|
||||
'display_name' => $this->displayName,
|
||||
'public_nick_name' => $this->publicNickName,
|
||||
'language' => $this->language,
|
||||
'region' => $this->region,
|
||||
'session_timeout' => $this->sessionTimeout,
|
||||
'first_name' => $this->firstName,
|
||||
'middle_name' => $this->middleName,
|
||||
'last_name' => $this->lastName,
|
||||
'legal_name' => $this->legalName,
|
||||
'tax_resident' => $this->taxResident,
|
||||
'date_of_birth' => $this->dateOfBirth->format('Y-m-d'),
|
||||
'place_of_birth' => $this->placeOfBirth,
|
||||
'country_of_birth' => $this->countryOfBirth,
|
||||
'nationality' => $this->nationality,
|
||||
'gender' => $this->gender,
|
||||
'version_terms_of_service' => $this->versionTos,
|
||||
'document_number' => $this->documentNumber,
|
||||
'document_type' => $this->documentType,
|
||||
'document_country_of_issuance' => $this->documentCountry,
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
@ -314,7 +314,7 @@ abstract class BunqRequest
|
||||
$signature = $this->generateSignature('post', $uri, $headers, $body);
|
||||
$headers['X-Bunq-Client-Signature'] = $signature;
|
||||
|
||||
Log::debug(sprintf('Going to send a signed bunq POST request to: %s', $fullUri));
|
||||
Log::debug(sprintf('Going to send a signed bunq POST request to: %s', $fullUri), $headers);
|
||||
|
||||
try {
|
||||
$response = Requests::post($fullUri, $headers, $body);
|
||||
|
@ -22,6 +22,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Services\Bunq\Request;
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Services\Bunq\Id\DeviceSessionId;
|
||||
use FireflyIII\Services\Bunq\Object\UserCompany;
|
||||
use FireflyIII\Services\Bunq\Object\UserPerson;
|
||||
@ -46,7 +47,7 @@ class DeviceSessionRequest extends BunqRequest
|
||||
private $userPerson;
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function call(): void
|
||||
{
|
||||
@ -61,11 +62,6 @@ class DeviceSessionRequest extends BunqRequest
|
||||
$this->userPerson = $this->extractUserPerson($response);
|
||||
$this->userCompany = $this->extractUserCompany($response);
|
||||
|
||||
Log::debug(sprintf('Session ID: %s', serialize($this->deviceSessionId)));
|
||||
Log::debug(sprintf('Session token: %s', serialize($this->sessionToken)));
|
||||
Log::debug(sprintf('Session user person: %s', serialize($this->userPerson)));
|
||||
Log::debug(sprintf('Session user company: %s', serialize($this->userCompany)));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@ -85,6 +81,14 @@ class DeviceSessionRequest extends BunqRequest
|
||||
return $this->sessionToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return UserCompany
|
||||
*/
|
||||
public function getUserCompany(): UserCompany
|
||||
{
|
||||
return $this->userCompany;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return UserPerson
|
||||
*/
|
||||
|
@ -80,6 +80,19 @@ class BunqToken
|
||||
return $this->updated;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'created' => $this->created->format('Y-m-d H:i:s.u'),
|
||||
'updated' => $this->updated->format('Y-m-d H:i:s.u'),
|
||||
'token' => $this->token,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $response
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user