Documentation
¶
Index ¶
- Constants
- Variables
- func GetDatabase(config Config) (GormDB, error)
- func NewPrefix(logger *log.Logger, prefix string) *log.Logger
- type BaseServices
- type EmailService
- type EmailServiceImpl
- type FileStorageService
- type FileStorageServiceImpl
- func (service *FileStorageServiceImpl) CreateStub(user *User) (*FileMetadata, error)
- func (service *FileStorageServiceImpl) GetData(id string) (*os.File, string, error)
- func (service *FileStorageServiceImpl) RemoveFile(fileMetadata FileMetadata) error
- func (service *FileStorageServiceImpl) RemoveMetadata(fileMetadata FileMetadata) error
- func (service *FileStorageServiceImpl) Upload(fileMetadata FileMetadata, data io.Reader, mimetype string) (*FileMetadata, error)
- type TokenService
- type TokenServiceImpl
- func (service *TokenServiceImpl) Check(tokenId string, tokenSecret string) (*Token, error)
- func (service *TokenServiceImpl) Create(user *User, purpose TokenPurposeEnum, expiration time.Time) (*Token, string, error)
- func (service *TokenServiceImpl) Invalidate(token *Token) (*Token, error)
- func (service *TokenServiceImpl) WithTransaction(tx GormDB) (TokenService, error)
Constants ¶
const UploadSizeLimit_b = 1_000_000
limit upload to ~1mb
Variables ¶
var ( ErrInvalidBasePath = errors.New("invalid base path") ErrNoSuchFile = errors.New("no such file") ErrFileNotUploaded = errors.New("file not uploaded") ErrInvalidMimeType = errors.New("invalid mimetype") ErrUploadSizeExceeded = errors.New("upload size exceeded") )
var AllowedMimeTypes = []string{
"image/jpeg",
"image/png",
"image/gif",
"image/webp",
}
var ErrTokenExpired = errors.New("Token expired")
var ErrTokenUsed = errors.New("Token used")
var ErrUnknownToken = errors.New("Invalid token")
Functions ¶
Types ¶
type BaseServices ¶
Struct with most commonly used services
func (BaseServices) NewPrefix ¶
func (b BaseServices) NewPrefix(prefix string) BaseServices
Creates a new logger with prefix from BaseServices
func (BaseServices) NewSession ¶
func (b BaseServices) NewSession() BaseServices
func (BaseServices) WithTransaction ¶
func (b BaseServices) WithTransaction(tx GormDB) BaseServices
type EmailService ¶
An EmailService is a service for sending emails. It's only a thin wrapper over github.com/wneessen/go-mail Configuration options are documented in config.SMTPConfig.
type EmailServiceImpl ¶
type EmailServiceImpl struct {
// contains filtered or unexported fields
}
func CreateEmailServiceImpl ¶
func CreateEmailServiceImpl(smtpConfig SMTPConfig, logger *log.Logger) (*EmailServiceImpl, error)
func (*EmailServiceImpl) Send ¶
func (service *EmailServiceImpl) Send(email string, subject string, body string) error
Sends an email message to address email, with subject and body from args. body is currently assumed to be valid html - watch out for injections. It's recommended to use html templates to create emails, but this struct is not responsible for this.
type FileStorageService ¶
type FileStorageService interface {
CreateStub(user *User) (*FileMetadata, error)
// generally not very useful, will be served by a static server either way
// FileMetadata, mimetype, error
GetData(id string) (*os.File, string, error)
// TODO how to recive an os.File from gin? data perhaps should be changed to reader
Upload(fileMetadata FileMetadata, data io.Reader, mimetype string) (*FileMetadata, error)
RemoveFile(fileMetadata FileMetadata) error
// NOTE it's responsibility of the caller to make sure that all references to this FileMetadata are removed
RemoveMetadata(fileMetadata FileMetadata) error
}
type FileStorageServiceImpl ¶
type FileStorageServiceImpl struct {
// contains filtered or unexported fields
}
func CreateFileStorageServiceImpl ¶
func CreateFileStorageServiceImpl(baseServices BaseServices, basePath string) (*FileStorageServiceImpl, error)
func (*FileStorageServiceImpl) CreateStub ¶
func (service *FileStorageServiceImpl) CreateStub(user *User) (*FileMetadata, error)
func (*FileStorageServiceImpl) RemoveFile ¶
func (service *FileStorageServiceImpl) RemoveFile(fileMetadata FileMetadata) error
func (*FileStorageServiceImpl) RemoveMetadata ¶
func (service *FileStorageServiceImpl) RemoveMetadata(fileMetadata FileMetadata) error
type TokenService ¶
type TokenService interface {
// Creates a new token. Returns database.Token and token secret (hashed secret is stored in the database).
// Token secret is confidential and should not be stored on the backend.
// purpose controls Check behavior.
// If TokenPurpose is TokenPurposeEmail, token is invalidated after Check is called on the token.
// If TokenPurpose is TokenPurposeSession, token expiration date is changed on each Check call
// (the date is moved exactly a week from call date, although that could change any time).
Create(user *User, purpose TokenPurposeEnum, expiration time.Time) (*Token, string, error)
// Checks if token with tokenId exists in the database.
// Returns it if tokenSecret matches database.Token.TokenHash.
Check(tokenId string, tokenSecret string) (*Token, error)
// Invalidates the token - the token cannot be used after that, Check will return ErrUnknownToken.
Invalidate(token *Token) (*Token, error)
// Returns TokenService that will execute queries within transaction tx.
// NOTE This won't work as expected if TokenService is using a different database.
// Maybe returning a rollback func would be a good idea. On the other hand, currently
// not deleting a token is not the worst thing that can happen.
WithTransaction(tx GormDB) (TokenService, error)
}
A TokenService is a service for managing tokens. Tokens are used for authorization instead of actual user credentials in scenarios where temporary, disposable credentials are desirable. Example scenarios: identifying user session after login, identifying the user from a verification email.
type TokenServiceImpl ¶
type TokenServiceImpl struct {
// contains filtered or unexported fields
}
func CreateTokenServiceImpl ¶
func CreateTokenServiceImpl(baseServices BaseServices) *TokenServiceImpl
TODO maybe TokenService should decide about expiration date instead of the caller especially since it will handle
func (*TokenServiceImpl) Check ¶
func (service *TokenServiceImpl) Check(tokenId string, tokenSecret string) (*Token, error)
func (*TokenServiceImpl) Create ¶
func (service *TokenServiceImpl) Create(user *User, purpose TokenPurposeEnum, expiration time.Time) (*Token, string, error)
func (*TokenServiceImpl) Invalidate ¶
func (service *TokenServiceImpl) Invalidate(token *Token) (*Token, error)
func (*TokenServiceImpl) WithTransaction ¶
func (service *TokenServiceImpl) WithTransaction(tx GormDB) (TokenService, error)