golang-jwt / jwt

Go implementation of JSON Web Tokens (JWT).

Home Page:https://golang-jwt.github.io/jwt/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SigningString produces a string without a signature

vault-thirteen opened this issue · comments

The SigningString method produces a string without a signature. A simple example is following.

import (
	"github.com/golang-jwt/jwt/v5"
)

const (
	WebTokenField_UserId    = "userId"
	WebTokenField_SessionId = "sessionId"
)

func (srv *Server) makeJWToken(userId uint, sessionId uint) (tokenString string, err error) {
	signingMethod := jwt.SigningMethodPS512

	claims := jwt.MapClaims{
		WebTokenField_UserId:    userId,
		WebTokenField_SessionId: sessionId,
	}

	token := jwt.NewWithClaims(signingMethod, claims, nil)

	tokenString, err = token.SigningString()
	if err != nil {
		return "", err
	}

	return tokenString, nil
}

When I use the function with arguments 2 and 2 like this

tokenString, err = srv.makeJWToken(2, 2)

it returns a following token string consisting of only two segments instead of three:

eyJhbGciOiJQUzUxMiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uSWQiOjIsInVzZXJJZCI6Mn0.

Where is the signature ?
What am I doing wrong ?

Thank you.

Most users don't need this method, instead use SignedString()

jwt/token.go

Lines 58 to 63 in 0cb4fa1

// SignedString creates and returns a complete, signed JWT. The token is signed
// using the SigningMethod specified in the token. Please refer to
// https://golang-jwt.github.io/jwt/usage/signing_methods/#signing-methods-and-key-types
// for an overview of the different signing methods and their respective key
// types.
func (t *Token) SignedString(key interface{}) (string, error) {

Thank you !