dgrijalva / jwt-go

ARCHIVE - Golang implementation of JSON Web Tokens (JWT). This project is now maintained at:
https://github.com/golang-jwt/jwt
MIT License
10.78k stars 994 forks source link

Getting 'key is of invalid type' when trying to ParseWithClaims #283

Closed jennyphan closed 6 years ago

jennyphan commented 6 years ago

I am trying to parse a JWT Token and I keep getting 'key is of invalid type'. I think that the encrypted key that I am passing in is invalid but I have tried so many different key values. I am using Azure B2C and I retrieved the public key but that did not work. I also used the kid value in the header and the secret key created in Azure for my registered apps. I am unable to resolve this issue. I am using RSA256. Variations of the key that I have tried

encryptedKey := "-----BEGIN CERTIFICATE-----xxxxxxxxxxxxxxxxxxxxxx-----END CERTIFICATE-----" where xxxxxx is public key from Azure encryptedKey := kid value from request header encryptedKey := secret key created in Azure Portal for my web api

token2, err := jwt.ParseWithClaims(token.AccessToken, &User{}, func(token *jwt.Token) (interface{}, error) {
    return []byte(encryptedKey), nil
})
jennyphan commented 6 years ago

This was a type issue with golang, I converted to the correct type and it is working.

ernsheong commented 6 years ago

@jennyphan what was the type issue?

jennyphan commented 6 years ago

I had to convert to convert to rsa.PublicKey type. I used that ParseWithClaims and it finally worked. It seemed to be an issue with returning the right type.

rsaPublicKey := -----BEGIN CERTIFICATE----- xxxx -----END CERTIFICATE-----

block, _ := pem.Decode([]byte(rsaPublicKey))
var cert *x509.Certificate
cert, _ = x509.ParseCertificate(block.Bytes)
pub := cert.PublicKey.(*rsa.PublicKey)

token2, err := parser.ParseWithClaims(tokenB, &User{}, func(token jwt.Token) (interface{}, error) { // Don't forget to validate the alg is what you expect: if _, ok := token.Method.(jwt.SigningMethodRSA); !ok { return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"]) }

    return pub, nil
})