1818package pulsar
1919
2020import (
21+ "encoding/base64"
2122 "strings"
23+ "time"
24+
25+ "github.com/golang-jwt/jwt/v4"
2226
2327 "github.com/streamnative/pulsarctl/pkg/pulsar/common/algorithm/algorithm"
2428 "github.com/streamnative/pulsarctl/pkg/pulsar/common/algorithm/keypair"
2529
26- "github.com/form3tech-oss/jwt-go"
2730 "github.com/pkg/errors"
2831)
2932
@@ -38,6 +41,10 @@ type Token interface {
3841 // object and the expire time
3942 Create (algorithm.Algorithm , interface {}, string , int64 ) (string , error )
4043
44+ // CreateToken creates a token object using the specified signature algorithm, private key
45+ // custom claim and header
46+ CreateToken (algorithm.Algorithm , interface {}, * jwt.MapClaims , map [string ]interface {}) (string , error )
47+
4148 // Validate a token is valid or not
4249 Validate (algorithm.Algorithm , string , interface {}) (string , int64 , error )
4350
@@ -77,13 +84,25 @@ func (t *token) CreateSecretKey(signatureAlgorithm algorithm.Algorithm) ([]byte,
7784func (t * token ) Create (algorithm algorithm.Algorithm , signKey interface {}, subject string ,
7885 expireTime int64 ) (string , error ) {
7986
80- claims := & jwt.StandardClaims {
81- Subject : subject ,
82- ExpiresAt : expireTime ,
87+ claims := & jwt.MapClaims {
88+ "sub" : subject ,
89+ "exp" : jwt . NewNumericDate ( time . Unix ( expireTime , 0 )) ,
8390 }
84- signMethod := parseAlgorithmToJwtSignMethod (algorithm )
85- tokenString := jwt . NewWithClaims ( signMethod , claims )
91+ return t . CreateToken (algorithm , signKey , claims , nil )
92+ }
8693
94+ func (t * token ) CreateToken (
95+ algorithm algorithm.Algorithm ,
96+ signKey interface {},
97+ mapClaims * jwt.MapClaims ,
98+ headers map [string ]interface {}) (string , error ) {
99+ signMethod := parseAlgorithmToJwtSignMethod (algorithm )
100+ tokenString := jwt .NewWithClaims (signMethod , mapClaims )
101+ if headers != nil && len (headers ) > 0 {
102+ for s , i := range headers {
103+ tokenString .Header [s ] = i
104+ }
105+ }
87106 return tokenString .SignedString (signKey )
88107}
89108
@@ -110,20 +129,20 @@ func (t *token) Validate(algorithm algorithm.Algorithm, tokenString string,
110129
111130func (t * token ) GetAlgorithm (tokenString string ) (string , error ) {
112131 parts := strings .Split (tokenString , "." )
113- algorithm , err := jwt . DecodeSegment (parts [0 ])
132+ alg , err := base64 . RawURLEncoding . DecodeString (parts [0 ])
114133 if err != nil {
115134 return "" , err
116135 }
117- return string (algorithm ), nil
136+ return string (alg ), nil
118137}
119138
120139func (t * token ) GetSubject (tokenString string ) (string , error ) {
121140 parts := strings .Split (tokenString , "." )
122- algorithm , err := jwt . DecodeSegment (parts [1 ])
141+ alg , err := base64 . RawURLEncoding . DecodeString (parts [1 ])
123142 if err != nil {
124143 return "" , err
125144 }
126- return string (algorithm ), nil
145+ return string (alg ), nil
127146}
128147
129148func parseAlgorithmToJwtSignMethod (a algorithm.Algorithm ) jwt.SigningMethod {
0 commit comments