Advertisement
Guest User

Untitled

a guest
Feb 6th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "github.com/gorilla/mux"
  6. "github.com/gorilla/securecookie"
  7. "github.com/jtblin/go-ldap-client"
  8. "io/ioutil"
  9. "log"
  10. "net/http"
  11. "strings"
  12. "html/template"
  13. )
  14.  
  15. var cookieHandler = securecookie.New(
  16. securecookie.GenerateRandomKey(64),
  17. securecookie.GenerateRandomKey(32))
  18.  
  19. var client *ldap.LDAPClient
  20.  
  21. func index(w http.ResponseWriter, r *http.Request) {
  22. r.ParseForm() // parsing parameters
  23. for k, v := range r.Form {
  24. fmt.Println("key:", k)
  25. fmt.Println("val:", strings.Join(v, ""))
  26. }
  27. fmt.Fprintf(w, "There is nothing : http://localhost:9090/login") // send data to client side
  28. }
  29.  
  30. func loadPage(title string) ([]byte, error) {
  31. filename := "/home/demm/go/src/awesomeProject/user_policy/pages/" + title + ".html"
  32. html, err := ioutil.ReadFile(filename)
  33. if err != nil {
  34. return nil, err
  35. }
  36. fmt.Printf("%s\n", html)
  37. return html, nil
  38. }
  39.  
  40. func setSession(userName string,response http.ResponseWriter){
  41. value := map[string]string{
  42. "name" :userName,
  43. }
  44. if encoded, err := cookieHandler.Encode("session", value); err == nil{
  45. cookie := &http.Cookie{
  46. Name: "session",
  47. Value: encoded,
  48. Path: "/",
  49. }
  50. http.SetCookie(response,cookie)
  51. }
  52. }
  53.  
  54. func clearSession(response http.ResponseWriter){
  55. cookie := &http.Cookie{
  56. Name: "session",
  57. Value: "",
  58. Path: "/",
  59. MaxAge: -1,
  60. }
  61. http.SetCookie(response,cookie)
  62. }
  63.  
  64. func authUser(login string, pass string) (bool, error) {
  65.  
  66. ok, user, err := client.Authenticate(login, pass)
  67. if err != nil {
  68. fmt.Printf("Error authenticating user %s: %+v", login, err)
  69. }
  70. if !ok {
  71. fmt.Printf("Authenticating failed for user %s", login)
  72. }
  73. log.Printf("User: %+v", user)
  74. fmt.Printf("Failuresssssssssssssswwwwwwwwwwqwqwrsds\n")
  75. return ok, err
  76. }
  77.  
  78.  
  79.  
  80.  
  81. func loginpage(w http.ResponseWriter, r *http.Request) {
  82. html, err := loadPage("login")
  83. if err != nil {
  84. return
  85. }
  86. fmt.Fprintf(w, string(html))
  87. }
  88.  
  89. func loginHandler(response http.ResponseWriter, request *http.Request){
  90. request.ParseForm()
  91. login := strings.Join(request.Form["login"], "")
  92. pass := strings.Join(request.Form["pass"], "")
  93. res, err := authUser(login, pass)
  94. redirectTarger := "/"
  95. if err != nil {
  96. fmt.Printf("Failure\n")
  97. }
  98.  
  99. if res{
  100. setSession(login,response)
  101. redirectTarger = "/internal"
  102. }
  103. http.Redirect(response,request,redirectTarger,302)
  104. }
  105.  
  106. func logoutHandler(response http.ResponseWriter,request *http.Request){
  107. clearSession(response)
  108. http.Redirect(response, request, "/",302)
  109. }
  110.  
  111. func internalPageHandler(w http.ResponseWriter, r *http.Request){
  112. t, err := template.ParseFiles("/home/demm/go/src/awesomeProject/user_policy/pages/form.html")
  113. if err !=nil{
  114. fmt.Fprintf(w, err.Error())
  115. }
  116. t.ExecuteTemplate(w, "form", nil)
  117. }
  118.  
  119.  
  120.  
  121. var router =mux.NewRouter()
  122. func main() {
  123. client = &ldap.LDAPClient{
  124. Base: "ou=users,ou=myd,dc=myd,dc=local",
  125. Host: "192.168.0.1",
  126. Port: 389,
  127. UseSSL: false,
  128.  
  129. //BindDN: "cn=testldap, ou=user,ou=users,ou=myd,dc=myd,dc=local",
  130. //BindPassword: "redonlypass",
  131. UserFilter: "(sAMAccountName=%s)",
  132. GroupFilter: "(memberUid=%s)",
  133. Attributes: []string{"givenName", "sn", "mail", "sAMAccountName"},
  134. }
  135.  
  136. defer client.Close()
  137.  
  138. //router.HandleFunc("/")
  139. router.HandleFunc("/loginpage", loginpage)
  140. router.HandleFunc("/login", loginHandler).Methods("POST")
  141. router.HandleFunc("/logout", logoutHandler)
  142. router.HandleFunc("/", index)
  143. router.HandleFunc("/internal", internalPageHandler)
  144. http.Handle("/", router)
  145. http.ListenAndServe(":9091", nil)
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement