From 8231b3d17605592d7009514a07dc424c39010d63 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Mon, 19 Dec 2016 22:47:45 +0100 Subject: [PATCH] Move login-code to http --- cmd/budgeteer/main.go | 38 +------------------------------------ http/http.go | 44 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 37 deletions(-) diff --git a/cmd/budgeteer/main.go b/cmd/budgeteer/main.go index 67e5f86..4d7cb93 100644 --- a/cmd/budgeteer/main.go +++ b/cmd/budgeteer/main.go @@ -13,40 +13,4 @@ func main() { h := &http.Handler{UserService=us} h.Serve() -} - -func restricted(c *gin.Context) { - claims, ok := verifyLogin(c) - if !ok { - c.Redirect(http.StatusTemporaryRedirect, "/login") - return - } - - name := claims["name"].(string) - c.String(http.StatusOK, "Welcome "+name+"!") -} - -func login(c *gin.Context) { - if _, ok := verifyLogin(c); ok { - c.Redirect(http.StatusTemporaryRedirect, "/api/v1/hello") - return - } - - c.HTML(http.StatusOK, "login", nil) -} - -func logout(c *gin.Context) { - clearLogin(c) -} - -func loginPost(c *gin.Context) { - username, _ := c.GetPostForm("username") - password, _ := c.GetPostForm("password") - - if username != "jan" || password != "passwort" { - c.AbortWithStatus(http.StatusUnauthorized) - return - } - - loginSuccess(c, username, "Jan Bader") -} +} \ No newline at end of file diff --git a/http/http.go b/http/http.go index 822bc42..c6c77cf 100644 --- a/http/http.go +++ b/http/http.go @@ -48,3 +48,47 @@ func (h *Handler) Serve() { router.Run(":1323") } + +func restricted(c *gin.Context) { + claims, ok := verifyLogin(c) + if !ok { + c.Redirect(http.StatusTemporaryRedirect, "/login") + return + } + + name := claims["name"].(string) + c.String(http.StatusOK, "Welcome "+name+"!") +} + +func verifyLogin(c *gin.Context) error { + tokenString, err := c.Cookie(authCookie) + if err != nil { + return nil, err + } + +} + +func login(c *gin.Context) { + if _, ok := verifyLogin(c); ok { + c.Redirect(http.StatusTemporaryRedirect, "/api/v1/hello") + return + } + + c.HTML(http.StatusOK, "login", nil) +} + +func logout(c *gin.Context) { + clearLogin(c) +} + +func loginPost(c *gin.Context) { + username, _ := c.GetPostForm("username") + password, _ := c.GetPostForm("password") + + if username != "jan" || password != "passwort" { + c.AbortWithStatus(http.StatusUnauthorized) + return + } + + loginSuccess(c, username, "Jan Bader") +}