example.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package main
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "github.com/jcmturner/gokrb5/client"
  6. "github.com/jcmturner/gokrb5/config"
  7. "github.com/jcmturner/gokrb5/keytab"
  8. "github.com/jcmturner/gokrb5/service"
  9. "github.com/jcmturner/gokrb5/testdata"
  10. "io/ioutil"
  11. "log"
  12. "net/http"
  13. "net/http/httptest"
  14. "os"
  15. )
  16. /*
  17. These examples have the following prerequisites:
  18. * Hashicorp Vagrant
  19. * VirtualBox
  20. The test environment relies upon a host only network configured within VirtualBox with a CIDR range of 10.80.0.0/16
  21. If this does not suit your setup then you will need to set the IP addresses for the private_network in the Vagrantfiles to something that suits you.
  22. You will also need to update the IPs referenced in the testdata/test_vectors.go file in the TEST_KRB5CONF constant.
  23. Before running execute the following commands (note that the KDC can take a long time to start up):
  24. cd $GOPATH/src/github.com/jcmturner/gokrb5/testenv/krb5kdc-vagrant && vagrant up
  25. cd $GOPATH/src/github.com/jcmturner/gokrb5/testenv/krbhttp-vagrant && vagrant up
  26. */
  27. func main() {
  28. s := httpServer()
  29. defer s.Close()
  30. httpRequest(s.URL)
  31. //httpRequest2(s.URL)
  32. //httpRequest("http://host.test.gokrb5/index.html")
  33. }
  34. func httpRequest(url string) {
  35. l := log.New(os.Stderr, "GOKRB5 Client: ", log.Ldate|log.Ltime|log.Lshortfile)
  36. b, err := hex.DecodeString(testdata.TESTUSER1_KEYTAB)
  37. kt, _ := keytab.Parse(b)
  38. c, _ := config.NewConfigFromString(testdata.TEST_KRB5CONF)
  39. cl := client.NewClientWithKeytab("testuser1", "TEST.GOKRB5", kt)
  40. cl.WithConfig(c)
  41. err = cl.Login()
  42. if err != nil {
  43. l.Printf("Error on AS_REQ: %v\n", err)
  44. }
  45. cl.EnableAutoSessionRenewal()
  46. r, _ := http.NewRequest("GET", url, nil)
  47. err = cl.SetSPNEGOHeader(r, "HTTP/host.test.gokrb5")
  48. if err != nil {
  49. l.Printf("Error setting client SPNEGO header: %v", err)
  50. }
  51. httpResp, err := http.DefaultClient.Do(r)
  52. if err != nil {
  53. l.Printf("Request error: %v\n", err)
  54. }
  55. fmt.Fprintf(os.Stdout, "Response Code: %v\n", httpResp.StatusCode)
  56. content, _ := ioutil.ReadAll(httpResp.Body)
  57. fmt.Fprintf(os.Stdout, "Response Body:\n%s\n", content)
  58. }
  59. func httpRequest2(url string) {
  60. l := log.New(os.Stderr, "GOKRB5 Client: ", log.Ldate|log.Ltime|log.Lshortfile)
  61. b, err := hex.DecodeString(testdata.TESTUSER2_KEYTAB)
  62. kt, _ := keytab.Parse(b)
  63. c, _ := config.NewConfigFromString(testdata.TEST_KRB5CONF)
  64. cl := client.NewClientWithKeytab("testuser2", "TEST.GOKRB5", kt)
  65. cl.WithConfig(c)
  66. err = cl.Login()
  67. if err != nil {
  68. l.Printf("Error on AS_REQ: %v\n", err)
  69. }
  70. cl.EnableAutoSessionRenewal()
  71. r, _ := http.NewRequest("GET", url, nil)
  72. err = cl.SetSPNEGOHeader(r, "HTTP/host.test.gokrb5")
  73. if err != nil {
  74. l.Printf("Error setting client SPNEGO header: %v", err)
  75. }
  76. httpResp, err := http.DefaultClient.Do(r)
  77. if err != nil {
  78. l.Printf("Request error: %v\n", err)
  79. }
  80. fmt.Fprintf(os.Stdout, "Response Code: %v\n", httpResp.StatusCode)
  81. content, _ := ioutil.ReadAll(httpResp.Body)
  82. fmt.Fprintf(os.Stdout, "Response Body:\n%s\n", content)
  83. }
  84. func httpServer() *httptest.Server {
  85. l := log.New(os.Stderr, "GOKRB5 Service: ", log.Ldate|log.Ltime|log.Lshortfile)
  86. b, _ := hex.DecodeString(testdata.HTTP_KEYTAB)
  87. kt, _ := keytab.Parse(b)
  88. th := http.HandlerFunc(testAppHandler)
  89. s := httptest.NewServer(service.SPNEGOKRB5Authenticate(th, kt, l))
  90. return s
  91. }
  92. func testAppHandler(w http.ResponseWriter, r *http.Request) {
  93. w.WriteHeader(http.StatusOK)
  94. ctx := r.Context()
  95. fmt.Fprintf(w, "<html>\nTEST.GOKRB5 Handler\nAuthenticed user: %s\nUser's realm: %s\n</html>", ctx.Value("cname").(string), ctx.Value("crealm").(string))
  96. return
  97. }