email

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2025 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTokenNotFound is returned when a token is not found
	ErrTokenNotFound = errors.New("token not found")

	// ErrTokenExpired is returned when a token has expired
	ErrTokenExpired = errors.New("token has expired")

	// ErrTokenAlreadyUsed is returned when a token has already been used
	ErrTokenAlreadyUsed = errors.New("token has already been used")
)

Functions

This section is empty.

Types

type EmailTemplate

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

EmailTemplate generates HTML emails using Hermes

func NewEmailTemplate

func NewEmailTemplate(serviceName, logoURL, logoWidth, iconURL, baseURL string) *EmailTemplate

NewEmailTemplate creates a new email template generator

func (*EmailTemplate) GenerateLoginEmail

func (t *EmailTemplate) GenerateLoginEmail(loginURL, otp string, validMinutes int, lang i18n.Language, translator *i18n.Translator) (htmlBody, textBody string, err error)

GenerateLoginEmail generates HTML and plain text for login link email

type Handler

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

Handler manages email authentication

func NewHandler

func NewHandler(
	cfg config.EmailAuthConfig,
	serviceCfg config.ServiceConfig,
	baseURL string,
	authPathPrefix string,
	authzChecker authz.Checker,
	translator *i18n.Translator,
	cookieSecret string,
	tokenKVS kvs.Store,
	emailQuotaKVS kvs.Store,
) (*Handler, error)

NewHandler creates a new email authentication handler

func (*Handler) Cleanup

func (h *Handler) Cleanup()

Cleanup removes expired tokens

func (h *Handler) SendLoginLink(email string, redirectURL string, lang i18n.Language) error

SendLoginLink sends a login link to the specified email address with redirect URL

func (*Handler) SetSender

func (h *Handler) SetSender(sender Sender)

SetSender sets the email sender (for testing)

func (*Handler) VerifyOTP

func (h *Handler) VerifyOTP(otp string) (email string, redirectURL string, error error)

VerifyOTP verifies an OTP and returns the associated email and redirect URL

func (*Handler) VerifyToken

func (h *Handler) VerifyToken(token string) (email string, redirectURL string, error error)

VerifyToken verifies a login token and returns the associated email and redirect URL

type MockSender

type MockSender struct {
	SendFunc     func(to, subject, body string) error
	SendHTMLFunc func(to, subject, htmlBody, textBody string) error
	Calls        []SendCall
	HTMLCalls    []SendHTMLCall
}

MockSender is a mock email sender for testing

func (*MockSender) Send

func (m *MockSender) Send(to, subject, body string) error

Send records the call and optionally executes a custom function

func (*MockSender) SendHTML

func (m *MockSender) SendHTML(to, subject, htmlBody, textBody string) error

SendHTML records the call and optionally executes a custom function

type SMTPSender

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

SMTPSender sends emails via SMTP

func NewSMTPSender

func NewSMTPSender(cfg config.SMTPConfig, parentEmail, parentName string) *SMTPSender

NewSMTPSender creates a new SMTP email sender

func (*SMTPSender) Send

func (s *SMTPSender) Send(to, subject, body string) error

Send sends an email via SMTP

func (*SMTPSender) SendHTML

func (s *SMTPSender) SendHTML(to, subject, htmlBody, textBody string) error

SendHTML sends an HTML email with plain text fallback via SMTP

type SendCall

type SendCall struct {
	To      string
	Subject string
	Body    string
}

SendCall represents a call to Send

type SendGridSender

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

SendGridSender sends emails via SendGrid API

func NewSendGridSender

func NewSendGridSender(cfg config.SendGridConfig, parentEmail, parentName string) *SendGridSender

NewSendGridSender creates a new SendGrid email sender

func (*SendGridSender) Send

func (s *SendGridSender) Send(to, subject, body string) error

Send sends an email via SendGrid API

func (*SendGridSender) SendHTML

func (s *SendGridSender) SendHTML(to, subject, htmlBody, textBody string) error

SendHTML sends an HTML email with plain text fallback via SendGrid API

type SendHTMLCall

type SendHTMLCall struct {
	To       string
	Subject  string
	HTMLBody string
	TextBody string
}

SendHTMLCall represents a call to SendHTML

type Sender

type Sender interface {
	Send(to, subject, body string) error
	SendHTML(to, subject, htmlBody, textBody string) error
}

Sender is an interface for sending emails

type SendmailSender added in v0.2.0

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

SendmailSender sends emails via sendmail command

func NewSendmailSender added in v0.2.0

func NewSendmailSender(cfg config.SendmailConfig, parentEmail, parentName string) *SendmailSender

NewSendmailSender creates a new sendmail sender

func (*SendmailSender) Send added in v0.2.0

func (s *SendmailSender) Send(to, subject, body string) error

Send sends an email via sendmail command

func (*SendmailSender) SendHTML added in v0.2.0

func (s *SendmailSender) SendHTML(to, subject, htmlBody, textBody string) error

SendHTML sends an HTML email with plain text fallback via sendmail command

type Token

type Token struct {
	Value       string
	Email       string
	OTP         string // One-Time Password (12-character alphanumeric)
	RedirectURL string // Original URL to redirect to after authentication
	CreatedAt   time.Time
	ExpiresAt   time.Time
	Used        bool
}

Token represents an email authentication token

func (*Token) IsValid

func (t *Token) IsValid() bool

IsValid checks if the token is still valid

type TokenStore

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

TokenStore manages email authentication tokens using a KVS backend

func NewTokenStore

func NewTokenStore(secret string, kvsStore kvs.Store) *TokenStore

NewTokenStore creates a new token store backed by KVS

func (*TokenStore) CleanupExpired

func (s *TokenStore) CleanupExpired()

CleanupExpired removes expired tokens (no-op for KVS with TTL support) The underlying KVS automatically handles expiration, so this is a no-op for compatibility.

func (*TokenStore) Count

func (s *TokenStore) Count() int

Count returns the number of tokens (for testing)

func (*TokenStore) DeleteToken

func (s *TokenStore) DeleteToken(tokenValue string)

DeleteToken removes a token from the store

func (*TokenStore) GenerateToken

func (s *TokenStore) GenerateToken(email string, redirectURL string, duration time.Duration) (string, error)

GenerateToken generates a new token for an email address with redirect URL

func (*TokenStore) VerifyOTP

func (s *TokenStore) VerifyOTP(otpInput string) (email string, redirectURL string, err error)

VerifyOTP verifies an OTP and returns the associated email and redirect URL

func (*TokenStore) VerifyToken

func (s *TokenStore) VerifyToken(tokenValue string) (email string, redirectURL string, err error)

VerifyToken verifies a token and returns the associated email and redirect URL

Jump to

Keyboard shortcuts

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