service

package
v0.0.0-...-813fb4a Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2014 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	InvalidConfig             = errgo.New("Invalid config")
	InvalidArguments          = errgo.New("Invalid arguments.")
	InvalidCredentials        = errgo.New("Invalid credentials.")
	InvalidVerificationEmail  = errgo.New("Email adress does not match current email for user.")
	ResetPasswordTokenExpired = errgo.New("The ResetPasswordToken has expired.")
	UserEmailMustBeVerified   = errgo.New("Email must be verified to authenticate.")
)

Functions

func IsEmailAlreadyTakenError

func IsEmailAlreadyTakenError(err error) bool

func IsLoginNameAlreadyTakenError

func IsLoginNameAlreadyTakenError(err error) bool

func IsNotFoundError

func IsNotFoundError(err error) bool

func IsServiceError

func IsServiceError(err error) bool

func IsUserEmailMustBeVerifiedError

func IsUserEmailMustBeVerifiedError(err error) bool

Types

type Config

type Config struct {
	AuthEmailMustBeVerified bool
	MaxItems                int

	// How long can a ResetPasswordToken be used?
	ResetPasswordExpireTime time.Duration
}

func (Config) Validate

func (c Config) Validate()

Validate checks all values and panics if any one is invalid.

func (Config) ValidateValues

func (c Config) ValidateValues() error

type Dependencies

type Dependencies struct {
	IdFactory   IdFactory
	Hasher      PasswordHasher
	UserStorage UserStorage

	// EventStream.Publish() is called for every succesfull event in the UserService. Should also forward to EventCollector.
	EventStream EventStream
}

type EventCollector

type EventCollector struct {
	MaxItems int
	Items    []Item
	Lock     *sync.Mutex
}

func NewEventCollector

func NewEventCollector(maxItems int) *EventCollector

func (*EventCollector) Get

func (esc *EventCollector) Get() []Item

func (*EventCollector) WriteJSONStreamOnce

func (esc *EventCollector) WriteJSONStreamOnce(w io.Writer) error

WriteJSONOnce writes the current collected items into the writer. Each item is written on its own line.

type EventStream

type EventStream interface {
	// Log forwards the given entry to the eventlog.
	Publish(tag string, entry []byte)
}

EventLog abstracts any eventlog for store the business events of the UserService. Could write to RabbitMQ, Apache Kafka or just plain files. This is a write-only interface, errors are propagted to stderr or similar..

type IdFactory

type IdFactory interface {
	NewUserID() string

	// Generates a new password token to be used for the password reset feature.
	// The result must never be empty.
	NewResetPasswordToken() string
}

type Item

type Item struct {
	Tag       string
	Json      []byte
	Timestamp time.Time
	// contains filtered or unexported fields
}

func (*Item) MarshalJSON

func (i *Item) MarshalJSON() ([]byte, error)

type PasswordHasher

type PasswordHasher interface {
	NeedsRehash(password_hash string) bool
	Hash(password string) string
	Verify(password, passwordHash string) bool
}

This follows the design of the PHP password_* functions. The client don't need to know anything about the user algorithms.

type UserService

type UserService struct {
	Dependencies
	Config

	// EventCollector is used by any consumer of the UserService which needs access to the previous events.
	EventCollector *EventCollector
}

func NewUserService

func NewUserService(config Config, deps Dependencies) *UserService

func (*UserService) Authenticate

func (us *UserService) Authenticate(loginName, loginPassword string) (string, error)

Authenticate checks whether a user with the given login credentials exists. Returns an error if the credentials are incorrect or the user cannot be authorized.

Error Helpers

func (*UserService) ChangeEmail

func (us *UserService) ChangeEmail(userID, email string) error

func (*UserService) ChangeLoginCredentials

func (us *UserService) ChangeLoginCredentials(userID, newLogin, newPassword string) error

func (*UserService) ChangeProfileName

func (us *UserService) ChangeProfileName(userID, profileName string) error

func (*UserService) CheckAndSetEmailVerified

func (us *UserService) CheckAndSetEmailVerified(userID, email string) error

func (*UserService) CreateUser

func (us *UserService) CreateUser(profileName, email, loginName, loginPassword string) (string, error)

func (*UserService) GetUser

func (us *UserService) GetUser(id string) (user.User, error)

func (*UserService) NewResetLoginCredentialsToken

func (us *UserService) NewResetLoginCredentialsToken(email string) (string, error)

NewResetLoginCredentialsToken creates a new reset password token, associates it with the user and returns it. The consumer should forward this token to the user's email (or via another communication medium which is known to reach the real user) to verify that the initiator is the real user.

Event: user.new_reset_password_token(user_id, email, token)

Returs the new token to reset the password with or an error if no user could be found.

func (*UserService) ResetCredentialsWithToken

func (us *UserService) ResetCredentialsWithToken(resetPasswordToken, new_login_name, new_login_password string) (string, error)

ResetCredentialsWithToken checks for users with the given token and resets their login credentials to given values.

Event: user.

func (*UserService) SetEmailVerified

func (us *UserService) SetEmailVerified(userID string) error

type UserStorage

type UserStorage interface {
	Save(user user.User) error
	Get(userId string) (user.User, error)

	FindByLoginName(loginName string) (user.User, error)
	FindByEmail(email string) (user.User, error)
	FindByResetPasswordToken(token string) (user.User, error)
}

type UserStorageWrapper

type UserStorageWrapper struct {
	UserStorage UserStorage
}

func (*UserStorageWrapper) FindByEmail

func (w *UserStorageWrapper) FindByEmail(email string) (user.User, error)

func (*UserStorageWrapper) FindByLoginName

func (w *UserStorageWrapper) FindByLoginName(loginName string) (user.User, error)

func (*UserStorageWrapper) FindByResetPasswordToken

func (w *UserStorageWrapper) FindByResetPasswordToken(token string) (user.User, error)

func (*UserStorageWrapper) Get

func (w *UserStorageWrapper) Get(userId string) (user.User, error)

func (*UserStorageWrapper) Save

func (w *UserStorageWrapper) Save(user user.User) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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