douqin3245 2016-04-06 11:25
浏览 169
已采纳

混合的已命名和未命名函数参数

I have this function for authenticating JWT tokens (not middleware), which says:

package main

import (
"net/http"
"log"
"fmt"
"github.com/dgrijalva/jwt-go"
)

func ValidateToken(w http.ResponseWriter, r *http.Request) *jwt.Token {

//parse token
token, err := jwt.ParseFromRequest(r, func(token *jwt.Token) (interface{}, err error) {
    return VerifyKey, nil
})

//validate token
if err != nil {

    switch err.(type) {

    //something went wrong during validation
    case *jwt.ValidationError:
        vErr := err.(*jwt.ValidationError)

        switch vErr.Errors {

        case jwt.ValidationErrorExpired:
            w.WriteHeader(http.StatusUnauthorized)
            fmt.Fprintln(w, "Token expired")
            return nil

        default:
            w.WriteHeader(http.StatusInternalServerError)
            fmt.Fprintln(w, "Error parsing token")
            log.Printf("Validation error: %v
", err)
            return nil
        }


    //something else went wrong
    default:
        w.WriteHeader(http.StatusInternalServerError)
        fmt.Fprintln(w, "Error parsing token")
        log.Printf("Validation error: %v
", err)
        return nil
    }

}

return token

}

Then, in my handlers, I call this function to get the token and check if it is valid using the Valid property in the JWT Token struct. However, when I run the web server, I get an error on line 13, saying:

Mixed named and unnamed function parameters

Line 13 is the jwt.ParseFromRequest() call. Any thoughts on what I am doing wrong? I am new to Go.

  • 写回答

2条回答 默认 最新

  • douxuanwei1980 2016-04-06 12:16
    关注

    You are defining a function inline (of type jwt.Keyfunc), but not binding it to anything. If VerifyKey is of type jwt.Keyfunc, then you can just change line 13 to

    token, err := jwt.ParseFromRequest(r, VerifyKey)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题