-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.go
65 lines (53 loc) · 1.36 KB
/
login.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
package printer
import (
"errors"
"io"
"net/http"
"net/url"
"strings"
)
const urlLogin = "/general/status.html"
var errLoginNoAuth = errors.New("printer: login: no auth cookie received (wrong password?)")
// login performs the login command against the remote printer. it is
// used internally as part of the printer creation process to ensure
// credentials are valid
func (p *printer) login(password string) error {
// login form values
data := url.Values{}
data.Set("B8d9", password)
data.Set("loginurl", urlLogin)
// get url & set path
u, err := url.ParseRequestURI(p.baseUrl)
if err != nil {
return err
}
u.Path = urlLogin
// make and do request
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(data.Encode()))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", p.userAgent)
resp, err := p.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// read and discard entire body (should be empty)
_, _ = io.Copy(io.Discard, resp.Body)
// confirm got cookie
foundAuthCookie := false
for _, c := range resp.Cookies() {
if c.Name == "AuthCookie" {
foundAuthCookie = true
break
}
}
if !foundAuthCookie {
return errLoginNoAuth
}
// set cookies in jar
p.httpClient.Jar.SetCookies(u, resp.Cookies())
return nil
}