example-AD.go 3.8 KB

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