streamline int overflow check for token timestamps

This commit is contained in:
Fabio Bozzo
2024-12-02 12:06:06 +01:00
parent 28272e6900
commit 5b816ccc62
3 changed files with 28 additions and 28 deletions

View File

@@ -1,9 +1,11 @@
package parse
import (
"fmt"
"time"
"github.com/ucan-wg/go-ucan/did"
"github.com/ucan-wg/go-ucan/pkg/policy/limits"
)
func OptionalDID(s *string) (did.DID, error) {
@@ -13,10 +15,15 @@ func OptionalDID(s *string) (did.DID, error) {
return did.Parse(*s)
}
func OptionalTimestamp(sec *int64) *time.Time {
func OptionalTimestamp(sec *int64) (*time.Time, error) {
if sec == nil {
return nil
return nil, nil
}
if *sec > limits.MaxInt53 || *sec < limits.MinInt53 {
return nil, fmt.Errorf("timestamp value %d exceeds safe integer bounds", *sec)
}
t := time.Unix(*sec, 0)
return &t
return &t, nil
}