Skip to content

Commit

Permalink
Added authenticate
Browse files Browse the repository at this point in the history
  • Loading branch information
s1lver committed Apr 14, 2023
1 parent d39a23d commit 1173648
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Changelog yii2 etcd component

## 1.0.1
- Added authenticate

## 1.0.0
- Initial version
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ to the require section of your composer.json file.
- `version`
- `range`
- `put`
- `authenticate`


## How to use
Expand All @@ -43,6 +44,8 @@ $config = [
'etcd' => [
'class' => \S1lver\Etcd\Etcd::class,
'host' => 'etcd:2379',
'user' => 'username',
'password' => 'password',
],
],
];
Expand Down
23 changes: 23 additions & 0 deletions src/Etcd.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions;
use JsonException;
use S1lver\Etcd\Rest\AuthenticateResponse;
use S1lver\Etcd\Rest\RangeResponse;
use yii\base\Component;

Expand All @@ -21,6 +22,8 @@ class Etcd extends Component
private const ETCD_VERSION = '/v3';

public string $host = '';
public string $user = '';
public string $password = '';

private Client $client;

Expand Down Expand Up @@ -56,6 +59,7 @@ public function getKey(string $key): RangeResponse
$this->host.self::ETCD_VERSION.EtcdEndpoint::RANGE,
[
RequestOptions::BODY => json_encode(['key' => $key], JSON_THROW_ON_ERROR),
RequestOptions::HEADERS => ['Authorization' => $this->authenticate()->token]
]
);

Expand All @@ -76,4 +80,23 @@ public function put(string $key, string $value): bool

return 200 === $response->getStatusCode();
}

/**
* @return AuthenticateResponse
* @throws GuzzleException|JsonException
*/
public function authenticate(): AuthenticateResponse
{
$response = $this->client->post(
$this->host.self::ETCD_VERSION.EtcdEndpoint::AUTHENTICATE,
[
RequestOptions::BODY => json_encode(
['name' => $this->user, 'password' => $this->password],
JSON_THROW_ON_ERROR
),
]
);

return new AuthenticateResponse(json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR));
}
}
2 changes: 2 additions & 0 deletions src/EtcdEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ final class EtcdEndpoint
public const PUT = '/kv/put';
public const RANGE = '/kv/range';

// Authentication
public const AUTHENTICATE = '/auth/authenticate';
}
13 changes: 13 additions & 0 deletions src/Rest/AuthenticateResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace S1lver\Etcd\Rest;

use yii\base\BaseObject;

class AuthenticateResponse extends BaseObject
{
public array $header = [];
public string $token = '';
}

0 comments on commit 1173648

Please sign in to comment.