Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sasl): improve sasl interface #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 2 additions & 18 deletions src/Client/SyncClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
use longlang\phpkafka\Protocol\KafkaRequest;
use longlang\phpkafka\Protocol\RequestHeader\RequestHeader;
use longlang\phpkafka\Protocol\ResponseHeader\ResponseHeader;
use longlang\phpkafka\Protocol\SaslAuthenticate\SaslAuthenticateRequest;
use longlang\phpkafka\Protocol\SaslAuthenticate\SaslAuthenticateResponse;
use longlang\phpkafka\Protocol\SaslHandshake\SaslHandshakeRequest;
use longlang\phpkafka\Protocol\SaslHandshake\SaslHandshakeResponse;
use longlang\phpkafka\Protocol\Type\Int32;
use longlang\phpkafka\Sasl\SaslInterface;
use longlang\phpkafka\Socket\SocketInterface;
Expand Down Expand Up @@ -203,22 +199,10 @@ protected function sendAuthInfo(): void
if (!isset($config['type']) || empty($config['type'])) {
return;
}
$class = new $config['type']($this->getConfig());
$class = new $config['type']($this, $this->getConfig());
if (!$class instanceof SaslInterface) {
return;
}
$handshakeRequest = new SaslHandshakeRequest();
$handshakeRequest->setMechanism($class->getName());
$correlationId = $this->send($handshakeRequest);
/** @var SaslHandshakeResponse $handshakeResponse */
$handshakeResponse = $this->recv($correlationId);
ErrorCode::check($handshakeResponse->getErrorCode());

$authenticateRequest = new SaslAuthenticateRequest();
$authenticateRequest->setAuthBytes($class->getAuthBytes());
$correlationId = $this->send($authenticateRequest);
/** @var SaslAuthenticateResponse $authenticateResponse */
$authenticateResponse = $this->recv($correlationId);
ErrorCode::check($authenticateResponse->getErrorCode());
$class->auth();
}
}
34 changes: 31 additions & 3 deletions src/Sasl/PlainSasl.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@

namespace longlang\phpkafka\Sasl;

use longlang\phpkafka\Client\ClientInterface;
use longlang\phpkafka\Config\CommonConfig;
use longlang\phpkafka\Exception\KafkaErrorException;
use longlang\phpkafka\Protocol\ErrorCode;
use longlang\phpkafka\Protocol\SaslAuthenticate\SaslAuthenticateRequest;
use longlang\phpkafka\Protocol\SaslAuthenticate\SaslAuthenticateResponse;
use longlang\phpkafka\Protocol\SaslHandshake\SaslHandshakeRequest;
use longlang\phpkafka\Protocol\SaslHandshake\SaslHandshakeResponse;

class PlainSasl implements SaslInterface
{
Expand All @@ -14,23 +20,45 @@ class PlainSasl implements SaslInterface
*/
protected $config;

public function __construct(CommonConfig $config)
/**
* @var ClientInterface
*/
protected $client;

public function __construct(ClientInterface $client, CommonConfig $config)
{
$this->client = $client;
$this->config = $config;
}

public function auth(): void {
$handshakeRequest = new SaslHandshakeRequest();
$handshakeRequest->setMechanism($this->getName());
$correlationId = $this->client->send($handshakeRequest);
/** @var SaslHandshakeResponse $handshakeResponse */
$handshakeResponse = $this->client->recv($correlationId);
ErrorCode::check($handshakeResponse->getErrorCode());

$authenticateRequest = new SaslAuthenticateRequest();
$authenticateRequest->setAuthBytes($this->getAuthBytes());
$correlationId = $this->client->send($authenticateRequest);
/** @var SaslAuthenticateResponse $authenticateResponse */
$authenticateResponse = $this->client->recv($correlationId);
ErrorCode::check($authenticateResponse->getErrorCode());
}

/**
* 授权模式.
*/
public function getName(): string
protected function getName(): string
{
return 'PLAIN';
}

/**
* 获得加密串.
*/
public function getAuthBytes(): string
protected function getAuthBytes(): string
{
$config = $this->config->getSasl();
if (empty($config['username']) || empty($config['password'])) {
Expand Down
13 changes: 3 additions & 10 deletions src/Sasl/SaslInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,12 @@

namespace longlang\phpkafka\Sasl;

use longlang\phpkafka\Client\ClientInterface;
use longlang\phpkafka\Config\CommonConfig;

interface SaslInterface
{
public function __construct(CommonConfig $config);
public function __construct(ClientInterface $client, CommonConfig $config);

/**
* 获得授权名称.
*/
public function getName(): string;

/**
* 返回授权信息.
*/
public function getAuthBytes(): string;
public function auth(): void;
}