Documentation
¶
Overview ¶
Package auth implements accounts, sessions, API tokens, the setup code, brute-force limits and the audit entries of these actions (spec 10). It owns the write domain auth (spec 7.5) and talks to storage only through storage.AuthStore.
Index ¶
- Constants
- Variables
- func CheckPassword(pw string) error
- func IsClientError(err error) bool
- func WithClient(ctx context.Context, c Client) context.Context
- func WithPrincipal(ctx context.Context, p Principal) context.Context
- type AuditInfo
- type Client
- type Deps
- type Login
- type NewToken
- type PasswordError
- type Principal
- type RateLimitError
- type Service
- func (s *Service) AddUser(ctx context.Context, by Principal, name, pw, lang string) (UserInfo, error)
- func (s *Service) Audit(ctx context.Context, from, to time.Time) ([]AuditInfo, error)
- func (s *Service) Authenticate(ctx context.Context, token string) (Principal, error)
- func (s *Service) AuthenticateToken(ctx context.Context, token string) (Principal, error)
- func (s *Service) CreateToken(ctx context.Context, by Principal, name string, expires time.Duration) (NewToken, error)
- func (s *Service) DeleteUser(ctx context.Context, by Principal, id int64) error
- func (s *Service) Login(ctx context.Context, name, pw, ip string) (Login, error)
- func (s *Service) Logout(ctx context.Context, p Principal) error
- func (s *Service) RevokeToken(ctx context.Context, by Principal, id string) error
- func (s *Service) SetPassword(ctx context.Context, by Principal, id int64, pw string) error
- func (s *Service) Setup(ctx context.Context, r SetupRequest) (Login, error)
- func (s *Service) SetupCode() string
- func (s *Service) SetupRequired(ctx context.Context) (bool, error)
- func (s *Service) Tokens(ctx context.Context) ([]TokenInfo, error)
- func (s *Service) UserByName(ctx context.Context, name string) (UserInfo, error)
- func (s *Service) Users(ctx context.Context) ([]UserInfo, error)
- type Settings
- type SetupRequest
- type TokenInfo
- type UserInfo
Constants ¶
const ( ViaSession = "session" ViaToken = "token" ViaCLI = "cli" )
Ways a principal authenticated.
const ( ReasonTooShort = "too_short" ReasonTooLong = "too_long" ReasonTooCommon = "too_common" )
Password policy reasons of a PasswordError.
const ( ActionSetup = "setup" ActionLogin = "login" ActionLoginFailed = "login_failed" ActionLogout = "logout" ActionUserAdd = "user_add" ActionUserDelete = "user_delete" ActionUserPassword = "user_password" ActionTokenCreate = "token_create" ActionTokenRevoke = "token_revoke" )
Audit actions.
const (
MinPasswordLen = 12
)
Password policy (spec 10).
const ScopeAdmin = "admin"
Scope of API tokens (spec 10; "control" is reserved).
const SessionCookie = "pvmon_session"
SessionCookie is the name of the session cookie (spec 10).
Variables ¶
var ( ErrBadCredential = errors.New("auth: invalid name or password") ErrSetupCode = errors.New("auth: invalid setup code") ErrSetupDone = errors.New("auth: setup already done") ErrNameTaken = errors.New("auth: name already taken") ErrInvalid = errors.New("auth: invalid input") ErrNotFound = errors.New("auth: not found") ErrLastUser = errors.New("auth: the last user cannot be deleted") )
Errors for branching in handlers and CLI jobs.
Functions ¶
func CheckPassword ¶
CheckPassword applies the password policy without hashing, so CLI jobs can reject a weak password before they make a backup.
func IsClientError ¶
IsClientError reports whether err is the caller's fault (validation, policy, not found).
func WithClient ¶
WithClient returns ctx carrying c.
Types ¶
type AuditInfo ¶
type AuditInfo struct {
ID int64 `json:"id"`
At time.Time `json:"at"`
Actor string `json:"actor,omitempty"`
Action string `json:"action"`
Detail json.RawMessage `json:"detail,omitempty"` // JSON object, never secrets
}
AuditInfo is one audit entry.
type Client ¶
type Client struct {
IP string // remote address, or the forwarded client of a trusted proxy
HTTPS bool // direct TLS or X-Forwarded-Proto https from a trusted proxy
}
Client describes where a request came from, resolved by the proxy middleware.
func ClientFrom ¶
ClientFrom returns the client of ctx; the zero Client when none was set.
type Deps ¶
type Deps struct {
Store storage.AuthStore
Clock util.Clock
Rand io.Reader // crypto/rand.Reader in production
Log zerolog.Logger
}
Deps are the dependencies of the service.
type Login ¶
type Login struct {
Token string // session token for the cookie; never logged
User UserInfo
Expires time.Time // absolute end of the session
}
Login is the result of a successful login or setup.
type PasswordError ¶
type PasswordError struct{ Reason string }
PasswordError reports a password that does not meet the policy.
func (*PasswordError) Error ¶
func (e *PasswordError) Error() string
type Principal ¶
type Principal struct {
UserID int64
Name string // user name, or token name for tokens
Via string // ViaSession, ViaToken or ViaCLI
TokenID string
// contains filtered or unexported fields
}
Principal is an authenticated caller.
func PrincipalFrom ¶
PrincipalFrom returns the principal of ctx; ok is false for anonymous requests.
type RateLimitError ¶
RateLimitError means too many failed attempts; retry after RetryAfter.
func (*RateLimitError) Error ¶
func (e *RateLimitError) Error() string
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service implements authentication and account management.
func (*Service) AddUser ¶
func (s *Service) AddUser(ctx context.Context, by Principal, name, pw, lang string, ) (UserInfo, error)
AddUser creates an admin.
func (*Service) Authenticate ¶
Authenticate resolves a session token; expired or unknown sessions are ErrUnauthorized.
func (*Service) AuthenticateToken ¶
AuthenticateToken resolves an API token pvm_<id>_<secret>.
func (*Service) CreateToken ¶
func (s *Service) CreateToken(ctx context.Context, by Principal, name string, expires time.Duration, ) (NewToken, error)
CreateToken creates an admin token; expires 0 = never.
func (*Service) DeleteUser ¶
DeleteUser deletes a user with its sessions and tokens; never the last one.
func (*Service) Login ¶
Login checks name and password. Unknown names and wrong passwords answer the same way and take the same time (spec 10).
func (*Service) RevokeToken ¶
RevokeToken deletes a token.
func (*Service) SetPassword ¶
SetPassword replaces a user's password and ends all of their sessions.
func (*Service) SetupRequired ¶
SetupRequired reports whether no user exists yet.
func (*Service) UserByName ¶
UserByName returns one user (CLI jobs).
type SetupRequest ¶
type SetupRequest struct {
Code, Name, Password, Lang, IP string
}
SetupRequest creates the first admin (POST /api/v1/setup).