This repository has been archived by the owner on Jan 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Cookie Refresh Improvements #115
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -348,13 +348,12 @@ func NewProcessCookieTest(opts ProcessCookieTestOpts) *ProcessCookieTest { | |
|
||
pc_test.opts = NewOptions() | ||
pc_test.opts.Upstreams = append(pc_test.opts.Upstreams, "unused") | ||
pc_test.opts.CookieSecret = "foobar" | ||
pc_test.opts.ClientID = "bazquux" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. D'oh! Thanks for cleaning up my sloppiness. :-) |
||
pc_test.opts.ClientSecret = "xyzzyplugh" | ||
pc_test.opts.CookieSecret = "0123456789abcdef" | ||
// First, set the CookieRefresh option so proxy.AesCipher is created, | ||
// needed to encrypt the access_token. | ||
pc_test.opts.CookieRefresh = time.Duration(24) * time.Hour | ||
pc_test.opts.CookieRefresh = time.Hour | ||
pc_test.opts.Validate() | ||
|
||
pc_test.proxy = NewOauthProxy(pc_test.opts, func(email string) bool { | ||
|
@@ -379,14 +378,13 @@ func NewProcessCookieTestWithDefaults() *ProcessCookieTest { | |
}) | ||
} | ||
|
||
func (p *ProcessCookieTest) MakeCookie(value, access_token string) *http.Cookie { | ||
cookie_value, _ := buildCookieValue( | ||
value, p.proxy.AesCipher, access_token) | ||
return p.proxy.MakeCookie(p.req, cookie_value, p.opts.CookieExpire) | ||
func (p *ProcessCookieTest) MakeCookie(value, access_token string, ref time.Time) *http.Cookie { | ||
cookie_value, _ := buildCookieValue(value, p.proxy.AesCipher, access_token) | ||
return p.proxy.MakeCookie(p.req, cookie_value, p.opts.CookieExpire, ref) | ||
} | ||
|
||
func (p *ProcessCookieTest) AddCookie(value, access_token string) { | ||
p.req.AddCookie(p.MakeCookie(value, access_token)) | ||
p.req.AddCookie(p.MakeCookie(value, access_token, time.Now())) | ||
} | ||
|
||
func (p *ProcessCookieTest) ProcessCookie() (email, user, access_token string, ok bool) { | ||
|
@@ -416,15 +414,16 @@ func TestProcessCookieFailIfParsingCookieValueFails(t *testing.T) { | |
pc_test.proxy.AesCipher, "my_access_token") | ||
pc_test.req.AddCookie(pc_test.proxy.MakeCookie( | ||
pc_test.req, value+"some bogus bytes", | ||
pc_test.opts.CookieExpire)) | ||
pc_test.opts.CookieExpire, time.Now())) | ||
_, _, _, ok := pc_test.ProcessCookie() | ||
assert.Equal(t, false, ok) | ||
} | ||
|
||
func TestProcessCookieRefreshNotSet(t *testing.T) { | ||
pc_test := NewProcessCookieTestWithDefaults() | ||
pc_test.proxy.CookieExpire = time.Duration(23) * time.Hour | ||
cookie := pc_test.MakeCookie("[email protected]", "") | ||
reference := time.Now().Add(time.Duration(-2) * time.Hour) | ||
cookie := pc_test.MakeCookie("[email protected]", "", reference) | ||
pc_test.req.AddCookie(cookie) | ||
|
||
_, _, _, ok := pc_test.ProcessCookie() | ||
|
@@ -435,36 +434,70 @@ func TestProcessCookieRefreshNotSet(t *testing.T) { | |
func TestProcessCookieRefresh(t *testing.T) { | ||
pc_test := NewProcessCookieTestWithDefaults() | ||
pc_test.proxy.CookieExpire = time.Duration(23) * time.Hour | ||
cookie := pc_test.MakeCookie("[email protected]", "my_access_token") | ||
reference := time.Now().Add(time.Duration(-2) * time.Hour) | ||
cookie := pc_test.MakeCookie("[email protected]", "my_access_token", reference) | ||
pc_test.req.AddCookie(cookie) | ||
|
||
pc_test.proxy.CookieRefresh = time.Duration(24) * time.Hour | ||
pc_test.proxy.CookieRefresh = time.Hour | ||
_, _, _, ok := pc_test.ProcessCookie() | ||
assert.Equal(t, true, ok) | ||
assert.NotEqual(t, []string(nil), pc_test.rw.HeaderMap["Set-Cookie"]) | ||
} | ||
|
||
func TestProcessCookieRefreshThresholdNotCrossed(t *testing.T) { | ||
pc_test := NewProcessCookieTestWithDefaults() | ||
pc_test.proxy.CookieExpire = time.Duration(25) * time.Hour | ||
cookie := pc_test.MakeCookie("[email protected]", "my_access_token") | ||
pc_test.proxy.CookieExpire = time.Duration(23) * time.Hour | ||
reference := time.Now().Add(time.Duration(-30) * time.Minute) | ||
cookie := pc_test.MakeCookie("[email protected]", "my_access_token", reference) | ||
pc_test.req.AddCookie(cookie) | ||
|
||
pc_test.proxy.CookieRefresh = time.Duration(24) * time.Hour | ||
pc_test.proxy.CookieRefresh = time.Hour | ||
_, _, _, ok := pc_test.ProcessCookie() | ||
assert.Equal(t, true, ok) | ||
assert.Equal(t, []string(nil), pc_test.rw.HeaderMap["Set-Cookie"]) | ||
} | ||
|
||
func TestProcessCookieFailIfCookieExpired(t *testing.T) { | ||
pc_test := NewProcessCookieTestWithDefaults() | ||
pc_test.proxy.CookieExpire = time.Duration(24) * time.Hour | ||
reference := time.Now().Add(time.Duration(25) * time.Hour * -1) | ||
cookie := pc_test.MakeCookie("[email protected]", "my_access_token", reference) | ||
pc_test.req.AddCookie(cookie) | ||
|
||
if _, _, _, ok := pc_test.ProcessCookie(); ok { | ||
t.Error("ProcessCookie() should have failed") | ||
} | ||
if set_cookie := pc_test.rw.HeaderMap["Set-Cookie"]; set_cookie != nil { | ||
t.Error("expected Set-Cookie to be nil, instead was: ", set_cookie) | ||
} | ||
} | ||
|
||
func TestProcessCookieFailIfRefreshSetAndCookieExpired(t *testing.T) { | ||
pc_test := NewProcessCookieTestWithDefaults() | ||
pc_test.proxy.CookieExpire = time.Duration(24) * time.Hour | ||
reference := time.Now().Add(time.Duration(25) * time.Hour * -1) | ||
cookie := pc_test.MakeCookie("[email protected]", "my_access_token", reference) | ||
pc_test.req.AddCookie(cookie) | ||
|
||
pc_test.proxy.CookieRefresh = time.Hour | ||
if _, _, _, ok := pc_test.ProcessCookie(); ok { | ||
t.Error("ProcessCookie() should have failed") | ||
} | ||
if set_cookie := pc_test.rw.HeaderMap["Set-Cookie"]; set_cookie != nil { | ||
t.Error("expected Set-Cookie to be nil, instead was: ", set_cookie) | ||
} | ||
} | ||
|
||
func TestProcessCookieFailIfRefreshSetAndTokenNoLongerValid(t *testing.T) { | ||
pc_test := NewProcessCookieTest(ProcessCookieTestOpts{ | ||
provider_validate_cookie_response: false, | ||
}) | ||
pc_test.proxy.CookieExpire = time.Duration(23) * time.Hour | ||
cookie := pc_test.MakeCookie("[email protected]", "my_access_token") | ||
reference := time.Now().Add(time.Duration(-24) * time.Hour) | ||
cookie := pc_test.MakeCookie("[email protected]", "my_access_token", reference) | ||
pc_test.req.AddCookie(cookie) | ||
|
||
pc_test.proxy.CookieRefresh = time.Duration(24) * time.Hour | ||
pc_test.proxy.CookieRefresh = time.Hour | ||
_, _, _, ok := pc_test.ProcessCookie() | ||
assert.Equal(t, false, ok) | ||
assert.Equal(t, []string(nil), pc_test.rw.HeaderMap["Set-Cookie"]) | ||
|
@@ -475,10 +508,11 @@ func TestProcessCookieFailIfRefreshSetAndUserNoLongerValid(t *testing.T) { | |
pc_test.validate_user = false | ||
|
||
pc_test.proxy.CookieExpire = time.Duration(23) * time.Hour | ||
cookie := pc_test.MakeCookie("[email protected]", "my_access_token") | ||
reference := time.Now().Add(time.Duration(-2) * time.Hour) | ||
cookie := pc_test.MakeCookie("[email protected]", "my_access_token", reference) | ||
pc_test.req.AddCookie(cookie) | ||
|
||
pc_test.proxy.CookieRefresh = time.Duration(24) * time.Hour | ||
pc_test.proxy.CookieRefresh = time.Hour | ||
_, _, _, ok := pc_test.ProcessCookie() | ||
assert.Equal(t, false, ok) | ||
assert.Equal(t, []string(nil), pc_test.rw.HeaderMap["Set-Cookie"]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,34 @@ | ||
package providers | ||
|
||
import ( | ||
"github.com/bitly/oauth2_proxy/api" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/bitly/oauth2_proxy/api" | ||
) | ||
|
||
func validateToken(p Provider, access_token string, header http.Header) bool { | ||
if access_token == "" || p.Data().ValidateUrl == nil { | ||
return false | ||
} | ||
url := p.Data().ValidateUrl.String() | ||
endpoint := p.Data().ValidateUrl.String() | ||
if len(header) == 0 { | ||
url = url + "?access_token=" + access_token | ||
params := url.Values{"access_token": {access_token}} | ||
endpoint = endpoint + "?" + params.Encode() | ||
} | ||
if resp, err := api.RequestUnparsedResponse(url, header); err != nil { | ||
resp, err := api.RequestUnparsedResponse(endpoint, header) | ||
if err != nil { | ||
log.Printf("token validation request failed: %s", err) | ||
return false | ||
} else { | ||
return resp.StatusCode == 200 | ||
} | ||
|
||
body, _ := ioutil.ReadAll(resp.Body) | ||
resp.Body.Close() | ||
if resp.StatusCode == 200 { | ||
return true | ||
} | ||
log.Printf("token validation request failed: status %d - %s", resp.StatusCode, body) | ||
return false | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure of the utility of
t.Before(time.Now().Add(time.Minute*5))
. Seems like it will always be true, unless there are clock skew issues between different oauth2_proxy instances running on different machines with the same cookie secret.