Skip to content

Commit

Permalink
xsrftoken: correct semantics of time vs now variable
Browse files Browse the repository at this point in the history
  • Loading branch information
alephoverflow committed Apr 7, 2019
1 parent 6886b33 commit 137fa75
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions xsrftoken/xsrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ func Generate(key, userID, actionID string) string {
return generateTokenAtTime(key, userID, actionID, time.Now())
}

// generateTokenAtTime is like Generate, but returns a token that expires 24 hours from given time.
func generateTokenAtTime(key, userID, actionID string, time time.Time) string {
// generateTokenAtTime is like Generate, but returns a token that expires 24 hours from the given time.
func generateTokenAtTime(key, userID, actionID string, t time.Time) string {
if len(key) == 0 {
panic("zero length xsrf secret key")
}
// Round time up and convert to milliseconds.
milliTime := (time.UnixNano() + 1e6 - 1) / 1e6
milliTime := (t.UnixNano() + 1e6 - 1) / 1e6

h := hmac.New(sha1.New, []byte(key))
fmt.Fprintf(h, "%s:%s:%d", clean(userID), clean(actionID), milliTime)
Expand All @@ -59,7 +59,7 @@ func Valid(token, key, userID, actionID string) bool {
}

// validTokenAtTime reports whether a token is valid at the given time.
func validTokenAtTime(token, key, userID, actionID string, time time.Time) bool {
func validTokenAtTime(token, key, userID, actionID string, t time.Time) bool {
if len(key) == 0 {
panic("zero length xsrf secret key")
}
Expand All @@ -72,7 +72,7 @@ func validTokenAtTime(token, key, userID, actionID string, time time.Time) bool
if err != nil {
return false
}
issueTime := time.Unix(0, millis*1e6)
issueTime := t.Unix(0, millis*1e6)

// Check that the token is not expired.
if time.Sub(issueTime) >= Timeout {
Expand All @@ -82,7 +82,7 @@ func validTokenAtTime(token, key, userID, actionID string, time time.Time) bool
// Check that the token is not from the future.
// Allow 1 minute grace period in case the token is being verified on a
// machine whose clock is behind the machine that issued the token.
if issueTime.After(time.Add(1 * time.Minute)) {
if issueTime.After(t.Add(1 * time.Minute)) {
return false
}

Expand Down

0 comments on commit 137fa75

Please sign in to comment.