httpServer.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // +build examples
  2. package main
  3. import (
  4. "encoding/hex"
  5. "fmt"
  6. "log"
  7. "net/http"
  8. "os"
  9. //"github.com/pkg/profile"
  10. "gopkg.in/jcmturner/gokrb5.v5/credentials"
  11. "gopkg.in/jcmturner/gokrb5.v5/keytab"
  12. "gopkg.in/jcmturner/gokrb5.v5/service"
  13. "gopkg.in/jcmturner/gokrb5.v5/testdata"
  14. )
  15. const (
  16. port = ":9080"
  17. )
  18. func main() {
  19. //defer profile.Start(profile.TraceProfile).Stop()
  20. // Create logger
  21. l := log.New(os.Stderr, "GOKRB5 Service: ", log.Ldate|log.Ltime|log.Lshortfile)
  22. // Load the service's keytab
  23. b, _ := hex.DecodeString(testdata.HTTP_KEYTAB)
  24. kt, _ := keytab.Parse(b)
  25. // Create the application's specific handler
  26. th := http.HandlerFunc(testAppHandler)
  27. // Set up handler mappings wrapping in the SPNEGOKRB5Authenticate handler wrapper
  28. mux := http.NewServeMux()
  29. mux.Handle("/", service.SPNEGOKRB5Authenticate(th, kt, "", false, l))
  30. // Start up the web server
  31. log.Fatal(http.ListenAndServe(port, mux))
  32. }
  33. // Simple application specific handler
  34. func testAppHandler(w http.ResponseWriter, r *http.Request) {
  35. w.WriteHeader(http.StatusOK)
  36. ctx := r.Context()
  37. creds := ctx.Value(service.CTXKeyCredentials).(credentials.Credentials)
  38. fmt.Fprintf(w,
  39. `<html>
  40. <h1>GOKRB5 Handler</h1>
  41. <ul>
  42. <li>Authenticed user: %s</li>
  43. <li>User's realm: %s</li>
  44. <li>Authn time: %v</li>
  45. <li>Session ID: %s</li>
  46. <ul>
  47. </html>`,
  48. creds.UserName(),
  49. creds.Domain(),
  50. creds.AuthTime(),
  51. creds.SessionID(),
  52. )
  53. return
  54. }