-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauthentication.go
41 lines (33 loc) · 916 Bytes
/
authentication.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
package zoneminder
import (
"fmt"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
)
const loginURL = "/index.php"
func login(host, username, password string) (*cookiejar.Jar, error) {
cookies, err := cookiejar.New(nil)
if err != nil {
log.Printf("Error creating cookie jar for zoneminder. %s", err)
}
requestURL := fmt.Sprintf("%s%s", host, loginURL)
postData := url.Values{
"username": {username},
"password": {password},
"action": {"login"},
"view": {"console"},
}
resp, requestError := http.PostForm(requestURL, postData)
if requestError != nil {
return cookies, fmt.Errorf("Error logging into zineminder. %s", requestError)
}
defer resp.Body.Close()
u, urlParseErr := url.Parse(requestURL)
if urlParseErr != nil {
return cookies, fmt.Errorf("Error parsing login info from zoneminder. %s", urlParseErr)
}
cookies.SetCookies(u, resp.Cookies())
return cookies, nil
}