MCPcopy Index your code
hub / github.com/devpew/Muffin-REST-API / isAuthorized

Function isAuthorized

main.go:46–74  ·  view source on GitHub ↗
(endpoint func(http.ResponseWriter, *http.Request))

Source from the content-addressed store, hash-verified

44}
45
46func 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
76func main() {
77 fmt.Println("My Simple Server")

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected