pssht  latest
SSH server library written in PHP
KeyStore.php
1 <?php
2 
3 /*
4 * This file is part of pssht.
5 *
6 * (c) François Poirotte <clicky@erebot.net>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 
12 namespace fpoirotte\Pssht;
13 
17 class KeyStore
18 {
20  protected $keys;
21 
25  public function __construct()
26  {
27  $this->keys = array();
28  }
29 
39  protected function getIdentifier(\fpoirotte\Pssht\Key\KeyInterface $key)
40  {
41  $encoder = new \fpoirotte\Pssht\Wire\Encoder();
42  $key->serialize($encoder);
43  return $encoder->getBuffer()->get(0);
44  }
45 
55  public function add($user, \fpoirotte\Pssht\Key\KeyInterface $key)
56  {
57  if (!is_string($user)) {
58  throw new \InvalidArgumentException();
59  }
60 
61  $this->keys[$user][$this->getIdentifier($key)] = $key;
62  }
63 
73  public function remove($user, \fpoirotte\Pssht\Key\KeyInterface $key)
74  {
75  if (!is_string($user)) {
76  throw new \InvalidArgumentException();
77  }
78 
79  unset($this->keys[$user][$this->getIdentifier($key)]);
80  }
81 
92  public function get($user)
93  {
94  if (!is_string($user)) {
95  throw new \InvalidArgumentException();
96  }
97 
98  if (!isset($this->keys[$user])) {
99  return new \ArrayIterator();
100  }
101 
102  return new \ArrayIterator($this->keys[$user]);
103  }
104 
119  public function exists($user, $key)
120  {
121  if (!is_string($user)) {
122  throw new \InvalidArgumentException();
123  }
124 
125  if ($key instanceof \fpoirotte\Pssht\Key\KeyInterface) {
126  $key = $this->getIdentifier($key);
127  }
128  if (!is_string($key)) {
129  throw new \InvalidArgumentException();
130  }
131 
132  return isset($this->keys[$user][$key]);
133  }
134 
145  public function count($user)
146  {
147  if (!is_string($user)) {
148  throw new \InvalidArgumentException();
149  }
150 
151  if (!isset($this->keys[$user])) {
152  return 0;
153  }
154 
155  return count($this->keys[$user]);
156  }
157 }
$keys
Public/private keys currently stored.
Definition: KeyStore.php:20
getIdentifier(\fpoirotte\Pssht\Key\KeyInterface $key)
Definition: KeyStore.php:39
add($user,\fpoirotte\Pssht\Key\KeyInterface $key)
Definition: KeyStore.php:55