example-AD.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // +build examples
  2. package main
  3. import (
  4. "encoding/hex"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/jcmturner/goidentity/v6"
  8. "github.com/jcmturner/gokrb5/v8/client"
  9. "github.com/jcmturner/gokrb5/v8/config"
  10. "github.com/jcmturner/gokrb5/v8/credentials"
  11. "github.com/jcmturner/gokrb5/v8/keytab"
  12. "github.com/jcmturner/gokrb5/v8/service"
  13. "github.com/jcmturner/gokrb5/v8/spnego"
  14. "github.com/jcmturner/gokrb5/v8/test/testdata"
  15. "io/ioutil"
  16. "log"
  17. "net/http"
  18. "net/http/httptest"
  19. "os"
  20. )
  21. func main() {
  22. s := httpServer()
  23. defer s.Close()
  24. b, _ := hex.DecodeString(testdata.TESTUSER1_USERKRB5_AD_KEYTAB)
  25. kt := keytab.New()
  26. kt.Unmarshal(b)
  27. c, _ := config.NewFromString(testdata.TEST_KRB5CONF)
  28. cl := client.NewWithKeytab("testuser1", "USER.GOKRB5", kt, c, client.DisablePAFXFAST(true))
  29. httpRequest(s.URL, cl)
  30. b, _ = hex.DecodeString(testdata.TESTUSER2_USERKRB5_AD_KEYTAB)
  31. kt = keytab.New()
  32. kt.Unmarshal(b)
  33. c, _ = config.NewFromString(testdata.TEST_KRB5CONF)
  34. cl = client.NewWithKeytab("testuser2", "USER.GOKRB5", kt, c, client.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 = spnego.SetSPNEGOHeader(cl, r, "HTTP/host.test.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 Tests: ", log.Ldate|log.Ltime|log.Lshortfile)
  59. b, _ := hex.DecodeString(testdata.HTTP_KEYTAB)
  60. kt := keytab.New()
  61. kt.Unmarshal(b)
  62. th := http.HandlerFunc(testAppHandler)
  63. s := httptest.NewServer(spnego.SPNEGOKRB5Authenticate(th, kt, service.Logger(l)))
  64. return s
  65. }
  66. func testAppHandler(w http.ResponseWriter, r *http.Request) {
  67. creds := goidentity.FromHTTPRequestContext(r)
  68. fmt.Fprint(w, "<html>\n<p><h1>TEST.GOKRB5 Handler</h1></p>\n")
  69. if creds != nil && creds.Authenticated() {
  70. fmt.Fprintf(w, "<ul><li>Authenticed user: %s</li>\n", creds.UserName())
  71. fmt.Fprintf(w, "<li>User's realm: %s</li>\n", creds.Domain())
  72. fmt.Fprint(w, "<li>Authz Attributes (Group Memberships):</li><ul>\n")
  73. for _, s := range creds.AuthzAttributes() {
  74. fmt.Fprintf(w, "<li>%v</li>\n", s)
  75. }
  76. fmt.Fprint(w, "</ul>\n")
  77. if ADCredsJSON, ok := creds.Attributes()[credentials.AttributeKeyADCredentials]; ok {
  78. ADCreds := new(credentials.ADCredentials)
  79. err := json.Unmarshal([]byte(ADCredsJSON), ADCreds)
  80. if err == nil {
  81. // Now access the fields of the ADCredentials struct. For example:
  82. fmt.Fprintf(w, "<li>EffectiveName: %v</li>\n", ADCreds.EffectiveName)
  83. fmt.Fprintf(w, "<li>FullName: %v</li>\n", ADCreds.FullName)
  84. fmt.Fprintf(w, "<li>UserID: %v</li>\n", ADCreds.UserID)
  85. fmt.Fprintf(w, "<li>PrimaryGroupID: %v</li>\n", ADCreds.PrimaryGroupID)
  86. fmt.Fprintf(w, "<li>Group SIDs: %v</li>\n", ADCreds.GroupMembershipSIDs)
  87. fmt.Fprintf(w, "<li>LogOnTime: %v</li>\n", ADCreds.LogOnTime)
  88. fmt.Fprintf(w, "<li>LogOffTime: %v</li>\n", ADCreds.LogOffTime)
  89. fmt.Fprintf(w, "<li>PasswordLastSet: %v</li>\n", ADCreds.PasswordLastSet)
  90. fmt.Fprintf(w, "<li>LogonServer: %v</li>\n", ADCreds.LogonServer)
  91. fmt.Fprintf(w, "<li>LogonDomainName: %v</li>\n", ADCreds.LogonDomainName)
  92. fmt.Fprintf(w, "<li>LogonDomainID: %v</li>\n", ADCreds.LogonDomainID)
  93. }
  94. }
  95. fmt.Fprint(w, "</ul>")
  96. } else {
  97. w.WriteHeader(http.StatusUnauthorized)
  98. fmt.Fprint(w, "Authentication failed")
  99. }
  100. fmt.Fprint(w, "</html>")
  101. return
  102. }