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 { if err != nil {
log.Panic("failed to connect database") 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) sshmanager := ssh.NewManager(config.SSHPrivateKey, config.SSHTimeout.Duration)
nodesYanic := runtimeYanic.NewNodes(&runtimeYanic.NodesConfig{}) nodesYanic := runtimeYanic.NewNodes(&runtimeYanic.NodesConfig{})

View File

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

View File

@ -10,7 +10,7 @@ import (
) )
func (ws *WebsocketServer) nodeHandler(logger *log.Entry, msg *wsLib.Message) error { 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) msg.Answer(msg.Subject, false)
logger.Warn("not logged in") logger.Warn("not logged in")
return nil return nil