Compare commits

...

2 commits

Author SHA1 Message Date
1e99
b0baf85078 add endpoint to check if password exists 2024-10-30 00:36:53 +01:00
1e99
c8bbe24d4d add configurable clear interval 2024-10-30 00:25:53 +01:00
4 changed files with 48 additions and 11 deletions

View file

@ -26,6 +26,7 @@ func run() error {
mux.Handle("GET /", routes.ServeFiles(embedFS, "static"))
mux.Handle("POST /api/password", routes.CreatePassword(storage, 12*1024, base64.StdEncoding))
mux.Handle("GET /api/password/{id}", routes.GetPassword(storage, base64.StdEncoding))
mux.Handle("HEAD /api/password/{id}", routes.HasPassword(storage))
address := os.Getenv("PASSED_ADDRESS")
if address == "" {

24
routes/has_password.go Normal file
View file

@ -0,0 +1,24 @@
package routes
import (
"net/http"
"git.1e99.eu/1e99/passed/storage"
)
func HasPassword(store storage.Store) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
id := req.PathValue("id")
found, err := store.HasPassword(id)
if err != nil {
http.Error(res, "", http.StatusInternalServerError)
return
}
if found {
res.WriteHeader(http.StatusNoContent)
} else {
res.WriteHeader(http.StatusNotFound)
}
}
}

View file

@ -6,11 +6,12 @@ import (
"time"
)
func NewRamStore() Store {
func NewRamStore(clearInterval time.Duration) Store {
store := &ram{
passwords: make(map[string]entry),
lock: sync.Mutex{},
close: make(chan bool),
clearInterval: clearInterval,
passwords: make(map[string]entry),
lock: sync.Mutex{},
close: make(chan bool),
}
go store.clearExpired()
@ -18,9 +19,10 @@ func NewRamStore() Store {
}
type ram struct {
passwords map[string]entry
lock sync.Mutex
close chan bool
clearInterval time.Duration
passwords map[string]entry
lock sync.Mutex
close chan bool
}
func (store *ram) CreatePassword(password []byte, expiresAt time.Time) (string, error) {
@ -58,13 +60,21 @@ func (store *ram) GetPassword(id string) ([]byte, error) {
return password.password, nil
}
func (store *ram) HasPassword(id string) (bool, error) {
store.lock.Lock()
defer store.lock.Unlock()
_, found := store.passwords[id]
return found, nil
}
func (store *ram) Close() error {
store.close <- true
return nil
}
func (store *ram) clearExpired() error {
ticker := time.NewTicker(20 * time.Second)
ticker := time.NewTicker(store.clearInterval)
defer ticker.Stop()
for {
@ -73,7 +83,6 @@ func (store *ram) clearExpired() error {
return nil
case <-ticker.C:
store.lock.Lock()
defer store.lock.Unlock()
time := time.Now()
for id, password := range store.passwords {
@ -81,6 +90,8 @@ func (store *ram) clearExpired() error {
delete(store.passwords, id)
}
}
store.lock.Unlock()
}
}
}

View file

@ -21,6 +21,7 @@ type entry struct {
type Store interface {
CreatePassword(password []byte, expiresAt time.Time) (string, error)
GetPassword(id string) ([]byte, error)
HasPassword(id string) (bool, error)
Close() error
}
@ -30,10 +31,10 @@ func NewStore() (Store, error) {
switch storeType {
case "ram":
return NewRamStore(), nil
return NewRamStore(20 * time.Second), nil
default:
log.Printf("No PASSED_STORE_TYPE provided, defaulting to memory store.")
return NewRamStore(), nil
return NewRamStore(20 * time.Second), nil
}
}