DataHouse/controllers/login.go

94 lines
1.8 KiB
Go

package controllers
import (
"github.com/astaxie/beego"
"git.kingpenguin.tk/chteufleur/datahouse.git/models/user"
"git.kingpenguin.tk/chteufleur/datahouse.git/models/variables"
"net/http"
"strings"
)
type LoginController struct {
beego.Controller
}
const (
UrlXmppAuth = "http://auth.xmpp.kingpenguin.tk/auth"
)
func (c *LoginController) Prepare() {
}
func (c *LoginController) Get() {
routeRedirect := c.Ctx.Input.Param(":route")
sess := c.GetSession(variables.SessionName)
if sess != nil {
if routeRedirect == "" {
c.Redirect(variables.UserRoute, 302)
} else {
c.Redirect("/"+routeRedirect, 302)
}
return
}
c.TplName = "login.tpl"
}
func (c *LoginController) Post() {
routeRedirect := c.Ctx.Input.Param(":route")
sess := c.GetSession(variables.SessionName)
if sess != nil {
if routeRedirect == "" {
c.Redirect(variables.RootRoute, 302)
} else {
c.Redirect("/"+routeRedirect, 302)
}
return
}
login := c.GetString("login")
passwd := c.GetString("password")
if !isLoginOK(login, passwd) {
c.Abort("403")
}
c.SetSession(variables.SessionName, login)
if routeRedirect == "" {
c.Redirect(variables.UserRoute, 302)
} else {
c.Redirect("/"+routeRedirect, 302)
}
}
func isLoginOK(lgn, pwd string) bool {
ret := false
if len(strings.Split(lgn, "@")) != 1 {
// JID inside
log.Info("Auth by JID")
usr := user.GetUserByLogin(strings.Split(lgn, "/")[0])
if usr.Id == 0 {
// User is not in database
ret = false
} else {
resp, _ := http.Get(UrlXmppAuth+"?domain=datahouse.kingpenguin.tk&method=POST&jid="+lgn+"&transaction_id="+pwd)
httpStatusCode := resp.StatusCode
if resp != nil && httpStatusCode == 200 {
ret = true
} else {
ret = false
}
}
} else {
log.Info("Standard auth")
usr := user.GetUserByLogin(lgn)
ret = pwd != "" && pwd == usr.Password
}
return ret
}