Skip to content

Commit

Permalink
Add test cases for malformed jwt fix
Browse files Browse the repository at this point in the history
Signed-off-by: ArkaSaha30 <arkasaha30@gmail.com>
  • Loading branch information
ArkaSaha30 committed Apr 7, 2023
1 parent 6ad19a7 commit 46b6da0
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions server/auth/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import (
"context"
"fmt"
"testing"
"time"

jwt "github.com/golang-jwt/jwt/v4"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -155,10 +157,43 @@ func testJWTNoUsernameRevisionInfo(t *testing.T, opts map[string]string, flag st

ctx := context.TODO()

ai, ok := jwt.info(ctx, "aaa", 120)
token, aerr := jwt.testNoDataAssign(ctx)
if aerr != nil {
t.Fatalf("%#v", aerr)
}

ai, ok := jwt.info(ctx, token, 120)
if ok || ai != nil {
t.Fatalf("expected aaa to fail to authenticate, got %+v", ai)
t.Fatalf("expected empty token to fail to authenticate, got %+v", ai)
}
}

func (t *tokenJWT) testNoDataAssign(ctx context.Context) (string, error) {
if t.verifyOnly {
return "", ErrVerifyOnly
}

// Future work: let a jwt token include permission information would be useful for
// permission checking in proxy side.
tk := jwt.NewWithClaims(t.signMethod,
jwt.MapClaims{
"exp": time.Now().Add(t.ttl).Unix(),
})

token, err := tk.SignedString(t.key)
if err != nil {
t.lg.Debug(
"failed to sign a JWT token",
zap.Error(err),
)
return "", err
}

t.lg.Debug(
"created/assigned a new JWT token",
zap.String("token", token),
)
return token, err
}

func TestJWTBad(t *testing.T) {
Expand Down

0 comments on commit 46b6da0

Please sign in to comment.