Added basic authentication

This commit is contained in:
AdamMiltonBarker 2020-09-10 17:50:29 +02:00
parent d69ce96a64
commit 92251b4b14
4 changed files with 75 additions and 41 deletions

View File

@ -34,6 +34,12 @@ Or you can add this line in composer.json
use Web3\Web3; use Web3\Web3;
$web3 = new Web3('http://localhost:8545'); $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 ### Using provider
@ -188,8 +194,8 @@ $functionData = $contract->at($contractAddress)->getData($functionName, $params)
``` ```
# Assign value to outside scope(from callback scope to outside scope) # Assign value to outside scope(from callback scope to outside scope)
Due to callback is not like javascript callback, Due to callback is not like javascript callback,
if we need to assign value to outside scope, if we need to assign value to outside scope,
we need to assign reference to callback. we need to assign reference to callback.
```php ```php
$newAccount = ''; $newAccount = '';
@ -245,7 +251,7 @@ docker-compose exec php ash
``` ```
/** /**
* testHost * testHost
* *
* @var string * @var string
*/ */
protected $testHost = 'http://ganache:8545'; protected $testHost = 'http://ganache:8545';

View File

@ -36,9 +36,9 @@ class HttpRequestManager extends RequestManager implements IRequestManager
* @param int $timeout * @param int $timeout
* @return void * @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; $this->client = new Client;
} }
@ -56,10 +56,20 @@ class HttpRequestManager extends RequestManager implements IRequestManager
} }
try { try {
$res = $this->client->post($this->host, [
'headers' => [ if(!$this->authu):
$headers = [
'content-type' => 'application/json' '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, 'body' => $payload,
'timeout' => $this->timeout, 'timeout' => $this->timeout,
'connect_timeout' => $this->timeout 'connect_timeout' => $this->timeout

View File

@ -2,9 +2,9 @@
/** /**
* This file is part of web3.php package. * This file is part of web3.php package.
* *
* (c) Kuan-Cheng,Lai <alk03073135@gmail.com> * (c) Kuan-Cheng,Lai <alk03073135@gmail.com>
* *
* @author Peter Lai <alk03073135@gmail.com> * @author Peter Lai <alk03073135@gmail.com>
* @license MIT * @license MIT
*/ */
@ -15,34 +15,52 @@ class RequestManager
{ {
/** /**
* host * host
* *
* @var string * @var string
*/ */
protected $host; protected $host;
/** /**
* timeout * timeout
* *
* @var float * @var float
*/ */
protected $timeout; protected $timeout;
/**
* auth username
*
* @var float
*/
protected $authu;
/**
* auth password
*
* @var float
*/
protected $authp;
/** /**
* construct * construct
* *
* @param string $host * @param string $host
* @param float $timeout * @param float $timeout
* @param string $authu
* @param string $authp
* @return void * @return void
*/ */
public function __construct($host, $timeout=1) public function __construct($host, $timeout = 1, $authu = NULL, $authp = NULL)
{ {
$this->host = $host; $this->host = $host;
$this->timeout = (float) $timeout; $this->timeout = (float) $timeout;
$this->authu = $authu;
$this->authp = $authp;
} }
/** /**
* get * get
* *
* @param string $name * @param string $name
* @return mixed * @return mixed
*/ */
@ -58,7 +76,7 @@ class RequestManager
/** /**
* set * set
* *
* @param string $name * @param string $name
* @param mixed $value * @param mixed $value
* @return bool * @return bool
@ -75,7 +93,7 @@ class RequestManager
/** /**
* getHost * getHost
* *
* @return string * @return string
*/ */
public function getHost() public function getHost()
@ -85,7 +103,7 @@ class RequestManager
/** /**
* getTimeout * getTimeout
* *
* @return float * @return float
*/ */
public function getTimeout() public function getTimeout()

View File

@ -2,9 +2,9 @@
/** /**
* This file is part of web3.php package. * This file is part of web3.php package.
* *
* (c) Kuan-Cheng,Lai <alk03073135@gmail.com> * (c) Kuan-Cheng,Lai <alk03073135@gmail.com>
* *
* @author Peter Lai <alk03073135@gmail.com> * @author Peter Lai <alk03073135@gmail.com>
* @license MIT * @license MIT
*/ */
@ -32,49 +32,49 @@ class Web3
/** /**
* eth * eth
* *
* @var \Web3\Eth * @var \Web3\Eth
*/ */
protected $eth; protected $eth;
/** /**
* net * net
* *
* @var \Web3\Net * @var \Web3\Net
*/ */
protected $net; protected $net;
/** /**
* personal * personal
* *
* @var \Web3\Personal * @var \Web3\Personal
*/ */
protected $personal; protected $personal;
/** /**
* shh * shh
* *
* @var \Web3\Shh * @var \Web3\Shh
*/ */
protected $shh; protected $shh;
/** /**
* utils * utils
* *
* @var \Web3\Utils * @var \Web3\Utils
*/ */
protected $utils; protected $utils;
/** /**
* methods * methods
* *
* @var array * @var array
*/ */
private $methods = []; private $methods = [];
/** /**
* allowedMethods * allowedMethods
* *
* @var array * @var array
*/ */
private $allowedMethods = [ private $allowedMethods = [
@ -87,12 +87,12 @@ class Web3
* @param string|\Web3\Providers\Provider $provider * @param string|\Web3\Providers\Provider $provider
* @return void * @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)) { if (is_string($provider) && (filter_var($provider, FILTER_VALIDATE_URL) !== false)) {
// check the uri schema // check the uri schema
if (preg_match('/^https?:\/\//', $provider) === 1) { if (preg_match('/^https?:\/\//', $provider) === 1) {
$requestManager = new HttpRequestManager($provider); $requestManager = new HttpRequestManager($provider, $timeout, $authu, $authp);
$this->provider = new HttpProvider($requestManager); $this->provider = new HttpProvider($requestManager);
} }
@ -103,7 +103,7 @@ class Web3
/** /**
* call * call
* *
* @param string $name * @param string $name
* @param array $arguments * @param array $arguments
* @return void * @return void
@ -149,7 +149,7 @@ class Web3
/** /**
* get * get
* *
* @param string $name * @param string $name
* @return mixed * @return mixed
*/ */
@ -165,7 +165,7 @@ class Web3
/** /**
* set * set
* *
* @param string $name * @param string $name
* @param mixed $value * @param mixed $value
* @return mixed * @return mixed
@ -182,7 +182,7 @@ class Web3
/** /**
* getProvider * getProvider
* *
* @return \Web3\Providers\Provider * @return \Web3\Providers\Provider
*/ */
public function getProvider() public function getProvider()
@ -192,7 +192,7 @@ class Web3
/** /**
* setProvider * setProvider
* *
* @param \Web3\Providers\Provider $provider * @param \Web3\Providers\Provider $provider
* @return bool * @return bool
*/ */
@ -207,7 +207,7 @@ class Web3
/** /**
* getEth * getEth
* *
* @return \Web3\Eth * @return \Web3\Eth
*/ */
public function getEth() public function getEth()
@ -221,7 +221,7 @@ class Web3
/** /**
* getNet * getNet
* *
* @return \Web3\Net * @return \Web3\Net
*/ */
public function getNet() public function getNet()
@ -235,7 +235,7 @@ class Web3
/** /**
* getPersonal * getPersonal
* *
* @return \Web3\Personal * @return \Web3\Personal
*/ */
public function getPersonal() public function getPersonal()
@ -249,7 +249,7 @@ class Web3
/** /**
* getShh * getShh
* *
* @return \Web3\Shh * @return \Web3\Shh
*/ */
public function getShh() public function getShh()
@ -263,7 +263,7 @@ class Web3
/** /**
* getUtils * getUtils
* *
* @return \Web3\Utils * @return \Web3\Utils
*/ */
public function getUtils() public function getUtils()
@ -277,7 +277,7 @@ class Web3
/** /**
* batch * batch
* *
* @param bool $status * @param bool $status
* @return void * @return void
*/ */