Closed
Description
Problem
I set up a simple login and authentication function in my application. It works fine in golang version 1.18.2
but when I updated to 1.18.3
, the ParseWithClaim
function always returns signature error
and it works fine after I used golang version 1.18.2
again.
Environment
OS=Ubuntu 20.04.4 (WSL 2)
Go=1.18.3
IDE=VSCode
github.com/golang-jwt/jwt=v3.2.2+incompatible
github.com/joho/godotenv=v1.4.0
Codes
.env
SECRET_KEY="secret"
jwt.go
func GenerateToken(userEmail string, userType string, userID uint) (string, error) {
var secretKey = os.Getenv("SECRET_KEY")
claims := CustomClaim{
userEmail,
userType,
int(userID),
jwt.StandardClaims{
IssuedAt: jwt.TimeFunc().Unix(),
},
}
setupToken := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
token, err := setupToken.SignedString([]byte(secretKey))
return token, err
}
func ValidateUserToken(tokenString string) (jwt.Claims, string) {
var secretKey = os.Getenv("SECRET_KEY")
highestType := os.Getenv("HIGHEST_TYPE")
commonType := os.Getenv("COMMON_TYPE")
unauthenticatedMessage := "Unauthenticated"
customClaim := &CustomClaim{}
_, err := jwt.ParseWithClaims(tokenString, customClaim, func(t *jwt.Token) (interface{}, error) {
return []byte(secretKey), nil
})
if err != nil {
fmt.Println(err, "error in function ValidateToken in jwt.go")
return nil, unauthenticatedMessage
} else {
if customClaim.UserType == highestType || customClaim.UserType == commonType {
return customClaim, ""
} else {
return nil, unauthenticatedMessage
}
}
}
main.go
func main() {
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Some error occured. Err: %s", err)
} else {
C.Config()
R.NewRoutes().Run()
}
}
Notes
I forgot to copy and past the error printed in the terminal but if I remembered correctly it says signature error
. I printed out the secretKey that was used from .env
and it was the same thing. So, it's not the dotenv
problem in my understanding
Metadata
Assignees
Labels
No labels