httpServer.go 1.2 KB

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