12
12
namespace League \OAuth2 \Server ;
13
13
14
14
use LogicException ;
15
- use RuntimeException ;
16
15
17
16
class CryptKey
18
17
{
@@ -22,6 +21,11 @@ class CryptKey
22
21
23
22
private const FILE_PREFIX = 'file:// ' ;
24
23
24
+ /**
25
+ * @var string Key contents
26
+ */
27
+ protected $ keyContents ;
28
+
25
29
/**
26
30
* @var string
27
31
*/
@@ -41,21 +45,26 @@ public function __construct($keyPath, $passPhrase = null, $keyPermissionsCheck =
41
45
{
42
46
$ this ->passPhrase = $ passPhrase ;
43
47
44
- if (\is_file ($ keyPath )) {
48
+ if (\strpos ($ keyPath , self ::FILE_PREFIX ) !== 0 && $ this ->isValidKey ($ keyPath , $ this ->passPhrase ?? '' )) {
49
+ $ this ->keyContents = $ keyPath ;
50
+ $ this ->keyPath = '' ;
51
+ // There's no file, so no need for permission check.
52
+ $ keyPermissionsCheck = false ;
53
+ } elseif (\is_file ($ keyPath )) {
45
54
if (\strpos ($ keyPath , self ::FILE_PREFIX ) !== 0 ) {
46
55
$ keyPath = self ::FILE_PREFIX . $ keyPath ;
47
56
}
48
57
49
58
if (!\is_readable ($ keyPath )) {
50
59
throw new LogicException (\sprintf ('Key path "%s" does not exist or is not readable ' , $ keyPath ));
51
60
}
52
- $ isFileKey = true ;
53
- $ contents = \file_get_contents ($ keyPath );
61
+ $ this ->keyContents = \file_get_contents ($ keyPath );
54
62
$ this ->keyPath = $ keyPath ;
63
+ if (!$ this ->isValidKey ($ this ->keyContents , $ this ->passPhrase ?? '' )) {
64
+ throw new LogicException ('Unable to read key from file ' . $ keyPath );
65
+ }
55
66
} else {
56
- $ isFileKey = false ;
57
- $ contents = $ keyPath ;
58
- $ this ->keyPath = $ this ->saveKeyToFile ($ keyPath );
67
+ throw new LogicException ('Unable to read key from file ' . $ keyPath );
59
68
}
60
69
61
70
if ($ keyPermissionsCheck === true ) {
@@ -72,41 +81,16 @@ public function __construct($keyPath, $passPhrase = null, $keyPermissionsCheck =
72
81
);
73
82
}
74
83
}
75
-
76
- if (!$ this ->isValidKey ($ contents , $ this ->passPhrase ?? '' )) {
77
- throw new LogicException ('Unable to read key ' . ($ isFileKey ? " from file $ keyPath " : '' ));
78
- }
79
84
}
80
85
81
86
/**
82
- * @param string $ key
87
+ * Get key contents
83
88
*
84
- * @throws RuntimeException
85
- *
86
- * @return string
89
+ * @return string Key contents
87
90
*/
88
- private function saveKeyToFile ( $ key )
91
+ public function getKeyContents (): string
89
92
{
90
- $ tmpDir = \sys_get_temp_dir ();
91
- $ keyPath = $ tmpDir . '/ ' . \sha1 ($ key ) . '.key ' ;
92
-
93
- if (\file_exists ($ keyPath )) {
94
- return self ::FILE_PREFIX . $ keyPath ;
95
- }
96
-
97
- if (\file_put_contents ($ keyPath , $ key ) === false ) {
98
- // @codeCoverageIgnoreStart
99
- throw new RuntimeException (\sprintf ('Unable to write key file to temporary directory "%s" ' , $ tmpDir ));
100
- // @codeCoverageIgnoreEnd
101
- }
102
-
103
- if (\chmod ($ keyPath , 0600 ) === false ) {
104
- // @codeCoverageIgnoreStart
105
- throw new RuntimeException (\sprintf ('The key file "%s" file mode could not be changed with chmod to 600 ' , $ keyPath ));
106
- // @codeCoverageIgnoreEnd
107
- }
108
-
109
- return self ::FILE_PREFIX . $ keyPath ;
93
+ return $ this ->keyContents ;
110
94
}
111
95
112
96
/**
0 commit comments