Documentation
¶
Overview ¶
Package aghuser contains types and logic for dealing with AdGuard Home's web users.
Index ¶
- Constants
- type DB
- type DefaultDB
- func (db *DefaultDB) All(ctx context.Context) (users []*User, err error)
- func (db *DefaultDB) ByLogin(ctx context.Context, login Login) (u *User, err error)
- func (db *DefaultDB) ByUUID(ctx context.Context, id UserID) (u *User, err error)
- func (db *DefaultDB) Create(ctx context.Context, u *User) (err error)
- type DefaultPassword
- type DefaultSessionStorage
- func (ds *DefaultSessionStorage) Close() (err error)
- func (ds *DefaultSessionStorage) DeleteByToken(ctx context.Context, t SessionToken) (err error)
- func (ds *DefaultSessionStorage) FindByToken(ctx context.Context, t SessionToken) (s *Session, err error)
- func (ds *DefaultSessionStorage) New(ctx context.Context, u *User) (s *Session, err error)
- type DefaultSessionStorageConfig
- type Login
- type Password
- type Session
- type SessionStorage
- type SessionToken
- type User
- type UserID
Constants ¶
const SessionTokenLength = 16
SessionTokenLength is the length of the web user session token.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DB ¶
type DB interface { // All retrieves all users from the database, sorted by login. // // TODO(s.chzhen): Consider function signature change to reflect the // in-memory implementation, as it currently always returns nil for error. All(ctx context.Context) (users []*User, err error) // ByLogin retrieves a user by their login. u must not be modified. // // TODO(s.chzhen): Remove this once user sessions support [UserID]. ByLogin(ctx context.Context, login Login) (u *User, err error) // ByUUID retrieves a user by their unique identifier. u must not be // modified. // // TODO(s.chzhen): Use this. ByUUID(ctx context.Context, id UserID) (u *User, err error) // Create adds a new user to the database. If the credentials already // exist, it returns the [errors.ErrDuplicated] error. It also can return // an error from the cryptographic randomness reader. u must not be // modified. Create(ctx context.Context, u *User) (err error) }
DB is an interface that defines methods for interacting with user information. All methods must be safe for concurrent use.
TODO(s.chzhen): Use this.
TODO(s.chzhen): Consider updating methods to return a clone.
type DefaultDB ¶
type DefaultDB struct {
// contains filtered or unexported fields
}
DefaultDB is the default in-memory implementation of the DB interface.
func NewDefaultDB ¶
func NewDefaultDB() (db *DefaultDB)
NewDefaultDB returns the new properly initialized *DefaultDB.
type DefaultPassword ¶
type DefaultPassword struct {
// contains filtered or unexported fields
}
DefaultPassword is the default bcrypt implementation of the Password interface.
func NewDefaultPassword ¶
func NewDefaultPassword(hash string) (p *DefaultPassword)
NewDefaultPassword returns the new properly initialized *DefaultPassword.
func (*DefaultPassword) Authenticate ¶
func (p *DefaultPassword) Authenticate(ctx context.Context, passwd string) (ok bool)
Authenticate implements the Password interface for *DefaultPassword.
func (*DefaultPassword) Hash ¶
func (p *DefaultPassword) Hash() (b []byte)
Hash implements the Password interface for *DefaultPassword.
type DefaultSessionStorage ¶ added in v0.107.62
type DefaultSessionStorage struct {
// contains filtered or unexported fields
}
DefaultSessionStorage is the default bbolt database implementation of the SessionStorage interface.
func NewDefaultSessionStorage ¶ added in v0.107.62
func NewDefaultSessionStorage( ctx context.Context, conf *DefaultSessionStorageConfig, ) (ds *DefaultSessionStorage, err error)
NewDefaultSessionStorage returns the new properly initialized *DefaultSessionStorage.
func (*DefaultSessionStorage) Close ¶ added in v0.107.62
func (ds *DefaultSessionStorage) Close() (err error)
Close implements the SessionStorage interface for *DefaultSessionStorage.
func (*DefaultSessionStorage) DeleteByToken ¶ added in v0.107.62
func (ds *DefaultSessionStorage) DeleteByToken(ctx context.Context, t SessionToken) (err error)
DeleteByToken implements the SessionStorage interface for *DefaultSessionStorage.
func (*DefaultSessionStorage) FindByToken ¶ added in v0.107.62
func (ds *DefaultSessionStorage) FindByToken(ctx context.Context, t SessionToken) (s *Session, err error)
FindByToken implements the SessionStorage interface for *DefaultSessionStorage.
func (*DefaultSessionStorage) New ¶ added in v0.107.62
New implements the SessionStorage interface for *DefaultSessionStorage.
type DefaultSessionStorageConfig ¶ added in v0.107.62
type DefaultSessionStorageConfig struct { // Logger is used for logging the operation of the session storage. It must // not be nil. Logger *slog.Logger // Clock is used to get the current time. It must not be nil. Clock timeutil.Clock // UserDB contains the web user information such as ID, login, and password. // It must not be nil. UserDB DB // DBPath is the path to the database file where session data is stored. It // must not be empty. DBPath string // SessionTTL is the default Time-To-Live duration for web user sessions. // It specifies how long a session should last and is a required field. SessionTTL time.Duration }
DefaultSessionStorageConfig represents the web user session storage configuration structure.
type Password ¶
type Password interface { // Authenticate returns true if the provided password is allowed. Authenticate(ctx context.Context, password string) (ok bool) // Hash returns a hashed representation of the web user password. Hash() (b []byte) }
Password is an interface that defines methods for handling web user passwords.
type Session ¶ added in v0.107.62
type Session struct { // Expire indicates when the session will expire. Expire time.Time // UserLogin is the login of the web user associated with the session. // // TODO(s.chzhen): Remove this field and associate the user by UserID. UserLogin Login // Token is the session token. Token SessionToken // UserID is the identifier of the web user associated with the session. UserID UserID }
Session represents a web user session.
type SessionStorage ¶ added in v0.107.62
type SessionStorage interface { // New creates a new session for the web user. New(ctx context.Context, u *User) (s *Session, err error) // FindByToken returns the stored session for the web user based on the session // token. // // TODO(s.chzhen): Consider function signature change to reflect the // in-memory implementation, as it currently always returns nil for error. FindByToken(ctx context.Context, t SessionToken) (s *Session, err error) // DeleteByToken removes a stored web user session by the provided token. DeleteByToken(ctx context.Context, t SessionToken) (err error) // Close releases the web user sessions database resources. Close() (err error) }
SessionStorage is an interface that defines methods for handling web user sessions. All methods must be safe for concurrent use.
TODO(s.chzhen): Add DeleteAll method.
type SessionToken ¶ added in v0.107.62
type SessionToken [SessionTokenLength]byte
SessionToken is the type for the web user session token.
func NewSessionToken ¶ added in v0.107.62
func NewSessionToken() (t SessionToken)
NewSessionToken returns a cryptographically secure randomly generated web user session token. If an error occurs during random generation, it will cause the program to crash.
type User ¶
type User struct { // Password stores the password information for the web user. It must not // be nil. Password Password // Login is the login name of the web user. It must not be empty. Login Login // ID is the unique identifier for the web user. It must not be empty. ID UserID }
User represents a web user.