From d8930af5eecd4ae453d2f2951e5d8463c1116133 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Guti=C3=A9rrez?= Date: Tue, 19 Jul 2016 15:01:31 +0200 Subject: [PATCH] key file auto-generation from string --- src/CryptKey.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/CryptKey.php b/src/CryptKey.php index e088abcf..617bb8e8 100644 --- a/src/CryptKey.php +++ b/src/CryptKey.php @@ -13,6 +13,9 @@ namespace League\OAuth2\Server; class CryptKey { + const RSA_KEY_PATTERN = + '/^(-----BEGIN (RSA )?(PUBLIC|PRIVATE) KEY-----\n)(.|\n)+(-----END (RSA )?(PUBLIC|PRIVATE) KEY-----)$/'; + /** * @var string */ @@ -29,6 +32,10 @@ class CryptKey */ public function __construct($keyPath, $passPhrase = null) { + if (preg_match(self::RSA_KEY_PATTERN, $keyPath)) { + $keyPath = $this->saveKeyToFile($keyPath); + } + if (strpos($keyPath, 'file://') !== 0) { $keyPath = 'file://' . $keyPath; } @@ -41,6 +48,24 @@ class CryptKey $this->passPhrase = $passPhrase; } + /** + * @param string $key + * + * @return string + */ + private function saveKeyToFile($key) + { + $keyPath = sys_get_temp_dir() . '/' . sha1($key) . '.key'; + + if (!file_exists($keyPath) && !mkdir($keyPath)) { + throw new \RuntimeException('"%s" key file could not be created', $keyPath); + } + + file_put_contents($keyPath, $key); + + return 'file://' . $keyPath; + } + /** * Retrieve key path. *