Mercurial > hg > isophonics-drupal-site
annotate core/lib/Drupal/Core/PrivateKey.php @ 19:fa3358dc1485 tip
Add ndrum files
author | Chris Cannam |
---|---|
date | Wed, 28 Aug 2019 13:14:47 +0100 |
parents | 4c8ae668cc8c |
children |
rev | line source |
---|---|
Chris@0 | 1 <?php |
Chris@0 | 2 |
Chris@0 | 3 namespace Drupal\Core; |
Chris@0 | 4 |
Chris@0 | 5 use Drupal\Core\State\StateInterface; |
Chris@0 | 6 use Drupal\Component\Utility\Crypt; |
Chris@0 | 7 |
Chris@0 | 8 /** |
Chris@0 | 9 * Manages the Drupal private key. |
Chris@0 | 10 */ |
Chris@0 | 11 class PrivateKey { |
Chris@0 | 12 |
Chris@0 | 13 /** |
Chris@0 | 14 * The state service. |
Chris@0 | 15 * |
Chris@0 | 16 * @var \Drupal\Core\State\StateInterface |
Chris@0 | 17 */ |
Chris@0 | 18 protected $state; |
Chris@0 | 19 |
Chris@0 | 20 /** |
Chris@0 | 21 * Constructs the token generator. |
Chris@0 | 22 * |
Chris@0 | 23 * @param \Drupal\Core\State\StateInterface $state |
Chris@0 | 24 * The state service. |
Chris@0 | 25 */ |
Chris@0 | 26 public function __construct(StateInterface $state) { |
Chris@0 | 27 $this->state = $state; |
Chris@0 | 28 } |
Chris@0 | 29 |
Chris@0 | 30 /** |
Chris@0 | 31 * Gets the private key. |
Chris@0 | 32 * |
Chris@0 | 33 * @return string |
Chris@0 | 34 * The private key. |
Chris@0 | 35 */ |
Chris@0 | 36 public function get() { |
Chris@0 | 37 if (!$key = $this->state->get('system.private_key')) { |
Chris@0 | 38 $key = $this->create(); |
Chris@0 | 39 $this->set($key); |
Chris@0 | 40 } |
Chris@0 | 41 |
Chris@0 | 42 return $key; |
Chris@0 | 43 } |
Chris@0 | 44 |
Chris@0 | 45 /** |
Chris@0 | 46 * Sets the private key. |
Chris@0 | 47 * |
Chris@0 | 48 * @param string $key |
Chris@0 | 49 * The private key to set. |
Chris@0 | 50 */ |
Chris@0 | 51 public function set($key) { |
Chris@0 | 52 return $this->state->set('system.private_key', $key); |
Chris@0 | 53 } |
Chris@0 | 54 |
Chris@0 | 55 /** |
Chris@0 | 56 * Creates a new private key. |
Chris@0 | 57 * |
Chris@0 | 58 * @return string |
Chris@0 | 59 * The private key. |
Chris@0 | 60 */ |
Chris@0 | 61 protected function create() { |
Chris@0 | 62 return Crypt::randomBytesBase64(55); |
Chris@0 | 63 } |
Chris@0 | 64 |
Chris@0 | 65 } |