Compare commits
2 commits
dfa37fc7fa
...
b0baf85078
Author | SHA1 | Date | |
---|---|---|---|
|
b0baf85078 | ||
|
c8bbe24d4d |
4 changed files with 48 additions and 11 deletions
1
main.go
1
main.go
|
@ -26,6 +26,7 @@ func run() error {
|
||||||
mux.Handle("GET /", routes.ServeFiles(embedFS, "static"))
|
mux.Handle("GET /", routes.ServeFiles(embedFS, "static"))
|
||||||
mux.Handle("POST /api/password", routes.CreatePassword(storage, 12*1024, base64.StdEncoding))
|
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("GET /api/password/{id}", routes.GetPassword(storage, base64.StdEncoding))
|
||||||
|
mux.Handle("HEAD /api/password/{id}", routes.HasPassword(storage))
|
||||||
|
|
||||||
address := os.Getenv("PASSED_ADDRESS")
|
address := os.Getenv("PASSED_ADDRESS")
|
||||||
if address == "" {
|
if address == "" {
|
||||||
|
|
24
routes/has_password.go
Normal file
24
routes/has_password.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,8 +6,9 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewRamStore() Store {
|
func NewRamStore(clearInterval time.Duration) Store {
|
||||||
store := &ram{
|
store := &ram{
|
||||||
|
clearInterval: clearInterval,
|
||||||
passwords: make(map[string]entry),
|
passwords: make(map[string]entry),
|
||||||
lock: sync.Mutex{},
|
lock: sync.Mutex{},
|
||||||
close: make(chan bool),
|
close: make(chan bool),
|
||||||
|
@ -18,6 +19,7 @@ func NewRamStore() Store {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ram struct {
|
type ram struct {
|
||||||
|
clearInterval time.Duration
|
||||||
passwords map[string]entry
|
passwords map[string]entry
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
close chan bool
|
close chan bool
|
||||||
|
@ -58,13 +60,21 @@ func (store *ram) GetPassword(id string) ([]byte, error) {
|
||||||
return password.password, nil
|
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 {
|
func (store *ram) Close() error {
|
||||||
store.close <- true
|
store.close <- true
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *ram) clearExpired() error {
|
func (store *ram) clearExpired() error {
|
||||||
ticker := time.NewTicker(20 * time.Second)
|
ticker := time.NewTicker(store.clearInterval)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
@ -73,7 +83,6 @@ func (store *ram) clearExpired() error {
|
||||||
return nil
|
return nil
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
store.lock.Lock()
|
store.lock.Lock()
|
||||||
defer store.lock.Unlock()
|
|
||||||
time := time.Now()
|
time := time.Now()
|
||||||
|
|
||||||
for id, password := range store.passwords {
|
for id, password := range store.passwords {
|
||||||
|
@ -81,6 +90,8 @@ func (store *ram) clearExpired() error {
|
||||||
delete(store.passwords, id)
|
delete(store.passwords, id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
store.lock.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,7 @@ type entry struct {
|
||||||
type Store interface {
|
type Store interface {
|
||||||
CreatePassword(password []byte, expiresAt time.Time) (string, error)
|
CreatePassword(password []byte, expiresAt time.Time) (string, error)
|
||||||
GetPassword(id string) ([]byte, error)
|
GetPassword(id string) ([]byte, error)
|
||||||
|
HasPassword(id string) (bool, error)
|
||||||
Close() error
|
Close() error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,10 +31,10 @@ func NewStore() (Store, error) {
|
||||||
|
|
||||||
switch storeType {
|
switch storeType {
|
||||||
case "ram":
|
case "ram":
|
||||||
return NewRamStore(), nil
|
return NewRamStore(20 * time.Second), nil
|
||||||
|
|
||||||
default:
|
default:
|
||||||
log.Printf("No PASSED_STORE_TYPE provided, defaulting to memory store.")
|
log.Printf("No PASSED_STORE_TYPE provided, defaulting to memory store.")
|
||||||
return NewRamStore(), nil
|
return NewRamStore(20 * time.Second), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue