Skip to content

Commit 35fc377

Browse files
authored
Merge pull request #1180 from RunSignUp-Team/keyStr
Use key as string without a temporary file
2 parents 0c86312 + bec2d33 commit 35fc377

File tree

4 files changed

+36
-47
lines changed

4 files changed

+36
-47
lines changed

src/AuthorizationValidators/BearerTokenValidator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Lcobucci\Clock\SystemClock;
1414
use Lcobucci\JWT\Configuration;
1515
use Lcobucci\JWT\Signer\Key\InMemory;
16-
use Lcobucci\JWT\Signer\Key\LocalFileReference;
1716
use Lcobucci\JWT\Signer\Rsa\Sha256;
1817
use Lcobucci\JWT\Validation\Constraint\SignedWith;
1918
use Lcobucci\JWT\Validation\Constraint\StrictValidAt;
@@ -78,7 +77,10 @@ private function initJwtConfiguration()
7877
\class_exists(StrictValidAt::class)
7978
? new StrictValidAt(new SystemClock(new DateTimeZone(\date_default_timezone_get())))
8079
: new ValidAt(new SystemClock(new DateTimeZone(\date_default_timezone_get()))),
81-
new SignedWith(new Sha256(), LocalFileReference::file($this->publicKey->getKeyPath()))
80+
new SignedWith(
81+
new Sha256(),
82+
InMemory::plainText($this->publicKey->getKeyContents(), $this->publicKey->getPassPhrase() ?? '')
83+
)
8284
);
8385
}
8486

src/CryptKey.php

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace League\OAuth2\Server;
1313

1414
use LogicException;
15-
use RuntimeException;
1615

1716
class CryptKey
1817
{
@@ -22,6 +21,11 @@ class CryptKey
2221

2322
private const FILE_PREFIX = 'file://';
2423

24+
/**
25+
* @var string Key contents
26+
*/
27+
protected $keyContents;
28+
2529
/**
2630
* @var string
2731
*/
@@ -41,21 +45,26 @@ public function __construct($keyPath, $passPhrase = null, $keyPermissionsCheck =
4145
{
4246
$this->passPhrase = $passPhrase;
4347

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)) {
4554
if (\strpos($keyPath, self::FILE_PREFIX) !== 0) {
4655
$keyPath = self::FILE_PREFIX . $keyPath;
4756
}
4857

4958
if (!\is_readable($keyPath)) {
5059
throw new LogicException(\sprintf('Key path "%s" does not exist or is not readable', $keyPath));
5160
}
52-
$isFileKey = true;
53-
$contents = \file_get_contents($keyPath);
61+
$this->keyContents = \file_get_contents($keyPath);
5462
$this->keyPath = $keyPath;
63+
if (!$this->isValidKey($this->keyContents, $this->passPhrase ?? '')) {
64+
throw new LogicException('Unable to read key from file ' . $keyPath);
65+
}
5566
} else {
56-
$isFileKey = false;
57-
$contents = $keyPath;
58-
$this->keyPath = $this->saveKeyToFile($keyPath);
67+
throw new LogicException('Unable to read key from file ' . $keyPath);
5968
}
6069

6170
if ($keyPermissionsCheck === true) {
@@ -72,41 +81,16 @@ public function __construct($keyPath, $passPhrase = null, $keyPermissionsCheck =
7281
);
7382
}
7483
}
75-
76-
if (!$this->isValidKey($contents, $this->passPhrase ?? '')) {
77-
throw new LogicException('Unable to read key' . ($isFileKey ? " from file $keyPath" : ''));
78-
}
7984
}
8085

8186
/**
82-
* @param string $key
87+
* Get key contents
8388
*
84-
* @throws RuntimeException
85-
*
86-
* @return string
89+
* @return string Key contents
8790
*/
88-
private function saveKeyToFile($key)
91+
public function getKeyContents(): string
8992
{
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;
11094
}
11195

11296
/**

src/Entities/Traits/AccessTokenTrait.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use DateTimeImmutable;
1313
use Lcobucci\JWT\Configuration;
1414
use Lcobucci\JWT\Signer\Key\InMemory;
15-
use Lcobucci\JWT\Signer\Key\LocalFileReference;
1615
use Lcobucci\JWT\Signer\Rsa\Sha256;
1716
use Lcobucci\JWT\Token;
1817
use League\OAuth2\Server\CryptKey;
@@ -46,7 +45,7 @@ public function initJwtConfiguration()
4645
{
4746
$this->jwtConfiguration = Configuration::forAsymmetricSigner(
4847
new Sha256(),
49-
LocalFileReference::file($this->privateKey->getKeyPath(), $this->privateKey->getPassPhrase() ?? ''),
48+
InMemory::plainText($this->privateKey->getKeyContents(), $this->privateKey->getPassPhrase() ?? ''),
5049
InMemory::plainText('')
5150
);
5251
}

tests/Utils/CryptKeyTest.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function testKeyCreation()
2323
$this->assertEquals('secret', $key->getPassPhrase());
2424
}
2525

26-
public function testKeyFileCreation()
26+
public function testKeyString()
2727
{
2828
$keyContent = \file_get_contents(__DIR__ . '/../Stubs/public.key');
2929

@@ -33,7 +33,10 @@ public function testKeyFileCreation()
3333

3434
$key = new CryptKey($keyContent);
3535

36-
$this->assertEquals(self::generateKeyPath($keyContent), $key->getKeyPath());
36+
$this->assertEquals(
37+
$keyContent,
38+
$key->getKeyContents()
39+
);
3740

3841
$keyContent = \file_get_contents(__DIR__ . '/../Stubs/private.key.crlf');
3942

@@ -43,7 +46,10 @@ public function testKeyFileCreation()
4346

4447
$key = new CryptKey($keyContent);
4548

46-
$this->assertEquals(self::generateKeyPath($keyContent), $key->getKeyPath());
49+
$this->assertEquals(
50+
$keyContent,
51+
$key->getKeyContents()
52+
);
4753
}
4854

4955
public function testUnsupportedKeyType()
@@ -83,9 +89,8 @@ public function testECKeyType()
8389
\openssl_pkey_export($res, $keyContent, 'mystrongpassword');
8490

8591
$key = new CryptKey($keyContent, 'mystrongpassword');
86-
$path = self::generateKeyPath($keyContent);
8792

88-
$this->assertEquals($path, $key->getKeyPath());
93+
$this->assertEquals('', $key->getKeyPath());
8994
$this->assertEquals('mystrongpassword', $key->getPassPhrase());
9095
} catch (\Throwable $e) {
9196
$this->fail('The EC key was not created');
@@ -109,9 +114,8 @@ public function testRSAKeyType()
109114
\openssl_pkey_export($res, $keyContent, 'mystrongpassword');
110115

111116
$key = new CryptKey($keyContent, 'mystrongpassword');
112-
$path = self::generateKeyPath($keyContent);
113117

114-
$this->assertEquals($path, $key->getKeyPath());
118+
$this->assertEquals('', $key->getKeyPath());
115119
$this->assertEquals('mystrongpassword', $key->getPassPhrase());
116120
} catch (\Throwable $e) {
117121
$this->fail('The RSA key was not created');

0 commit comments

Comments
 (0)