freifunkmanager/websocket/hd_auth.go

56 lines
1.3 KiB
Go
Raw Normal View History

package websocket
import (
log "github.com/sirupsen/logrus"
"github.com/genofire/golang-lib/websocket"
)
func (ws *WebsocketServer) loginHandler(logger *log.Entry, msg *websocket.Message) error {
_, ok := ws.loggedIn[msg.Session]
if ok {
msg.Answer(msg.Subject, true)
logger.Warn("already loggedIn")
return nil
}
secret, ok := msg.Body.(string)
if !ok {
logger.Warn("invalid secret format")
msg.Answer(msg.Subject, false)
return nil
}
ok = (ws.secret == secret)
if ok {
ws.loggedIn[msg.Session] = true
2018-07-16 23:08:56 +02:00
logger.Debug("login done")
} else {
logger.Warn("wrong secret")
}
msg.Answer(msg.Subject, ok)
return nil
}
func (ws *WebsocketServer) authStatusHandler(logger *log.Entry, msg *websocket.Message) error {
login, ok := ws.loggedIn[msg.Session]
2018-07-16 23:08:56 +02:00
defer logger.Debug("auth status done")
if !ok {
msg.Answer(msg.Subject, false)
return nil
}
msg.Answer(msg.Subject, login)
return nil
}
func (ws *WebsocketServer) logoutHandler(logger *log.Entry, msg *websocket.Message) error {
_, ok := ws.loggedIn[msg.Session]
if !ok {
msg.Answer(msg.Subject, false)
logger.Warn("logout without login")
return nil
}
ws.loggedIn[msg.Session] = false
delete(ws.loggedIn, msg.Session)
2018-07-16 23:08:56 +02:00
logger.Debug("logout done")
msg.Answer(msg.Subject, true)
return nil
}