httpServer.go 1.4 KB

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