httpServer.go 1.0 KB

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