From 5c0a901b1b3e522ed523e966feb9901b8c7e01ff Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Fri, 30 Aug 2013 11:46:22 +0200 Subject: Login implemented --- login.go | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 6 +++ tpls.go | 2 + tpls/login.tpl | 17 +++++++++ 4 files changed, 140 insertions(+) create mode 100644 login.go create mode 100644 tpls/login.tpl diff --git a/login.go b/login.go new file mode 100644 index 0000000..f678216 --- /dev/null +++ b/login.go @@ -0,0 +1,115 @@ +package main + +import ( + "code.google.com/p/go.crypto/bcrypt" + "github.com/gorilla/sessions" + "kch42.de/gostuff/mailremind/model" + "log" + "net/http" +) + +type loginTpldata struct { + Error, Success string +} + +type loginFormdata struct { + Mail, Password string +} + +func userFromSess(sess *sessions.Session) model.User { + _id, ok := sess.Values["uid"] + if !ok { + return nil + } + + id, ok := _id.(string) + if !ok { + return nil + } + + uid, err := db.ParseDBID(id) + if err != nil { + return nil + } + + user, err := dbcon.UserByID(uid) + if err != nil { + return nil + } + + return user +} + +func login(rw http.ResponseWriter, req *http.Request) { + outdata := &loginTpldata{} + defer func() { + if err := tplLogin.Execute(rw, outdata); err != nil { + log.Printf("Error executing template in login: %s", err) + } + }() + + sess, err := SessionStorage.Get(req, "mailremind-sess") + if err != nil { + sess, _ = SessionStorage.New(req, "mailremind-sess") + } + defer func() { + if err := sess.Save(req, rw); err != nil { + log.Printf("Error while saving session: %s", err) + outdata.Success = "" + outdata.Error = "Error while saving session." + return + } + }() + + if user := userFromSess(sess); user != nil { + outdata.Success = "You are already logged in" + return + } + + if req.Method != "POST" { + return + } + + if err := req.ParseForm(); err != nil { + outdata.Error = "Data of form could not be understand. If this happens again, please contact support!" + return + } + + indata := new(loginFormdata) + if err := formdec.Decode(indata, req.Form); (err != nil) || (indata.Mail == "") || (indata.Password == "") { + outdata.Error = "Input data wrong or missing. Please fill in all values." + return + } + + user, err := dbcon.UserByMail(indata.Mail) + switch err { + case nil: + case model.NotFound: + outdata.Error = "E-Mail or password was wrong." + return + default: + log.Printf("Error while loding user data (login): %s", err) + outdata.Error = "User data could not be loaded. Please contact support, if this happens again." + return + } + + if bcrypt.CompareHashAndPassword(user.PWHash(), []byte(indata.Password)) != nil { + outdata.Error = "E-Mail or password was wrong." + return + } + + sess.Values["uid"] = user.ID().String() + outdata.Success = "Login successful" +} + +func logincheck(rw http.ResponseWriter, req *http.Request) { + sess, _ := SessionStorage.Get(req, "mailremind-sess") + user := userFromSess(sess) + outdata := new(msgTpldata) + if user == nil { + outdata.Msg = "" + } else { + outdata.Msg = user.Email() + } + tplMsg.Execute(rw, outdata) +} diff --git a/main.go b/main.go index 4eb58c2..8af1fc1 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,8 @@ import ( "flag" "fmt" "github.com/gorilla/mux" + "github.com/gorilla/securecookie" + "github.com/gorilla/sessions" "github.com/kch42/simpleconf" _ "kch42.de/gostuff/mailremind/model/mysql" "log" @@ -17,6 +19,8 @@ func debug(rw http.ResponseWriter, req *http.Request) { var conf simpleconf.Config var baseurl string +var SessionStorage = sessions.NewCookieStore(securecookie.GenerateRandomKey(32), securecookie.GenerateRandomKey(32)) + func main() { confpath := flag.String("config", "", "Path to config file") flag.Parse() @@ -51,6 +55,8 @@ func main() { router.PathPrefix("/static").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(staticpath)))) router.HandleFunc("/register", register) router.HandleFunc("/activate", activate) + router.HandleFunc("/login", login) + router.HandleFunc("/logincheck", logincheck) http.Handle("/", router) diff --git a/tpls.go b/tpls.go index 846bdda..10f4322 100644 --- a/tpls.go +++ b/tpls.go @@ -19,6 +19,7 @@ func loadTpl(tplpath, name string) *template.Template { var ( tplRegister *template.Template tplMsg *template.Template + tplLogin *template.Template ) func initTpls() { @@ -29,6 +30,7 @@ func initTpls() { tplRegister = loadTpl(tplpath, "register") tplMsg = loadTpl(tplpath, "msg") + tplLogin = loadTpl(tplpath, "login") } type msgTpldata struct { diff --git a/tpls/login.tpl b/tpls/login.tpl new file mode 100644 index 0000000..34541e4 --- /dev/null +++ b/tpls/login.tpl @@ -0,0 +1,17 @@ +{{define "title"}}Login{{end}} + +{{define "content"}} + {{if .Error}} +
{{.Error}}
+ {{end}} + + {{if .Success}} +
{{.Success}}
+ {{else}} +
+

E-Mail

+

Password

+

+
+ {{end}} +{{end}} \ No newline at end of file -- cgit v1.2.3-70-g09d2