-
Notifications
You must be signed in to change notification settings - Fork 8
/
auth.go
60 lines (50 loc) · 1.22 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
package salt
import (
"context"
"encoding/json"
"fmt"
)
type loginRequest struct {
Username string `json:"username"`
Password string `json:"password"`
EAuth string `json:"eauth"`
}
type loginResponse struct {
Return []struct {
User string `json:"user"`
Token string `json:"token"`
EAuth string `json:"eauth"`
Start float64 `json:"start"`
Expire float64 `json:"expire"`
Perms []string `json:"perms"`
} `json:"return"`
}
// Login 认证
// https://docs.saltstack.com/en/latest/ref/netapi/all/salt.netapi.rest_cherrypy.html#login
func (c *Client) Login(ctx context.Context) error {
postData := loginRequest{
Username: c.username,
Password: c.password,
EAuth: c.eauth,
}
data, err := c.post(ctx, "login", postData)
if err != nil {
return err
}
var loginResp loginResponse
if err := json.Unmarshal(data, &loginResp); err != nil {
return err
}
c.token = loginResp.Return[0].Token
return nil
}
// Logout 登出
// https://docs.saltstack.com/en/latest/ref/netapi/all/salt.netapi.rest_cherrypy.html#logout
func (c *Client) Logout(ctx context.Context) error {
_, err := c.post(ctx, "logout", nil)
if err != nil {
return fmt.Errorf("logout error: %w", err)
}
c.token = ""
return nil
}