Skip to content

Commit

Permalink
home: imp docs, spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
ainar-g committed Apr 6, 2021
1 parent 7392cba commit 4457725
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions internal/home/auth.go
Expand Up @@ -88,11 +88,13 @@ func InitAuth(dbFilename string, users []User, sessionTTL uint32) *Auth {
if err.Error() == "invalid argument" {
log.Error("AdGuard Home cannot be initialized due to an incompatible file system.\nPlease read the explanation here: https://github.com/AdguardTeam/AdGuardHome/internal/wiki/Getting-Started#limitations")
}

return nil
}
a.loadSessions()
a.users = users
log.Info("auth: initialized. users:%d sessions:%d", len(a.users), len(a.sessions))

return &a
}

Expand All @@ -110,6 +112,7 @@ func (a *Auth) loadSessions() {
tx, err := a.db.Begin(true)
if err != nil {
log.Error("auth: bbolt.Begin: %s", err)

return
}
defer func() {
Expand Down Expand Up @@ -138,6 +141,7 @@ func (a *Auth) loadSessions() {
} else {
removed++
}

return nil
}

Expand All @@ -151,6 +155,7 @@ func (a *Auth) loadSessions() {
log.Error("bolt.Commit(): %s", err)
}
}

log.Debug("auth: loaded %d sessions from DB (removed %d expired)", len(a.sessions), removed)
}

Expand All @@ -170,6 +175,7 @@ func (a *Auth) storeSession(data []byte, s *session) bool {
tx, err := a.db.Begin(true)
if err != nil {
log.Error("auth: bbolt.Begin: %s", err)

return false
}
defer func() {
Expand All @@ -179,19 +185,24 @@ func (a *Auth) storeSession(data []byte, s *session) bool {
bkt, err := tx.CreateBucketIfNotExists(bucketName())
if err != nil {
log.Error("auth: bbolt.CreateBucketIfNotExists: %s", err)

return false
}

err = bkt.Put(data, s.serialize())
if err != nil {
log.Error("auth: bbolt.Put: %s", err)

return false
}

err = tx.Commit()
if err != nil {
log.Error("auth: bbolt.Commit: %s", err)

return false
}

return true
}

Expand All @@ -200,26 +211,32 @@ func (a *Auth) removeSession(sess []byte) {
tx, err := a.db.Begin(true)
if err != nil {
log.Error("auth: bbolt.Begin: %s", err)

return
}

defer func() {
_ = tx.Rollback()
}()

bkt := tx.Bucket(bucketName())
if bkt == nil {
log.Error("auth: bbolt.Bucket")

return
}

err = bkt.Delete(sess)
if err != nil {
log.Error("auth: bbolt.Put: %s", err)

return
}

err = tx.Commit()
if err != nil {
log.Error("auth: bbolt.Commit: %s", err)

return
}

Expand Down Expand Up @@ -347,8 +364,6 @@ func (a *Auth) httpCookie(req loginJSON) (string, error) {
// a well-maintained third-party module.
//
// TODO(a.garipov): Support header Forwarded from RFC 7329.
//
// TODO(a.garipov): !! test!
func realIP(r *http.Request) (ip net.IP, err error) {
proxyHeaders := []string{
"CF-Connecting-IP",
Expand Down Expand Up @@ -474,7 +489,6 @@ func optionalAuthThird(w http.ResponseWriter, r *http.Request) (authFirst bool)
if glProcessCookie(r) {
log.Debug("auth: authentification was handled by GL-Inet submodule")
ok = true

} else if err == nil {
r := Context.auth.checkSession(cookie.Value)
if r == checkSessionOK {
Expand Down Expand Up @@ -508,6 +522,7 @@ func optionalAuthThird(w http.ResponseWriter, r *http.Request) (authFirst bool)
}
authFirst = true
}

return authFirst
}

Expand Down

0 comments on commit 4457725

Please sign in to comment.