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.v7/client"
  11. "gopkg.in/jcmturner/gokrb5.v7/config"
  12. "gopkg.in/jcmturner/gokrb5.v7/keytab"
  13. "gopkg.in/jcmturner/gokrb5.v7/spnego"
  14. "gopkg.in/jcmturner/gokrb5.v7/test/testdata"
  15. )
  16. const (
  17. port = ":9080"
  18. kRB5CONF = `[libdefaults]
  19. default_realm = TEST.GOKRB5
  20. dns_lookup_realm = false
  21. dns_lookup_kdc = false
  22. ticket_lifetime = 24h
  23. forwardable = yes
  24. default_tkt_enctypes = aes256-cts-hmac-sha1-96
  25. default_tgs_enctypes = aes256-cts-hmac-sha1-96
  26. [realms]
  27. TEST.GOKRB5 = {
  28. kdc = 127.0.0.1:88
  29. admin_server = 127.0.0.1:749
  30. default_domain = test.gokrb5
  31. }
  32. [domain_realm]
  33. .test.gokrb5 = TEST.GOKRB5
  34. test.gokrb5 = TEST.GOKRB5
  35. `
  36. )
  37. func main() {
  38. //defer profile.Start(profile.TraceProfile).Stop()
  39. // Load the keytab
  40. kb, _ := hex.DecodeString(testdata.TESTUSER1_KEYTAB)
  41. kt := keytab.New()
  42. err := kt.Unmarshal(kb)
  43. if err != nil {
  44. panic(err)
  45. }
  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. // Create the client with the keytab
  56. cl := client.NewClientWithKeytab("testuser1", "TEST.GOKRB5", kt, 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 = spnego.SetSPNEGOHeader(cl, 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. }