services

package
v0.0.0-...-dbcc324 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2022 License: MIT Imports: 30 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthClient

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

AuthClient is the client that handles authentication requests

func NewAuthClient

func NewAuthClient(cfg *config.Config, orm *ent.Client) *AuthClient

NewAuthClient creates a new authentication client

func (*AuthClient) CheckPassword

func (c *AuthClient) CheckPassword(password, hash string) error

CheckPassword check if a given password matches a given hash

func (*AuthClient) DeletePasswordTokens

func (c *AuthClient) DeletePasswordTokens(ctx echo.Context, userID int) error

DeletePasswordTokens deletes all password tokens in the database for a belonging to a given user. This should be called after a successful password reset.

func (*AuthClient) GeneratePasswordResetToken

func (c *AuthClient) GeneratePasswordResetToken(ctx echo.Context, userID int) (string, *ent.PasswordToken, error)

GeneratePasswordResetToken generates a password reset token for a given user. For security purposes, the token itself is not stored in the database but rather a hash of the token, exactly how passwords are handled. This method returns both the generated token as well as the token entity which only contains the hash.

func (*AuthClient) GetAuthenticatedUser

func (c *AuthClient) GetAuthenticatedUser(ctx echo.Context) (*ent.User, error)

GetAuthenticatedUser returns the authenticated user if the user is logged in

func (*AuthClient) GetAuthenticatedUserID

func (c *AuthClient) GetAuthenticatedUserID(ctx echo.Context) (int, error)

GetAuthenticatedUserID returns the authenticated user's ID, if the user is logged in

func (*AuthClient) GetValidPasswordToken

func (c *AuthClient) GetValidPasswordToken(ctx echo.Context, token string, userID int) (*ent.PasswordToken, error)

GetValidPasswordToken returns a valid password token entity for a given user and a given token. Since the actual token is not stored in the database for security purposes, all non-expired token entities are fetched from the database belonging to the requesting user and a hash of the provided token is compared with the hash stored in the database.

func (*AuthClient) HashPassword

func (c *AuthClient) HashPassword(password string) (string, error)

HashPassword returns a hash of a given password

func (*AuthClient) Login

func (c *AuthClient) Login(ctx echo.Context, userID int) error

Login logs in a user of a given ID

func (*AuthClient) Logout

func (c *AuthClient) Logout(ctx echo.Context) error

Logout logs the requesting user out

func (*AuthClient) RandomToken

func (c *AuthClient) RandomToken(length int) (string, error)

RandomToken generates a random token string of a given length

type Container

type Container struct {
	// Validator stores a validator
	Validator *Validator

	// Web stores the web framework
	Web *echo.Echo

	// Config stores the application configuration
	Config *config.Config

	// Cache contains the cache interface
	Cache *cache.Cache

	// Database stores the connection to the database
	Database *sql.DB

	// ORM stores a client to the ORM
	ORM *ent.Client

	// Mail stores an email sending client
	Mail *MailClient

	// Auth stores an authentication client
	Auth *AuthClient

	// TemplateRenderer stores a service to easily render and cache templates
	TemplateRenderer *TemplateRenderer
	// contains filtered or unexported fields
}

Container contains all services used by the application and provides an easy way to handle dependency injection including within tests

func NewContainer

func NewContainer() *Container

NewContainer creates and initializes a new Container

func (*Container) Shutdown

func (c *Container) Shutdown() error

Shutdown shuts the Container down and disconnects all connections

type InvalidPasswordTokenError

type InvalidPasswordTokenError struct{}

InvalidPasswordTokenError is an error returned when an invalid token is provided

func (InvalidPasswordTokenError) Error

Error implements the error interface.

type MailClient

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

MailClient provides a client for sending email This is purposely not completed because there are many different methods and services for sending email, many of which are very different. Choose what works best for you and populate the methods below

func NewMailClient

func NewMailClient(cfg *config.Config, templates *TemplateRenderer) (*MailClient, error)

NewMailClient creates a new MailClient

func (*MailClient) Send

func (c *MailClient) Send(ctx echo.Context, to, body string) error

Send sends an email to a given email address with a given body

func (*MailClient) SendTemplate

func (c *MailClient) SendTemplate(ctx echo.Context, to, template string, data interface{}) error

SendTemplate sends an email to a given email address using a template and data which is passed to the template The template name should only include the filename without the extension or directory. The funcmap will be automatically added to the template and the data will be passed in.

type NotAuthenticatedError

type NotAuthenticatedError struct{}

NotAuthenticatedError is an error returned when a user is not authenticated

func (NotAuthenticatedError) Error

func (e NotAuthenticatedError) Error() string

Error implements the error interface.

type TemplateRenderer

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

TemplateRenderer provides a flexible and easy to use method of rendering simple templates or complex sets of templates while also providing caching and/or hot-reloading depending on your current environment

func NewTemplateRenderer

func NewTemplateRenderer(cfg *config.Config) *TemplateRenderer

NewTemplateRenderer creates a new TemplateRenderer

func (*TemplateRenderer) Execute

func (t *TemplateRenderer) Execute(cacheGroup, cacheID, baseName string, data interface{}) (*bytes.Buffer, error)

Execute executes a cached template with the data provided See Parse() for an explanation of the parameters

func (*TemplateRenderer) GetTemplatesPath

func (t *TemplateRenderer) GetTemplatesPath() string

GetTemplatesPath gets the complete path to the templates directory

func (*TemplateRenderer) Load

func (t *TemplateRenderer) Load(cacheGroup, cacheID string) (*template.Template, error)

Load loads a template from the cache

func (*TemplateRenderer) Parse

func (t *TemplateRenderer) Parse(cacheGroup, cacheID, baseName string, files []string, directories []string) error

Parse parses a set of templates and caches them for quick execution If the application environment is set to local, the cache will be bypassed and templates will be parsed upon each request so hot-reloading is possible without restarts.

All template files and template directories must be provided relative to the templates directory and without template extensions. Those two values can be altered via the config package.

cacheGroup is used to separate templates in to groups within the cache to avoid potential conflicts with the cacheID.

baseName is the filename of the base template without any paths or an extension. files is a slice of all individual template files that will be included in the parse. directories is a slice of directories which all template files witin them will be included in the parse

Also included will be the function map provided by the funcmap package.

An example usage of this: t.Parse(

"page",
"home",
"main",
[]string{
	"layouts/main",
	"pages/home",
},
[]string{"components"},

)

This will perform a template parse which will:

  • Be cached using a key of "page:home"
  • Include the layouts/main.gohtml and pages/home.gohtml templates
  • Include all templates within the components directory
  • Include the function map within the funcmap package
  • Set the base template as main.gohtml

func (*TemplateRenderer) ParseAndExecute

func (t *TemplateRenderer) ParseAndExecute(cacheGroup, cacheID, baseName string, files []string, directories []string, data interface{}) (*bytes.Buffer, error)

ParseAndExecute is a wrapper around Parse() and Execute()

type Validator

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

Validator provides validation mainly validating structs within the web context

func NewValidator

func NewValidator() *Validator

NewValidator creats a new Validator

func (*Validator) Validate

func (v *Validator) Validate(i interface{}) error

Validate validates a struct

Jump to

Keyboard shortcuts

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