sms

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2026 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package sms is the abstraction over an SMS backend, mirroring pkg/email. A Sender delivers a short text Message (a verification OTP) to a phone number. Concrete senders for Twilio, AWS SNS, and Azure Communication Services live in sibling files; NewLogOnly is the disabled/dev default that logs instead of delivering.

All senders are safe for concurrent use and honor ctx cancellation via the injected *http.Client. Each provider's base URL is an injectable struct field so tests can point Send at an httptest server without any hardcoded endpoint.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidMessage is returned (wrapped) when a Message fails
	// validation.
	ErrInvalidMessage = errors.New("sms: invalid message")

	// ErrTransport is returned (wrapped) when a backend rejects the send
	// or the HTTP round trip fails.
	ErrTransport = errors.New("sms: transport failure")

	// ErrProviderUnavailable is returned (wrapped) when a provider is
	// configured but cannot be reached, or a provider arm is not yet
	// implemented.
	ErrProviderUnavailable = errors.New("sms: provider unavailable")
)

Sentinel errors returned (wrapped) by the sms package. Callers test with errors.Is.

Functions

func ParseAzureConnectionString

func ParseAzureConnectionString(cs string) (endpoint, accessKey string, err error)

ParseAzureConnectionString splits an ACS connection string into its endpoint and accesskey halves. It returns a wrapped ErrTransport when either part is missing.

Types

type AzureConfig

type AzureConfig struct {
	// Endpoint is the ACS resource endpoint, e.g.
	// "https://my-resource.communication.azure.com".
	Endpoint string

	// AccessKey is the base64-encoded HMAC key from the connection
	// string. It signs each request.
	AccessKey string

	// From is the originating number (alphanumeric sender id or E.164)
	// used when Message.From is empty.
	From string

	// HTTPClient overrides the HTTP client for tests. Empty uses a
	// client with azureHTTPTimeout.
	HTTPClient *http.Client
}

AzureConfig configures an Azure Communication Services SMS Sender. Endpoint, AccessKey, and From are required. The endpoint + access key are typically supplied together as a connection string of the form "endpoint=https://x.communication.azure.com/;accesskey=BASE64" — ParseAzureConnectionString splits one into the two fields.

type Message

type Message struct {
	// To is the destination phone number in E.164 form (e.g.
	// "+14155550123").
	To string

	// From is the sender id / originating number. May be empty when the
	// Sender injects a configured default.
	From string

	// Body is the message text.
	Body string
}

Message is a single outgoing SMS. Validate must pass before a Sender dispatches it.

func (Message) Validate

func (m Message) Validate() error

Validate checks the message is well-formed: a non-empty E.164-looking To and a non-empty Body. The check is deliberately permissive on the number shape (a leading '+' and digits) — providers do the authoritative validation, and identity's own normalization runs in the service layer.

type SNSConfig

type SNSConfig struct {
	// Region is the AWS region, e.g. "us-east-1". It selects the
	// regional SNS endpoint and is part of the SigV4 credential scope.
	Region string

	// AccessKeyID and SecretAccessKey are the IAM credentials used to
	// SigV4-sign the Publish request.
	AccessKeyID     string
	SecretAccessKey string

	// SessionToken, when set, is sent as X-Amz-Security-Token for
	// temporary credentials. Optional.
	SessionToken string

	// SenderID is the optional originating sender id, applied as the
	// AWS.SNS.SMS.SenderID message attribute and as the From fallback.
	SenderID string

	// BaseURL overrides the regional endpoint for tests. Empty derives
	// "https://sns.{region}.amazonaws.com/" from Region.
	BaseURL string

	// HTTPClient overrides the HTTP client for tests. Empty uses a
	// client with snsHTTPTimeout.
	HTTPClient *http.Client
	// contains filtered or unexported fields
}

SNSConfig configures an AWS SNS Sender. Region, AccessKeyID, and SecretAccessKey are required; SenderID is an optional originating id.

type Sender

type Sender interface {
	Send(ctx context.Context, m Message) error
}

Sender delivers an SMS Message. Implementations validate the message and return a wrapped ErrInvalidMessage for malformed input, or a wrapped ErrTransport / ErrProviderUnavailable for backend failures.

func NewAzure

func NewAzure(cfg AzureConfig) (Sender, error)

NewAzure builds an Azure Communication Services SMS Sender from cfg. It returns a wrapped ErrTransport on missing/invalid credentials.

func NewLogOnly

func NewLogOnly(logger *zap.Logger) Sender

NewLogOnly returns a Sender that logs each Send at WARN and returns nil. It is the disabled/dev default — installed when GATEWAY_SMS_ ENABLED is false — so service code can always call Send without a nil check.

func NewSNS

func NewSNS(cfg SNSConfig) (Sender, error)

NewSNS builds an AWS SNS Sender from cfg. SigV4 signing is done with the standard library (crypto/hmac + crypto/sha256); no AWS SDK dependency is pulled in. Returns a wrapped ErrTransport on missing credentials.

func NewTwilio

func NewTwilio(cfg TwilioConfig) (Sender, error)

NewTwilio builds a Twilio Sender from cfg. It returns a wrapped ErrTransport if a required credential is missing.

type TwilioConfig

type TwilioConfig struct {
	// AccountSID is the Twilio account identifier (the "AC..." string).
	// It forms both the basic-auth username and the URL path segment.
	AccountSID string

	// AuthToken is the Twilio auth token (basic-auth password).
	AuthToken string

	// From is the originating number or messaging-service sender id used
	// when Message.From is empty.
	From string

	// BaseURL overrides the REST API root for tests. Empty uses
	// twilioDefaultBaseURL.
	BaseURL string

	// HTTPClient overrides the HTTP client (and thus the timeout) for
	// tests. Empty uses a client with twilioHTTPTimeout.
	HTTPClient *http.Client
}

TwilioConfig configures a Twilio Sender. AccountSID, AuthToken, and From are required.

Jump to

Keyboard shortcuts

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