普通网友 2017-06-29 03:43
浏览 497
已采纳

jwt密钥无效

I am following this example https://www.youtube.com/watch?v=eVlxuST7dCA to make a jwt auth. When I run the code below I get "Key is invalid" error. WHen I try printing tokenString it is empty. The github to this sample is https://github.com/potatogopher/jwt-go-example/blob/master/server.go Why am I getting invalid error?

var privateKey []byte
privateKey, err := ioutil.ReadFile("demo.rsa")

token := jwt.New(jwt.GetSigningMethod("RS256"))
tokenString, err := token.SignedString(privateKey)

fmt.Println("TOKEN:", tokenString)
  • 写回答

2条回答 默认 最新

  • duanjianxu4288 2017-06-30 06:22
    关注

    I think the example code you're referring to uses an outdated API of jwt-go. The RS256 signing method requires the key to be a rsa.PrivateKey and not a byte buffer. This means, that the private key first has to be parsed using the jwt.ParseRSAPrivateKeyFromPEMfunction.

    I've updated your example below:

    func main() {
        tokenString, err := createSignedTokenString()
        if err != nil {
            panic(err)
        }
        fmt.Printf("Signed token string:
    %v
    ", tokenString)
    
        token, err := parseTokenFromSignedTokenString(tokenString)
        if err != nil {
            panic(err)
        }
        fmt.Printf("Parsed token valid = %v, raw token:
    %v
    ", token.Valid, token.Raw)
    }
    
    func createSignedTokenString() (string, error) {
        privateKey, err := ioutil.ReadFile("demo.rsa")
        if err != nil {
            return "", fmt.Errorf("error reading private key file: %v
    ", err)
        }
    
        key, err := jwt.ParseRSAPrivateKeyFromPEM(privateKey)
        if err != nil {
            return "", fmt.Errorf("error parsing RSA private key: %v
    ", err)
        }
    
        token := jwt.New(jwt.SigningMethodRS256)
        tokenString, err := token.SignedString(key)
        if err != nil {
            return "", fmt.Errorf("error signing token: %v
    ", err)
        }
    
        return tokenString, nil
    }
    
    func parseTokenFromSignedTokenString(tokenString string) (*jwt.Token, error) {
        publicKey, err := ioutil.ReadFile("demo.rsa.pub")
        if err != nil {
            return nil, fmt.Errorf("error reading public key file: %v
    ", err)
        }
    
        key, err := jwt.ParseRSAPublicKeyFromPEM(publicKey)
        if err != nil {
            return nil, fmt.Errorf("error parsing RSA public key: %v
    ", err)
        }
    
        parsedToken, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
            if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
                return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
            }
            return key, nil
        })
        if err != nil {
            return nil, fmt.Errorf("error parsing token: %v", err)
        }
    
        return parsedToken, nil
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 绿盟安全扫描--检测到目标站点存在javascript框架库漏洞
  • ¥30 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包