Documentation
¶
Overview ¶
Package user provides services for managing users.
Index ¶
- Variables
- type Locale
- type Secret
- type Session
- type SessionToken
- type User
- type UserService
- func (s UserService) CreateSession(ctx context.Context, userSecret Secret, userAgent string) (SessionToken, error)
- func (s UserService) CreateUser(ctx context.Context, name string) (UserWithSecret, error)
- func (s UserService) DeleteSession(ctx context.Context, userSecret Secret, sessionID int64) error
- func (s UserService) ListSessions(ctx context.Context, userSecret Secret) ([]Session, error)
- func (s UserService) UpdateUserLocale(ctx context.Context, secret Secret, locale Locale) error
- func (s UserService) UpdateUserName(ctx context.Context, secret Secret, name string) error
- func (s UserService) User(ctx context.Context, secret Secret) (User, error)
- func (s UserService) ValidateSession(ctx context.Context, token SessionToken) (Session, error)
- type UserServiceConfig
- type UserSessionStorage
- type UserStorage
- type UserWithSecret
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidEmail = errors.New("invalid email")
ErrInvalidEmail is returned when the email is invalid.
var ErrInvalidSession = errors.New("invalid session")
ErrInvalidSession is returned when the session is invalid, either because it is unknown or expired.
var ErrPasswordTooShort = errors.New("password too short")
ErrPasswordTooShort is returned when the password is too short.
var ErrUnknownUser = errors.New("unknown user")
ErrUnknownUser is returned when the user is unknown.
Functions ¶
This section is empty.
Types ¶
type Locale ¶
type Locale string
Locale is a user's preferred languages. It is used for localization. The format of the string is specified by RFC 2616 but is validated by language.ParseAcceptLanguage, which is more lax.
func ParseLocale ¶
ParseLocale parses a locale string into a Locale type.
func (Locale) String ¶
String implements the fmt.Stringer interface.
func (Locale) Tags ¶
Tags returns the Locale as a list of language tags. If l is empty or invalid, then this function returns one language.Und. The returned list is never empty.
type Secret ¶
type Secret string
Secret is a secret identifier for a user. This secret is generated once and never changes. It is used to both authenticate and identify a user, so it should be kept secret.
func (Secret) MarshalText ¶
func (Secret) PrettyString ¶
PrettyString returns the secret as a pretty string.
func (*Secret) UnmarshalText ¶
type Session ¶
type Session struct {
// ID uniquely identifies the session.
ID int64
// UserSecret is the secret of the user that the session belongs to.
UserSecret Secret
// UserAgent is the user agent that the session was created with.
UserAgent string
// CreatedAt is the time that the session was created.
CreatedAt time.Time
// LastUsed is the time that the session was last used.
LastUsed time.Time
// ExpiresAt is the time that the session will expire.
// If zero, the session does not expire.
ExpiresAt time.Time
}
Session is a user session.
type SessionToken ¶
type SessionToken string
SessionToken is a token that represents a user session. The type represents an already validated token. The random part is not exposed to the user except via [String].
type UserService ¶
type UserService struct {
// contains filtered or unexported fields
}
UserService is a service for managing users.
func NewUserService ¶
func NewUserService(c UserServiceConfig) (*UserService, error)
NewUserService creates a new user service.
func (UserService) CreateSession ¶
func (s UserService) CreateSession(ctx context.Context, userSecret Secret, userAgent string) (SessionToken, error)
func (UserService) CreateUser ¶
func (s UserService) CreateUser(ctx context.Context, name string) (UserWithSecret, error)
func (UserService) DeleteSession ¶
func (UserService) ListSessions ¶
func (UserService) UpdateUserLocale ¶
func (UserService) UpdateUserName ¶
func (UserService) ValidateSession ¶
func (s UserService) ValidateSession(ctx context.Context, token SessionToken) (Session, error)
type UserServiceConfig ¶
type UserServiceConfig struct {
fx.In
UserStorage
UserSessionStorage
}
UserServiceConfig is a dependency injection container for UserService.
type UserSessionStorage ¶
type UserSessionStorage interface {
// RegisterSession registers a session for a user. The token is generated by
// [UserService]. The userAgent is optional.
RegisterSession(ctx context.Context, token []byte, userSecret Secret, userAgent string) error
// ValidateSession validates a session for a user. The user that the session
// belongs to is returned.
ValidateSession(ctx context.Context, token []byte) (Session, error)
// ListSessions lists all sessions for a user.
ListSessions(ctx context.Context, userSecret Secret) ([]Session, error)
// DeleteSession deletes a session for a user.
DeleteSession(ctx context.Context, userSecret Secret, sessionID int64) error
}
type UserStorage ¶
type UserStorage interface {
// CreateUser creates a user in the storage with the given name.
CreateUser(ctx context.Context, secret Secret, name string) (User, error)
// User gets the user identified by the given secret.
User(ctx context.Context, secret Secret) (User, error)
// UpdateUserName updates the user's name.
UpdateUserName(ctx context.Context, secret Secret, name string) error
// UpdateUserLocale updates the user's locale.
UpdateUserLocale(ctx context.Context, secret Secret, locale Locale) error
}
type UserWithSecret ¶
UserWithSecret is a user with their secret.