example.go 2.9 KB

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