-
Notifications
You must be signed in to change notification settings - Fork 0
/
system.go
72 lines (60 loc) · 1.84 KB
/
system.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
package manager
import (
"encoding/base64"
"fmt"
)
type loginResponse struct {
Username string `xml:"username"`
Hostname string `xml:"hostname"`
IsAdmin string `xml:"isAdmin"`
SessionID string `xml:"authSid"`
AuthPassed int `xml:"authPassed"`
}
// Login perform the authentication against the QNAP storage.
// Any existing session will be logged-out, first.
func (s *QnapSession) Login(username, password string) error {
// make sure to close any existing sessions
s.Logout()
// perform login
var result loginResponse
res, err := s.conn.NewRequest(). // see https://download.qnap.com/dev/API_QNAP_QTS_Authentication.pdf
ExpectContentType("application/json").
SetQueryParam("user", username).
SetQueryParam("pwd", encodePassword(password)).
SetQueryParam("remme", "0").
SetResult(&result).
Get("cgi-bin/authLogin.cgi")
if err != nil {
return fmt.Errorf("failed to perform request: %v", err)
}
if res.StatusCode() != 200 {
return fmt.Errorf("failed to perform request: unexpected HTTP status code: %v", res.StatusCode())
}
if result.AuthPassed != 1 {
return fmt.Errorf("failed to perform request: authentication failed: %v", string(res.Body()))
}
s.sessionID = result.SessionID
s.conn.SetQueryParam("sid", s.sessionID)
return nil
}
// Logout invalidates the session.
func (s *QnapSession) Logout() error {
// no logged-in?
if s.sessionID == "" {
return nil
}
res, err := s.conn.NewRequest().
Get("cgi-bin/authLogout.cgi")
if err != nil {
return fmt.Errorf("failed to perform request: %v", err)
}
if res.StatusCode() != 200 {
return fmt.Errorf("failed to perform request: unexpected HTTP status code: %v", res.StatusCode())
}
s.sessionID = ""
s.conn.SetQueryParam("sid", "")
return nil
}
func encodePassword(pwd string) string {
return base64.StdEncoding.EncodeToString([]byte(pwd))
}