Use a couple of defer in internal/home/auth.go

This commit is contained in:
jvoisin 2020-12-21 19:39:39 +01:00
parent bdff46ec1d
commit 7eb3e00b35
1 changed files with 2 additions and 7 deletions

View File

@ -230,16 +230,15 @@ func (a *Auth) CheckSession(sess string) int {
update := false update := false
a.lock.Lock() a.lock.Lock()
defer a.lock.Unlock()
s, ok := a.sessions[sess] s, ok := a.sessions[sess]
if !ok { if !ok {
a.lock.Unlock()
return -1 return -1
} }
if s.expire <= now { if s.expire <= now {
delete(a.sessions, sess) delete(a.sessions, sess)
key, _ := hex.DecodeString(sess) key, _ := hex.DecodeString(sess)
a.removeSession(key) a.removeSession(key)
a.lock.Unlock()
return 1 return 1
} }
@ -250,8 +249,6 @@ func (a *Auth) CheckSession(sess string) int {
s.expire = newExpire s.expire = newExpire
} }
a.lock.Unlock()
if update { if update {
key, _ := hex.DecodeString(sess) key, _ := hex.DecodeString(sess)
if a.storeSession(key, s) { if a.storeSession(key, s) {
@ -517,18 +514,16 @@ func (a *Auth) GetCurrentUser(r *http.Request) User {
} }
a.lock.Lock() a.lock.Lock()
defer a.lock.Unlock()
s, ok := a.sessions[cookie.Value] s, ok := a.sessions[cookie.Value]
if !ok { if !ok {
a.lock.Unlock()
return User{} return User{}
} }
for _, u := range a.users { for _, u := range a.users {
if u.Name == s.userName { if u.Name == s.userName {
a.lock.Unlock()
return u return u
} }
} }
a.lock.Unlock()
return User{} return User{}
} }