-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
72 lines (61 loc) · 1.63 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
package mobiapi
import (
"errors"
"net/http"
"net/url"
"strconv"
"github.com/PuerkitoBio/goquery"
)
func (api *MobiAPI) postlogin(doc *goquery.Document) {
if val, exists := doc.Find("body").Attr("uid"); exists {
api.uid, _ = strconv.Atoi(val)
api.name = doc.Find("#botton div strong").Text()
}
}
// Authenticate with password.
func (api *MobiAPI) PasswordAuth(login, password string) (bool, error) {
if login == "" || password == "" {
return false, errors.New("EmptyCred")
}
purl, err := url.Parse("https://" + api.domain)
if err != nil {
return false, err
}
api.client.Jar.SetCookies(purl, []*http.Cookie{})
resp, doc, err := api.request("POST", "logowanie", "login="+login+"&haslo="+password)
if err != nil {
return false, err
}
if resp.Request.Response.StatusCode == 302 {
api.signedin = true
api.postlogin(doc)
return true, nil
}
return false, nil
}
// Authenticate with provided token from cookie
func (api *MobiAPI) TokenAuth(token string) (bool, error) {
purl, err := url.Parse("https://" + api.domain)
if err != nil {
return false, err
}
api.client.Jar.SetCookies(purl, []*http.Cookie{})
for _, cookie := range api.client.Jar.Cookies(purl) {
if cookie.Name != "SERVERID" {
cookie.Value = token
api.client.Jar.SetCookies(purl, []*http.Cookie{cookie})
break
}
}
resp, doc, err := api.request("GET", "logowanie", "")
if err != nil {
return false, err
}
if resp.Request.URL.Path == "/dziennik/historia" {
api.signedin = true
api.postlogin(doc)
return true, nil
} else {
return false, errors.New("AuthUnable")
}
}