1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| func TokenMiddleware(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { tokenStr := r.Header.Get("authorization")
if tokenStr == "" { handler.RespondError(w, http.StatusUnauthorized, handler.New(false, handler.Message{Code: "0401", Details: "Unauthorized"}, nil, handler.Meta{})) }
token, _ := jwt.Parse(tokenStr, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { handler.RespondError(w, http.StatusUnauthorized, handler.New(false, handler.Message{Code: "0401", Details: "Unauthorized"}, nil, handler.Meta{})) }
return []byte("YuSecret"), nil })
if !token.Valid { handler.RespondError(w, http.StatusUnauthorized, handler.New(false, handler.Message{Code: "0401", Details: "Unauthorized"}, nil, handler.Meta{})) return }
next.ServeHTTP(w, r)
}
|