diff --git a/jwt.go b/jwt.go index a53d712..729dcd4 100644 --- a/jwt.go +++ b/jwt.go @@ -7,7 +7,6 @@ import ( "encoding/base64" "fmt" "math/big" - "time" ) func i2osp(n *big.Int, l int) []byte { @@ -32,15 +31,14 @@ func es256(key *ecdsa.PrivateKey, body string) ([]byte, error) { return append(i2osp(r, 32), i2osp(s, 32)...), nil } -func GenerateBearer(key *ecdsa.PrivateKey, keyId, teamId string) (string, int64, error) { - iat := time.Now().Unix() +func GenerateBearer(key *ecdsa.PrivateKey, keyId, teamId string, issuedAt int64) (string, error) { h := fmt.Sprintf(`{"alg":"ES256","typ":"JWT","kid":"%s"}`, keyId) - p := fmt.Sprintf(`{"iss":"%s","iat":%d}`, teamId, iat) + p := fmt.Sprintf(`{"iss":"%s","iat":%d}`, teamId, issuedAt) hp := base64.RawURLEncoding.EncodeToString([]byte(h)) + "." + base64.RawURLEncoding.EncodeToString([]byte(p)) sig, err := es256(key, hp) if err != nil { - return "", 0, err + return "", err } t := hp + "." + base64.RawURLEncoding.EncodeToString([]byte(sig)) - return t, iat, nil + return t, nil } diff --git a/token.go b/token.go index 7e48502..d50982f 100644 --- a/token.go +++ b/token.go @@ -76,7 +76,8 @@ func (t *Token) Generate() (string, error) { if t.Key == nil { return "", errors.New("key is nil") } - bearer, issuedAt, err := GenerateBearer(t.Key, t.KeyID, t.TeamID) + issuedAt := time.Now().Unix() + bearer, err := GenerateBearer(t.Key, t.KeyID, t.TeamID, issuedAt) if err != nil { return "", err }