Skip to content

Commit de72eb3

Browse files
committed
Use timestamp as date in header
1 parent bc13176 commit de72eb3

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

validator/datevalidate.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,20 @@ func NewDateValidator() *DateValidator {
3838

3939
// Validate return error when checking if header date is valid or not
4040
func (v *DateValidator) Validate(r *http.Request) error {
41-
timestampStr := r.Header.Get("timestamp")
42-
if timestampStr != "" {
43-
timestamp, err := strconv.ParseInt(timestampStr, 10, 64)
44-
if err == nil {
45-
clientTimestamp := time.Unix(timestamp, 0)
46-
serverTime := time.Now()
47-
start := serverTime.Add(-v.TimeGap)
48-
stop := serverTime.Add(v.TimeGap)
49-
if clientTimestamp.Before(start) || clientTimestamp.After(stop) {
50-
return ErrDateNotInRange
51-
}
52-
return nil
53-
}
41+
timestampStr := r.Header.Get("date")
42+
if timestampStr == "" {
43+
return errors.New("date header is required")
5444
}
55-
t, err := http.ParseTime(r.Header.Get("date"))
45+
46+
timestamp, err := strconv.ParseInt(timestampStr, 10, 64)
5647
if err != nil {
57-
return newPublicError(fmt.Sprintf("Could not parse date header. Error: %s", err.Error()))
48+
return newPublicError(fmt.Sprintf("Could not parse date header to timestamp. Error: %s", err.Error()))
5849
}
50+
clientTimestamp := time.Unix(timestamp, 0)
5951
serverTime := time.Now()
6052
start := serverTime.Add(-v.TimeGap)
6153
stop := serverTime.Add(v.TimeGap)
62-
if t.Before(start) || t.After(stop) {
54+
if clientTimestamp.Before(start) || clientTimestamp.After(stop) {
6355
return ErrDateNotInRange
6456
}
6557
return nil

0 commit comments

Comments
 (0)