-
Notifications
You must be signed in to change notification settings - Fork 12
/
auth.go
59 lines (54 loc) · 892 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
50
51
52
53
54
55
56
57
58
59
package kasa
import (
uuid "github.com/gofrs/uuid"
)
type auth struct {
Username string
Password string
URL string
Token string
UUID string
}
func (a *auth) generateToken() error {
req, err := a.getRequest()
if err != nil {
return err
}
res, err := req.execute()
if err != nil {
return err
}
a.Token = res.Token
return err
}
func (a *auth) generateUUID() error {
if a.UUID != "" {
return nil
}
uuid, err := uuid.NewV4()
if err != nil {
return err
}
a.UUID = uuid.String()
return nil
}
func (a *auth) getRequest() (request, error) {
var r request
var err error
err = a.generateUUID()
if err != nil {
return r, err
}
return request{
URL: a.URL,
RequestBody: requestBody{
methodLogin,
params{
AppType: appType,
CloudUserName: a.Username,
CloudPassword: a.Password,
TerminalUUID: a.UUID,
},
},
}, nil
}