I think it actually works now.

This commit is contained in:
Neale Pickett 2021-12-05 16:16:52 -07:00
parent 69e6d70c4b
commit 36878fc14b
1 changed files with 10 additions and 3 deletions

View File

@ -6,6 +6,7 @@ import (
"crypto/sha256" "crypto/sha256"
"encoding/base64" "encoding/base64"
"encoding/binary" "encoding/binary"
"log"
"time" "time"
) )
@ -23,7 +24,9 @@ func (t T) computeMac(secret []byte) []byte {
// String returns the string encoding of the token // String returns the string encoding of the token
func (t T) String() string { func (t T) String() string {
f := new(bytes.Buffer) f := new(bytes.Buffer)
binary.Write(f, binary.BigEndian, t.expiration) if err := binary.Write(f, binary.BigEndian, t.expiration.Unix()); err != nil {
log.Fatal(err)
}
f.Write(t.mac) f.Write(t.mac)
return base64.StdEncoding.EncodeToString(f.Bytes()) return base64.StdEncoding.EncodeToString(f.Bytes())
} }
@ -55,8 +58,12 @@ func Parse(b []byte) (T, error) {
mac: make([]byte, sha256.Size), mac: make([]byte, sha256.Size),
} }
f := bytes.NewReader(b) f := bytes.NewReader(b)
if err := binary.Read(f, binary.BigEndian, &t.expiration); err != nil { {
return t, err var sec int64
if err := binary.Read(f, binary.BigEndian, &sec); err != nil {
return t, err
}
t.expiration = time.Unix(sec, 0)
} }
if n, err := f.Read(t.mac); err != nil { if n, err := f.Read(t.mac); err != nil {
return t, err return t, err