Skip to content

Commit

Permalink
Mock now in CSRF struct instead of package
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Meves committed Mar 6, 2021
1 parent 89aadd9 commit e7d86ce
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
16 changes: 11 additions & 5 deletions pkg/cookies/csrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"github.com/vmihailenco/msgpack/v4"
)

var now = time.Now

// CSRF manages various nonces stored in the CSRF cookie during the initial
// authentication flows.
type CSRF interface {
Expand All @@ -40,6 +38,7 @@ type csrf struct {
OIDCNonce []byte `msgpack:"n,omitempty"`

cookieOpts *options.Cookie
nowFunc func() time.Time
}

// NewCSRF creates a CSRF with random nonces
Expand Down Expand Up @@ -110,7 +109,7 @@ func (c csrf) SetCookie(rw http.ResponseWriter, req *http.Request) (*http.Cookie
encoded,
c.cookieOpts,
c.cookieOpts.Expire,
now(),
c.now(),
)
http.SetCookie(rw, cookie)

Expand All @@ -125,7 +124,7 @@ func (c csrf) ClearCookie(rw http.ResponseWriter, req *http.Request) {
"",
c.cookieOpts,
time.Hour*-1,
now(),
c.now(),
))
}

Expand All @@ -142,7 +141,7 @@ func (c csrf) encodeCookie() (string, error) {
return "", err
}

return encryption.SignedValue(c.cookieOpts.Secret, c.cookieName(), encrypted, now())
return encryption.SignedValue(c.cookieOpts.Secret, c.cookieName(), encrypted, c.now())
}

// decodeCSRFCookie validates the signature then decrypts and decodes a CSRF
Expand Down Expand Up @@ -174,6 +173,13 @@ func (c csrf) cookieName() string {
return csrfCookieName(c.cookieOpts)
}

func (c csrf) now() time.Time {
if c.nowFunc != nil {
return c.nowFunc()
}
return time.Now()
}

func csrfCookieName(opts *options.Cookie) string {
return fmt.Sprintf("%v_csrf", opts.Name)
}
Expand Down
16 changes: 12 additions & 4 deletions pkg/cookies/csrf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,14 @@ var _ = Describe("CSRF Cookie Tests", func() {

Context("Cookie Management", func() {
var req *http.Request
var origNow func() time.Time

testNow := time.Unix(nowEpoch, 0)

BeforeEach(func() {
now = func() time.Time {
return time.Unix(nowEpoch, 0)
origNow = privateCSRF.nowFunc
privateCSRF.nowFunc = func() time.Time {
return testNow
}

req = &http.Request{
Expand All @@ -138,6 +142,10 @@ var _ = Describe("CSRF Cookie Tests", func() {
}
})

AfterEach(func() {
privateCSRF.nowFunc = origNow
})

Context("SetCookie", func() {
It("adds the encoded CSRF cookie to a ResponseWriter", func() {
rw := httptest.NewRecorder()
Expand All @@ -153,7 +161,7 @@ var _ = Describe("CSRF Cookie Tests", func() {
"; Path=%s; Domain=%s; Expires=%s; HttpOnly; Secure",
cookiePath,
cookieDomain,
testCookieExpires(now().Add(cookieOpts.Expire)),
testCookieExpires(testNow.Add(cookieOpts.Expire)),
),
))
})
Expand All @@ -171,7 +179,7 @@ var _ = Describe("CSRF Cookie Tests", func() {
privateCSRF.cookieName(),
cookiePath,
cookieDomain,
testCookieExpires(now().Add(time.Hour*-1)),
testCookieExpires(testNow.Add(time.Hour*-1)),
),
))
})
Expand Down

0 comments on commit e7d86ce

Please sign in to comment.