Skip to content

Commit 0535076

Browse files
authored
Upgrade jwt package, support custom claim and header (streamnative#906)
* Upgrade jwt package Support custom claim and header * Fixed go import * Remove golang 13, 14, 15 ci check
1 parent 6caf269 commit 0535076

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

.github/workflows/ci-release-checks.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
runs-on: ubuntu-latest
1515
strategy:
1616
matrix:
17-
go-version: [ 1.13, 1.14, 1.15, 1.16, 1.17, 1.18 ]
17+
go-version: [ 1.16, 1.17, 1.18 ]
1818
steps:
1919
- uses: actions/checkout@v2
2020
- uses: actions/setup-go@v1

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ require (
77
github.com/apache/pulsar-client-go v0.9.0
88
github.com/docker/go-connections v0.4.0
99
github.com/fatih/color v1.7.0
10-
github.com/form3tech-oss/jwt-go v3.2.3+incompatible
1110
github.com/ghodss/yaml v1.0.0
11+
github.com/golang-jwt/jwt/v4 v4.4.3
1212
github.com/golang/protobuf v1.5.2
1313
github.com/imdario/mergo v0.3.8
1414
github.com/kris-nova/logger v0.0.0-20181127235838-fd0d87064b06

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.m
112112
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
113113
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
114114
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
115-
github.com/form3tech-oss/jwt-go v3.2.3+incompatible h1:7ZaBxOI7TMoYBfyA3cQHErNNyAWIKUMIwqxEtgHOs5c=
116-
github.com/form3tech-oss/jwt-go v3.2.3+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
117115
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
118116
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
119117
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
@@ -144,6 +142,8 @@ github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
144142
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
145143
github.com/golang-jwt/jwt v3.2.1+incompatible h1:73Z+4BJcrTC+KczS6WvTPvRGOp1WmfEP4Q1lOd9Z/+c=
146144
github.com/golang-jwt/jwt v3.2.1+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
145+
github.com/golang-jwt/jwt/v4 v4.4.3 h1:Hxl6lhQFj4AnOX6MLrsCb/+7tCj7DxP7VA+2rDIq5AU=
146+
github.com/golang-jwt/jwt/v4 v4.4.3/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
147147
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
148148
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
149149
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=

pkg/pulsar/token.go

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818
package pulsar
1919

2020
import (
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,
7784
func (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

111130
func (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

120139
func (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

129148
func parseAlgorithmToJwtSignMethod(a algorithm.Algorithm) jwt.SigningMethod {

0 commit comments

Comments
 (0)