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;
$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';

View File

@ -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

View File

@ -2,9 +2,9 @@
/**
* This file is part of web3.php package.
*
*
* (c) Kuan-Cheng,Lai <alk03073135@gmail.com>
*
*
* @author Peter Lai <alk03073135@gmail.com>
* @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()

View File

@ -2,9 +2,9 @@
/**
* This file is part of web3.php package.
*
*
* (c) Kuan-Cheng,Lai <alk03073135@gmail.com>
*
*
* @author Peter Lai <alk03073135@gmail.com>
* @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
*/