example-AD.go 3.8 KB

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