Passed
Pull Request — master (#1180)
by
unknown
60:28 queued 25:27
created

CryptKey::getKeyPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
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 Lcobucci\JWT\Signer\Key;
15
use Lcobucci\JWT\Signer\Key\InMemory;
16
use Lcobucci\JWT\Signer\Key\LocalFileReference;
17
use LogicException;
18
19
class CryptKey
20
{
21
    const RSA_KEY_PATTERN =
22
        '/^(-----BEGIN (RSA )?(PUBLIC|PRIVATE) KEY-----)\R.*(-----END (RSA )?(PUBLIC|PRIVATE) KEY-----)\R?$/s';
23
24
    /**
25
     * @var Key
26
     */
27
    protected $key;
28
29
    /**
30
     * @var string
31
     */
32
    protected $keyPath;
33
34
    /**
35
     * @var null|string
36
     */
37 56
    protected $passPhrase;
38
39 56
    /**
40 1
     * @param string      $keyPath
41 55
     * @param null|string $passPhrase
42 1
     * @param bool        $keyPermissionsCheck
43 1
     */
44
    public function __construct($keyPath, $passPhrase = null, $keyPermissionsCheck = true)
45
    {
46
        if ($rsaMatch = \preg_match(static::RSA_KEY_PATTERN, $keyPath)) {
47 55
            $this->key = InMemory::plainText($keyPath);
48 2
        } elseif ($rsaMatch === false) {
49
            throw new \RuntimeException(
50
                \sprintf('PCRE error [%d] encountered during key match attempt', \preg_last_error())
51 55
            );
52 1
        } else {
53
            if (\strpos($keyPath, 'file://') !== 0) {
54
                $keyPath = 'file://' . $keyPath;
55 54
            }
56
57 54
            if (!\file_exists($keyPath) || !\is_readable($keyPath)) {
58 54
                throw new LogicException(\sprintf('Key path "%s" does not exist or is not readable', $keyPath));
59
            }
60
61
            if ($keyPermissionsCheck === true) {
62
                // Verify the permissions of the key
63
                $keyPathPerms = \decoct(\fileperms($keyPath) & 0777);
64
                if (\in_array($keyPathPerms, ['400', '440', '600', '640', '660'], true) === false) {
65
                    \trigger_error(\sprintf(
66
                        'Key file "%s" permissions are not correct, recommend changing to 600 or 660 instead of %s',
67 54
                        $keyPath,
68 54
                        $keyPathPerms
69 54
                    ), E_USER_NOTICE);
70
                }
71
            }
72
73
            $this->keyPath = $keyPath;
74
            $this->passPhrase = $passPhrase;
75
            $this->key = LocalFileReference::file($this->keyPath, $this->passPhrase ?? '');
76
        }
77
    }
78 1
79
    /**
80 1
     * Get key
81 1
     *
82
     * @return Key
83 1
     */
84
    public function getKey(): Key
85
    {
86
        return $this->key;
87 1
    }
88
89
    /**
90
     * Retrieve key path.
91
     *
92
     * @return string
93 1
     */
94
    public function getKeyPath()
95
    {
96
        return $this->keyPath;
97
    }
98
99 1
    /**
100
     * Retrieve key pass phrase.
101
     *
102
     * @return null|string
103
     */
104
    public function getPassPhrase()
105
    {
106
        return $this->passPhrase;
107 19
    }
108
}
109