neko/internal/member/object/provider.go

153 lines
3.4 KiB
Go
Raw Normal View History

2021-03-15 00:57:19 +13:00
package object
import (
"github.com/demodesk/neko/pkg/types"
2021-03-15 00:57:19 +13:00
)
2021-03-15 04:58:18 +13:00
func New(config Config) types.MemberProvider {
return &MemberProviderCtx{
2021-03-15 01:07:03 +13:00
config: config,
2021-03-15 02:44:03 +13:00
entries: make(map[string]*MemberEntry),
2021-03-15 00:57:19 +13:00
}
}
2021-03-15 04:58:18 +13:00
type MemberProviderCtx struct {
2021-03-15 01:07:03 +13:00
config Config
2021-03-15 02:44:03 +13:00
entries map[string]*MemberEntry
2021-03-15 00:57:19 +13:00
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) Connect() error {
var err error
2021-03-15 04:58:18 +13:00
if provider.config.AdminPassword != "" {
// create default admin account at startup
2021-03-15 04:58:18 +13:00
_, err = provider.Insert("admin", provider.config.AdminPassword, types.MemberProfile{
2021-11-04 09:59:38 +13:00
Name: "Administrator",
IsAdmin: true,
CanLogin: true,
CanConnect: true,
CanWatch: true,
CanHost: true,
2021-12-10 09:48:02 +13:00
CanShareMedia: true,
2021-11-04 09:59:38 +13:00
CanAccessClipboard: true,
SendsInactiveCursor: true,
CanSeeInactiveCursors: true,
})
}
2021-03-15 04:58:18 +13:00
if provider.config.UserPassword != "" {
// create default user account at startup
2021-03-15 04:58:18 +13:00
_, err = provider.Insert("user", provider.config.UserPassword, types.MemberProfile{
2021-11-04 09:59:38 +13:00
Name: "User",
IsAdmin: false,
CanLogin: true,
CanConnect: true,
CanWatch: true,
CanHost: true,
2021-12-10 09:48:02 +13:00
CanShareMedia: true,
2021-11-04 09:59:38 +13:00
CanAccessClipboard: true,
SendsInactiveCursor: true,
CanSeeInactiveCursors: false,
})
}
return err
2021-03-15 00:57:19 +13:00
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) Disconnect() error {
2021-03-15 00:57:19 +13:00
return nil
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) Authenticate(username string, password string) (string, types.MemberProfile, error) {
2021-03-15 00:57:19 +13:00
// id will be also username
id := username
2021-03-15 04:58:18 +13:00
entry, ok := provider.entries[id]
2021-03-15 00:57:19 +13:00
if !ok {
2021-08-30 03:09:13 +12:00
return "", types.MemberProfile{}, types.ErrMemberDoesNotExist
2021-03-15 00:57:19 +13:00
}
// TODO: Use hash function.
if entry.Password != password {
2021-08-30 03:09:13 +12:00
return "", types.MemberProfile{}, types.ErrMemberInvalidPassword
2021-03-15 00:57:19 +13:00
}
return id, entry.Profile, nil
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) Insert(username string, password string, profile types.MemberProfile) (string, error) {
2021-03-15 00:57:19 +13:00
// id will be also username
id := username
2021-03-15 04:58:18 +13:00
_, ok := provider.entries[id]
2021-03-15 00:57:19 +13:00
if ok {
2021-08-30 03:09:13 +12:00
return "", types.ErrMemberAlreadyExists
2021-03-15 00:57:19 +13:00
}
2021-03-15 04:58:18 +13:00
provider.entries[id] = &MemberEntry{
2021-03-15 02:44:03 +13:00
// TODO: Use hash function.
Password: password,
2021-03-15 08:26:58 +13:00
Profile: profile,
2021-03-15 02:44:03 +13:00
}
2021-03-15 00:57:19 +13:00
return id, nil
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) UpdateProfile(id string, profile types.MemberProfile) error {
entry, ok := provider.entries[id]
2021-03-15 00:57:19 +13:00
if !ok {
2021-08-30 03:09:13 +12:00
return types.ErrMemberDoesNotExist
2021-03-15 00:57:19 +13:00
}
entry.Profile = profile
return nil
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) UpdatePassword(id string, password string) error {
entry, ok := provider.entries[id]
2021-03-15 00:57:19 +13:00
if !ok {
2021-08-30 03:09:13 +12:00
return types.ErrMemberDoesNotExist
2021-03-15 00:57:19 +13:00
}
// TODO: Use hash function.
entry.Password = password
return nil
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) Select(id string) (types.MemberProfile, error) {
entry, ok := provider.entries[id]
2021-03-15 02:44:03 +13:00
if !ok {
2021-08-30 03:09:13 +12:00
return types.MemberProfile{}, types.ErrMemberDoesNotExist
2021-03-15 00:57:19 +13:00
}
return entry.Profile, nil
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) SelectAll(limit int, offset int) (map[string]types.MemberProfile, error) {
2021-03-15 02:44:03 +13:00
profiles := make(map[string]types.MemberProfile)
2021-03-15 00:57:19 +13:00
i := 0
2021-03-15 04:58:18 +13:00
for id, entry := range provider.entries {
2021-03-15 02:44:03 +13:00
if i >= offset && (limit == 0 || i < offset+limit) {
profiles[id] = entry.Profile
2021-03-15 00:57:19 +13:00
}
i = i + 1
}
return profiles, nil
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) Delete(id string) error {
_, ok := provider.entries[id]
2021-03-15 00:57:19 +13:00
if !ok {
2021-08-30 03:09:13 +12:00
return types.ErrMemberDoesNotExist
2021-03-15 00:57:19 +13:00
}
2021-03-15 04:58:18 +13:00
delete(provider.entries, id)
2021-03-15 00:57:19 +13:00
return nil
}