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/db/db.go | 17 +++++++++-------- backend/db/schemas.go | 11 ++++++----- 2 files changed, 15 insertions(+), 13 deletions(-) (limited to 'backend/db') diff --git a/backend/db/db.go b/backend/db/db.go index 4451f34..d40de87 100644 --- a/backend/db/db.go +++ b/backend/db/db.go @@ -28,7 +28,7 @@ const TitleLimit = 100 const ContentLimit = 100000 // creates a new paste with title, content and hash -func New(ip, content, expiry, title string) error { +func New(ip, content, expiry, title, password string) (Paste, error) { // generate hash from ip hash := hashing.GenerateURI(ip) @@ -42,14 +42,15 @@ func New(ip, content, expiry, title string) error { } // if any errors were found if errs != "" { - return fmt.Errorf(errs) + return Paste{}, fmt.Errorf(errs) } // create new struct new := Paste{ - Hash: hash, - Content: content, - Title: title, + Hash: hash, + Content: content, + Title: title, + Password: password, } // check if expiry @@ -58,12 +59,12 @@ func New(ip, content, expiry, title string) error { // if time format not current if err != nil { - return err + return Paste{}, err } // time is in the past if time.Now().After(t) { - return fmt.Errorf("time %s is in the past", t.String()) + return Paste{}, fmt.Errorf("time %s is in the past", t.String()) } new.Expiry = t @@ -76,7 +77,7 @@ func New(ip, content, expiry, title string) error { // insert struct log.Infof("create new paste with hash %s", hash) insertErr := insert(new) - return insertErr + return new, insertErr } // lookup diff --git a/backend/db/schemas.go b/backend/db/schemas.go index 62c5a11..4c73f82 100644 --- a/backend/db/schemas.go +++ b/backend/db/schemas.go @@ -8,9 +8,10 @@ import ( // Paste represents a single paste type Paste struct { - ID bson.ObjectId `bson:"_id,omitempty"` - Hash string - Content string - Expiry time.Time `bson:"expiry"` - Title string + ID bson.ObjectId `bson:"_id,omitempty"` + Hash string + Content string + Expiry time.Time `bson:"expiry"` + Title string + Password string } -- 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/db/db.go | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) (limited to 'backend/db') diff --git a/backend/db/db.go b/backend/db/db.go index d40de87..9bfe55a 100644 --- a/backend/db/db.go +++ b/backend/db/db.go @@ -27,22 +27,21 @@ func init() { const TitleLimit = 100 const ContentLimit = 100000 -// creates a new paste with title, content and hash -func New(ip, content, expiry, title, password string) (Paste, error) { +// creates a new paste with title, content and hash, returns the hash of the created paste +func New(ip, content, expiry, title, password string) (string, error) { // generate hash from ip hash := hashing.GenerateURI(ip) // check for size of title and content - errs := "" - if len(title) > TitleLimit { - errs += fmt.Sprintf("title is longer than character limit of %d\n", TitleLimit) - } - if len(content) > ContentLimit { - errs += fmt.Sprintf("content is longer than character limit of %d\n", ContentLimit) + errs := checkLengths(title, content) + if errs != nil { + return "", errs } - // if any errors were found - if errs != "" { - return Paste{}, fmt.Errorf(errs) + + // hash given password + hashedPass, err := hashing.HashPassword(password) + if err != nil { + return "", fmt.Errorf("could not hash password: %s", err.Error()) } // create new struct @@ -50,7 +49,7 @@ func New(ip, content, expiry, title, password string) (Paste, error) { Hash: hash, Content: content, Title: title, - Password: password, + Password: hashedPass, } // check if expiry @@ -59,12 +58,12 @@ func New(ip, content, expiry, title, password string) (Paste, error) { // if time format not current if err != nil { - return Paste{}, err + return "", err } // time is in the past if time.Now().After(t) { - return Paste{}, fmt.Errorf("time %s is in the past", t.String()) + return "", fmt.Errorf("time %s is in the past", t.String()) } new.Expiry = t @@ -77,7 +76,23 @@ func New(ip, content, expiry, title, password string) (Paste, error) { // insert struct log.Infof("create new paste with hash %s", hash) insertErr := insert(new) - return new, insertErr + return hash, insertErr +} + +func checkLengths(title string, content string) error { + errs := "" + if len(title) > TitleLimit { + errs += fmt.Sprintf("title is longer than character limit of %d\n", TitleLimit) + } + if len(content) > ContentLimit { + errs += fmt.Sprintf("content is longer than character limit of %d\n", ContentLimit) + } + // if any errors were found + if errs != "" { + return fmt.Errorf(errs) + } + + return nil } // lookup -- cgit v1.2.3