1
0
Fork 0
forked from 0x2E/fusion
fusion/api/session.go

53 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package api
import (
"net/http"
"github.com/0x2e/fusion/conf"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
)
type Session struct{}
func (s Session) Create(c echo.Context) error {
var req struct {
Password string `json:"password" validate:"required"`
}
if err := bindAndValidate(&req, c); err != nil {
return err
}
if req.Password != conf.Conf.Password {
return echo.NewHTTPError(http.StatusUnauthorized, "Wrong password")
}
sess, _ := session.Get("login", c)
//使用非https请求时为保证Set-Cookie能正常生效对Option进行特殊设置
if conf.Conf.InSecure {
sess.Options.Secure = false
sess.Options.SameSite = http.SameSiteDefaultMode
}
sess.Values["password"] = conf.Conf.Password
if err := sess.Save(c.Request(), c.Response()); err != nil {
return c.NoContent(http.StatusInternalServerError)
}
return c.NoContent(http.StatusCreated)
}
func (s Session) Check(c echo.Context) (bool, error) {
sess, err := session.Get("login", c)
if err != nil {
return false, err
}
v, ok := sess.Values["password"]
if !ok {
return false, nil
}
return v == conf.Conf.Password, nil
}