| 1: | <?php |
| 2: | /* |
| 3: | You may not change or alter any portion of this comment or credits |
| 4: | of supporting developers from this source code or any supporting source code |
| 5: | which is considered copyrighted (c) material of the original comment or credit authors. |
| 6: | |
| 7: | This program is distributed in the hope that it will be useful, |
| 8: | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9: | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 10: | */ |
| 11: | |
| 12: | namespace Xmf\Jwt; |
| 13: | |
| 14: | use Xmf\Key\Basic; |
| 15: | use Xmf\Key\FileStorage; |
| 16: | use Xmf\Key\StorageInterface; |
| 17: | |
| 18: | /** |
| 19: | * Build a key to be used for JSON Web Token processing |
| 20: | * |
| 21: | * @category Xmf\Jwt\KeyFactory |
| 22: | * @package Xmf |
| 23: | * @author Richard Griffith <richard@geekwright.com> |
| 24: | * @copyright 2016-2018 XOOPS Project (https://xoops.org) |
| 25: | * @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html) |
| 26: | * @link https://xoops.org |
| 27: | */ |
| 28: | class KeyFactory |
| 29: | { |
| 30: | /** |
| 31: | * Create a Key object for JWT use based on default choices. If the key has not been |
| 32: | * established, create it. |
| 33: | * |
| 34: | * @param string $keyName name of the key |
| 35: | * @param StorageInterface $storage key store to use, defaults to FileStorage |
| 36: | * |
| 37: | * @return Basic |
| 38: | * |
| 39: | * @throws \InvalidArgumentException on unusable key name |
| 40: | */ |
| 41: | public static function build($keyName, StorageInterface $storage = null) |
| 42: | { |
| 43: | if (empty($keyName) || !is_string($keyName)) { |
| 44: | throw new \InvalidArgumentException('keyName must be a non-empty string'); |
| 45: | } |
| 46: | $storage = (null === $storage) ? new FileStorage() : $storage; |
| 47: | $key = new Basic($storage, $keyName); |
| 48: | $key->create(); // will automatically skip if key has already been generated |
| 49: | return $key; |
| 50: | } |
| 51: | } |
| 52: |