0

I try to setup HTTP authentication for my private geth node on my server using nginx as reverse proxy. I followed this instruction: https://ethereum.stackexchange.com/questions/30357/restricted-access-authentication-for-a-remote-geth-node

I tested my setup with curl and it is working fine so far:

curl
     -X POST
     --header "Content-Type: application/json"
     --data '{"jsonrpc":"2.0","method":"eth_accounts","params":[],"id":1}'
     https://<user>:<pw>@<domain>

Result: {"jsonrpc":"2.0","id":1,"result":[<addresses>]}

Opening the domain in my browser works fine as well, I see the authentication prompt and after entering the credentials there is no error.

My node looks like this:

docker run -d ethereum/client-go:stable
    --datadir "/root"                  
    --port 30001                       
    --nat "any"                        
    --nodiscover                       
    --rpc                              
    --rpcaddr "0.0.0.0"                
    --rpcport 8545                     
    --rpcapi "eth,net,web3,rpc"        
    --rpccorsdomain "*"                
    --rpcvhosts "*"

Problem

Unfortunately I can't connect to my node with web3:

web3 = new Web3(new Web3.providers.HttpProvider("https://<user>:<pw>@<domain>"));
console.log(web3.eth.coinbase);

Result in browser:

Error: CONNECTION ERROR: Couldn't connect to node https://<user>:<pw>@<domain>.

// this error I just get in Chrome, not in Firefox. But if I ask with `curl -I` I can see `Access-Control-Allow-Origin` is set.
web3.min.js:1 Access to XMLHttpRequest at 'https://<user>:<pw>@<domain>' from origin 'https://localhost' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

1 Answers1

0

To start, you would want to make your code request look like your curl request.

You are not specifying the type of request and you are missing your headers that you use with your curl request. By default most http libraries will use a GET request if it is not specified (I have not used web3), which is what you are doing with web3.

I would start by adding the request and the header to your web3 request. Here is an example of that using httpheaderprovider (source: https://github.com/EthereumEx/httpheaderprovider)

var Web3 = require('web3');
var web3 = new Web3();

var HttpHeaderProvider = require('httpheaderprovider');

var headers = {
  "Ocp-Apim-Subscription-Key": "mykeyfromtheapiportal",
  "header2": "foobar"
}
var provider = new HttpHeaderProvider('https://scicoria.azure-api.net', headers);

web3.setProvider(provider);

var coinbase = web3.eth.coinbase;
console.log(coinbase);

var balance = web3.eth.getBalance(coinbase);
console.log(balance.toString(10));

also see this

user3738936
  • 936
  • 8
  • 22
  • Hey, thanks for your answer. Your code is for nodejs. I tried it with nodejs and it is working without any problems even if I just use `web3` without `httpheaderprovider `. But my code is client side JS. –  Jan 13 '19 at 13:10