-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.go
188 lines (158 loc) · 3.57 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"syscall"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh/terminal"
)
type AuthData struct {
Creds *Creds
Cookies BasicCookies
}
type BasicCookie struct {
Name string
Value string
}
type BasicCookies []*BasicCookie
type Creds struct {
Email string
Password string
}
func (creds *Creds) Auth() (uploads *http.Response, err error) {
if creds == nil || (creds.Email == "" && creds.Password == "") {
return nil, errors.New("no credentials")
}
postdata := url.Values{
"then": {"uploads"},
"email": {creds.Email},
"password": {creds.Password},
}
resp, err := client.PostForm("https://overcast.fm/login", postdata)
if err != nil {
return
}
defer func() {
if err != nil {
resp.Body.Close()
}
}()
if resp.StatusCode != 200 {
err = errors.Errorf("unexpected HTTP response on login: %d", resp.StatusCode)
return
}
if strings.HasSuffix(resp.Request.URL.Path, "login") {
err = errors.New("failed to login: wrong password")
return
}
if !strings.HasSuffix(resp.Request.URL.Path, "uploads") {
err = errors.Errorf("failed to login: request in unknown place %s", resp.Request.URL.String())
return
}
return resp, nil
}
func (bc BasicCookies) Auth() (uploads *http.Response, err error) {
if len(bc) == 0 {
return nil, errors.New("no cookies found")
}
cookies := make([]*http.Cookie, len(bc))
for i, cookie := range bc {
cookies[i] = &http.Cookie{
Name: cookie.Name,
Value: cookie.Value,
}
}
client.Jar.SetCookies(overcastURL, cookies)
uploads, err = client.Get("https://overcast.fm/uploads")
if err != nil {
err = errors.WithMessage(err, "failed to load uploads page")
return
}
defer func() {
if err != nil {
uploads.Body.Close()
uploads = nil
}
}()
if !strings.HasSuffix(uploads.Request.URL.Path, "uploads") {
err = errors.New("cookies have expired")
return
}
return
}
var staleCookiesErr = errors.New("Cookies are stale, but password had worked")
func (ad *AuthData) Auth() (uploads *http.Response, err error) {
defer func() {
// on successful authorization
if err == nil || err == staleCookiesErr {
ad.saveCookies()
}
}()
if ad == nil {
return nil, errors.New("no auth data found")
}
if len(ad.Cookies) != 0 {
uploads, err = ad.Cookies.Auth()
if err == nil {
return
}
}
if ad.Creds != nil {
uploads, err = ad.Creds.Auth()
if err == nil {
if len(ad.Cookies) != 0 {
err = staleCookiesErr
}
return
}
}
return
}
func (ad *AuthData) saveCookies() {
cookies := client.Jar.Cookies(overcastURL)
ad.Cookies = make([]*BasicCookie, len(cookies))
for i, cookie := range cookies {
ad.Cookies[i] = &BasicCookie{
Name: cookie.Name,
Value: cookie.Value,
}
}
}
func (ad *AuthData) GetCookies() []*http.Cookie {
res := make([]*http.Cookie, len(ad.Cookies))
for i, cookie := range ad.Cookies {
res[i] = &http.Cookie{
Name: cookie.Name,
Value: cookie.Value,
}
}
return res
}
func ParseAuthData(data []byte) (res *AuthData, err error) {
res = &AuthData{}
err = json.Unmarshal(data, &res)
return
}
func inputCreds() (creds *Creds) {
creds = &Creds{}
var err error
creds.Email, err = Input("Email: ")
if err != nil {
fmt.Printf("[WARN] Failed to read email: %s\n", err)
return nil
}
fmt.Print("Password: ")
var bytePassword []byte
bytePassword, err = terminal.ReadPassword(int(syscall.Stdin))
fmt.Print("\n")
if err != nil {
fmt.Printf("[WARN] Failed to read password: %s\n", err)
return nil
} else {
creds.Password = strings.TrimSpace(string(bytePassword))
}
return
}