Files
AdGuardHome/internal/home/auth.go
Stanislav Chzhen bd3774ed69 Pull request 2434: AGDNS-2743-auth-mw-usage
Merge in DNS/adguard-home from AGDNS-2743-auth-mw-usage to master

Squashed commit of the following:

commit 9e3054a42f1b04a00c207810a8dc08696e44c466
Merge: 610c6fc45 1317e296f
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Wed Jul 9 17:09:26 2025 +0300

    Merge branch 'master' into AGDNS-2743-auth-mw-usage

commit 610c6fc45d2d8848e9b89afb0037b98d83106afa
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Tue Jul 8 19:03:44 2025 +0300

    home: imp docs

commit 4633c8991ca77ec182b17a299d9f7e75e145f2ee
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Thu Jul 3 21:01:54 2025 +0300

    home: add tests

commit 586b714dafc342670b441fb29e3c7c8fde83ba4c
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Thu Jul 3 14:25:28 2025 +0300

    home: fix first run

commit 7c2e5d41f0feaae7b50ea0e69cf2767ba17d59df
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Wed Jul 2 23:52:16 2025 +0300

    home: imp code

commit b90031495ae3b6fab7361528fb3a3013139a4965
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Wed Jul 2 14:06:42 2025 +0300

    home: rm unused

commit 90cb29f2daac7b825b03857414bb04a692736e22
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Wed Jul 2 13:55:45 2025 +0300

    home: auth mw usage
2025-07-09 17:28:45 +03:00

189 lines
5.0 KiB
Go

package home
import (
"context"
"fmt"
"log/slog"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/aghuser"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/logutil/slogutil"
"github.com/AdguardTeam/golibs/netutil"
"github.com/AdguardTeam/golibs/netutil/httputil"
"github.com/AdguardTeam/golibs/timeutil"
"golang.org/x/crypto/bcrypt"
)
// sessionsDBName is the name of the file where session data is stored.
const sessionsDBName = "sessions.db"
// webUser represents a user of the Web UI.
//
// TODO(s.chzhen): Improve naming.
type webUser struct {
// Name represents the login name of the web user.
Name string `yaml:"name"`
// PasswordHash is the hashed representation of the web user password.
PasswordHash string `yaml:"password"`
// UserID is the unique identifier of the web user.
UserID aghuser.UserID `yaml:"-"`
}
// toUser returns the new properly initialized *aghuser.User using stored
// properties. It panics if there is an error generating the user ID.
func (wu *webUser) toUser() (u *aghuser.User) {
uid := wu.UserID
if uid == (aghuser.UserID{}) {
uid = aghuser.MustNewUserID()
}
return &aghuser.User{
Password: aghuser.NewDefaultPassword(wu.PasswordHash),
Login: aghuser.Login(wu.Name),
ID: uid,
}
}
// authConfig is the configuration structure for [auth].
type authConfig struct {
// baseLogger is used for creating other loggers. It must not be nil.
baseLogger *slog.Logger
// rateLimiter manages the rate limiting for login attempts. It must not be
// nil.
rateLimiter loginRaateLimiter
// trustedProxies is a set of subnets considered as trusted.
trustedProxies netutil.SubnetSet
// dbFilename is the name of the file where session data is stored. It must
// not be empty.
dbFilename string
// users contains web user information from the configuration file.
users []webUser
// sessionTTL is the TTL (Time To Live) for web user sessions.
sessionTTL time.Duration
// isGLiNet indicates whether GLiNet mode is enabled.
isGLiNet bool
}
// auth stores web user information and handles authentication.
type auth struct {
logger *slog.Logger
rateLimiter loginRaateLimiter
trustedProxies netutil.SubnetSet
sessions aghuser.SessionStorage
users aghuser.DB
isGLiNet bool
}
// newAuth returns the new properly initialized *auth.
func newAuth(ctx context.Context, conf *authConfig) (a *auth, err error) {
userDB := aghuser.NewDefaultDB()
for i, u := range conf.users {
err = userDB.Create(ctx, u.toUser())
if err != nil {
return nil, fmt.Errorf("users: at index %d: %w", i, err)
}
}
s, err := aghuser.NewDefaultSessionStorage(ctx, &aghuser.DefaultSessionStorageConfig{
Logger: conf.baseLogger.With(slogutil.KeyPrefix, "session_storage"),
Clock: timeutil.SystemClock{},
UserDB: userDB,
DBPath: conf.dbFilename,
SessionTTL: conf.sessionTTL,
})
if err != nil {
return nil, fmt.Errorf("creating session storage: %w", err)
}
return &auth{
logger: conf.baseLogger.With(slogutil.KeyPrefix, "auth"),
rateLimiter: conf.rateLimiter,
trustedProxies: conf.trustedProxies,
sessions: s,
users: userDB,
isGLiNet: conf.isGLiNet,
}, nil
}
// middleware returns authentication middleware.
func (a *auth) middleware() (mw httputil.Middleware) {
if a.isGLiNet {
return newAuthMiddlewareGLiNet(&authMiddlewareGLiNetConfig{
logger: a.logger,
clock: timeutil.SystemClock{},
tokenFilePrefix: glFilePrefix,
ttl: glTokenTimeout,
maxTokenSize: MaxFileSize,
})
}
return newAuthMiddlewareDefault(&authMiddlewareDefaultConfig{
logger: a.logger,
rateLimiter: a.rateLimiter,
trustedProxies: a.trustedProxies,
sessions: a.sessions,
users: a.users,
})
}
// usersList returns a copy of a users list.
func (a *auth) usersList(ctx context.Context) (webUsers []webUser) {
users, err := a.users.All(ctx)
if err != nil {
// Should not happen.
panic(err)
}
webUsers = make([]webUser, 0, len(users))
for _, u := range users {
webUsers = append(webUsers, webUser{
Name: string(u.Login),
PasswordHash: string(u.Password.Hash()),
UserID: u.ID,
})
}
return webUsers
}
// addUser adds a new user with the given password. u must not be nil.
func (a *auth) addUser(ctx context.Context, u *webUser, password string) (err error) {
if len(password) == 0 {
return errors.Error("empty password")
}
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return fmt.Errorf("generating hash: %w", err)
}
u.PasswordHash = string(hash)
err = a.users.Create(ctx, u.toUser())
if err != nil {
// Should not happen.
panic(err)
}
a.logger.DebugContext(ctx, "added user", "login", u.Name)
return nil
}
// close closes the authentication database.
func (a *auth) close(ctx context.Context) {
err := a.sessions.Close()
if err != nil {
a.logger.ErrorContext(ctx, "closing session storage", slogutil.KeyError, err)
}
}