From 92251b4b1432aae2d7621f276e080edf7128698e Mon Sep 17 00:00:00 2001 From: AdamMiltonBarker Date: Thu, 10 Sep 2020 17:50:29 +0200 Subject: [PATCH] Added basic authentication --- README.md | 12 ++++-- src/RequestManagers/HttpRequestManager.php | 20 +++++++--- src/RequestManagers/RequestManager.php | 40 ++++++++++++++------ src/Web3.php | 44 +++++++++++----------- 4 files changed, 75 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index aa1d5f9..e771f8a 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,12 @@ Or you can add this line in composer.json use Web3\Web3; $web3 = new Web3('http://localhost:8545'); + +// timeout +$web3 = new Web3('http://localhost:8545', 0.1); + +// basic auth +$web3 = new Web3('http://localhost:8545', 0.1, "AuthUser", "AuthPassword"); ``` ### Using provider @@ -188,8 +194,8 @@ $functionData = $contract->at($contractAddress)->getData($functionName, $params) ``` # Assign value to outside scope(from callback scope to outside scope) -Due to callback is not like javascript callback, -if we need to assign value to outside scope, +Due to callback is not like javascript callback, +if we need to assign value to outside scope, we need to assign reference to callback. ```php $newAccount = ''; @@ -245,7 +251,7 @@ docker-compose exec php ash ``` /** * testHost - * + * * @var string */ protected $testHost = 'http://ganache:8545'; diff --git a/src/RequestManagers/HttpRequestManager.php b/src/RequestManagers/HttpRequestManager.php index b7e4e75..23b4f7e 100644 --- a/src/RequestManagers/HttpRequestManager.php +++ b/src/RequestManagers/HttpRequestManager.php @@ -36,9 +36,9 @@ class HttpRequestManager extends RequestManager implements IRequestManager * @param int $timeout * @return void */ - public function __construct($host, $timeout = 1) + public function __construct($host, $timeout = 1, $authu = NULL, $authp = NULL) { - parent::__construct($host, $timeout); + parent::__construct($host, $timeout, $authu, $authp); $this->client = new Client; } @@ -56,10 +56,20 @@ class HttpRequestManager extends RequestManager implements IRequestManager } try { - $res = $this->client->post($this->host, [ - 'headers' => [ + + if(!$this->authu): + $headers = [ 'content-type' => 'application/json' - ], + ]; + else: + $headers = [ + 'content-type' => 'application/json', + 'Authorization' => 'Basic ' . base64_encode($this->authu . ":" . $this->authp) + ]; + endif; + + $res = $this->client->post($this->host, [ + 'headers' => $headers, 'body' => $payload, 'timeout' => $this->timeout, 'connect_timeout' => $this->timeout diff --git a/src/RequestManagers/RequestManager.php b/src/RequestManagers/RequestManager.php index 585dae0..5966c35 100644 --- a/src/RequestManagers/RequestManager.php +++ b/src/RequestManagers/RequestManager.php @@ -2,9 +2,9 @@ /** * This file is part of web3.php package. - * + * * (c) Kuan-Cheng,Lai - * + * * @author Peter Lai * @license MIT */ @@ -15,34 +15,52 @@ class RequestManager { /** * host - * + * * @var string */ protected $host; /** * timeout - * + * * @var float */ protected $timeout; - + + /** + * auth username + * + * @var float + */ + protected $authu; + + /** + * auth password + * + * @var float + */ + protected $authp; + /** * construct - * + * * @param string $host * @param float $timeout + * @param string $authu + * @param string $authp * @return void */ - public function __construct($host, $timeout=1) + public function __construct($host, $timeout = 1, $authu = NULL, $authp = NULL) { $this->host = $host; $this->timeout = (float) $timeout; + $this->authu = $authu; + $this->authp = $authp; } /** * get - * + * * @param string $name * @return mixed */ @@ -58,7 +76,7 @@ class RequestManager /** * set - * + * * @param string $name * @param mixed $value * @return bool @@ -75,7 +93,7 @@ class RequestManager /** * getHost - * + * * @return string */ public function getHost() @@ -85,7 +103,7 @@ class RequestManager /** * getTimeout - * + * * @return float */ public function getTimeout() diff --git a/src/Web3.php b/src/Web3.php index 35a2cde..2ad968b 100644 --- a/src/Web3.php +++ b/src/Web3.php @@ -2,9 +2,9 @@ /** * This file is part of web3.php package. - * + * * (c) Kuan-Cheng,Lai - * + * * @author Peter Lai * @license MIT */ @@ -32,49 +32,49 @@ class Web3 /** * eth - * + * * @var \Web3\Eth */ protected $eth; /** * net - * + * * @var \Web3\Net */ protected $net; /** * personal - * + * * @var \Web3\Personal */ protected $personal; /** * shh - * + * * @var \Web3\Shh */ protected $shh; /** * utils - * + * * @var \Web3\Utils */ protected $utils; /** * methods - * + * * @var array */ private $methods = []; /** * allowedMethods - * + * * @var array */ private $allowedMethods = [ @@ -87,12 +87,12 @@ class Web3 * @param string|\Web3\Providers\Provider $provider * @return void */ - public function __construct($provider) + public function __construct($provider, $timeout = 1, $authu = NULL, $authp = NULL) { if (is_string($provider) && (filter_var($provider, FILTER_VALIDATE_URL) !== false)) { // check the uri schema if (preg_match('/^https?:\/\//', $provider) === 1) { - $requestManager = new HttpRequestManager($provider); + $requestManager = new HttpRequestManager($provider, $timeout, $authu, $authp); $this->provider = new HttpProvider($requestManager); } @@ -103,7 +103,7 @@ class Web3 /** * call - * + * * @param string $name * @param array $arguments * @return void @@ -149,7 +149,7 @@ class Web3 /** * get - * + * * @param string $name * @return mixed */ @@ -165,7 +165,7 @@ class Web3 /** * set - * + * * @param string $name * @param mixed $value * @return mixed @@ -182,7 +182,7 @@ class Web3 /** * getProvider - * + * * @return \Web3\Providers\Provider */ public function getProvider() @@ -192,7 +192,7 @@ class Web3 /** * setProvider - * + * * @param \Web3\Providers\Provider $provider * @return bool */ @@ -207,7 +207,7 @@ class Web3 /** * getEth - * + * * @return \Web3\Eth */ public function getEth() @@ -221,7 +221,7 @@ class Web3 /** * getNet - * + * * @return \Web3\Net */ public function getNet() @@ -235,7 +235,7 @@ class Web3 /** * getPersonal - * + * * @return \Web3\Personal */ public function getPersonal() @@ -249,7 +249,7 @@ class Web3 /** * getShh - * + * * @return \Web3\Shh */ public function getShh() @@ -263,7 +263,7 @@ class Web3 /** * getUtils - * + * * @return \Web3\Utils */ public function getUtils() @@ -277,7 +277,7 @@ class Web3 /** * batch - * + * * @param bool $status * @return void */