Skip to content

Commit

Permalink
added an unlimited_viewsoption
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaardsholt committed Oct 8, 2021
1 parent 1237999 commit 6e54b61
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
12 changes: 2 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,11 @@ import (

"github.com/Gaardsholt/pass-along/config"
"github.com/Gaardsholt/pass-along/secret"
. "github.com/Gaardsholt/pass-along/types"
"github.com/gorilla/mux"
"github.com/rs/zerolog/log"
)

type Page struct {
Content string `json:"content"`
Startup time.Time `json:"startup"`
}
type Entry struct {
Content string `json:"content"`
ExpiresIn int `json:"expires_in"`
}

var secretStore secret.SecretStore
var templates map[string]*template.Template
var startupTime time.Time
Expand Down Expand Up @@ -71,7 +63,7 @@ func NewHandler(w http.ResponseWriter, r *http.Request) {

log.Debug().Msg("Creating a new secret")

myId, err := secretStore.Add(entry.Content, entry.ExpiresIn)
myId, err := secretStore.Add(entry)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
Expand Down
23 changes: 15 additions & 8 deletions secret/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import (
"time"

"github.com/Gaardsholt/pass-along/config"
. "github.com/Gaardsholt/pass-along/types"
"golang.org/x/crypto/pbkdf2"
)

type Secret struct {
Content string `json:"content"`
Expires time.Time `json:"expires"`
TimeAdded time.Time `json:"time_added"`
Content string `json:"content"`
Expires time.Time `json:"expires"`
TimeAdded time.Time `json:"time_added"`
UnlimitedViews bool `json:"unlimited_views"`
}

type SecretStore map[string][]byte
Expand Down Expand Up @@ -106,14 +108,15 @@ func decrypt(ciphertext []byte, encryptionKey string) (*Secret, error) {
return &p, nil
}

func (ss SecretStore) Add(content string, expiresIn int) (id string, err error) {
func (ss SecretStore) Add(entry Entry) (id string, err error) {
expires := time.Now().Add(
time.Hour*time.Duration(0) +
time.Minute*time.Duration(0) +
time.Second*time.Duration(expiresIn),
time.Second*time.Duration(entry.ExpiresIn),
)

mySecret := new(content, expires)
mySecret := new(entry.Content, expires)
mySecret.UnlimitedViews = entry.UnlimitedViews
id = mySecret.hash()

baah, err := mySecret.encrypt(id)
Expand All @@ -132,13 +135,17 @@ func (ss SecretStore) Get(id string) (content string, gotData bool) {
if err != nil {
log.Fatal(err)
}
if s.Expires.After(time.Now().UTC()) {

isExpired := s.Expires.UTC().After(time.Now().UTC())
if isExpired {
content = s.Content
} else {
gotData = false
}

delete(ss, id)
if !isExpired || !s.UnlimitedViews {
delete(ss, id)
}
}

return content, gotData
Expand Down
13 changes: 13 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package types

import "time"

type Page struct {
Content string `json:"content"`
Startup time.Time `json:"startup"`
}
type Entry struct {
Content string `json:"content"`
ExpiresIn int `json:"expires_in"`
UnlimitedViews bool `json:"unlimited_views"`
}

0 comments on commit 6e54b61

Please sign in to comment.