-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
161 lines (132 loc) · 3.94 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package tendone
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
)
type loginRequestWrap struct {
SysLogin loginRequest `json:"sysLogin"`
}
type loginResponseWrap struct {
SysLogin loginResponse `json:"sysLogin"`
}
// loginRequest is the struct used to login to the AP. It's also used to logout
type loginRequest struct {
Username string `json:"username"`
Password string `json:"password"`
// Time is formatted as "2006;1;2;15;4;5" and is used to be printed in the logs
Time string `json:"time"`
TimeZone int `json:"timeZone"`
Logoff bool `json:"logoff"`
}
// loginResponse is the struct that is returned when logging in. It's also used
// to logout
type loginResponse struct {
UserType string `json:"userType"`
Login bool `json:"Login"`
// When loggin in Logoff is boolean, when logging out Logoff is a string :(
Logoff interface{} `json:"logoff"`
}
// IsAutheticated checks if the session is authenticated. This is done by checking
// if the main index.html page redirects to the login page.
//
// TODO: Check if this is the only way or I could use the API used for all the
// library
func (s *Session) IsAutheticated() (bool, error) {
req, err := http.NewRequest("GET", s.uri, nil)
if err != nil {
return false, err
}
for _, c := range s.cookies {
req.AddCookie(c)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return false, err
}
if resp.StatusCode != 200 {
return false, nil
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return false, err
}
// To check if we are authenticated we simply check if the body
// contains the strings from the login page.
sbody := string(body)
if strings.Contains(sbody, "login-body") && strings.Contains(sbody, "login-title") {
return false, nil
}
return true, nil
}
// Login is used to login a session with the AP. This should be the first function
// to be called after creating a new session.
//
// You can't have multiple sessions for the same AP. You should always call
// logout after you are done with the session.
// For more information, please refer to the known issues in the docs about session management.
func (s *Session) Login(user, passwd string) (bool, error) {
bpasswd := base64.StdEncoding.EncodeToString([]byte(passwd))
sysLogin := loginRequestWrap{
SysLogin: loginRequest{
Logoff: false,
Password: bpasswd,
Time: time.Now().Format("2006;1;2;15;4;5"),
TimeZone: 12,
Username: user,
},
}
rbody, err := json.Marshal(sysLogin)
resp, err := http.Post(s.uri+MODULES_PATH, "application/json", bytes.NewReader(rbody))
if err != nil {
return false, err
}
var LoginResponse loginResponseWrap
if err := json.NewDecoder(resp.Body).Decode(&LoginResponse); err != nil {
return false, err
}
s.cookies = resp.Cookies()
fmt.Println(LoginResponse)
if LoginResponse.SysLogin.Login && LoginResponse.SysLogin.UserType == user {
return true, nil
}
return false, nil
}
// Logout is used to logout a session with the AP. This should be the last function
// to be called before the session is destroyed. You should call this function
// even though the session is destroyed because the AP has NOT a strong security
// and session management, so if you leave the session open there is a possiblity
// that someone could use it to access the access point.
func (s *Session) Logout() (bool, error) {
sysLogin := loginRequestWrap{
SysLogin: loginRequest{
Logoff: true,
},
}
rbody, err := json.Marshal(sysLogin)
if err != nil {
return false, err
}
resp, err := fetch(s, rbody)
if err != nil {
return false, err
}
var SysLoginResponse loginResponseWrap
if err := json.Unmarshal(resp, &SysLoginResponse); err != nil {
return false, errors.Join(err, errors.New("Body: "+string(resp)))
}
logoff, ok := SysLoginResponse.SysLogin.Logoff.(string)
if !ok {
return false, fmt.Errorf("Logoff is not a string")
}
if logoff == "ok" {
return true, nil
}
return false, nil
}