neko/internal/member/multiuser/provider.go

83 lines
2.1 KiB
Go
Raw Normal View History

2023-01-13 12:00:09 +13:00
package multiuser
import (
"errors"
"fmt"
"github.com/demodesk/neko/pkg/types"
"github.com/demodesk/neko/pkg/utils"
)
func New(config Config) types.MemberProvider {
return &MemberProviderCtx{
config: config,
}
}
type MemberProviderCtx struct {
config Config
}
func (provider *MemberProviderCtx) Connect() error {
return nil
}
func (provider *MemberProviderCtx) Disconnect() error {
return nil
}
func (provider *MemberProviderCtx) Authenticate(username string, password string) (string, types.MemberProfile, error) {
// generate random token
token, err := utils.NewUID(5)
if err != nil {
return "", types.MemberProfile{}, err
}
// id is username with token
id := fmt.Sprintf("%s-%s", username, token)
// if logged in as administrator
if provider.config.AdminPassword == password {
2023-09-26 05:28:05 +13:00
profile := provider.config.AdminProfile
if profile.Name == "" {
profile.Name = username
}
return id, profile, nil
2023-01-13 12:00:09 +13:00
}
// if logged in as user
if provider.config.UserPassword == password {
2023-09-26 05:28:05 +13:00
profile := provider.config.UserProfile
if profile.Name == "" {
profile.Name = username
}
return id, profile, nil
2023-01-13 12:00:09 +13:00
}
return "", types.MemberProfile{}, types.ErrMemberInvalidPassword
}
func (provider *MemberProviderCtx) Insert(username string, password string, profile types.MemberProfile) (string, error) {
return "", errors.New("new user is created on first login in multiuser mode")
}
func (provider *MemberProviderCtx) UpdateProfile(id string, profile types.MemberProfile) error {
2023-01-13 12:11:00 +13:00
return nil
2023-01-13 12:00:09 +13:00
}
func (provider *MemberProviderCtx) UpdatePassword(id string, password string) error {
return errors.New("password can only be modified in config while in multiuser mode")
}
func (provider *MemberProviderCtx) Select(id string) (types.MemberProfile, error) {
return types.MemberProfile{}, errors.New("cannot select user in multiuser mode")
}
func (provider *MemberProviderCtx) SelectAll(limit int, offset int) (map[string]types.MemberProfile, error) {
2023-01-13 12:11:00 +13:00
return map[string]types.MemberProfile{}, nil
2023-01-13 12:00:09 +13:00
}
func (provider *MemberProviderCtx) Delete(id string) error {
return errors.New("cannot delete user in multiuser mode")
}