httpServer.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // +build examples
  2. package main
  3. import (
  4. "encoding/hex"
  5. "fmt"
  6. "github.com/jcmturner/gokrb5/keytab"
  7. "github.com/jcmturner/gokrb5/service"
  8. "github.com/jcmturner/gokrb5/testdata"
  9. "log"
  10. "net/http"
  11. "os"
  12. )
  13. func main() {
  14. // Create logger
  15. l := log.New(os.Stderr, "GOKRB5 Service: ", log.Ldate|log.Ltime|log.Lshortfile)
  16. // Load the service's keytab
  17. b, _ := hex.DecodeString(testdata.HTTP_KEYTAB)
  18. kt, _ := keytab.Parse(b)
  19. // Create the application's specific handler
  20. th := http.HandlerFunc(testAppHandler)
  21. // Set up handler mappings wrapping in the SPNEGOKRB5Authenticate handler wrapper
  22. mux := http.NewServeMux()
  23. mux.Handle("/", service.SPNEGOKRB5Authenticate(th, kt, l))
  24. // Start up the web server
  25. log.Fatal(http.ListenAndServe(":9080", mux))
  26. }
  27. // Simple application specific handler
  28. func testAppHandler(w http.ResponseWriter, r *http.Request) {
  29. w.WriteHeader(http.StatusOK)
  30. ctx := r.Context()
  31. fmt.Fprintf(w, "<html>\nTEST.GOKRB5 Handler\nAuthenticed user: %s\nUser's realm: %s\n</html>", ctx.Value("cname").(string), ctx.Value("crealm").(string))
  32. return
  33. }