From ddbf4935b9b9adffa5f2939e56cb49cbd5139dd1 Mon Sep 17 00:00:00 2001 From: Ryan Mehri Date: Sun, 10 May 2020 17:00:46 -0600 Subject: Update post to return hash and password --- backend/api/routes.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'backend/api') diff --git a/backend/api/routes.go b/backend/api/routes.go index a65c886..3b08781 100644 --- a/backend/api/routes.go +++ b/backend/api/routes.go @@ -23,6 +23,7 @@ func insertFunc(w http.ResponseWriter, r *http.Request) { expiry := r.FormValue("expiry") content := r.FormValue("content") title := r.FormValue("title") + password := r.FormValue("password") // get ip ip := getIP(r) @@ -30,11 +31,21 @@ func insertFunc(w http.ResponseWriter, r *http.Request) { log.Infof("got content '%s' and ip '%s'", content, ip) // insert content - err := db.New(ip, content, expiry, title) + paste, err := db.New(ip, content, expiry, title, password) if err != nil { w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, "got err: %s", err.Error()) } + + // if successful return paste hash + w.Header().Set("Content-Type", "application/json") + pasteMap := map[string]interface{}{ + "hash": paste.Hash, + "password": paste.Password, + } + + jsonData, _ := json.Marshal(pasteMap) + fmt.Fprintf(w, "%+v", string(jsonData)) } func getHashFunc(w http.ResponseWriter, r *http.Request) { -- cgit v1.2.3 From 01d3631b893b09ec4c5f9daade727e8f88aa8d22 Mon Sep 17 00:00:00 2001 From: Ryan Mehri Date: Sun, 10 May 2020 17:32:31 -0600 Subject: Add password hashing --- backend/api/routes.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'backend/api') diff --git a/backend/api/routes.go b/backend/api/routes.go index 3b08781..d578632 100644 --- a/backend/api/routes.go +++ b/backend/api/routes.go @@ -31,7 +31,7 @@ func insertFunc(w http.ResponseWriter, r *http.Request) { log.Infof("got content '%s' and ip '%s'", content, ip) // insert content - paste, err := db.New(ip, content, expiry, title, password) + hash, err := db.New(ip, content, expiry, title, password) if err != nil { w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, "got err: %s", err.Error()) @@ -40,8 +40,7 @@ func insertFunc(w http.ResponseWriter, r *http.Request) { // if successful return paste hash w.Header().Set("Content-Type", "application/json") pasteMap := map[string]interface{}{ - "hash": paste.Hash, - "password": paste.Password, + "hash": hash, } jsonData, _ := json.Marshal(pasteMap) @@ -59,10 +58,11 @@ func getHashFunc(w http.ResponseWriter, r *http.Request) { return } - // otherwise, return paste content and current time + // otherwise, return paste content, title, and current time w.Header().Set("Content-Type", "application/json") pasteMap := map[string]interface{}{ "timestamp": time.Now(), + "title": paste.Title, "content": paste.Content, } -- cgit v1.2.3 From 86cb3213840403053aa9336dfdc5c947807c73d9 Mon Sep 17 00:00:00 2001 From: Ryan Mehri Date: Sun, 10 May 2020 18:09:49 -0600 Subject: Add auth check to get --- backend/api/routes.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'backend/api') diff --git a/backend/api/routes.go b/backend/api/routes.go index d578632..f41505b 100644 --- a/backend/api/routes.go +++ b/backend/api/routes.go @@ -52,9 +52,16 @@ func getHashFunc(w http.ResponseWriter, r *http.Request) { paste, err := cache.C.Get(hash) // if hash was not found - if err != nil { + if err == cache.PasteNotFound { w.WriteHeader(http.StatusNotFound) - fmt.Fprintf(w, "got err: %s", err.Error()) + fmt.Fprintf(w, "got err: %s", err) + return + } + + // if paste is password protected + if err == cache.UserUnauthorized { + w.WriteHeader(http.StatusUnauthorized) + fmt.Fprintf(w, "got err: %s", err) return } @@ -64,6 +71,7 @@ func getHashFunc(w http.ResponseWriter, r *http.Request) { "timestamp": time.Now(), "title": paste.Title, "content": paste.Content, + "expiry": paste.Expiry, } jsonData, _ := json.Marshal(pasteMap) -- cgit v1.2.3