diff options
| author | Ryan Mehri <[email protected]> | 2020-05-12 23:12:18 -0600 |
|---|---|---|
| committer | Ryan Mehri <[email protected]> | 2020-05-12 23:12:18 -0600 |
| commit | 03c97b801ce55287eb25f21b48130a857a5f4a4a (patch) | |
| tree | 2573730eb9b0c74cbcdab172e47eda9f6b23606a /backend | |
| parent | Check password when paste is cached (diff) | |
| download | ctrl-v-03c97b801ce55287eb25f21b48130a857a5f4a4a.tar.xz ctrl-v-03c97b801ce55287eb25f21b48130a857a5f4a4a.zip | |
Remove duplication
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/cache/cache.go | 41 |
1 files changed, 13 insertions, 28 deletions
diff --git a/backend/cache/cache.go b/backend/cache/cache.go index 1bbec78..71007e5 100644 --- a/backend/cache/cache.go +++ b/backend/cache/cache.go @@ -28,45 +28,30 @@ func (c *Cache) Get(hash, userPassword string) (db.Paste, error) { c.lock.RLock() // check if hash in cache - v, ok := c.m[hash] + p, ok := c.m[hash] c.lock.RUnlock() - if ok { - // validate password - passErr := checkPassword(v.Password, userPassword) - if passErr != nil { - return db.Paste{}, passErr - } else { - return v, nil - } - } - // if it doesnt, lookup from db - p, err := db.Lookup(hash) - if err != nil { - return p, PasteNotFound - } + if !ok { + var err error - // validate password - passErr := checkPassword(p.Password, userPassword) - if passErr != nil { - return db.Paste{}, passErr - } + p, err = db.Lookup(hash) + if err != nil { + return db.Paste{}, PasteNotFound + } - c.add(p) - return p, err -} + c.add(p) + } -func checkPassword(dbPassword, parsedPassword string) error { // if there is a password, check the provided one against it - if dbPassword != "" { + if p.Password != "" { // if passwords do not match, the user is unauthorized - if !hashing.PasswordsEqual(dbPassword, parsedPassword) { - return UserUnauthorized + if !hashing.PasswordsEqual(p.Password, userPassword) { + return db.Paste{}, UserUnauthorized } } - return nil + return p, nil } func (c *Cache) add(p db.Paste) { |