Skip to content

Commit 737aea3

Browse files
authored
fix: fix token exp (streamnative#976)
* fix: fix token exp Signed-off-by: Zixuan Liu <[email protected]> * Improve condition Signed-off-by: Zixuan Liu <[email protected]> --------- Signed-off-by: Zixuan Liu <[email protected]>
1 parent 4ed0bae commit 737aea3

File tree

2 files changed

+80
-6
lines changed

2 files changed

+80
-6
lines changed

pkg/pulsar/token.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,18 @@ func (t *token) CreateSecretKey(signatureAlgorithm algorithm.Algorithm) ([]byte,
8484
func (t *token) Create(algorithm algorithm.Algorithm, signKey interface{}, subject string,
8585
expireTime int64) (string, error) {
8686

87-
claims := &jwt.MapClaims{
88-
"sub": subject,
89-
"exp": jwt.NewNumericDate(time.Unix(expireTime, 0)),
87+
var claims *jwt.MapClaims
88+
if expireTime <= 0 {
89+
claims = &jwt.MapClaims{
90+
"sub": subject,
91+
}
92+
} else {
93+
claims = &jwt.MapClaims{
94+
"sub": subject,
95+
"exp": jwt.NewNumericDate(time.Unix(expireTime, 0)),
96+
}
9097
}
98+
9199
return t.CreateToken(algorithm, signKey, claims, nil)
92100
}
93101

@@ -110,7 +118,7 @@ func (t *token) Validate(algorithm algorithm.Algorithm, tokenString string,
110118
signKey interface{}) (string, int64, error) {
111119

112120
// verify the signature algorithm
113-
parsedToken, err := jwt.ParseWithClaims(tokenString, &jwt.StandardClaims{},
121+
parsedToken, err := jwt.ParseWithClaims(tokenString, &jwt.RegisteredClaims{},
114122
func(jt *jwt.Token) (i interface{}, e error) {
115123
signMethod := parseAlgorithmToJwtSignMethod(algorithm)
116124
if jt.Method != signMethod {
@@ -120,8 +128,13 @@ func (t *token) Validate(algorithm algorithm.Algorithm, tokenString string,
120128
})
121129

122130
// get the subject and the expire time
123-
if claim, ok := parsedToken.Claims.(*jwt.StandardClaims); parsedToken.Valid && ok {
124-
return claim.Subject, claim.ExpiresAt, nil
131+
if claim, ok := parsedToken.Claims.(*jwt.RegisteredClaims); parsedToken.Valid && ok {
132+
expiresAt := claim.ExpiresAt
133+
exp := int64(0)
134+
if expiresAt != nil {
135+
exp = expiresAt.Unix()
136+
}
137+
return claim.Subject, exp, nil
125138
}
126139

127140
return "", 0, err

pkg/pulsar/token_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package pulsar
19+
20+
import (
21+
"testing"
22+
"time"
23+
24+
"github.com/streamnative/pulsarctl/pkg/pulsar/common/algorithm/algorithm"
25+
"github.com/stretchr/testify/require"
26+
)
27+
28+
func TestCreateToken(t *testing.T) {
29+
tokenProvider := token{}
30+
31+
alg := algorithm.HS256
32+
key, err := tokenProvider.CreateSecretKey(alg)
33+
require.NoError(t, err)
34+
35+
subject := "test-role"
36+
myToken, err := tokenProvider.Create(alg, key, subject, 0)
37+
require.NoError(t, err)
38+
39+
parsedSubject, exp, err := tokenProvider.Validate(alg, myToken, key)
40+
require.NoError(t, err)
41+
require.Equal(t, subject, parsedSubject)
42+
require.Equal(t, exp, int64(0))
43+
}
44+
45+
func TestCreateTokenWithExp(t *testing.T) {
46+
tokenProvider := token{}
47+
48+
alg := algorithm.HS256
49+
key, err := tokenProvider.CreateSecretKey(alg)
50+
require.NoError(t, err)
51+
52+
subject := "test-role"
53+
exp := time.Now().Add(time.Hour).Unix()
54+
myToken, err := tokenProvider.Create(alg, key, subject, exp)
55+
require.NoError(t, err)
56+
57+
parsedSubject, exp, err := tokenProvider.Validate(alg, myToken, key)
58+
require.NoError(t, err)
59+
require.Equal(t, subject, parsedSubject)
60+
require.Equal(t, exp, exp)
61+
}

0 commit comments

Comments
 (0)