app

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: May 29, 2021 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package app provides business logic.

Package app is a generated GoMock package.

Index

Constants

View Source
const ServiceName = "auth"

ServiceName provides name of this microservice for logs/metrics.

Variables

View Source
var (
	ErrAccessDenied  = errors.New("access denied")
	ErrAlreadyExist  = errors.New("already exists")
	ErrNotFound      = errors.New("not found")
	ErrValidate      = errors.New("validate")
	ErrWrongPassword = errors.New("wrong password")
)

Errors.

View Source
var (
	Metric def.Metrics // Common metrics used by all packages.

)

Functions

func InitMetrics

func InitMetrics(reg *prometheus.Registry)

InitMetrics must be called once before using this package. It registers and initializes metrics used by this package.

Types

type AccessToken

type AccessToken string

AccessToken is a token tied to some identity and permissions.

func (AccessToken) Format

func (s AccessToken) Format(f fmt.State, c rune)

Format wraps sensitive.String.

func (AccessToken) MarshalJSON

func (s AccessToken) MarshalJSON() ([]byte, error)

MarshalJSON wraps sensitive.String.

func (AccessToken) MarshalText

func (s AccessToken) MarshalText() ([]byte, error)

MarshalText wraps sensitive.String.

type App

type App struct {
	// contains filtered or unexported fields
}

App implements interface Appl.

func New

func New(repo Repo, cfg Config) *App

New creates and returns new App.

func (*App) Authenticate

func (a *App) Authenticate(ctx Ctx, accessToken AccessToken) (*User, error)

func (*App) LoginByEmail

func (a *App) LoginByEmail(ctx Ctx, email string, password sensitive.String) (AccessToken, error)

func (*App) LoginByUserID

func (a *App) LoginByUserID(ctx Ctx, userID string, password sensitive.String) (AccessToken, error)

func (*App) Logout

func (a *App) Logout(ctx Ctx, accessToken AccessToken) error

func (*App) LogoutUser

func (a *App) LogoutUser(ctx Ctx, userName dom.UserName) error

func (*App) Register

func (a *App) Register(ctx Ctx, userID string, password sensitive.String, user *User) error

type Appl

type Appl interface {
	// Register creates and returns new user account.
	// User can provide optional userID (username).
	// These fields will be ignored in input and set automatically:
	// Name, PassHash, Role, CreateTime.
	// If userID=="admin" then user's role will be set to RoleAdmin.
	// Errors: ErrAlreadyExist, ErrValidate.
	Register(_ Ctx, userID string, password sensitive.String, _ *User) error
	// LoginByUserID creates and returns AccessToken for the user.
	// Errors: ErrNotFound, ErrWrongPassword.
	LoginByUserID(_ Ctx, userID string, password sensitive.String) (AccessToken, error)
	// LoginByEmail work in same way as LoginByUserID.
	LoginByEmail(_ Ctx, email string, password sensitive.String) (AccessToken, error)
	// Authenticate returns identity tied to AccessToken.
	// Errors: ErrNotFound.
	Authenticate(Ctx, AccessToken) (*User, error)
	// Logout invalidates given AccessToken.
	// Errors: none.
	Logout(_ Ctx, _ AccessToken) error
	// LogoutUser invalidates all user's AccessToken.
	// Errors: none.
	LogoutUser(_ Ctx, _ dom.UserName) error
}

Appl provides application features (use cases) service.

type Config

type Config struct {
	Secret sensitive.Bytes
}

Config contains configuration for business-logic.

type Ctx

type Ctx = context.Context

Ctx is a synonym for convenience.

type MatchUser

type MatchUser struct{ *User }

MatchUser provides gomock.Matcher for matching User and *User:

  • empty Name match any Name
  • PassHash is ignored
  • CreateTime is ignored

On successful match Name/PassHash/CreateTime of matching User will be set to values from matched User (which means empty Name will match any Name only once, but next time it'll match only previous Name).

func (MatchUser) Matches

func (m MatchUser) Matches(x interface{}) bool

Matches implements gomock.Matcher.

func (MatchUser) String

func (m MatchUser) String() string

String implements gomock.Matcher.

type MockAppl

type MockAppl struct {
	// contains filtered or unexported fields
}

MockAppl is a mock of Appl interface.

func NewMockAppl

func NewMockAppl(ctrl *gomock.Controller) *MockAppl

NewMockAppl creates a new mock instance.

func (*MockAppl) Authenticate

func (m *MockAppl) Authenticate(arg0 Ctx, arg1 AccessToken) (*User, error)

Authenticate mocks base method.

func (*MockAppl) EXPECT

func (m *MockAppl) EXPECT() *MockApplMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockAppl) LoginByEmail

func (m *MockAppl) LoginByEmail(arg0 Ctx, email string, password sensitive.String) (AccessToken, error)

LoginByEmail mocks base method.

func (*MockAppl) LoginByUserID

func (m *MockAppl) LoginByUserID(arg0 Ctx, userID string, password sensitive.String) (AccessToken, error)

LoginByUserID mocks base method.

func (*MockAppl) Logout

func (m *MockAppl) Logout(arg0 Ctx, arg1 AccessToken) error

Logout mocks base method.

func (*MockAppl) LogoutUser

func (m *MockAppl) LogoutUser(arg0 Ctx, arg1 dom.UserName) error

LogoutUser mocks base method.

func (*MockAppl) Register

func (m *MockAppl) Register(arg0 Ctx, userID string, password sensitive.String, arg3 *User) error

Register mocks base method.

type MockApplMockRecorder

type MockApplMockRecorder struct {
	// contains filtered or unexported fields
}

MockApplMockRecorder is the mock recorder for MockAppl.

func (*MockApplMockRecorder) Authenticate

func (mr *MockApplMockRecorder) Authenticate(arg0, arg1 interface{}) *gomock.Call

Authenticate indicates an expected call of Authenticate.

func (*MockApplMockRecorder) LoginByEmail

func (mr *MockApplMockRecorder) LoginByEmail(arg0, email, password interface{}) *gomock.Call

LoginByEmail indicates an expected call of LoginByEmail.

func (*MockApplMockRecorder) LoginByUserID

func (mr *MockApplMockRecorder) LoginByUserID(arg0, userID, password interface{}) *gomock.Call

LoginByUserID indicates an expected call of LoginByUserID.

func (*MockApplMockRecorder) Logout

func (mr *MockApplMockRecorder) Logout(arg0, arg1 interface{}) *gomock.Call

Logout indicates an expected call of Logout.

func (*MockApplMockRecorder) LogoutUser

func (mr *MockApplMockRecorder) LogoutUser(arg0, arg1 interface{}) *gomock.Call

LogoutUser indicates an expected call of LogoutUser.

func (*MockApplMockRecorder) Register

func (mr *MockApplMockRecorder) Register(arg0, userID, password, arg3 interface{}) *gomock.Call

Register indicates an expected call of Register.

type MockRepo

type MockRepo struct {
	// contains filtered or unexported fields
}

MockRepo is a mock of Repo interface.

func NewMockRepo

func NewMockRepo(ctrl *gomock.Controller) *MockRepo

NewMockRepo creates a new mock instance.

func (*MockRepo) AddAccessToken

func (m *MockRepo) AddAccessToken(arg0 Ctx, arg1 AccessToken, arg2 dom.UserName) error

AddAccessToken mocks base method.

func (*MockRepo) AddUser

func (m *MockRepo) AddUser(arg0 Ctx, arg1 User) error

AddUser mocks base method.

func (*MockRepo) DelAccessToken

func (m *MockRepo) DelAccessToken(arg0 Ctx, arg1 AccessToken) error

DelAccessToken mocks base method.

func (*MockRepo) DelAccessTokens

func (m *MockRepo) DelAccessTokens(arg0 Ctx, arg1 dom.UserName) error

DelAccessTokens mocks base method.

func (*MockRepo) EXPECT

func (m *MockRepo) EXPECT() *MockRepoMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockRepo) GetUser

func (m *MockRepo) GetUser(arg0 Ctx, arg1 dom.UserName) (*User, error)

GetUser mocks base method.

func (*MockRepo) GetUserByAccessToken

func (m *MockRepo) GetUserByAccessToken(arg0 Ctx, arg1 AccessToken) (*User, error)

GetUserByAccessToken mocks base method.

func (*MockRepo) GetUserByEmail

func (m *MockRepo) GetUserByEmail(arg0 Ctx, arg1 string) (*User, error)

GetUserByEmail mocks base method.

type MockRepoMockRecorder

type MockRepoMockRecorder struct {
	// contains filtered or unexported fields
}

MockRepoMockRecorder is the mock recorder for MockRepo.

func (*MockRepoMockRecorder) AddAccessToken

func (mr *MockRepoMockRecorder) AddAccessToken(arg0, arg1, arg2 interface{}) *gomock.Call

AddAccessToken indicates an expected call of AddAccessToken.

func (*MockRepoMockRecorder) AddUser

func (mr *MockRepoMockRecorder) AddUser(arg0, arg1 interface{}) *gomock.Call

AddUser indicates an expected call of AddUser.

func (*MockRepoMockRecorder) DelAccessToken

func (mr *MockRepoMockRecorder) DelAccessToken(arg0, arg1 interface{}) *gomock.Call

DelAccessToken indicates an expected call of DelAccessToken.

func (*MockRepoMockRecorder) DelAccessTokens

func (mr *MockRepoMockRecorder) DelAccessTokens(arg0, arg1 interface{}) *gomock.Call

DelAccessTokens indicates an expected call of DelAccessTokens.

func (*MockRepoMockRecorder) GetUser

func (mr *MockRepoMockRecorder) GetUser(arg0, arg1 interface{}) *gomock.Call

GetUser indicates an expected call of GetUser.

func (*MockRepoMockRecorder) GetUserByAccessToken

func (mr *MockRepoMockRecorder) GetUserByAccessToken(arg0, arg1 interface{}) *gomock.Call

GetUserByAccessToken indicates an expected call of GetUserByAccessToken.

func (*MockRepoMockRecorder) GetUserByEmail

func (mr *MockRepoMockRecorder) GetUserByEmail(arg0, arg1 interface{}) *gomock.Call

GetUserByEmail indicates an expected call of GetUserByEmail.

type PassHash

type PassHash struct {
	Salt sensitive.Bytes
	Hash sensitive.Bytes
}

PassHash contains hashed password.

type Repo

type Repo interface {
	// AddUser creates user.
	// Errors: ErrAlreadyExist.
	AddUser(Ctx, User) error
	// GetUser reads User by UserName.
	// Errors: ErrNotFound.
	GetUser(Ctx, dom.UserName) (*User, error)
	// GetUserByEmail reads User by email.
	// Errors: ErrNotFound.
	GetUserByEmail(Ctx, string) (*User, error)
	// GetUserByAccessToken reads User by AccessToken.
	// Errors: ErrNotFound.
	GetUserByAccessToken(Ctx, AccessToken) (*User, error)
	// AddAccessToken creates and returns AccessToken for given user.
	// Errors: ErrNotFound.
	AddAccessToken(Ctx, AccessToken, dom.UserName) error
	// DelAccessToken deletes given AccessToken.
	// Errors: none.
	DelAccessToken(Ctx, AccessToken) error
	// DelAccessTokens deletes all AccessToken for given user.
	// Errors: none.
	DelAccessTokens(Ctx, dom.UserName) error
}

Repo provides data storage.

type Role

type Role int

Role defines possible roles for a user.

const (
	RoleAdmin Role
	RoleUser
)

Roles.

func (Role) String added in v0.5.0

func (i Role) String() string

type User

type User struct {
	Name        dom.UserName
	PassHash    PassHash
	Email       string
	DisplayName string
	Role        Role
	CreateTime  time.Time
}

User contains data needed for authentication, identity and permissions.

Jump to

Keyboard shortcuts

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