Skip to content

Commit

Permalink
Change NodeAuthenticator to RequestInterceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
codesoap committed May 4, 2022
1 parent 298ce14 commit 07bdf3a
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 30 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ the send subcommand sends funds to an address.
ACCOUNT_INDEX is an optional parameter, which must be a number between 0
and 4,294,967,295. It allows you to use multiple accounts derived from
the same seed. By default the account with index 0 is chosen.

Environment:
ATTO_BASIC_AUTH_USERNAME The username for HTTP Basic Authentication.
If set, HTTP Basic Authentication will be
used when making requests to the node.
ATTO_BASIC_AUTH_PASSWORD The password to use for HTTP Basic
Authentication.
```

# Technical details
Expand Down
7 changes: 7 additions & 0 deletions cmd/atto-safesign/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,11 @@ Nano network.
ACCOUNT_INDEX is an optional parameter, which allows you to use
different accounts derived from the given seed. By default the account
with index 0 is chosen.

Environment:
ATTO_BASIC_AUTH_USERNAME The username for HTTP Basic Authentication.
If set, HTTP Basic Authentication will be
used when making requests to the node.
ATTO_BASIC_AUTH_PASSWORD The password to use for HTTP Basic
Authentication.
```
20 changes: 20 additions & 0 deletions cmd/atto-safesign/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"math/big"
"net/http"
"os"

"github.com/codesoap/atto"
Expand Down Expand Up @@ -47,6 +48,13 @@ Nano network.
ACCOUNT_INDEX is an optional parameter, which allows you to use
different accounts derived from the given seed. By default the account
with index 0 is chosen.
Environment:
ATTO_BASIC_AUTH_USERNAME The username for HTTP Basic Authentication.
If set, HTTP Basic Authentication will be
used when making requests to the node.
ATTO_BASIC_AUTH_PASSWORD The password to use for HTTP Basic
Authentication.
`

var accountIndexFlag uint
Expand Down Expand Up @@ -80,6 +88,18 @@ func init() {
flag.Usage()
os.Exit(1)
}
setUpNodeAuthentication()
}

func setUpNodeAuthentication() {
if os.Getenv("ATTO_BASIC_AUTH_USERNAME") != "" {
username := os.Getenv("ATTO_BASIC_AUTH_USERNAME")
password := os.Getenv("ATTO_BASIC_AUTH_PASSWORD")
atto.RequestInterceptor = func(request *http.Request) error {
request.SetBasicAuth(username, password)
return nil
}
}
}

func main() {
Expand Down
9 changes: 6 additions & 3 deletions cmd/atto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"math/big"
"net/http"
"os"

"github.com/codesoap/atto"
Expand Down Expand Up @@ -83,9 +84,11 @@ func init() {

func setUpNodeAuthentication() {
if os.Getenv("ATTO_BASIC_AUTH_USERNAME") != "" {
atto.DefaultNodeAuthenticator = atto.NodeBasicAuth{
Username: os.Getenv("ATTO_BASIC_AUTH_USERNAME"),
Password: os.Getenv("ATTO_BASIC_AUTH_PASSWORD"),
username := os.Getenv("ATTO_BASIC_AUTH_USERNAME")
password := os.Getenv("ATTO_BASIC_AUTH_PASSWORD")
atto.RequestInterceptor = func(request *http.Request) error {
request.SetBasicAuth(username, password)
return nil
}
}
}
Expand Down
25 changes: 0 additions & 25 deletions node_auth.go

This file was deleted.

10 changes: 10 additions & 0 deletions request_interceptor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package atto

import "net/http"

// The RequestInterceptor is a function that is used to modify all HTTP
// requests that are sent to a node. If it is nil, requests are not
// modified.
//
// May be used, for example, to authenticate requests.
var RequestInterceptor func(request *http.Request) error
7 changes: 5 additions & 2 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,11 @@ func doRPC(requestBody, node string) (responseBytes []byte, err error) {
return
}
req.Header.Add("Content-Type", "application/json")
if DefaultNodeAuthenticator != nil {
DefaultNodeAuthenticator.Authenticate(req)
if RequestInterceptor != nil {
if err = RequestInterceptor(req); err != nil {
err = fmt.Errorf("request interceptor failed: %v", err)
return
}
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
Expand Down

0 comments on commit 07bdf3a

Please sign in to comment.