1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* VerifiesAccessToken.php |
5
|
|
|
* Copyright (c) 2017 [email protected] |
6
|
|
|
* |
7
|
|
|
* This file is part of Firefly III. |
8
|
|
|
* |
9
|
|
|
* Firefly III is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU General Public License as published by |
11
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
12
|
|
|
* (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* Firefly III is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU General Public License |
20
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace FireflyIII\Console\Commands; |
24
|
|
|
|
25
|
|
|
use FireflyIII\Repositories\User\UserRepositoryInterface; |
26
|
|
|
use Log; |
27
|
|
|
use Preferences; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Trait VerifiesAccessToken. |
31
|
|
|
* |
32
|
|
|
* Verifies user access token for sensitive commands. |
33
|
|
|
*/ |
34
|
|
|
trait VerifiesAccessToken |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* Abstract method to make sure trait knows about method "option". |
38
|
|
|
* |
39
|
|
|
* @param string|null $key |
40
|
|
|
* |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
|
|
abstract public function option($key = null); |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Returns false when given token does not match given user token. |
47
|
|
|
* |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
|
|
protected function verifyAccessToken(): bool |
51
|
|
|
{ |
52
|
|
|
$userId = (int)$this->option('user'); |
53
|
|
|
$token = (string)$this->option('token'); |
54
|
|
|
/** @var UserRepositoryInterface $repository */ |
55
|
|
|
$repository = app(UserRepositoryInterface::class); |
56
|
|
|
$user = $repository->findNull($userId); |
57
|
|
|
|
58
|
|
|
if (null === $user) { |
59
|
|
|
Log::error(sprintf('verifyAccessToken(): no such user for input "%d"', $userId)); |
60
|
|
|
|
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
$accessToken = Preferences::getForUser($user, 'access_token', null); |
|
|
|
|
64
|
|
|
if (null === $accessToken) { |
65
|
|
|
Log::error(sprintf('User #%d has no access token, so cannot access command line options.', $userId)); |
66
|
|
|
|
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
if (!($accessToken->data === $token)) { |
70
|
|
|
Log::error(sprintf('Invalid access token for user #%d.', $userId)); |
71
|
|
|
Log::error(sprintf('Token given is "%s", expected something else.', $token)); |
72
|
|
|
|
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|