httpClient.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // +build examples
  2. package main
  3. import (
  4. "encoding/hex"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "os"
  9. //"github.com/pkg/profile"
  10. "gopkg.in/jcmturner/gokrb5.v6/client"
  11. "gopkg.in/jcmturner/gokrb5.v6/config"
  12. "gopkg.in/jcmturner/gokrb5.v6/keytab"
  13. "gopkg.in/jcmturner/gokrb5.v6/testdata"
  14. )
  15. const (
  16. port = ":9080"
  17. kRB5CONF = `[libdefaults]
  18. default_realm = TEST.GOKRB5
  19. dns_lookup_realm = false
  20. dns_lookup_kdc = false
  21. ticket_lifetime = 24h
  22. forwardable = yes
  23. default_tkt_enctypes = aes256-cts-hmac-sha1-96
  24. default_tgs_enctypes = aes256-cts-hmac-sha1-96
  25. [realms]
  26. TEST.GOKRB5 = {
  27. kdc = 127.0.0.1:88
  28. admin_server = 127.0.0.1:749
  29. default_domain = test.gokrb5
  30. }
  31. [domain_realm]
  32. .test.gokrb5 = TEST.GOKRB5
  33. test.gokrb5 = TEST.GOKRB5
  34. `
  35. )
  36. func main() {
  37. //defer profile.Start(profile.TraceProfile).Stop()
  38. // Load the keytab
  39. kb, _ := hex.DecodeString(testdata.TESTUSER1_KEYTAB)
  40. kt, err := keytab.Parse(kb)
  41. if err != nil {
  42. panic(err)
  43. }
  44. // Create the client with the keytab
  45. cl := client.NewClientWithKeytab("testuser1", "TEST.GOKRB5", kt)
  46. // Load the client krb5 config
  47. conf, err := config.NewConfigFromString(kRB5CONF)
  48. if err != nil {
  49. panic(err)
  50. }
  51. addr := os.Getenv("TEST_KDC_ADDR")
  52. if addr != "" {
  53. conf.Realms[0].KDC = []string{addr + ":88"}
  54. }
  55. // Apply the config to the client
  56. cl.WithConfig(conf)
  57. // Log in the client
  58. err = cl.Login()
  59. if err != nil {
  60. panic(err)
  61. }
  62. // Form the request
  63. url := "http://localhost" + port
  64. r, err := http.NewRequest("GET", url, nil)
  65. if err != nil {
  66. panic(err)
  67. }
  68. // Apply the client's auth headers to the request
  69. err = cl.SetSPNEGOHeader(r, "HTTP/host.test.gokrb5")
  70. if err != nil {
  71. panic(err)
  72. }
  73. // Make the request
  74. resp, err := http.DefaultClient.Do(r)
  75. if err != nil {
  76. panic(err)
  77. }
  78. b, err := ioutil.ReadAll(resp.Body)
  79. if err != nil {
  80. panic(err)
  81. }
  82. fmt.Println(string(b))
  83. }