mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
member manager login & logout.
This commit is contained in:
parent
304cb5d655
commit
524c76ae3c
@ -30,17 +30,7 @@ func (api *ApiManagerCtx) Login(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: Proper login.
|
||||
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,
|
||||
})
|
||||
|
||||
session, token, err := api.members.Login(data.Username, data.Password)
|
||||
if err != nil {
|
||||
utils.HttpUnauthorized(w, err)
|
||||
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) {
|
||||
session := auth.GetSession(r)
|
||||
|
||||
// TODO: Proper logout.
|
||||
err := api.sessions.Delete(session.ID())
|
||||
err := api.members.Logout(session.ID())
|
||||
if err != nil {
|
||||
utils.HttpUnauthorized(w, err)
|
||||
return
|
||||
|
@ -1,6 +1,7 @@
|
||||
package member
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
@ -13,10 +14,11 @@ import (
|
||||
"demodesk/neko/internal/types"
|
||||
)
|
||||
|
||||
func New(config *config.Member) *MemberManagerCtx {
|
||||
func New(sessions types.SessionManager, config *config.Member) *MemberManagerCtx {
|
||||
manager := &MemberManagerCtx{
|
||||
logger: log.With().Str("module", "member").Logger(),
|
||||
config: config,
|
||||
logger: log.With().Str("module", "member").Logger(),
|
||||
sessions: sessions,
|
||||
config: config,
|
||||
}
|
||||
|
||||
switch config.Provider {
|
||||
@ -40,6 +42,7 @@ func New(config *config.Member) *MemberManagerCtx {
|
||||
|
||||
type MemberManagerCtx struct {
|
||||
logger zerolog.Logger
|
||||
sessions types.SessionManager
|
||||
config *config.Member
|
||||
mu sync.Mutex
|
||||
provider types.MemberProvider
|
||||
@ -91,6 +94,13 @@ func (manager *MemberManagerCtx) UpdateProfile(id string, profile types.MemberPr
|
||||
manager.mu.Lock()
|
||||
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)
|
||||
}
|
||||
|
||||
@ -105,5 +115,33 @@ func (manager *MemberManagerCtx) Delete(id string) error {
|
||||
manager.mu.Lock()
|
||||
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)
|
||||
}
|
||||
|
||||
//
|
||||
// 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)
|
||||
}
|
||||
|
@ -26,4 +26,7 @@ type MemberProvider interface {
|
||||
|
||||
type MemberManager interface {
|
||||
MemberProvider
|
||||
|
||||
Login(username string, password string) (Session, string, error)
|
||||
Logout(id string) error
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user