contracts

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2023 License: MIT-0 Imports: 3 Imported by: 11

README

MailHedgehog set of contracts

Set of contracts used in MailHedgehog what reused in child packages.

Development

go mod tidy
go mod verify
go mod vendor
go test --cover

Credits

  • Think Studio

Documentation

Index

Constants

View Source
const (
	SearchParamFrom    SearchParam = "from"
	SearchParamTo                  = "to"
	SearchParamContent             = "content"
)

Variables

View Source
var MessageSharingExpiredAtFormat = "2006-01-02 15:04:05"

MessageSharingExpiredAtFormat consider always be in UTC

Functions

func CreatePasswordHash

func CreatePasswordHash(password string) ([]byte, error)

Types

type Authentication

type Authentication interface {

	// SMTP returns object what process authentication for SMTP protocol.
	SMTP() SmtpAuthentication

	// Dashboard returns object what process authentication for UI dashboard.
	Dashboard() DashboardAuthentication

	// UsersStorage returns object what manipulate users in application.
	UsersStorage() UsersStorage
}

Authentication interface represents a backend flow authenticate user to SMTP for store message, or to dashboard for manage stored messages.

type AuthenticationConfig

type AuthenticationConfig struct {
	Smtp struct {
		IpsAllowList struct {
			Enabled bool `yaml:"enabled"`
		} `yaml:"ips_allowlist"`
		ViaIpAuthentication struct {
			Enabled bool `yaml:"enabled"`
		} `yaml:"via_ip"`
		ViaPasswordAuthentication struct {
			Enabled bool `yaml:"enabled"`
		} `yaml:"via_password"`
	} `yaml:"smtp"`
	Dashboard struct {
		ViaEmailAuthentication struct {
			Enabled bool `yaml:"enabled"`
		} `yaml:"via_email"`
		ViaPasswordAuthentication struct {
			Enabled bool `yaml:"enabled"`
		} `yaml:"via_password"`
	} `yaml:"dashboard"`
}

type DashboardAuthentication

type DashboardAuthentication interface {

	// RequiresAuthentication define is need authentication for dashboard.
	RequiresAuthentication() bool

	// ViaPasswordAuthentication returns object what can manage password for authentication.
	ViaPasswordAuthentication() ViaPasswordAuthentication

	// ViaEmailAuthentication returns object what can manage emails what can be authenticated without password.
	ViaEmailAuthentication() ViaEmailAuthentication
}

type DbConnectionConfig added in v1.0.3

type DbConnectionConfig map[string]interface{}

type DbConnectionsConfig added in v1.0.3

type DbConnectionsConfig struct {
	Connections map[string]DbConnectionConfig `yaml:"connections"`
}

type IpsAllowList

type IpsAllowList interface {

	// Enabled shows is allowlist flow enabled.
	Enabled() bool

	// Allowed check is IP in allowlist for specific user.
	Allowed(username string, ip string) bool

	// AddIp to auth allowlist storage related to specific user.
	AddIp(username string, ip string) error
	// DeleteIp from auth allowlist storage related to specific user.
	DeleteIp(username string, ip string) error
	// ClearAllIps from auth allowlist storage related to specific user.
	ClearAllIps(username string) error
}

IpsAllowList allow application while authentication checks IP before authenticate client and if IP not in allowed list then application will return unauthenticated response.

type MessageSharing added in v1.0.2

type MessageSharing interface {
	// Add new sharing record to storage.
	Add(emailSharingRecord *SharedMessageRecord) (*SharedMessageRecord, error)

	// Find single record by ID.
	Find(id string) (*SharedMessageRecord, error)

	// DeleteExpired will iterate over all shared records and delete all expired records.
	DeleteExpired() (bool, error)
}

MessageSharing represents interface of manipulation of shared messages

type MessagesRepo added in v1.0.1

type MessagesRepo interface {
	// Store `message` to specific `room`.
	Store(message *smtpMessage.SmtpMessage) (smtpMessage.MessageID, error)
	// List retrieve list of messages based on `query` starting with `offset` index and count limited by `limit`.
	// `query` - represents of key->value map, where key is search parameter.
	List(query SearchQuery, offset, limit int) ([]smtpMessage.SmtpMessage, int, error)
	// Count total messages in storage.
	Count() int
	// Delete delete specific message from storage by `messageId`.
	Delete(messageId smtpMessage.MessageID) error
	// Load find specific message from storage by `messageId`.
	Load(messageId smtpMessage.MessageID) (*smtpMessage.SmtpMessage, error)
}

MessagesRepo represents repository to manipulate messages related to specific room.

type MessagesStorage added in v1.0.1

type MessagesStorage interface {
	// RoomsRepo returns room repository
	RoomsRepo() RoomsRepo

	// MessagesRepo returns messages repository related to specific room
	MessagesRepo(room Room) MessagesRepo
}

MessagesStorage interface represents a backend flow to store or retrieve messages

type MessagesStorageConfiguration added in v1.0.1

type MessagesStorageConfiguration struct {
	PerRoomLimit int `yaml:"per_room_limit"`
}

type Room added in v1.0.1

type Room string

Room name. Each room contains set of emails. By room application diverge what emails should be displayed for login user. Now application expects have room name same as logged username (in case auth disabled - will be used name "default").

type RoomsRepo added in v1.0.1

type RoomsRepo interface {
	// List of rooms in system.
	List(offset, limit int) ([]Room, error)
	// Count total count rooms in storage.
	Count() int
	// Delete all messages in room from storage.
	Delete(room Room) error
}

RoomsRepo represents repository to manipulate rooms.

type SearchParam added in v1.0.1

type SearchParam = string

SearchParam represents search key.

type SearchQuery added in v1.0.1

type SearchQuery = map[string]string

SearchQuery represents key->value map what describe search params.

type SharedMessageRecord added in v1.0.2

type SharedMessageRecord struct {
	Id        string
	Room      Room
	MessageId smtpMessage.MessageID
	ExpiredAt time.Time
}

SharedMessageRecord represents one record of shared message.

func NewSharedMessageRecord added in v1.0.2

func NewSharedMessageRecord(room Room, messageID smtpMessage.MessageID) *SharedMessageRecord

NewSharedMessageRecord creates new object of SharedMessageRecord

func (*SharedMessageRecord) Exists added in v1.0.2

func (record *SharedMessageRecord) Exists() bool

Exists checks is record exists in storage ()is created identification.

func (*SharedMessageRecord) GetExpiredAtString added in v1.0.2

func (record *SharedMessageRecord) GetExpiredAtString() string

GetExpiredAtString string representation of expired at time.

func (*SharedMessageRecord) IsExpired added in v1.0.2

func (record *SharedMessageRecord) IsExpired() bool

IsExpired checks is record expired

func (*SharedMessageRecord) SetExpirationInHours added in v1.0.2

func (record *SharedMessageRecord) SetExpirationInHours(hours int) *SharedMessageRecord

SetExpirationInHours setting expiration time future from now in passed hours.

type SmtpAuthentication

type SmtpAuthentication interface {

	// RequiresAuthentication define is need authentication for SMTP
	RequiresAuthentication() bool

	// IpsAllowList returns object what can manage IPs allowlist.
	IpsAllowList() IpsAllowList

	// ViaPasswordAuthentication returns object what can manage password for authentication.
	ViaPasswordAuthentication() ViaPasswordAuthentication

	// ViaIpAuthentication returns object what can manage IPs what can be authenticated without password.
	ViaIpAuthentication() ViaIpAuthentication
}

SmtpAuthentication contains methods related to SMTP authentication to send email messages to application.

type UserResource

type UserResource struct {
	Username            string
	DashboardAuthEmails []string
	SmtpAuthIPs         []string
	SmtpAllowListedIPs  []string
}

type UsersStorage

type UsersStorage interface {

	// Exists check is username exists in storage.
	Exists(username string) bool

	// Add to auth storage.
	Add(username string) error

	// Delete from auth storage.
	Delete(username string) error

	// List from auth storage.
	List(searchQuery string, offset, limit int) ([]UserResource, int, error)
}

type ViaEmailAuthentication

type ViaEmailAuthentication interface {

	// Enabled shows is authentication via email flow enabled.
	Enabled() bool

	// SendToken to email.
	SendToken(username string, email string) error

	// Authenticate user by token sent to email.
	Authenticate(username string, email string, token string) bool

	// AddEmail for login to auth storage related to user
	AddEmail(username string, email string) error
	// DeleteEmail for login from auth storage related to user
	DeleteEmail(username string, email string) error
	// ClearAllEmails for login from auth storage related to user
	ClearAllEmails(username string) error
}

ViaEmailAuthentication allow login by token sent to email.

type ViaIpAuthentication

type ViaIpAuthentication interface {

	// Enabled shows is authentication via IP flow enabled.
	Enabled() bool

	// Authenticate check is application can bypass password and authenticate client just by username and IP.
	Authenticate(username string, ip string) bool

	// AddIp for "IP auth" to auth storage related to user.
	AddIp(username string, ip string) error
	// DeleteIp for "IP auth" from auth storage related to user.
	DeleteIp(username string, ip string) error
	// ClearAllIps for "IP auth" from auth storage related to user.
	ClearAllIps(username string) error
}

ViaIpAuthentication allow application authenticate client by username and IP without checking password.

type ViaPasswordAuthentication

type ViaPasswordAuthentication interface {

	// Enabled shows is authentication via email flow enabled.
	Enabled() bool

	// Authenticate check is credentials (login/password) are valid.
	Authenticate(username string, password string) bool

	// SetPassword to auth storage related to user.
	SetPassword(username string, password string) error
}

ViaPasswordAuthentication allow login by password.

Jump to

Keyboard shortcuts

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