-
Notifications
You must be signed in to change notification settings - Fork 1
/
goffive.go
129 lines (116 loc) · 2.48 KB
/
goffive.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package goffive
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"time"
)
type Auth struct {
User string `json:"username"`
Pass string `json:"password"`
LoginProviderName string `json:"loginProviderName"`
}
type AuthResponse struct {
Token struct {
Token string `json:"token"`
Name string `json:"name"`
Timeout int64 `json:"timeout"`
StartTime string `json:"startTime"`
ExpirationMicros uint64 `json:"expirationMicros"`
LastUpdateMicros uint64 `json:"lastUpdateMicros"`
} `json:"token"`
}
type Client struct {
user string
password string
url string
conn *http.Client
token string
ASM *asm
LTM *ltm
}
func authorization(user string, pass string, url string) (string, error) {
client := &http.Client{
Timeout: time.Second * 30,
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
path := fmt.Sprintf("%s/mgmt/shared/authn/login", url)
b := new(bytes.Buffer)
err := json.NewEncoder(b).Encode(&Auth{
User: user,
Pass: pass,
LoginProviderName: "tmos",
})
req, err := http.NewRequest("POST", path, b)
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
var authResp AuthResponse
dec := json.NewDecoder(bytes.NewBuffer(body))
err = dec.Decode(&authResp)
if err != nil {
return "", err
}
return authResp.Token.Token, nil
}
func New(user string, pass string, url string) (*Client, error) {
if user == "" || pass == "" || url == "" {
return nil, errors.New("didn't find credentials")
}
token, err := authorization(user, pass, url)
if err != nil {
return nil, err
}
client := &http.Client{
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
fmt.Println(token)
return &Client{
conn: client,
user: user,
password: pass,
url: url,
token: token,
ASM: &asm{
conn: client,
token: token,
url: url,
},
LTM: <m{
conn: client,
token: token,
url: url,
},
}, nil
}