-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
49 lines (37 loc) · 776 Bytes
/
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
package xiaomirouterapi
import (
"net/http"
"net/url"
)
const (
authURL = "xqsystem/login"
)
type authResponse struct {
Token string `json:"token"`
}
func (api *MiWifiApi) Auth(user string, pass string) error {
apiUrl := api.buildURL(authURL)
nonce := genNonce()
passHash := genPasswordHash(nonce, pass)
req := url.Values{}
req.Add("username", user)
req.Add("logtype", "2")
req.Add("password", passHash)
req.Add("nonce", nonce)
resp := authResponse{}
err := sendPostRequest(apiUrl, req, &resp)
if err != nil {
return err
}
api.token = &resp.Token
return nil
}
func (api *MiWifiApi) Logout() error {
apiURL := api.buildApiURL("logout", "web")
resp, err := http.Get(apiURL)
if err != nil {
return err
}
_ = resp.Body.Close()
return nil
}