enhanced session

This commit is contained in:
Martin/Geno 2018-08-10 17:33:39 +02:00
parent 88d7e53cdb
commit a198abb8d0
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
3 changed files with 35 additions and 19 deletions

View File

@ -48,7 +48,7 @@ func main() {
if err != nil {
log.Panic("failed to connect database")
}
db.AutoMigrate(&runtime.Node{}, &websocket.Auth{})
db.AutoMigrate(&runtime.Node{}, &websocket.Session{})
sshmanager := ssh.NewManager(config.SSHPrivateKey, config.SSHTimeout.Duration)
nodesYanic := runtimeYanic.NewNodes(&runtimeYanic.NodesConfig{})

View File

@ -1,29 +1,39 @@
package websocket
import (
"time"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"dev.sum7.eu/genofire/golang-lib/websocket"
)
type Auth struct {
SessionID uuid.UUID
type Session struct {
SessionID uuid.UUID `json:"-" gorm:"primary_key"`
Lastseen *time.Time `json:"-"`
Ping bool `json:"ping"`
}
func (ws *WebsocketServer) IsLoggedIn(msg *websocket.Message) bool {
auth := Auth{
func (ws *WebsocketServer) IsLoggedIn(msg *websocket.Message) (*Session, bool) {
session := Session{
SessionID: msg.Session,
}
err := ws.db.First(&auth)
return err.Error == nil
err := ws.db.First(&session)
if err.Error == nil {
now := time.Now()
session.Lastseen = &now
ws.db.Save(&session)
return &session, true
}
return nil, false
}
func (ws *WebsocketServer) loginHandler(logger *log.Entry, msg *websocket.Message) error {
auth := Auth{
session := Session{
SessionID: msg.Session,
}
err := ws.db.First(&auth)
err := ws.db.First(&session)
if err.Error == nil {
msg.Answer(msg.Subject, true)
logger.Warn("already loggedIn")
@ -35,39 +45,45 @@ func (ws *WebsocketServer) loginHandler(logger *log.Entry, msg *websocket.Messag
msg.Answer(msg.Subject, false)
return nil
}
ok = (ws.secret == secret)
if ok {
err = ws.db.Create(&auth)
if ws.secret == secret {
now := time.Now()
session.Lastseen = &now
err = ws.db.Create(&session)
if err.Error != nil {
log.Warnf("database error: %s", err.Error.Error())
msg.Answer(msg.Subject, false)
return err.Error
}
logger.Debug("done")
msg.Answer(msg.Subject, session)
} else {
logger.Warn("wrong secret")
msg.Answer(msg.Subject, false)
}
msg.Answer(msg.Subject, ok)
return nil
}
func (ws *WebsocketServer) authStatusHandler(logger *log.Entry, msg *websocket.Message) error {
defer logger.Debug("done")
msg.Answer(msg.Subject, ws.IsLoggedIn(msg))
sess, ok := ws.IsLoggedIn(msg)
if ok {
msg.Answer(msg.Subject, sess)
} else {
msg.Answer(msg.Subject, false)
}
return nil
}
func (ws *WebsocketServer) logoutHandler(logger *log.Entry, msg *websocket.Message) error {
auth := Auth{
session := Session{
SessionID: msg.Session,
}
err := ws.db.First(&auth)
err := ws.db.First(&session)
if err.Error != nil {
msg.Answer(msg.Subject, false)
logger.Warn("logout without login")
return nil
}
err = ws.db.Delete(&auth)
err = ws.db.Delete(&session)
logger.Debug("done")
msg.Answer(msg.Subject, err.Error == nil)
return err.Error

View File

@ -10,7 +10,7 @@ import (
)
func (ws *WebsocketServer) nodeHandler(logger *log.Entry, msg *wsLib.Message) error {
if !ws.IsLoggedIn(msg) {
if _, ok := ws.IsLoggedIn(msg); !ok {
msg.Answer(msg.Subject, false)
logger.Warn("not logged in")
return nil