| 44 | } |
| 45 | |
| 46 | func isAuthorized(endpoint func(http.ResponseWriter, *http.Request)) http.Handler { |
| 47 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 48 | |
| 49 | w.Header().Set("Connection", "close") |
| 50 | defer r.Body.Close() |
| 51 | |
| 52 | if r.Header["Token"] != nil { |
| 53 | token, err := jwt.Parse(r.Header["Token"][0], func(token *jwt.Token) (interface{}, error) { |
| 54 | if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { |
| 55 | return nil, fmt.Errorf("There was an error") |
| 56 | } |
| 57 | return mySigningKey, nil |
| 58 | }) |
| 59 | |
| 60 | if err != nil { |
| 61 | w.WriteHeader(http.StatusForbidden) |
| 62 | w.Header().Add("Content-Type", "application/json") |
| 63 | return |
| 64 | } |
| 65 | |
| 66 | if token.Valid { |
| 67 | endpoint(w, r) |
| 68 | } |
| 69 | |
| 70 | } else { |
| 71 | fmt.Fprintf(w, "Not Authorized") |
| 72 | } |
| 73 | }) |
| 74 | } |
| 75 | |
| 76 | func main() { |
| 77 | fmt.Println("My Simple Server") |