git.iko.soy/typeable/oauth2-proxy
on master help sign in
Fix ticket retrieval with an invalid ticket

(cherry picked from commit 66bbf146ec45d127bdd374120743aeef936894a7)

commit22199fa4173ca2f1b4b72f545231c7103ff86848 authorJoel Speed <Joel.speed@hotmail.co.uk> dateThu 30 May 2019, 10:10 (7 years ago) parent3155ada
1 file changed+21−20
pkg/sessions/redis/redis_store.go+21−20
pkg/sessions/redis/redis_store.go+21−20
@@ -192,26 +192,9 @@ func (store *SessionStore) makeCookie(req *http.Request, value string, expires t
192192}
193193
194194func (store *SessionStore) storeValue(value string, expiration time.Duration, requestCookie *http.Cookie) (string, error) {
195− var ticket *TicketData
196− if requestCookie != nil {
197− var err error
198− val, _, ok := cookie.Validate(requestCookie, store.CookieOptions.CookieSecret, store.CookieOptions.CookieExpire)
199− if !ok {
200− ticket, err = newTicket()
201− if err != nil {
202− return "", fmt.Errorf("error creating new ticket: %s", err)
203− }
204− }
205− ticket, err = decodeTicket(store.CookieOptions.CookieName, val)
206− if err != nil {
207− return "", err
208− }
209− } else {
210− var err error
211− ticket, err = newTicket()
212− if err != nil {
213− return "", fmt.Errorf("error creating new ticket: %s", err)
214− }
195+ ticket, err := store.getTicket(requestCookie)
196+ if err != nil {
197+ return "", fmt.Errorf("error getting ticket: %v", err)
215198 }
216199
217200 ciphertext := make([]byte, len(value))
@@ -232,6 +215,24 @@ func (store *SessionStore) storeValue(value string, expiration time.Duration, re
232215 return ticket.encodeTicket(store.CookieOptions.CookieName), nil
233216}
234217
218+// getTicket retrieves an existing ticket from the cookie if present,
219+// or creates a new ticket
220+func (store *SessionStore) getTicket(requestCookie *http.Cookie) (*TicketData, error) {
221+ if requestCookie == nil {
222+ return newTicket()
223+ }
224+
225+ // An existing cookie exists, try to retrieve the ticket
226+ val, _, ok := cookie.Validate(requestCookie, store.CookieOptions.CookieSecret, store.CookieOptions.CookieExpire)
227+ if !ok {
228+ // Cookie is invalid, create a new ticket
229+ return newTicket()
230+ }
231+
232+ // Valid cookie, decode the ticket
233+ return decodeTicket(store.CookieOptions.CookieName, val)
234+}
235+
235236func newTicket() (*TicketData, error) {
236237 rawID := make([]byte, 16)
237238 if _, err := io.ReadFull(rand.Reader, rawID); err != nil {