member manager login & logout.

This commit is contained in:
Miroslav Šedivý 2021-03-14 19:59:34 +01:00
parent 304cb5d655
commit 524c76ae3c
4 changed files with 47 additions and 16 deletions

View File

@ -30,17 +30,7 @@ func (api *ApiManagerCtx) Login(w http.ResponseWriter, r *http.Request) {
return return
} }
// TODO: Proper login. session, token, err := api.members.Login(data.Username, data.Password)
session, token, err := api.sessions.Create(data.Username, types.MemberProfile{
Name: data.Username,
IsAdmin: true,
CanLogin: true,
CanConnect: true,
CanWatch: true,
CanHost: true,
CanAccessClipboard: true,
})
if err != nil { if err != nil {
utils.HttpUnauthorized(w, err) utils.HttpUnauthorized(w, err)
return return
@ -70,8 +60,7 @@ func (api *ApiManagerCtx) Login(w http.ResponseWriter, r *http.Request) {
func (api *ApiManagerCtx) Logout(w http.ResponseWriter, r *http.Request) { func (api *ApiManagerCtx) Logout(w http.ResponseWriter, r *http.Request) {
session := auth.GetSession(r) session := auth.GetSession(r)
// TODO: Proper logout. err := api.members.Logout(session.ID())
err := api.sessions.Delete(session.ID())
if err != nil { if err != nil {
utils.HttpUnauthorized(w, err) utils.HttpUnauthorized(w, err)
return return

View File

@ -1,6 +1,7 @@
package member package member
import ( import (
"fmt"
"sync" "sync"
"github.com/rs/zerolog" "github.com/rs/zerolog"
@ -13,10 +14,11 @@ import (
"demodesk/neko/internal/types" "demodesk/neko/internal/types"
) )
func New(config *config.Member) *MemberManagerCtx { func New(sessions types.SessionManager, config *config.Member) *MemberManagerCtx {
manager := &MemberManagerCtx{ manager := &MemberManagerCtx{
logger: log.With().Str("module", "member").Logger(), logger: log.With().Str("module", "member").Logger(),
config: config, sessions: sessions,
config: config,
} }
switch config.Provider { switch config.Provider {
@ -40,6 +42,7 @@ func New(config *config.Member) *MemberManagerCtx {
type MemberManagerCtx struct { type MemberManagerCtx struct {
logger zerolog.Logger logger zerolog.Logger
sessions types.SessionManager
config *config.Member config *config.Member
mu sync.Mutex mu sync.Mutex
provider types.MemberProvider provider types.MemberProvider
@ -91,6 +94,13 @@ func (manager *MemberManagerCtx) UpdateProfile(id string, profile types.MemberPr
manager.mu.Lock() manager.mu.Lock()
defer manager.mu.Unlock() defer manager.mu.Unlock()
// update corresponding session, if exists
if _, ok := manager.sessions.Get(id); ok {
if err := manager.sessions.Update(id, profile); err != nil {
manager.logger.Err(err).Msg("error while updating session")
}
}
return manager.provider.UpdateProfile(id, profile) return manager.provider.UpdateProfile(id, profile)
} }
@ -105,5 +115,33 @@ func (manager *MemberManagerCtx) Delete(id string) error {
manager.mu.Lock() manager.mu.Lock()
defer manager.mu.Unlock() defer manager.mu.Unlock()
// destroy corresponding session, if exists
if _, ok := manager.sessions.Get(id); ok {
if err := manager.sessions.Delete(id); err != nil {
manager.logger.Err(err).Msg("error while deleting session")
}
}
return manager.provider.Delete(id) return manager.provider.Delete(id)
} }
//
// member -> session
//
func (manager *MemberManagerCtx) Login(username string, password string) (types.Session, string, error) {
id, profile, err := manager.provider.Authenticate(username, password)
if err != nil {
return nil, "", err
}
return manager.sessions.Create(id, profile)
}
func (manager *MemberManagerCtx) Logout(id string) error {
if _, ok := manager.sessions.Get(id); !ok {
return fmt.Errorf("session not found")
}
return manager.sessions.Delete(id)
}

View File

@ -26,4 +26,7 @@ type MemberProvider interface {
type MemberManager interface { type MemberManager interface {
MemberProvider MemberProvider
Login(username string, password string) (Session, string, error)
Logout(id string) error
} }

View File

@ -138,6 +138,7 @@ func (neko *Neko) Start() {
) )
neko.memberManager = member.New( neko.memberManager = member.New(
neko.sessionManager,
neko.Configs.Member, neko.Configs.Member,
) )