Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Trailing comma in JWT's payload #2517

Open
my3rs opened this issue Jan 7, 2025 · 0 comments
Open

[BUG] Trailing comma in JWT's payload #2517

my3rs opened this issue Jan 7, 2025 · 0 comments
Assignees

Comments

@my3rs
Copy link

my3rs commented Jan 7, 2025

Describe the bug
There's a syntax issue in the JWT token's payload: it contains a trailing comma. Here's how the decoded payload appears:

{
  "iat": 1736252478,
  "exp": 1736252838,
  "iss": "NAME",
  "sub": "admin",
}

Expected behavior

Expected decoded payload:

{
  "iat": 1736252478,
  "exp": 1736252838,
  "iss": "NAME",
  "sub": "admin"
}

To Reproduce

type JWTService struct {
	signer        *jwt.Signer
	verifier      *jwt.Verifier
}

func newJWTService() *JWTService {
	private, public := jwt.MustLoadRSA(config.PrivateKey, config.PublicKey)

	return &JWTService{
		signer:        jwt.NewSigner(jwt.RS256, private, config.AccessTokenMaxAge),
		verifier:      jwt.NewVerifier(jwt.RS256, public),
	}
}

func (s *JWTService) GenerateTokenPair(user model.User) (jwt.TokenPair, error) {
	now := time.Now()

	// Create refresh claims with user ID as subject
	refreshClaims := jwt.Claims{
		Subject:  fmt.Sprintf("%s", user.Username),
		Issuer:   "NAME",
		IssuedAt: now.Unix(),
		Expiry:   now.Add(time.Second * s.config.RefreshTokenMaxAge).Unix(),
	}

	// Create access claims with user details
	accessClaims := jwt.Claims{
		Subject:  fmt.Sprintf("%s", user.Username),
		Issuer:   "NAME",
		IssuedAt: now.Unix(),
		Expiry:   now.Add(time.Second * s.config.AccessTokenMaxAge).Unix(),
	}

	tokenPair, err := s.signer.NewTokenPair(accessClaims, refreshClaims, s.config.RefreshTokenMaxAge)
	if err != nil {
		return jwt.TokenPair{}, err
	}

	fmt.Printf("access token: %s\n", tokenPair.AccessToken)

	return tokenPair, nil
}

iris.Version

  • v12.2.11
@my3rs my3rs changed the title [BUG] [BUG] Trailing comma in JWT's payload Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants