Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "github.com/gorilla/mux"
- "github.com/gorilla/securecookie"
- "github.com/jtblin/go-ldap-client"
- "io/ioutil"
- "log"
- "net/http"
- "strings"
- "html/template"
- )
- var cookieHandler = securecookie.New(
- securecookie.GenerateRandomKey(64),
- securecookie.GenerateRandomKey(32))
- var client *ldap.LDAPClient
- func index(w http.ResponseWriter, r *http.Request) {
- r.ParseForm() // parsing parameters
- for k, v := range r.Form {
- fmt.Println("key:", k)
- fmt.Println("val:", strings.Join(v, ""))
- }
- fmt.Fprintf(w, "There is nothing : http://localhost:9090/login") // send data to client side
- }
- func loadPage(title string) ([]byte, error) {
- filename := "/home/demm/go/src/awesomeProject/user_policy/pages/" + title + ".html"
- html, err := ioutil.ReadFile(filename)
- if err != nil {
- return nil, err
- }
- fmt.Printf("%s\n", html)
- return html, nil
- }
- func setSession(userName string,response http.ResponseWriter){
- value := map[string]string{
- "name" :userName,
- }
- if encoded, err := cookieHandler.Encode("session", value); err == nil{
- cookie := &http.Cookie{
- Name: "session",
- Value: encoded,
- Path: "/",
- }
- http.SetCookie(response,cookie)
- }
- }
- func clearSession(response http.ResponseWriter){
- cookie := &http.Cookie{
- Name: "session",
- Value: "",
- Path: "/",
- MaxAge: -1,
- }
- http.SetCookie(response,cookie)
- }
- func authUser(login string, pass string) (bool, error) {
- ok, user, err := client.Authenticate(login, pass)
- if err != nil {
- fmt.Printf("Error authenticating user %s: %+v", login, err)
- }
- if !ok {
- fmt.Printf("Authenticating failed for user %s", login)
- }
- log.Printf("User: %+v", user)
- fmt.Printf("Failuresssssssssssssswwwwwwwwwwqwqwrsds\n")
- return ok, err
- }
- func loginpage(w http.ResponseWriter, r *http.Request) {
- html, err := loadPage("login")
- if err != nil {
- return
- }
- fmt.Fprintf(w, string(html))
- }
- func loginHandler(response http.ResponseWriter, request *http.Request){
- request.ParseForm()
- login := strings.Join(request.Form["login"], "")
- pass := strings.Join(request.Form["pass"], "")
- res, err := authUser(login, pass)
- redirectTarger := "/"
- if err != nil {
- fmt.Printf("Failure\n")
- }
- if res{
- setSession(login,response)
- redirectTarger = "/internal"
- }
- http.Redirect(response,request,redirectTarger,302)
- }
- func logoutHandler(response http.ResponseWriter,request *http.Request){
- clearSession(response)
- http.Redirect(response, request, "/",302)
- }
- func internalPageHandler(w http.ResponseWriter, r *http.Request){
- t, err := template.ParseFiles("/home/demm/go/src/awesomeProject/user_policy/pages/form.html")
- if err !=nil{
- fmt.Fprintf(w, err.Error())
- }
- t.ExecuteTemplate(w, "form", nil)
- }
- var router =mux.NewRouter()
- func main() {
- client = &ldap.LDAPClient{
- Base: "ou=users,ou=myd,dc=myd,dc=local",
- Host: "192.168.0.1",
- Port: 389,
- UseSSL: false,
- //BindDN: "cn=testldap, ou=user,ou=users,ou=myd,dc=myd,dc=local",
- //BindPassword: "redonlypass",
- UserFilter: "(sAMAccountName=%s)",
- GroupFilter: "(memberUid=%s)",
- Attributes: []string{"givenName", "sn", "mail", "sAMAccountName"},
- }
- defer client.Close()
- //router.HandleFunc("/")
- router.HandleFunc("/loginpage", loginpage)
- router.HandleFunc("/login", loginHandler).Methods("POST")
- router.HandleFunc("/logout", logoutHandler)
- router.HandleFunc("/", index)
- router.HandleFunc("/internal", internalPageHandler)
- http.Handle("/", router)
- http.ListenAndServe(":9091", nil)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement