From 456c28fa552a569f288a6367e7d116bde5266b30 Mon Sep 17 00:00:00 2001 From: Meutel Date: Sat, 29 Jul 2017 10:37:15 +0200 Subject: [PATCH] Authentification with cookies --- .gitignore | 1 + authcookie/main.go | 76 ++++++++++++++++++++++++++++++++ authcookie/templates/footer.html | 2 + authcookie/templates/header.html | 4 ++ authcookie/templates/home.html | 10 +++++ authcookie/templates/login.html | 16 +++++++ 6 files changed, 109 insertions(+) create mode 100644 authcookie/main.go create mode 100644 authcookie/templates/footer.html create mode 100644 authcookie/templates/header.html create mode 100644 authcookie/templates/home.html create mode 100644 authcookie/templates/login.html diff --git a/.gitignore b/.gitignore index 15155d1..c80b02e 100644 --- a/.gitignore +++ b/.gitignore @@ -60,3 +60,4 @@ tpl-ex3/tpl-ex3 form-ex1/form-ex1 form-ex2/form-ex2 webcounter/webcounter +authcookie/authcookie diff --git a/authcookie/main.go b/authcookie/main.go new file mode 100644 index 0000000..dd1577b --- /dev/null +++ b/authcookie/main.go @@ -0,0 +1,76 @@ +package main + +import ( + "html/template" + "log" + "net/http" + + "github.com/gorilla/sessions" +) + +type AuthCookie struct { + templates *template.Template + store *sessions.CookieStore +} + +func (app *AuthCookie) HomePage(res http.ResponseWriter, req *http.Request) { + app.templates.ExecuteTemplate(res, "home.html", app.CurrentSession(res, req).Values) +} +func (app *AuthCookie) LoginPage(res http.ResponseWriter, req *http.Request) { + formErr := make(map[string]string) + switch req.Method { + case "POST": + username := req.FormValue("username") + if username == "" { + formErr["username"] = "Empty username" + } + password := req.FormValue("password") + if password == "" { + formErr["password"] = "Empty password" + } + if len(formErr) == 0 { + app.SaveUsername(username, res, req) + RedirectHome(res, req) + return + } + fallthrough + case "GET": + app.templates.ExecuteTemplate(res, "login.html", formErr) + } +} +func (app *AuthCookie) LogoutPage(res http.ResponseWriter, req *http.Request) { + app.SaveUsername("", res, req) + RedirectHome(res, req) +} +func (app *AuthCookie) CurrentSession(res http.ResponseWriter, req *http.Request) *sessions.Session { + session, _ := app.store.Get(req, "session") + return session +} +func (app *AuthCookie) SaveUsername(username string, res http.ResponseWriter, req *http.Request) { + session := app.CurrentSession(res, req) + session.Values["username"] = username + session.Save(req, res) +} +func RedirectHome(res http.ResponseWriter, req *http.Request) { + http.Redirect(res, req, "/", http.StatusSeeOther) +} + +/* +Create an http application that supports at least 2 endpoints: +login and logout. login should accept a form and save a cookie. +(with at least the username) logout should clear the cookie. +*/ +func main() { + tpl, err := template.ParseGlob("templates/*.html") + if err != nil { + log.Fatalln(err) + } + app := AuthCookie{ + templates: tpl, + store: sessions.NewCookieStore([]byte("flQ6QzM/c3Jtdl9ycDx6OXRIfFgK")), + } + http.HandleFunc("/", app.HomePage) + http.HandleFunc("/login", app.LoginPage) + http.HandleFunc("/logout", app.LogoutPage) + http.ListenAndServe(":9000", nil) +} diff --git a/authcookie/templates/footer.html b/authcookie/templates/footer.html new file mode 100644 index 0000000..b605728 --- /dev/null +++ b/authcookie/templates/footer.html @@ -0,0 +1,2 @@ + + diff --git a/authcookie/templates/header.html b/authcookie/templates/header.html new file mode 100644 index 0000000..0fd7698 --- /dev/null +++ b/authcookie/templates/header.html @@ -0,0 +1,4 @@ + + + Auth cookie + diff --git a/authcookie/templates/home.html b/authcookie/templates/home.html new file mode 100644 index 0000000..737ae4f --- /dev/null +++ b/authcookie/templates/home.html @@ -0,0 +1,10 @@ +{{ template "header.html" }} +
+{{ if .username }} +Logout +{{ else }} +Login +{{ end }} +
+

Hello {{ .username }}

+{{ template "footer.html" }} diff --git a/authcookie/templates/login.html b/authcookie/templates/login.html new file mode 100644 index 0000000..78a6a8f --- /dev/null +++ b/authcookie/templates/login.html @@ -0,0 +1,16 @@ +{{ template "header.html" }} +

Authentification

+
+ +
+ +
+ +
+{{ template "footer.html" }}