httpServer.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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.v7/keytab"
  11. "gopkg.in/jcmturner/gokrb5.v7/service"
  12. "gopkg.in/jcmturner/gokrb5.v7/spnego"
  13. "gopkg.in/jcmturner/gokrb5.v7/test/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.New()
  25. kt.Unmarshal(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. mux.Handle("/", spnego.SPNEGOKRB5Authenticate(th, kt, service.Logger(l)))
  31. // Start up the web server
  32. log.Fatal(http.ListenAndServe(port, mux))
  33. }
  34. // Simple application specific handler
  35. func testAppHandler(w http.ResponseWriter, r *http.Request) {
  36. w.WriteHeader(http.StatusOK)
  37. ctx := r.Context()
  38. creds := ctx.Value(spnego.CTXKeyCredentials).(goidentity.Identity)
  39. fmt.Fprintf(w,
  40. `<html>
  41. <h1>GOKRB5 Handler</h1>
  42. <ul>
  43. <li>Authenticed user: %s</li>
  44. <li>User's realm: %s</li>
  45. <li>Authn time: %v</li>
  46. <li>Session ID: %s</li>
  47. <ul>
  48. </html>`,
  49. creds.UserName(),
  50. creds.Domain(),
  51. creds.AuthTime(),
  52. creds.SessionID(),
  53. )
  54. return
  55. }