Skip to content

Commit

Permalink
Change GenerateBearer function signature
Browse files Browse the repository at this point in the history
  • Loading branch information
bergusman committed Jul 8, 2021
1 parent ee20339 commit d70f94c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
10 changes: 4 additions & 6 deletions jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/base64"
"fmt"
"math/big"
"time"
)

func i2osp(n *big.Int, l int) []byte {
Expand All @@ -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
}
3 changes: 2 additions & 1 deletion token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit d70f94c

Please sign in to comment.