Skip to content

Commit

Permalink
cmd call CreateToken instead of Create
Browse files Browse the repository at this point in the history
  • Loading branch information
coderzc committed May 31, 2024
1 parent a7087a8 commit cd82391
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
8 changes: 4 additions & 4 deletions pkg/cmdutils/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ type Token interface {
CreateSecretKey(algorithm.Algorithm) ([]byte, error)

// Create creates a token object using the specified signature algorithm, private key,
// object and the expire time and header
Create(algorithm.Algorithm, interface{}, string, int64, map[string]interface{}) (string, error)
// object and the expire time
Create(algorithm.Algorithm, interface{}, string, int64) (string, error)

// CreateToken creates a token object using the specified signature algorithm, private key
// custom claim and header
Expand Down Expand Up @@ -74,7 +74,7 @@ func (t *token) CreateSecretKey(signatureAlgorithm algorithm.Algorithm) ([]byte,
}

func (t *token) Create(algorithm algorithm.Algorithm, signKey interface{}, subject string,
expireTime int64, headers map[string]interface{}) (string, error) {
expireTime int64) (string, error) {

var claims *jwt.MapClaims
if expireTime <= 0 {
Expand All @@ -88,7 +88,7 @@ func (t *token) Create(algorithm algorithm.Algorithm, signKey interface{}, subje
}
}

return t.CreateToken(algorithm, signKey, claims, headers)
return t.CreateToken(algorithm, signKey, claims, nil)
}

func (t *token) CreateToken(
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmdutils/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestCreateToken(t *testing.T) {
require.NoError(t, err)

subject := "test-role"
myToken, err := tokenProvider.Create(alg, key, subject, 0, nil)
myToken, err := tokenProvider.Create(alg, key, subject, 0)
require.NoError(t, err)

parsedSubject, exp, err := tokenProvider.Validate(alg, myToken, key)
Expand All @@ -52,7 +52,7 @@ func TestCreateTokenWithExp(t *testing.T) {

subject := "test-role"
exp := time.Now().Add(time.Hour).Unix()
myToken, err := tokenProvider.Create(alg, key, subject, exp, nil)
myToken, err := tokenProvider.Create(alg, key, subject, exp)
require.NoError(t, err)

parsedSubject, exp, err := tokenProvider.Validate(alg, myToken, key)
Expand Down
16 changes: 14 additions & 2 deletions pkg/ctl/token/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/streamnative/pulsarctl/pkg/pulsar/common/algorithm/algorithm"
"github.com/streamnative/pulsarctl/pkg/pulsar/common/algorithm/keypair"

"github.com/golang-jwt/jwt/v4"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -168,8 +169,19 @@ func doCreate(vc *cmdutils.VerbCmd, args *createCmdArgs) error {
return err
}

tokenString, err := token.Create(algorithm.Algorithm(args.signatureAlgorithm), signKey, args.subject, expireTime,
headers)
var claims *jwt.MapClaims
if expireTime <= 0 {
claims = &jwt.MapClaims{
"sub": args.subject,
}
} else {
claims = &jwt.MapClaims{
"sub": args.subject,
"exp": jwt.NewNumericDate(time.Unix(expireTime, 0)),
}
}

tokenString, err := token.CreateToken(algorithm.Algorithm(args.signatureAlgorithm), signKey, claims, headers)
if err != nil {
return err
}
Expand Down

0 comments on commit cd82391

Please sign in to comment.