example.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. b, _ := hex.DecodeString(testdata.TESTUSER1_KEYTAB)
  31. kt, _ := keytab.Parse(b)
  32. c, _ := config.NewConfigFromString(testdata.TEST_KRB5CONF)
  33. cl := client.NewClientWithKeytab("testuser1", "TEST.GOKRB5", kt)
  34. cl.WithConfig(c)
  35. httpRequest(s.URL, cl)
  36. b, _ = hex.DecodeString(testdata.TESTUSER2_KEYTAB)
  37. kt, _ = keytab.Parse(b)
  38. c, _ = config.NewConfigFromString(testdata.TEST_KRB5CONF)
  39. cl = client.NewClientWithKeytab("testuser2", "TEST.GOKRB5", kt)
  40. cl.WithConfig(c)
  41. httpRequest(s.URL, cl)
  42. //httpRequest("http://host.test.gokrb5/index.html")
  43. }
  44. func httpRequest(url string, cl client.Client) {
  45. l := log.New(os.Stderr, "GOKRB5 Client: ", log.Ldate|log.Ltime|log.Lshortfile)
  46. err := cl.Login()
  47. if err != nil {
  48. l.Printf("Error on AS_REQ: %v\n", err)
  49. }
  50. cl.EnableAutoSessionRenewal()
  51. r, _ := http.NewRequest("GET", url, nil)
  52. err = cl.SetSPNEGOHeader(r, "HTTP/host.test.gokrb5")
  53. if err != nil {
  54. l.Printf("Error setting client SPNEGO header: %v", err)
  55. }
  56. httpResp, err := http.DefaultClient.Do(r)
  57. if err != nil {
  58. l.Printf("Request error: %v\n", err)
  59. }
  60. fmt.Fprintf(os.Stdout, "Response Code: %v\n", httpResp.StatusCode)
  61. content, _ := ioutil.ReadAll(httpResp.Body)
  62. fmt.Fprintf(os.Stdout, "Response Body:\n%s\n", content)
  63. }
  64. func httpServer() *httptest.Server {
  65. l := log.New(os.Stderr, "GOKRB5 Service: ", log.Ldate|log.Ltime|log.Lshortfile)
  66. b, _ := hex.DecodeString(testdata.HTTP_KEYTAB)
  67. kt, _ := keytab.Parse(b)
  68. th := http.HandlerFunc(testAppHandler)
  69. s := httptest.NewServer(service.SPNEGOKRB5Authenticate(th, kt, l))
  70. return s
  71. }
  72. func testAppHandler(w http.ResponseWriter, r *http.Request) {
  73. w.WriteHeader(http.StatusOK)
  74. ctx := r.Context()
  75. fmt.Fprintf(w, "<html>\nTEST.GOKRB5 Handler\nAuthenticed user: %s\nUser's realm: %s\n</html>", ctx.Value("cname").(string), ctx.Value("crealm").(string))
  76. return
  77. }