KeyClient.php in Lockr 7.2
File
vendor/lockr/lockr-client/src/KeyClient.php
View source
<?php
namespace Lockr;
use Lockr\KeyWrapper\MultiKeyWrapper;
class KeyClient {
protected $client;
protected $encoded = null;
public function __construct(Lockr $client) {
$this->client = $client;
}
public function encrypted($encoded = true) {
$this->encoded = $encoded;
return $this;
}
public function get($name) {
$body = $this->client
->get($this
->uri($name));
if (null !== $this->encoded) {
return MultiKeyWrapper::decrypt($body['key_value'], $this->encoded);
}
return $body['key_value'];
}
public function create($key_size) {
$body = $this->client
->get("/v1/generate-key?key_size={$key_size}");
return base64_decode($body['key_value']);
}
public function set($name, $value, $label, $encoded = null) {
if ($this->encoded) {
if ($encoded === NULL) {
$ret = MultiKeyWrapper::encrypt($value);
}
else {
$ret = MultiKeyWrapper::reencrypt($value, $encoded);
}
$value = $ret['ciphertext'];
}
$data = array(
'key_value' => $value,
'key_label' => $label,
);
$this->client
->patch($this
->uri($name), $data);
if ($this->encoded) {
return $ret['encoded'];
}
return true;
}
public function delete($name) {
$this->client
->delete($this
->uri($name));
}
protected function uri($name) {
return '/v1/key/' . urlencode($name);
}
}