-
Notifications
You must be signed in to change notification settings - Fork 0
/
authenticate.go
73 lines (58 loc) · 1.57 KB
/
authenticate.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
package main
import (
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"github.com/PuerkitoBio/goquery"
)
var jar, _ = cookiejar.New(nil)
func login(username string, password string) {
log.Println("trying to log in")
err, loginPage := fetch(LOGIN_ADDR)
if wouldPanic(err) {
return
}
defer loginPage.Body.Close()
doc, err := goquery.NewDocumentFromReader(loginPage.Body)
if wouldPanic(err) {
return
}
// check if already logged in
logoffButton := doc.Find("[href='http://logoff.now']")
if logoffButton.Length() > 0 {
log.Println("already logged in")
return
}
// get token to log in with
token, exists := doc.Find("input[name=ta_id]").Attr("value")
if !exists {
log.Println("no token found")
return
}
log.Println("token:", token)
cookies := loginPage.Cookies()
parsedUrl, _ := url.Parse(LOGIN_ADDR)
jar.SetCookies(parsedUrl, cookies)
postData := url.Values{}
postData.Set("ta_id", token)
postData.Set("uid", username)
postData.Set("pwd", password)
postData.Set("voucher_logon_btn", "++Login++")
encodedData := strings.NewReader(postData.Encode())
client := &http.Client{Jar: jar}
loginResponse, loginErr := client.Post(LOGIN_ADDR, "application/x-www-form-urlencoded", encodedData)
if wouldPanic(loginErr) {
return
}
loginResponseParsed, loginParseError := goquery.NewDocumentFromReader(loginResponse.Body)
if wouldPanic(loginParseError) {
return
}
errorElement := loginResponseParsed.Find(".message-wrapper.error")
if errorElement.Length() > 0 {
log.Println("login error", errorElement.Text())
}
log.Println("logged in")
}