httpServer.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // +build examples
  2. package main
  3. import (
  4. "encoding/hex"
  5. "fmt"
  6. "log"
  7. "net/http"
  8. "os"
  9. "github.com/gorilla/sessions"
  10. "github.com/jcmturner/goidentity/v6"
  11. "github.com/jcmturner/gokrb5/v8/keytab"
  12. "github.com/jcmturner/gokrb5/v8/service"
  13. "github.com/jcmturner/gokrb5/v8/spnego"
  14. "github.com/jcmturner/gokrb5/v8/test/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.New()
  26. kt.Unmarshal(b)
  27. // Create the application's specific handler
  28. th := http.HandlerFunc(testAppHandler)
  29. // Set up handler mappings wrapping in the SPNEGOKRB5Authenticate handler wrapper
  30. mux := http.NewServeMux()
  31. mux.Handle("/", spnego.SPNEGOKRB5Authenticate(th, kt, service.Logger(l), service.SessionManager(NewSessionMgr("gokrb5"))))
  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. creds := goidentity.FromHTTPRequestContext(r)
  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. }
  56. type SessionMgr struct {
  57. skey []byte
  58. store sessions.Store
  59. cookieName string
  60. }
  61. func NewSessionMgr(cookieName string) SessionMgr {
  62. skey := []byte("thisistestsecret") // Best practice is to load this key from a secure location.
  63. return SessionMgr{
  64. skey: skey,
  65. store: sessions.NewCookieStore(skey),
  66. cookieName: cookieName,
  67. }
  68. }
  69. func (smgr SessionMgr) Get(r *http.Request, k string) ([]byte, error) {
  70. s, err := smgr.store.Get(r, smgr.cookieName)
  71. if err != nil {
  72. return nil, err
  73. }
  74. if s == nil {
  75. return nil, error.New("nil session")
  76. }
  77. b, ok := s.Values[k].([]byte)
  78. if !ok {
  79. return nil, fmt.Errorf("could not get bytes held in session at %s", k)
  80. }
  81. return b, nil
  82. }
  83. func (smgr SessionMgr) New(w http.ResponseWriter, r *http.Request, k string, v []byte) error {
  84. s, err := smgr.store.New(r, smgr.cookieName)
  85. if err != nil {
  86. return fmt.Errorf("could not get new session from session manager: %v", err)
  87. }
  88. s.Values[k] = v
  89. return s.Save(r, w)
  90. }