Skip to content

Commit

Permalink
Merge pull request #16 from TJUBlockchainLab/add-basic-auth
Browse files Browse the repository at this point in the history
Add basic auth
  • Loading branch information
jason-aelf authored Aug 31, 2021
2 parents 8f0cc6d + 3e3420d commit 5e6233d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
2 changes: 2 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type AElfClient struct {
Host string
Version string
PrivateKey string
UserName string
Password string
}

//const const.
Expand Down
9 changes: 7 additions & 2 deletions client/network.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"encoding/base64"
"encoding/json"
"errors"

Expand All @@ -23,8 +24,10 @@ func (a *AElfClient) GetNetworkInfo() (*dto.NetworkInfo, error) {
//RemovePeer Attempt to remove a node from the connected network nodes by given the ipAddress.
func (a *AElfClient) RemovePeer(ipAddress string) (bool, error) {
url := a.Host + REMOVEPEER
combine := a.UserName + ":" + a.Password
combineToBase64 := "Basic " + base64.StdEncoding.EncodeToString([]byte(combine))
params := map[string]interface{}{"address": ipAddress}
peerBytes, err := util.GetRequest("DELETE", url, a.Version, params)
peerBytes, err := util.GetRequestWithAuth("DELETE", url, a.Version, params, combineToBase64)
if err != nil {
return false, errors.New("Remove Peer error:" + err.Error())
}
Expand All @@ -36,8 +39,10 @@ func (a *AElfClient) RemovePeer(ipAddress string) (bool, error) {
//AddPeer Attempt to add a node to the connected network nodes.Input parameter contains the ipAddress of the node.
func (a *AElfClient) AddPeer(ipAddress string) (bool, error) {
url := a.Host + ADDPEER
combine := a.UserName + ":" + a.Password
combineToBase64 := "Basic " + base64.StdEncoding.EncodeToString([]byte(combine))
params := map[string]interface{}{"Address": ipAddress}
peerBytes, err := util.PostRequest(url, a.Version, params)
peerBytes, err := util.PostRequestWithAuth(url, a.Version, params, combineToBase64)
if err != nil {
return false, errors.New("Add Peer error:" + err.Error())
}
Expand Down
71 changes: 71 additions & 0 deletions utils/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,43 @@ import (
"time"
)


//GetRequest GetRequest with Authorization.
func GetRequestWithAuth(method, url, version string, params map[string]interface{}, basicAuth string) ([]byte, error) {
var apiURL string
if params == nil {
apiURL = url
} else {
strParams := Map2UrlParams(params)
apiURL = url + "?" + strParams
}
request, err := http.NewRequest(method, apiURL, nil)
if err != nil {
return nil, err
}
if version != "" {
request.Header.Set("Accept", "application/json;v="+version)
} else {
request.Header.Set("Accept", "application/json")
}
if basicAuth != "" {
request.Header.Set("Authorization", basicAuth)
}
fmt.Printf("%v", request.Header)
client := http.Client{Timeout: 5 * time.Second}
resp, err := client.Do(request)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return data, nil
}


//GetRequest GetRequest.
func GetRequest(method, url, version string, params map[string]interface{}) ([]byte, error) {
var apiURL string
Expand Down Expand Up @@ -73,6 +110,40 @@ func PostRequest(url, version string, params map[string]interface{}) ([]byte, er
return data, nil
}

//PostRequest Post Request with Authorization.
func PostRequestWithAuth(url, version string, params map[string]interface{}, basicAuth string) ([]byte, error) {
jsonParams := ""
if params != nil {
bytesData, _ := json.Marshal(params)
jsonParams = string(bytesData)
}

request, err := http.NewRequest("POST", url, strings.NewReader(jsonParams))
if err != nil {
return nil, err
}
if version != "" {
request.Header.Set("Content-Type", "application/json;v="+version)
} else {
request.Header.Set("Content-Type", "application/json")
}
if basicAuth != "" {
request.Header.Set("Authorization", basicAuth)
}
fmt.Printf("%v", request.Header)
client := &http.Client{Timeout: time.Second * 5}
resp, err := client.Do(request)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return data, nil
}

//Map2UrlParams Map 2Url Params.
func Map2UrlParams(params map[string]interface{}) string {
var strParams string
Expand Down

0 comments on commit 5e6233d

Please sign in to comment.