Debian Codegraph @

examples/httpClient.go

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