user

package
v0.0.0-...-29719d2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 4, 2025 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Package user provides services for managing users.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidEmail = errors.New("invalid email")

ErrInvalidEmail is returned when the email is invalid.

View Source
var ErrInvalidSession = errors.New("invalid session")

ErrInvalidSession is returned when the session is invalid, either because it is unknown or expired.

View Source
var ErrPasswordTooShort = errors.New("password too short")

ErrPasswordTooShort is returned when the password is too short.

View Source
var ErrUnknownUser = errors.New("unknown user")

ErrUnknownUser is returned when the user is unknown.

View Source
var Module = fx.Module("user",
	fx.Decorate(func(slog *slog.Logger) *slog.Logger {
		return slog.With("module", "user")
	}),
	fx.Provide(
		NewUserService,
	),
)

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

func ParseLocale(locale string) (Locale, error)

ParseLocale parses a locale string into a Locale type.

func (Locale) String

func (l Locale) String() string

String implements the fmt.Stringer interface.

func (Locale) Tags

func (l Locale) Tags() []language.Tag

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.

func (Locale) Validate

func (l Locale) Validate() error

Validate checks if the Locale is valid.

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 (s Secret) MarshalText() ([]byte, error)

func (Secret) PrettyString

func (s Secret) PrettyString() string

PrettyString returns the secret as a pretty string.

func (*Secret) UnmarshalText

func (s *Secret) UnmarshalText(text []byte) error

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 User

type User struct {
	Name   string
	Locale Locale
}

User is a user in the system.

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 (s UserService) DeleteSession(ctx context.Context, userSecret Secret, sessionID int64) error

func (UserService) ListSessions

func (s UserService) ListSessions(ctx context.Context, userSecret Secret) ([]Session, error)

func (UserService) UpdateUserLocale

func (s UserService) UpdateUserLocale(ctx context.Context, secret Secret, locale Locale) error

func (UserService) UpdateUserName

func (s UserService) UpdateUserName(ctx context.Context, secret Secret, name string) error

func (UserService) User

func (s UserService) User(ctx context.Context, secret Secret) (User, error)

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

type UserWithSecret struct {
	User
	Secret Secret
}

UserWithSecret is a user with their secret.

Directories

Path Synopsis
Package openapi provides primitives to interact with the openapi HTTP API.
Package openapi provides primitives to interact with the openapi HTTP API.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL