example-AD.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // +build examples
  2. package main
  3. import (
  4. "encoding/hex"
  5. "fmt"
  6. "gopkg.in/jcmturner/gokrb5.v4/client"
  7. "gopkg.in/jcmturner/gokrb5.v4/config"
  8. "gopkg.in/jcmturner/gokrb5.v4/credentials"
  9. "gopkg.in/jcmturner/gokrb5.v4/keytab"
  10. "gopkg.in/jcmturner/gokrb5.v4/service"
  11. "gopkg.in/jcmturner/gokrb5.v4/testdata"
  12. "io/ioutil"
  13. "log"
  14. "net/http"
  15. "net/http/httptest"
  16. "os"
  17. )
  18. func main() {
  19. s := httpServer()
  20. defer s.Close()
  21. b, _ := hex.DecodeString(testdata.TESTUSER1_KEYTAB)
  22. kt, _ := keytab.Parse(b)
  23. c, _ := config.NewConfigFromString(testdata.TEST_KRB5CONF_AD)
  24. cl := client.NewClientWithKeytab("testuser1", "TEST.GOKRB5", kt)
  25. cl.WithConfig(c)
  26. httpRequest(s.URL, cl)
  27. b, _ = hex.DecodeString(testdata.TESTUSER2_KEYTAB)
  28. kt, _ = keytab.Parse(b)
  29. c, _ = config.NewConfigFromString(testdata.TEST_KRB5CONF_AD)
  30. cl = client.NewClientWithKeytab("testuser2", "TEST.GOKRB5", kt)
  31. cl.WithConfig(c)
  32. httpRequest(s.URL, cl)
  33. //httpRequest("http://host.test.gokrb5/index.html")
  34. }
  35. func httpRequest(url string, cl client.Client) {
  36. l := log.New(os.Stderr, "GOKRB5 Client: ", log.Ldate|log.Ltime|log.Lshortfile)
  37. err := cl.Login()
  38. if err != nil {
  39. l.Printf("Error on AS_REQ: %v\n", err)
  40. }
  41. r, _ := http.NewRequest("GET", url, nil)
  42. err = cl.SetSPNEGOHeader(r, "HTTP/host.test.gokrb5")
  43. if err != nil {
  44. l.Printf("Error setting client SPNEGO header: %v", err)
  45. }
  46. httpResp, err := http.DefaultClient.Do(r)
  47. if err != nil {
  48. l.Printf("Request error: %v\n", err)
  49. }
  50. fmt.Fprintf(os.Stdout, "Response Code: %v\n", httpResp.StatusCode)
  51. content, _ := ioutil.ReadAll(httpResp.Body)
  52. fmt.Fprintf(os.Stdout, "Response Body:\n%s\n", content)
  53. }
  54. func httpServer() *httptest.Server {
  55. l := log.New(os.Stderr, "GOKRB5 Service: ", log.Ldate|log.Ltime|log.Lshortfile)
  56. b, _ := hex.DecodeString(testdata.SYSHTTP_KEYTAB)
  57. kt, _ := keytab.Parse(b)
  58. th := http.HandlerFunc(testAppHandler)
  59. s := httptest.NewServer(service.SPNEGOKRB5Authenticate(th, kt, "sysHTTP", l))
  60. return s
  61. }
  62. func testAppHandler(w http.ResponseWriter, r *http.Request) {
  63. ctx := r.Context()
  64. fmt.Fprint(w, "<html>\n<p><h1>TEST.GOKRB5 Handler</h1></p>\n")
  65. if validuser, ok := ctx.Value(service.CTXKeyAuthenticated).(bool); ok && validuser {
  66. if creds, ok := ctx.Value(service.CTXKeyCredentials).(credentials.Credentials); ok {
  67. fmt.Fprintf(w, "<ul><li>Authenticed user: %s</li>\n", creds.UserName())
  68. fmt.Fprintf(w, "<li>User's realm: %s</li>\n", creds.Domain())
  69. fmt.Fprint(w, "<li>Authz Attributes (Group Memberships):</li><ul>\n")
  70. for _, s := range creds.AuthzAttributes() {
  71. fmt.Fprintf(w, "<li>%v</li>\n", s)
  72. }
  73. fmt.Fprint(w, "</ul>\n")
  74. if ADCreds, ok := creds.Attributes[credentials.AttributeKeyADCredentials].(credentials.ADCredentials); ok {
  75. // Now access the fields of the ADCredentials struct. For example:
  76. fmt.Fprintf(w, "<li>EffectiveName: %v</li>\n", ADCreds.EffectiveName)
  77. fmt.Fprintf(w, "<li>FullName: %v</li>\n", ADCreds.FullName)
  78. fmt.Fprintf(w, "<li>UserID: %v</li>\n", ADCreds.UserID)
  79. fmt.Fprintf(w, "<li>PrimaryGroupID: %v</li>\n", ADCreds.PrimaryGroupID)
  80. fmt.Fprintf(w, "<li>Group SIDs: %v</li>\n", ADCreds.GroupMembershipSIDs)
  81. fmt.Fprintf(w, "<li>LogOnTime: %v</li>\n", ADCreds.LogOnTime)
  82. fmt.Fprintf(w, "<li>LogOffTime: %v</li>\n", ADCreds.LogOffTime)
  83. fmt.Fprintf(w, "<li>PasswordLastSet: %v</li>\n", ADCreds.PasswordLastSet)
  84. fmt.Fprintf(w, "<li>LogonServer: %v</li>\n", ADCreds.LogonServer)
  85. fmt.Fprintf(w, "<li>LogonDomainName: %v</li>\n", ADCreds.LogonDomainName)
  86. fmt.Fprintf(w, "<li>LogonDomainID: %v</li>\n", ADCreds.LogonDomainID)
  87. }
  88. fmt.Fprint(w, "</ul>")
  89. }
  90. } else {
  91. w.WriteHeader(http.StatusUnauthorized)
  92. fmt.Fprint(w, "Authentication failed")
  93. }
  94. fmt.Fprint(w, "</html>")
  95. return
  96. }