example.go 2.6 KB

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