example.go 3.2 KB

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