Merge lp:~wallyworld/goetveld/auth-cookie-fix into lp:goetveld

Proposed by Ian Booth
Status: Merged
Merged at revision: 42
Proposed branch: lp:~wallyworld/goetveld/auth-cookie-fix
Merge into: lp:goetveld
Diff against target: 285 lines (+55/-32)
9 files modified
auth.go (+34/-9)
bazaar.go (+2/-3)
doc.go (+0/-1)
form.go (+4/-4)
html/parse.go (+1/-1)
html/parse_test.go (+1/-1)
html/token.go (+1/-1)
log.go (+7/-7)
rietveld.go (+5/-5)
To merge this branch: bzr merge lp:~wallyworld/goetveld/auth-cookie-fix
Reviewer Review Type Date Requested Status
John A Meinel (community) Approve
Dimiter Naydenov (community) Approve
Review via email: mp+147585@code.launchpad.net

Description of the change

Fix auth cookie handling

I came across this issue when my cached credentials expired and I couldn't login again to propose a code review.

It turns out that the authentication with my username/password was succeeding, and the server returned the correct
cookie information, but the cookies were being ignored by the client and not saved to the .goetveld_codereview.appspot.com
file. Playing with curl and reviewing the Go http client source showed that unless a cookie jar was set up when the client
is created, any cookies generated by the server are ignored. And the relevant tests failed also it turns out.

I've made the necessary changes and everything now works. Perhaps this library was developed using an earlier version of Go
where the API was different? Do we need to test if this change works with eariler Go versions? I'm using 1.0.3.

My changes only affected the auth.go file, but when I ran go fmt over the code base it made small changes to a bunch of other files, and these are included also.

To post a comment you must log in.
Revision history for this message
Ian Booth (wallyworld) wrote :

Please take a look.

Revision history for this message
Dimiter Naydenov (dimitern) wrote :

Looks good. I think we should land this.

review: Approve
Revision history for this message
John A Meinel (jameinel) wrote :

It would probably be easier to review if you posted the 'go fmt' fix before the cookie change, but I can confirm that with go-1.0.3 I'm able to use lbox now, so LGTM.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'auth.go'
2--- auth.go 2012-03-12 20:12:51 +0000
3+++ auth.go 2013-02-11 01:15:27 +0000
4@@ -81,18 +81,37 @@
5 }
6
7 type standardAuth struct {
8- m sync.RWMutex
9- lastLogin time.Time
10- user string
11- ui AuthUI
12- loginURL string
13- cookies []*http.Cookie
14+ m sync.RWMutex
15+ lastLogin time.Time
16+ user string
17+ ui AuthUI
18+ loginURL string
19+ cookies []*http.Cookie
20 }
21
22 var redirectBlocked = errors.New("redirect blocked")
23
24+// A simple cookie jar with just enough functionality for what we need here.
25+type SimpleCookieJar struct {
26+ m sync.Mutex
27+ cookies map[string][]*http.Cookie
28+}
29+
30+func (j *SimpleCookieJar) SetCookies(u *url.URL, cookies []*http.Cookie) {
31+ j.m.Lock()
32+ defer j.m.Unlock()
33+ j.cookies[u.Host] = cookies
34+}
35+
36+func (j *SimpleCookieJar) Cookies(u *url.URL) []*http.Cookie {
37+ j.m.Lock()
38+ defer j.m.Unlock()
39+ return j.cookies[u.Host]
40+}
41+
42 var client = http.Client{
43 CheckRedirect: func(r *http.Request, v []*http.Request) error { return redirectBlocked },
44+ Jar: &SimpleCookieJar{cookies: make(map[string][]*http.Cookie)},
45 }
46
47 func (auth *standardAuth) Login(rietveldURL string, after time.Time) (err error) {
48@@ -170,7 +189,11 @@
49 "continue": []string{marker},
50 "auth": []string{args["Auth"]},
51 }
52- r, err = client.Get(rietveldURL + "/_ah/login?" + authForm.Encode())
53+ authUrl, err := url.Parse(rietveldURL + "/_ah/login?" + authForm.Encode())
54+ if err != nil {
55+ return err
56+ }
57+ r, err = client.Get(authUrl.String())
58 if err == nil {
59 r.Body.Close()
60 return &LoginError{"AuthError", r.Status}
61@@ -180,10 +203,12 @@
62
63 logf("Login on %s successful.", rietveldURL)
64 auth.cookies = nil
65- for _, cookie := range r.Cookies() {
66+ for _, cookie := range client.Jar.Cookies(authUrl) {
67 auth.cookies = append(auth.cookies, cookie)
68 }
69- auth.lastLogin = time.Now()
70+ if auth.cookies != nil {
71+ auth.lastLogin = time.Now()
72+ }
73 return nil
74 }
75
76
77=== modified file 'bazaar.go'
78--- bazaar.go 2012-11-22 05:45:31 +0000
79+++ bazaar.go 2013-02-11 01:15:27 +0000
80@@ -62,7 +62,6 @@
81
82 var bzrRe = regexp.MustCompile(`(?m)^=== (added|removed|renamed|modified) (file|directory) (?:'.*' => )?'(.*)'(?: \(prop.*)?$`)
83
84-
85 var bzrRevInfo = `=== added file '[revision details]'
86 --- [revision details] 2012-01-01 00:00:00 +0000
87 +++ [revision details] 2012-01-01 00:00:00 +0000
88@@ -85,7 +84,7 @@
89
90 diff := &FileDiff{
91 Path: "[revision details]",
92- Op: Added,
93+ Op: Added,
94 Text: []byte(fmt.Sprintf(bzrRevInfo, b.oldRevision, b.newRevision)),
95 }
96 patch = append(patch, diff)
97@@ -114,7 +113,7 @@
98 }
99
100 func (b *bzrBranches) Base(filename string) (io.ReadCloser, error) {
101- output, _, err := run("bzr", "cat", "-r", "revid:" + b.oldRevision, filepath.Join(b.newPath, filename))
102+ output, _, err := run("bzr", "cat", "-r", "revid:"+b.oldRevision, filepath.Join(b.newPath, filename))
103 if err != nil {
104 return nil, err
105 }
106
107=== modified file 'doc.go'
108--- doc.go 2011-11-17 05:54:48 +0000
109+++ doc.go 2013-02-11 01:15:27 +0000
110@@ -22,4 +22,3 @@
111 fmt.Printf("Created new issue: %s\n", rietveld.CodeReview.IssueURL(issue.Id))
112 */
113 package rietveld
114-
115
116=== modified file 'form.go'
117--- form.go 2012-07-03 18:03:56 +0000
118+++ form.go 2013-02-11 01:15:27 +0000
119@@ -4,9 +4,9 @@
120 "bytes"
121 "encoding/json"
122 "fmt"
123- "launchpad.net/goetveld/rietveld/html"
124 "io"
125 "io/ioutil"
126+ "launchpad.net/goetveld/rietveld/html"
127 "mime/multipart"
128 "net/http"
129 "reflect"
130@@ -111,7 +111,7 @@
131 }
132
133 type editLoadHandler struct {
134- op *opInfo
135+ op *opInfo
136 form chan map[string]string
137 }
138
139@@ -137,7 +137,7 @@
140 }
141
142 type editHandler struct {
143- op *opInfo
144+ op *opInfo
145 form <-chan map[string]string
146 }
147
148@@ -155,7 +155,7 @@
149 diffNicks := !reflect.DeepEqual(oldNicks, newNicks)
150 if diffMails && diffNicks {
151 // Send both. The server will deduplicate.
152- addrs = make([]string, 0, len(newMails) + len(newNicks))
153+ addrs = make([]string, 0, len(newMails)+len(newNicks))
154 addrs = append(addrs, newMails...)
155 addrs = append(addrs, newNicks...)
156 } else if diffMails {
157
158=== modified file 'html/parse.go'
159--- html/parse.go 2012-07-03 18:03:56 +0000
160+++ html/parse.go 2013-02-11 01:15:27 +0000
161@@ -6,9 +6,9 @@
162
163 import (
164 "errors"
165- a "launchpad.net/goetveld/rietveld/html/atom"
166 "fmt"
167 "io"
168+ a "launchpad.net/goetveld/rietveld/html/atom"
169 "strings"
170 )
171
172
173=== modified file 'html/parse_test.go'
174--- html/parse_test.go 2012-07-03 18:03:56 +0000
175+++ html/parse_test.go 2013-02-11 01:15:27 +0000
176@@ -8,11 +8,11 @@
177 "bufio"
178 "bytes"
179 "errors"
180- "launchpad.net/goetveld/rietveld/html/atom"
181 "flag"
182 "fmt"
183 "io"
184 "io/ioutil"
185+ "launchpad.net/goetveld/rietveld/html/atom"
186 "os"
187 "path/filepath"
188 "runtime"
189
190=== modified file 'html/token.go'
191--- html/token.go 2012-07-03 18:03:56 +0000
192+++ html/token.go 2013-02-11 01:15:27 +0000
193@@ -6,8 +6,8 @@
194
195 import (
196 "bytes"
197+ "io"
198 "launchpad.net/goetveld/rietveld/html/atom"
199- "io"
200 "strconv"
201 "strings"
202 )
203
204=== modified file 'log.go'
205--- log.go 2011-11-09 16:03:12 +0000
206+++ log.go 2013-02-11 01:15:27 +0000
207@@ -15,7 +15,7 @@
208 var globalLogger log_Logger
209 var globalDebug bool
210
211-const logPrefix = "RIETVELD "
212+const logPrefix = "RIETVELD "
213
214 // Specify the *log.Logger object where log messages should be sent to.
215 func SetLogger(logger log_Logger) {
216@@ -30,36 +30,36 @@
217
218 func log(v ...interface{}) {
219 if globalLogger != nil {
220- globalLogger.Output(2, logPrefix + fmt.Sprint(v...))
221+ globalLogger.Output(2, logPrefix+fmt.Sprint(v...))
222 }
223 }
224
225 func logln(v ...interface{}) {
226 if globalLogger != nil {
227- globalLogger.Output(2, logPrefix + fmt.Sprintln(v...))
228+ globalLogger.Output(2, logPrefix+fmt.Sprintln(v...))
229 }
230 }
231
232 func logf(format string, v ...interface{}) {
233 if globalLogger != nil {
234- globalLogger.Output(2, logPrefix + fmt.Sprintf(format, v...))
235+ globalLogger.Output(2, logPrefix+fmt.Sprintf(format, v...))
236 }
237 }
238
239 func debug(v ...interface{}) {
240 if globalDebug && globalLogger != nil {
241- globalLogger.Output(2, logPrefix + fmt.Sprint(v...))
242+ globalLogger.Output(2, logPrefix+fmt.Sprint(v...))
243 }
244 }
245
246 func debugln(v ...interface{}) {
247 if globalDebug && globalLogger != nil {
248- globalLogger.Output(2, logPrefix + fmt.Sprintln(v...))
249+ globalLogger.Output(2, logPrefix+fmt.Sprintln(v...))
250 }
251 }
252
253 func debugf(format string, v ...interface{}) {
254 if globalDebug && globalLogger != nil {
255- globalLogger.Output(2, logPrefix + fmt.Sprintf(format, v...))
256+ globalLogger.Output(2, logPrefix+fmt.Sprintf(format, v...))
257 }
258 }
259
260=== modified file 'rietveld.go'
261--- rietveld.go 2012-03-15 02:02:17 +0000
262+++ rietveld.go 2013-02-11 01:15:27 +0000
263@@ -76,17 +76,17 @@
264
265 // Comment holds a message to be added to an issue's thread.
266 type Comment struct {
267- Subject string
268- Message string
269+ Subject string
270+ Message string
271
272 // If Reviewers and/or Cc is not nil, the respective list of people
273 // in the issue will be updated to the provided list before the
274 // comment is added.
275- Reviewers []string
276- Cc []string
277+ Reviewers []string
278+ Cc []string
279
280 // If NoMail is true, do not mail people when adding comment.
281- NoMail bool
282+ NoMail bool
283
284 // If PublishDrafts is true, inline comments made in the code
285 // and not yet published will be delivered.

Subscribers

People subscribed via source and target branches