This repository has been archived by the owner on May 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
115 lines (95 loc) · 2.72 KB
/
session.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package goil
import (
"bytes"
"fmt"
"net/http"
"net/http/cookiejar"
"net/url"
)
type Session struct {
Client *http.Client
}
// Endpoint used by Login
const loginEndpoint string = BaseURLString + "signin/"
// Login creates a session given a username/password combo and a *http.Client
// Warning: It is your responsibility to add a timeout
func Login(username string, password string, client *http.Client) (*Session, error) {
// Init
sess := &Session{}
// Prepare login form
loginForm := url.Values{}
loginForm.Add("password", password)
loginForm.Add("username", username)
// Make cookiejar
cookieJar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}
client.Jar = cookieJar
// Prepare request
req, err := http.NewRequest("POST", loginEndpoint, bytes.NewBufferString(loginForm.Encode()))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
// Execute request
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
sess.Client = client
return sess, nil
}
// Endpoint used by Logout
const logoutEndpoint string = BaseURLString + "logout/"
// Logout does what its name says
func (s *Session) Logout() error {
resp, err := s.Client.Get(logoutEndpoint)
if err != nil {
return err
}
resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("Logout failed: Status code returned was %d instead of %d", resp.StatusCode, 200)
}
return nil
}
// CreateSessionByCookie creates a Session with the given http.Cookie and http.Client
func CreateSessionByCookie(cookie *http.Cookie, client *http.Client) *Session {
// Create a slice around it
cookies := make([]*http.Cookie, 1)
cookies[0] = cookie
// Make cookiejar
cookieJar, _ := cookiejar.New(nil)
client.Jar = cookieJar
// Add cookie to the cookie jar
cookieJar.SetCookies(BaseURL, cookies)
// Create a session
sess := &Session{client}
return sess
}
// CreateSessionByCookieValue creates a Session with the given cookie value and http.Client
func CreateSessionByCookieValue(cookieValue string, client *http.Client) *Session {
// Create a cookie
cookie := &http.Cookie{
Name: "login",
Value: cookieValue,
}
// Use CreateSessionByCookie & return its output
return CreateSessionByCookie(cookie, client)
}
// Retrieve the login cookie from a session
func (s *Session) Cookie() (*http.Cookie, error) {
if s == nil {
return nil, fmt.Errorf("Can't retrieve a cookie from a non-existent session")
}
// Iterate through the cookies in order to find the right one
for _, k := range s.Client.Jar.Cookies(BaseURL) {
if k.Name == "login" {
return k, nil
}
}
// If no logincookie was found, return an error
return nil, fmt.Errorf("No valid login cookie found :(")
}