Skip to content

Commit f17a241

Browse files
author
Roman Bylbas
committed
Added authorization header to RpcClient
Signed-off-by: Roman Bylbas <romka.bulbas@gmail.com>
1 parent edd46dc commit f17a241

3 files changed

Lines changed: 26 additions & 17 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ extension=secp256k1.so
3131

3232
## Usage
3333
### Creating RpcClient
34-
Create `RpcClient` by passing node url to constructor
34+
Create `RpcClient` by passing node url and authorization token (optional) to constructor
3535
```php
3636
$nodeUrl = 'http://127.0.0.1:7777';
37-
$client = new Casper\Rpc\RpcClient($nodeUrl);
37+
$bearerToken = '6ae6c8b31f09df244019ffef60c274e4'; // Optional
38+
39+
$client = new Casper\Rpc\RpcClient($nodeUrl, $bearerToken);
3840
```
3941

4042
### RPC call examples

docs/API/RpcClientAPI.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ Class for interacting with the network via RPC
44
---
55
## Constructor
66
```php
7-
__constructor(string $nodeUrl)
7+
__constructor(string $nodeUrl, string $bearerToken = null)
88
```
99
### Parameters
10-
| Name | Type | Description | Required |
11-
|---|---|---|---|
12-
| `$nodeUrl` | `string` | Full node url string | Yes |
10+
| Name | Type | Description | Required |
11+
|----------------|---|----------------------------|---|
12+
| `$nodeUrl` | `string` | Full node url string | Yes |
13+
| `$bearerToken` | `string` | Authorization bearer token | Yes |
1314

1415
---
1516
## Put deploy

src/Rpc/RpcClient.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,14 @@ class RpcClient
6767

6868
private string $nodeUrl;
6969

70+
private ?string $bearerToken = null;
71+
7072
private ?string $lastApiVersion = null;
7173

72-
public function __construct(string $nodeUrl)
74+
public function __construct(string $nodeUrl, string $bearerToken = null)
7375
{
7476
$this->nodeUrl = $nodeUrl;
77+
$this->bearerToken = $bearerToken;
7578
}
7679

7780
public function getLastApiVersion(): ?string
@@ -461,20 +464,23 @@ private function rpcCallMethod(string $method, array $params = array()): array
461464
{
462465
$url = $this->nodeUrl . '/rpc';
463466
$curl = curl_init($url);
464-
$data = array(
465-
'id' => self::ID,
466-
'jsonrpc' => self::JSON_RPC,
467-
'method' => $method,
468-
'params' => $params
469-
);
467+
468+
$headers = ['Accept: application/json', 'Content-type: application/json'];
469+
if ($this->bearerToken !== null) {
470+
$headers[] = 'Authorization: Bearer ' . $this->bearerToken;
471+
}
470472

471473
curl_setopt($curl, CURLOPT_URL, $url);
472474
curl_setopt($curl, CURLOPT_POST, true);
473475
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
474-
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
475-
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
476-
'Accept: application/json',
477-
'Content-type: application/json',
476+
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
477+
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(
478+
array(
479+
'id' => self::ID,
480+
'jsonrpc' => self::JSON_RPC,
481+
'method' => $method,
482+
'params' => $params
483+
)
478484
));
479485

480486
$response = curl_exec($curl);

0 commit comments

Comments
 (0)