title: Go: validate subsequent http requests with authenticated client via certificate
body: <p>I'm currently writing a HTTP server(net/http) which hosts multiple endpoints and requires client authentication(step 1) before accessing these endpoints. Upon successful authentication, the server issues a short-lived token which the client then use to access these endpoints. when the client sends the token(via HTTP Header), have a piece of code at the start of every handler function to check the client is authenticated and presented token is valid. I'm looking for a hook/wrapper which can intercept and validate the client instead of calling <code>isAuthenticated(r)</code> from every endpoint function.</p>
  
  <pre><code>func getMyEndpoint(w http.ResponseWriter, r *http.Request) {
          if valid := isAuthenticated(r); !valid {
              w.WriteHeader(http.StatusUnauthorized)
              io.WriteString(w, "Invalid token or Client not authenticated."
              return
          }
          ...
  }
  
  func server() {
  
          http.HandleFunc("/login", clientLoginWithCertAuth)
          http.HandleFunc("/endpoint1", getMyEndpoint)
          http.HandleFunc("/endpoint2", putMyEndpoint)
  
          server := &amp;http.Server{
                  Addr: ":443",
                  TLSConfig: &amp;tls.Config{
                          ClientCAs:  caCertPool,
                          ClientAuth: tls.VerifyClientCertIfGiven,
                  },
          }
  
          if err := server.ListenAndServeTLS("cert.pem", "key.pem"); err != nil {
              panic(err)
          }
  }
  </code></pre>