2021-03-15 00:57:19 +13:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
2023-11-20 03:31:18 +13:00
|
|
|
"crypto/sha256"
|
2021-03-15 00:57:19 +13:00
|
|
|
"encoding/json"
|
2021-08-30 03:12:37 +12:00
|
|
|
"io"
|
2021-03-15 00:57:19 +13:00
|
|
|
"os"
|
|
|
|
|
2022-07-14 10:58:22 +12:00
|
|
|
"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 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 00:57:19 +13:00
|
|
|
}
|
|
|
|
|
2023-11-20 03:31:18 +13:00
|
|
|
func (provider *MemberProviderCtx) hash(password string) string {
|
|
|
|
// if hash is disabled, return password as plain text
|
|
|
|
if !provider.config.Hash {
|
|
|
|
return password
|
|
|
|
}
|
|
|
|
|
|
|
|
sha256 := sha256.New()
|
|
|
|
sha256.Write([]byte(password))
|
|
|
|
return string(sha256.Sum(nil))
|
|
|
|
}
|
|
|
|
|
2021-03-15 04:58:18 +13:00
|
|
|
func (provider *MemberProviderCtx) Connect() error {
|
2021-03-15 00:57:19 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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, err := provider.getEntry(id)
|
2021-03-15 00:57:19 +13:00
|
|
|
if err != nil {
|
|
|
|
return "", types.MemberProfile{}, err
|
|
|
|
}
|
|
|
|
|
2023-11-20 03:31:18 +13:00
|
|
|
if entry.Password != provider.hash(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
|
|
|
entries, err := provider.deserialize()
|
2021-03-15 00:57:19 +13:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2021-03-15 02:44:03 +13:00
|
|
|
_, ok := 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 02:44:03 +13:00
|
|
|
entries[id] = MemberEntry{
|
2023-11-20 03:31:18 +13:00
|
|
|
Password: provider.hash(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
|
|
|
|
2021-03-15 04:58:18 +13:00
|
|
|
return id, provider.serialize(entries)
|
2021-03-15 00:57:19 +13:00
|
|
|
}
|
|
|
|
|
2021-03-15 04:58:18 +13:00
|
|
|
func (provider *MemberProviderCtx) UpdateProfile(id string, profile types.MemberProfile) error {
|
|
|
|
entries, err := provider.deserialize()
|
2021-03-15 00:57:19 +13:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
entry, ok := entries[id]
|
|
|
|
if !ok {
|
2021-08-30 03:09:13 +12:00
|
|
|
return types.ErrMemberDoesNotExist
|
2021-03-15 00:57:19 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
entry.Profile = profile
|
|
|
|
entries[id] = entry
|
|
|
|
|
2021-03-15 04:58:18 +13:00
|
|
|
return provider.serialize(entries)
|
2021-03-15 00:57:19 +13:00
|
|
|
}
|
|
|
|
|
2021-03-15 04:58:18 +13:00
|
|
|
func (provider *MemberProviderCtx) UpdatePassword(id string, password string) error {
|
|
|
|
entries, err := provider.deserialize()
|
2021-03-15 00:57:19 +13:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
entry, ok := entries[id]
|
|
|
|
if !ok {
|
2021-08-30 03:09:13 +12:00
|
|
|
return types.ErrMemberDoesNotExist
|
2021-03-15 00:57:19 +13:00
|
|
|
}
|
|
|
|
|
2023-11-20 03:31:18 +13:00
|
|
|
entry.Password = provider.hash(password)
|
2021-03-15 00:57:19 +13:00
|
|
|
entries[id] = entry
|
|
|
|
|
2021-03-15 04:58:18 +13:00
|
|
|
return provider.serialize(entries)
|
2021-03-15 00:57:19 +13:00
|
|
|
}
|
|
|
|
|
2021-03-15 04:58:18 +13:00
|
|
|
func (provider *MemberProviderCtx) Select(id string) (types.MemberProfile, error) {
|
|
|
|
entry, err := provider.getEntry(id)
|
2021-03-15 00:57:19 +13:00
|
|
|
if err != nil {
|
|
|
|
return types.MemberProfile{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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 00:57:19 +13:00
|
|
|
profiles := map[string]types.MemberProfile{}
|
|
|
|
|
2021-03-15 04:58:18 +13:00
|
|
|
entries, err := provider.deserialize()
|
2021-03-15 00:57:19 +13:00
|
|
|
if err != nil {
|
|
|
|
return profiles, err
|
|
|
|
}
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for id, entry := range 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 {
|
|
|
|
entries, err := provider.deserialize()
|
2021-03-15 00:57:19 +13:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := entries[id]
|
|
|
|
if !ok {
|
2021-08-30 03:09:13 +12:00
|
|
|
return types.ErrMemberDoesNotExist
|
2021-03-15 00:57:19 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
delete(entries, id)
|
|
|
|
|
2021-03-15 04:58:18 +13:00
|
|
|
return provider.serialize(entries)
|
2021-03-15 00:57:19 +13:00
|
|
|
}
|
|
|
|
|
2021-03-15 04:58:18 +13:00
|
|
|
func (provider *MemberProviderCtx) deserialize() (map[string]MemberEntry, error) {
|
2021-03-17 03:28:40 +13:00
|
|
|
file, err := os.OpenFile(provider.config.Path, os.O_RDONLY|os.O_CREATE, os.ModePerm)
|
2021-03-15 00:57:19 +13:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-30 03:12:37 +12:00
|
|
|
raw, err := io.ReadAll(file)
|
2021-03-15 00:57:19 +13:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(raw) == 0 {
|
|
|
|
return map[string]MemberEntry{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var entries map[string]MemberEntry
|
|
|
|
if err := json.Unmarshal([]byte(raw), &entries); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries, nil
|
|
|
|
}
|
|
|
|
|
2021-03-15 04:58:18 +13:00
|
|
|
func (provider *MemberProviderCtx) getEntry(id string) (MemberEntry, error) {
|
|
|
|
entries, err := provider.deserialize()
|
2021-03-15 00:57:19 +13:00
|
|
|
if err != nil {
|
|
|
|
return MemberEntry{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
entry, ok := entries[id]
|
|
|
|
if !ok {
|
2021-08-30 03:09:13 +12:00
|
|
|
return MemberEntry{}, types.ErrMemberDoesNotExist
|
2021-03-15 00:57:19 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
return entry, nil
|
|
|
|
}
|
|
|
|
|
2021-03-15 04:58:18 +13:00
|
|
|
func (provider *MemberProviderCtx) serialize(data map[string]MemberEntry) error {
|
2021-03-15 00:57:19 +13:00
|
|
|
raw, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-30 03:12:37 +12:00
|
|
|
return os.WriteFile(provider.config.Path, raw, os.ModePerm)
|
2021-03-15 00:57:19 +13:00
|
|
|
}
|