瀏覽代碼

run example

Jonathan Turner 8 年之前
父節點
當前提交
891246e655
共有 4 個文件被更改,包括 15 次插入25 次删除
  1. 1 0
      .travis.yml
  2. 6 6
      examples/example-AD.go
  3. 3 16
      examples/example.go
  4. 5 3
      examples/httpServer.go

+ 1 - 0
.travis.yml

@@ -44,6 +44,7 @@ script:
   #- golint -set_exit_status $(go list ./...) # golint to be added
   - go test -v -race -tags="integration dns" ./... # Run all the tests with the race detector enabled and integration tests
   - GOARCH=386 go test -v -tags="integration dns" ./... # 32bit tests
+  - go run -tags="examples" examples/example.go
 
 addons:
   hosts:

+ 6 - 6
examples/example-AD.go

@@ -24,15 +24,15 @@ func main() {
 
 	b, _ := hex.DecodeString(testdata.TESTUSER1_KEYTAB)
 	kt, _ := keytab.Parse(b)
-	c, _ := config.NewConfigFromString(testdata.TEST_KRB5CONF_AD)
-	cl := client.NewClientWithKeytab("testuser1", "TEST.GOKRB5", kt)
+	c, _ := config.NewConfigFromString(testdata.TEST_KRB5CONF)
+	cl := client.NewClientWithKeytab("testuser1", "USER.GOKRB5", kt)
 	cl.WithConfig(c)
 	httpRequest(s.URL, cl)
 
 	b, _ = hex.DecodeString(testdata.TESTUSER2_KEYTAB)
 	kt, _ = keytab.Parse(b)
-	c, _ = config.NewConfigFromString(testdata.TEST_KRB5CONF_AD)
-	cl = client.NewClientWithKeytab("testuser2", "TEST.GOKRB5", kt)
+	c, _ = config.NewConfigFromString(testdata.TEST_KRB5CONF)
+	cl = client.NewClientWithKeytab("testuser2", "USER.GOKRB5", kt)
 	cl.WithConfig(c)
 	httpRequest(s.URL, cl)
 
@@ -47,7 +47,7 @@ func httpRequest(url string, cl client.Client) {
 		l.Printf("Error on AS_REQ: %v\n", err)
 	}
 	r, _ := http.NewRequest("GET", url, nil)
-	err = cl.SetSPNEGOHeader(r, "HTTP/host.test.gokrb5")
+	err = cl.SetSPNEGOHeader(r, "HTTP/host.res.gokrb5")
 	if err != nil {
 		l.Printf("Error setting client SPNEGO header: %v", err)
 	}
@@ -65,7 +65,7 @@ func httpServer() *httptest.Server {
 	b, _ := hex.DecodeString(testdata.SYSHTTP_KEYTAB)
 	kt, _ := keytab.Parse(b)
 	th := http.HandlerFunc(testAppHandler)
-	s := httptest.NewServer(service.SPNEGOKRB5Authenticate(th, kt, "sysHTTP", l))
+	s := httptest.NewServer(service.SPNEGOKRB5Authenticate(th, kt, "sysHTTP", false, l))
 	return s
 }
 

+ 3 - 16
examples/example.go

@@ -18,19 +18,6 @@ import (
 	"os"
 )
 
-/*
-These examples have the following prerequisites:
-* Hashicorp Vagrant
-* VirtualBox
-
-The test environment relies upon a host only network configured within VirtualBox with a CIDR range of 10.80.0.0/16
-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.
-You will also need to update the IPs referenced in the testdata/test_vectors.go file in the TEST_KRB5CONF constant.
-
-Before running execute the following commands (note that the KDC can take a long time to start up):
-cd $GOPATH/src/gopkg.in/jcmturner/gokrb5.v5/testenv/krb5kdc-vagrant && vagrant up
-cd $GOPATH/src/gopkg.in/jcmturner/gokrb5.v5/testenv/krbhttp-vagrant && vagrant up
-*/
 func main() {
 	s := httpServer()
 	defer s.Close()
@@ -78,15 +65,15 @@ func httpServer() *httptest.Server {
 	b, _ := hex.DecodeString(testdata.HTTP_KEYTAB)
 	kt, _ := keytab.Parse(b)
 	th := http.HandlerFunc(testAppHandler)
-	s := httptest.NewServer(service.SPNEGOKRB5Authenticate(th, kt, "", l))
+	s := httptest.NewServer(service.SPNEGOKRB5Authenticate(th, kt, "", false, l))
 	return s
 }
 
 func testAppHandler(w http.ResponseWriter, r *http.Request) {
 	ctx := r.Context()
 	fmt.Fprint(w, "<html>\n<p><h1>TEST.GOKRB5 Handler</h1></p>\n")
-	if validuser, ok := ctx.Value(service.CTXKey_Authenticated).(bool); ok && validuser {
-		if creds, ok := ctx.Value(service.CTXKey_Credentials).(credentials.Credentials); ok {
+	if validuser, ok := ctx.Value(service.CTXKeyAuthenticated).(bool); ok && validuser {
+		if creds, ok := ctx.Value(service.CTXKeyCredentials).(credentials.Credentials); ok {
 			fmt.Fprintf(w, "<ul><li>Authenticed user: %s</li>\n", creds.Username)
 			fmt.Fprintf(w, "<li>User's realm: %s</li></ul>\n", creds.Realm)
 		}

+ 5 - 3
examples/httpServer.go

@@ -1,4 +1,4 @@
-// +build examples
+//// +build examples
 
 package main
 
@@ -27,7 +27,7 @@ func main() {
 
 	// Set up handler mappings wrapping in the SPNEGOKRB5Authenticate handler wrapper
 	mux := http.NewServeMux()
-	mux.Handle("/", service.SPNEGOKRB5Authenticate(th, kt, "", l))
+	mux.Handle("/", service.SPNEGOKRB5Authenticate(th, kt, "", false, l))
 
 	// Start up the web server
 	log.Fatal(http.ListenAndServe(":9080", mux))
@@ -37,6 +37,8 @@ func main() {
 func testAppHandler(w http.ResponseWriter, r *http.Request) {
 	w.WriteHeader(http.StatusOK)
 	ctx := r.Context()
-	fmt.Fprintf(w, "<html>\nTEST.GOKRB5 Handler\nAuthenticed user: %s\nUser's realm: %s\n</html>", ctx.Value(service.CREDENTIALS_CTXKEY).(credentials.Credentials).Username, ctx.Value(service.CREDENTIALS_CTXKEY).(credentials.Credentials).Realm)
+	fmt.Fprintf(w, "<html>\nTEST.GOKRB5 Handler\nAuthenticed user: %s\nUser's realm: %s\n</html>",
+		ctx.Value(service.CTXKeyCredentials).(credentials.Credentials).Username,
+		ctx.Value(service.CTXKeyCredentials).(credentials.Credentials).Realm)
 	return
 }