-
Notifications
You must be signed in to change notification settings - Fork 9
/
auth.go
77 lines (64 loc) · 1.61 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package lotus
import (
"encoding/json"
"fmt"
"github.com/filswan/go-swan-lib/client/web"
"github.com/filswan/go-swan-lib/logs"
)
const (
FILECOIN_AUTH_VERIFY = "Filecoin.AuthVerify"
)
type AuthVerify struct {
LotusJsonRpcResult
Result []string `json:"result"`
}
func LotusCheckAuth(apiUrl, token, expectedAuth string) (bool, error) {
auths, err := LotusAuthVerify(apiUrl, token)
if err != nil {
logs.GetLogger().Error(err)
return false, err
}
for _, auth := range auths {
if auth == expectedAuth {
return true, nil
}
}
return false, nil
}
func LotusAuthVerify(apiUrl, token string) ([]string, error) {
if len(apiUrl) == 0 {
err := fmt.Errorf("api url is required")
return nil, err
}
if len(token) == 0 {
err := fmt.Errorf("token is required")
return nil, err
}
var params []interface{}
params = append(params, token)
jsonRpcParams := LotusJsonRpcParams{
JsonRpc: LOTUS_JSON_RPC_VERSION,
Method: FILECOIN_AUTH_VERIFY,
Params: params,
Id: LOTUS_JSON_RPC_ID,
}
//here the api url should be miner's api url, need to change later on
response := web.HttpGetNoToken(apiUrl, jsonRpcParams)
if response == "" {
err := fmt.Errorf("no response from:%s", apiUrl)
//logs.GetLogger().Error(err)
return nil, err
}
authVerify := &AuthVerify{}
err := json.Unmarshal([]byte(response), authVerify)
if err != nil {
//logs.GetLogger().Error(err)
return nil, err
}
if authVerify.Error != nil {
err := fmt.Errorf("error, code:%d, message:%s", authVerify.Error.Code, authVerify.Error.Message)
//logs.GetLogger().Error(err)
return nil, err
}
return authVerify.Result, nil
}