Completed
Push — master ( ad6e19...c672ee )
by Andrew
14s
created

CryptKey::getKeyPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Cryptography key holder.
4
 *
5
 * @author      Julián Gutiérrez <[email protected]>
6
 * @copyright   Copyright (c) Alex Bilbie
7
 * @license     http://mit-license.org/
8
 *
9
 * @link        https://github.com/thephpleague/oauth2-server
10
 */
11
12
namespace League\OAuth2\Server;
13
14
use LogicException;
15
use RuntimeException;
16
17
class CryptKey
18
{
19
    const RSA_KEY_PATTERN =
20
        '/^(-----BEGIN (RSA )?(PUBLIC|PRIVATE) KEY-----)\R.*(-----END (RSA )?(PUBLIC|PRIVATE) KEY-----)\R?$/s';
21
22
    /**
23
     * @var string
24
     */
25
    protected $keyPath;
26
27
    /**
28
     * @var null|string
29
     */
30
    protected $passPhrase;
31
32
    /**
33
     * @param string      $keyPath
34
     * @param null|string $passPhrase
35
     * @param bool        $keyPermissionsCheck
36
     */
37 55
    public function __construct($keyPath, $passPhrase = null, $keyPermissionsCheck = true)
38
    {
39 55
        if ($rsaMatch = preg_match(static::RSA_KEY_PATTERN, $keyPath)) {
40 1
            $keyPath = $this->saveKeyToFile($keyPath);
41 54
        } elseif ($rsaMatch === false) {
42 1
            throw new \RuntimeException(
43 1
                sprintf('PCRE error [%d] encountered during key match attempt', preg_last_error())
44
            );
45
        }
46
47 54
        if (strpos($keyPath, 'file://') !== 0) {
48 2
            $keyPath = 'file://' . $keyPath;
49
        }
50
51 54
        if (!file_exists($keyPath) || !is_readable($keyPath)) {
52 1
            throw new LogicException(sprintf('Key path "%s" does not exist or is not readable', $keyPath));
53
        }
54
55 53
        if ($keyPermissionsCheck === true) {
56
            // Verify the permissions of the key
57 53
            $keyPathPerms = decoct(fileperms($keyPath) & 0777);
58 53
            if (in_array($keyPathPerms, ['400', '440', '600', '640', '660'], true) === false) {
59
                trigger_error(sprintf(
60
                    'Key file "%s" permissions are not correct, recommend changing to 600 or 660 instead of %s',
61
                    $keyPath,
62
                    $keyPathPerms
63
                ), E_USER_NOTICE);
64
            }
65
        }
66
67 53
        $this->keyPath = $keyPath;
68 53
        $this->passPhrase = $passPhrase;
69 53
    }
70
71
    /**
72
     * @param string $key
73
     *
74
     * @throws RuntimeException
75
     *
76
     * @return string
77
     */
78 1
    private function saveKeyToFile($key)
79
    {
80 1
        $tmpDir = sys_get_temp_dir();
81 1
        $keyPath = $tmpDir . '/' . sha1($key) . '.key';
82
83 1
        if (file_exists($keyPath)) {
84
            return 'file://' . $keyPath;
85
        }
86
87 1
        if (file_put_contents($keyPath, $key) === false) {
88
            // @codeCoverageIgnoreStart
89
            throw new RuntimeException(sprintf('Unable to write key file to temporary directory "%s"', $tmpDir));
90
            // @codeCoverageIgnoreEnd
91
        }
92
93 1
        if (chmod($keyPath, 0600) === false) {
94
            // @codeCoverageIgnoreStart
95
            throw new RuntimeException(sprintf('The key file "%s" file mode could not be changed with chmod to 600', $keyPath));
96
            // @codeCoverageIgnoreEnd
97
        }
98
99 1
        return 'file://' . $keyPath;
100
    }
101
102
    /**
103
     * Retrieve key path.
104
     *
105
     * @return string
106
     */
107 14
    public function getKeyPath()
108
    {
109 14
        return $this->keyPath;
110
    }
111
112
    /**
113
     * Retrieve key pass phrase.
114
     *
115
     * @return null|string
116
     */
117 10
    public function getPassPhrase()
118
    {
119 10
        return $this->passPhrase;
120
    }
121
}
122