store passwords on disk using JSON instead of gob

This commit is contained in:
1e99 2024-10-30 11:51:20 +01:00
parent aaa1715f32
commit 7536e5d9af
3 changed files with 20 additions and 15 deletions

View file

@ -1,7 +1,7 @@
package storage package storage
import ( import (
"encoding/gob" "encoding/json"
"log" "log"
"os" "os"
"path" "path"
@ -20,6 +20,11 @@ type dir struct {
path string path string
} }
type dirEntry struct {
Password []byte `json:"password"`
ExpiresAt time.Time `json:"expires-at"`
}
func (store *dir) Create(password []byte, expiresAt time.Time) (string, error) { func (store *dir) Create(password []byte, expiresAt time.Time) (string, error) {
for range 1000 { for range 1000 {
id := generateId(24) id := generateId(24)
@ -39,12 +44,12 @@ func (store *dir) Create(password []byte, expiresAt time.Time) (string, error) {
defer file.Close() defer file.Close()
entry := entry{ entry := dirEntry{
Password: password, Password: password,
ExpiresAt: expiresAt, ExpiresAt: expiresAt,
} }
err = gob.NewEncoder(file).Encode(&entry) err = json.NewEncoder(file).Encode(&entry)
if err != nil { if err != nil {
log.Printf("%s", err) log.Printf("%s", err)
return "", err return "", err
@ -72,8 +77,8 @@ func (store *dir) Get(id string) ([]byte, error) {
defer file.Close() defer file.Close()
var entry entry var entry dirEntry
err = gob.NewDecoder(file).Decode(&entry) err = json.NewDecoder(file).Decode(&entry)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -113,8 +118,8 @@ func (store *dir) ClearExpired() error {
defer file.Close() defer file.Close()
var entry entry var entry dirEntry
err = gob.NewDecoder(file).Decode(&entry) err = json.NewDecoder(file).Decode(&entry)
if err != nil { if err != nil {
return err return err
} }

View file

@ -7,7 +7,7 @@ import (
func NewRamStore() Store { func NewRamStore() Store {
store := &ram{ store := &ram{
passwords: make(map[string]entry), passwords: make(map[string]ramEntry),
lock: sync.Mutex{}, lock: sync.Mutex{},
} }
@ -15,10 +15,15 @@ func NewRamStore() Store {
} }
type ram struct { type ram struct {
passwords map[string]entry passwords map[string]ramEntry
lock sync.Mutex lock sync.Mutex
} }
type ramEntry struct {
Password []byte
ExpiresAt time.Time
}
func (store *ram) Create(password []byte, expiresAt time.Time) (string, error) { func (store *ram) Create(password []byte, expiresAt time.Time) (string, error) {
store.lock.Lock() store.lock.Lock()
defer store.lock.Unlock() defer store.lock.Unlock()
@ -30,7 +35,7 @@ func (store *ram) Create(password []byte, expiresAt time.Time) (string, error) {
continue continue
} }
store.passwords[id] = entry{ store.passwords[id] = ramEntry{
Password: password, Password: password,
ExpiresAt: expiresAt, ExpiresAt: expiresAt,
} }

View file

@ -14,11 +14,6 @@ var (
ErrFull = errors.New("storage is filled") ErrFull = errors.New("storage is filled")
) )
type entry struct {
Password []byte
ExpiresAt time.Time
}
type Store interface { type Store interface {
Create(password []byte, expiresAt time.Time) (string, error) Create(password []byte, expiresAt time.Time) (string, error)
Get(id string) ([]byte, error) Get(id string) ([]byte, error)