2022-01-21 15:36:31 +01:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/contrib/sessions"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-01-21 23:53:23 +01:00
|
|
|
userkey = "authorized_user"
|
|
|
|
loginKey = "login_msg"
|
2022-01-21 15:36:31 +01:00
|
|
|
)
|
|
|
|
|
2022-01-21 23:53:23 +01:00
|
|
|
func leftLoginMsg(c *gin.Context, msg string) {
|
|
|
|
session := sessions.Default(c)
|
|
|
|
session.Set(loginKey, msg)
|
|
|
|
session.Save()
|
|
|
|
}
|
|
|
|
|
2022-01-21 15:36:31 +01:00
|
|
|
func login(c *gin.Context) {
|
|
|
|
session := sessions.Default(c)
|
|
|
|
|
|
|
|
username := c.PostForm("username")
|
|
|
|
passwd := c.PostForm("passwd")
|
|
|
|
|
|
|
|
// Validate form input
|
|
|
|
if strings.Trim(username, " ") == "" || strings.Trim(passwd, " ") == "" {
|
2022-01-21 23:53:23 +01:00
|
|
|
leftLoginMsg(c, "User name or password cannot be empty")
|
|
|
|
c.Redirect(http.StatusSeeOther, "/login")
|
2022-01-21 15:36:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for username and password match, usually from a database
|
|
|
|
if username != "hello" || passwd != "world" {
|
2022-01-21 23:53:23 +01:00
|
|
|
leftLoginMsg(c, "Username/password does not match")
|
|
|
|
c.Redirect(http.StatusSeeOther, "/login")
|
2022-01-21 15:36:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the username in the session
|
2022-01-21 23:53:23 +01:00
|
|
|
session.Set(userkey, username)
|
2022-01-21 15:36:31 +01:00
|
|
|
|
|
|
|
if err := session.Save(); err != nil {
|
2022-01-21 23:53:23 +01:00
|
|
|
leftLoginMsg(c, "Failed to save session data")
|
|
|
|
c.Redirect(http.StatusSeeOther, "/login")
|
2022-01-21 15:36:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
host = &c.Request.Host
|
|
|
|
c.Redirect(http.StatusSeeOther, "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
func logout(c *gin.Context) {
|
|
|
|
session := sessions.Default(c)
|
2022-01-21 23:53:23 +01:00
|
|
|
|
2022-01-21 15:36:31 +01:00
|
|
|
user := session.Get(userkey)
|
2022-01-21 23:53:23 +01:00
|
|
|
if user != nil {
|
|
|
|
session.Delete(userkey)
|
|
|
|
session.Save()
|
2022-01-21 15:36:31 +01:00
|
|
|
}
|
2022-01-21 23:53:23 +01:00
|
|
|
|
|
|
|
leftLoginMsg(c, "Welcome to WiTTY")
|
2022-01-21 15:36:31 +01:00
|
|
|
c.Redirect(http.StatusFound, "/login")
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthRequired is a simple middleware to check the session
|
|
|
|
func AuthRequired(c *gin.Context) {
|
|
|
|
session := sessions.Default(c)
|
|
|
|
user := session.Get(userkey)
|
2022-01-21 23:53:23 +01:00
|
|
|
|
2022-01-21 15:36:31 +01:00
|
|
|
if user == nil {
|
2022-01-21 23:53:23 +01:00
|
|
|
leftLoginMsg(c, "Not authorized, login first")
|
2022-01-21 15:36:31 +01:00
|
|
|
c.Redirect(http.StatusTemporaryRedirect, "/login")
|
|
|
|
c.Abort()
|
|
|
|
return
|
|
|
|
}
|
2022-01-21 23:53:23 +01:00
|
|
|
|
2022-01-21 15:36:31 +01:00
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
|
|
|
|
func loginPage(c *gin.Context) {
|
2022-01-21 23:53:23 +01:00
|
|
|
session := sessions.Default(c)
|
|
|
|
msg := session.Get(loginKey)
|
|
|
|
|
|
|
|
if msg == nil {
|
|
|
|
msg = "Login first"
|
|
|
|
}
|
|
|
|
|
|
|
|
c.HTML(http.StatusOK, "login.html", gin.H{"msg": msg})
|
2022-01-21 15:36:31 +01:00
|
|
|
}
|