MCPcopy Create free account
hub / github.com/cloud-native-go/examples / verifyHandler

Function verifyHandler

ch12/jwt/verify.go:26–44  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

24)
25
26func verifyHandler(w http.ResponseWriter, r *http.Request) {
27 header := r.Header.Get("Authorization")
28 token := strings.TrimPrefix(header, "Bearer ")
29
30 if header == "" || token == header {
31 log.Println("Missing authorization header")
32 w.WriteHeader(http.StatusUnauthorized)
33 return
34 }
35
36 claims, err := verifyToken(token)
37 if err != nil {
38 log.Println("Invalid authorization token:", err)
39 w.WriteHeader(http.StatusUnauthorized)
40 return
41 }
42
43 fmt.Fprintf(w, "Welcome back, %s!\n", claims.Name)
44}

Callers 3

TestHandlersFunction · 0.85
TestVerifyNoHeaderFunction · 0.85
TestVerifyInvalidHeaderFunction · 0.85

Calls 2

verifyTokenFunction · 0.85
GetMethod · 0.65

Tested by 3

TestHandlersFunction · 0.68
TestVerifyNoHeaderFunction · 0.68
TestVerifyInvalidHeaderFunction · 0.68