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. goidentity "gopkg.in/jcmturner/goidentity.v3"
  10. "gopkg.in/jcmturner/gokrb5.v6/keytab"
  11. "gopkg.in/jcmturner/gokrb5.v6/service"
  12. "gopkg.in/jcmturner/gokrb5.v6/testdata"
  13. )
  14. const (
  15. port = ":9080"
  16. )
  17. func main() {
  18. //defer profile.Start(profile.TraceProfile).Stop()
  19. // Create logger
  20. l := log.New(os.Stderr, "GOKRB5 Service: ", log.Ldate|log.Ltime|log.Lshortfile)
  21. // Load the service's keytab
  22. b, _ := hex.DecodeString(testdata.HTTP_KEYTAB)
  23. kt, _ := keytab.Parse(b)
  24. // Create the application's specific handler
  25. th := http.HandlerFunc(testAppHandler)
  26. // Set up handler mappings wrapping in the SPNEGOKRB5Authenticate handler wrapper
  27. mux := http.NewServeMux()
  28. c := service.NewConfig(kt)
  29. mux.Handle("/", service.SPNEGOKRB5Authenticate(th, c, 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).(goidentity.Identity)
  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. }