SkyLothar / lua-resty-jwt

JWT For The Great Openresty
Apache License 2.0
513 stars 178 forks source link

Decode secret is not a valid cert\/public key #97

Open waynercheung opened 3 years ago

waynercheung commented 3 years ago

Hi, I've got the "Decode secret is not a valid cert\/public key" error. This is my code:

print("========")
local jwt = require "jwt"
local cjson = require("cjson")

local jwt_token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.OfyWHrVEyPY4JdL5FqWVKdn3Q3tz52Hsd2QK7FLl65hYnO-15bc32ceZTRqbUgLsSnn0nH-hrxM4Es49IFUG6b6eAXMNCAeOiWxtFjuzOVqymx9zwYgHYT4Psk0Furyg6kJFqR5RcYazHkzBL2sbKAQkBp1ln5so3v4YCC4zcI2FaLlvo_jxeJmgQIYZ5amGurzZFvBm_jLrALqzFuXG_YRgUl5_qPGYWThl0rrfdWeqG415uuEstEEjirIPY4j5nfMPJmnsNCpJ0Pm3JMQMazJTL2Uo0sOS-M_NAXzIyNF-53NS0gsswtB5FianU_IRWv4Jm3gr4yghoGIx9_oG2g"

--local public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwewRGFmQrXGUqhAoG8uhZVAZGHTDsgcdmFXu6nPypXJ0A+KDF+c3Z2HUB8syMB9WrL51M0FirJ372MKGOWulQGG2Fvsr9nBxFEIWVMsoIk3HYgiVQNJ+Gnw+2uuunYllyq8NRyReSdN8H4uAekcxEHLTYItKYMo6s6IuVjfIRrbMOqaQuDIM5B5gbyCAd0ODE5ROgw1Kb7N3cewRcQh8JpCNMdHY2xvCVvOwJBxTv+py/2feZGPXmj/aZv15JiISI/JEyKMuDUGVbybx4lNnJNW2MwADgAzZPCVxBy+1pclELx2lPP5HUrzBEESOoNJXxiM68H2sA3pcc7M0SVI7PwIDAQABB"
local public_key = [[MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwewRGFmQrXGUqhAoG8uh
ZVAZGHTDsgcdmFXu6nPypXJ0A+KDF+c3Z2HUB8syMB9WrL51M0FirJ372MKGOWul
QGG2Fvsr9nBxFEIWVMsoIk3HYgiVQNJ+Gnw+2uuunYllyq8NRyReSdN8H4uAekcx
EHLTYItKYMo6s6IuVjfIRrbMOqaQuDIM5B5gbyCAd0ODE5ROgw1Kb7N3cewRcQh8
JpCNMdHY2xvCVvOwJBxTv+py/2feZGPXmj/aZv15JiISI/JEyKMuDUGVbybx4lNn
JNW2MwADgAzZPCVxBy+1pclELx2lPP5HUrzBEESOoNJXxiM68H2sA3pcc7M0SVI7
PwIDAQAB]]

local jwt_obj = jwt:verify(public_key, jwt_token)
ngx.say(cjson.encode(jwt_obj))
return ngx.exit(200)

result:

{"raw_payload":"eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0","raw_header":"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9","reason":"Decode secret is not a valid cert\/public key: MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwewRGFmQrXGUqhAoG8uh\nZVAZGHTDsgcdmFXu6nPypXJ0A+KDF+c3Z2HUB8syMB9WrL51M0FirJ372MKGOWul\nQGG2Fvsr9nBxFEIWVMsoIk3HYgiVQNJ+Gnw+2uuunYllyq8NRyReSdN8H4uAekcx\nEHLTYItKYMo6s6IuVjfIRrbMOqaQuDIM5B5gbyCAd0ODE5ROgw1Kb7N3cewRcQh8\nJpCNMdHY2xvCVvOwJBxTv+py\/2feZGPXmj\/aZv15JiISI\/JEyKMuDUGVbybx4lNn\nJNW2MwADgAzZPCVxBy+1pclELx2lPP5HUrzBEESOoNJXxiM68H2sA3pcc7M0SVI7\nPwIDAQAB","header":{"typ":"JWT","alg":"RS256"},"valid":true,"verified":false,"payload":{"iat":1516239022,"sub":"1234567890","admin":true,"name":"John Doe"},"signature":"OfyWHrVEyPY4JdL5FqWVKdn3Q3tz52Hsd2QK7FLl65hYnO-15bc32ceZTRqbUgLsSnn0nH-hrxM4Es49IFUG6b6eAXMNCAeOiWxtFjuzOVqymx9zwYgHYT4Psk0Furyg6kJFqR5RcYazHkzBL2sbKAQkBp1ln5so3v4YCC4zcI2FaLlvo_jxeJmgQIYZ5amGurzZFvBm_jLrALqzFuXG_YRgUl5_qPGYWThl0rrfdWeqG415uuEstEEjirIPY4j5nfMPJmnsNCpJ0Pm3JMQMazJTL2Uo0sOS-M_NAXzIyNF-53NS0gsswtB5FianU_IRWv4Jm3gr4yghoGIx9_oG2g"}

Is there any wrong with my code ?

learner0811 commented 1 year ago

You should add -----BEGIN PUBLIC KEY-----\\n-----END PUBLIC KEY----- This work for me