Passed
Push — master ( 49de82...9f1fc0 )
by James
35:45 queued 23:44
created

OAuthKeys::keysInDatabase()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * OAuthKeys.php
4
 * Copyright (c) 2019 thegrump[email protected]
5
 *
6
 * This file is part of Firefly III (https://github.com/firefly-iii).
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
namespace FireflyIII\Support\System;
23
24
use Artisan;
25
use Crypt;
26
use Laravel\Passport\Console\KeysCommand;
27
28
/**
29
 * Class OAuthKeys
30
 */
31
class OAuthKeys
32
{
33
    private const PRIVATE_KEY = 'oauth_private_key';
34
    private const PUBLIC_KEY  = 'oauth_public_key';
35
36
    /**
37
     *
38
     */
39
    public static function generateKeys(): void
40
    {
41
        Artisan::registerCommand(new KeysCommand());
42
        Artisan::call('passport:keys');
43
    }
44
45
    /**
46
     * @return bool
47
     */
48
    public static function hasKeyFiles(): bool
49
    {
50
        $private = storage_path('oauth-private.key');
51
        $public  = storage_path('oauth-public.key');
52
53
        return file_exists($private) && file_exists($public);
54
    }
55
56
    /**
57
     * @return bool
58
     */
59
    public static function keysInDatabase(): bool
60
    {
61
        return app('fireflyconfig')->has(self::PRIVATE_KEY) && app('fireflyconfig')->has(self::PUBLIC_KEY);
0 ignored issues
show
Bug introduced by James Cole
The method has() does not exist on FireflyIII\Support\Facades\FireflyConfig. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
        return app('fireflyconfig')->/** @scrutinizer ignore-call */ has(self::PRIVATE_KEY) && app('fireflyconfig')->has(self::PUBLIC_KEY);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
    }
63
64
    /**
65
     *
66
     */
67
    public static function restoreKeysFromDB(): void
68
    {
69
        $privateContent = Crypt::decrypt(app('fireflyconfig')->get(self::PRIVATE_KEY)->data);
70
        $publicContent  = Crypt::decrypt(app('fireflyconfig')->get(self::PUBLIC_KEY)->data);
71
        $private        = storage_path('oauth-private.key');
72
        $public         = storage_path('oauth-public.key');
73
        file_put_contents($private, $privateContent);
74
        file_put_contents($public, $publicContent);
75
    }
76
77
    /**
78
     *
79
     */
80
    public static function storeKeysInDB(): void
81
    {
82
        $private = storage_path('oauth-private.key');
83
        $public  = storage_path('oauth-public.key');
84
        app('fireflyconfig')->set(self::PRIVATE_KEY, Crypt::encrypt(file_get_contents($private)));
85
        app('fireflyconfig')->set(self::PUBLIC_KEY, Crypt::encrypt(file_get_contents($public)));
86
    }
87
88
    /**
89
     *
90
     */
91
    public static function verifyKeysRoutine(): void
92
    {
93
        if (!self::keysInDatabase() && !self::hasKeyFiles()) {
94
            self::generateKeys();
95
            self::storeKeysInDB();
96
97
            return;
98
        }
99
        if (self::keysInDatabase() && !self::hasKeyFiles()) {
100
            self::restoreKeysFromDB();
101
102
            return;
103
        }
104
        if (!self::keysInDatabase() && self::hasKeyFiles()) {
105
            self::storeKeysInDB();
106
107
            return;
108
        }
109
    }
110
111
}