util

package
v0.0.0-...-4d677a2 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2025 License: MPL-2.0 Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DevelopmentEnv = "development"
	StagingEnv     = "staging"
	ProductionEnv  = "production"

	AccountsServiceName     = "accounts"
	PremiumServiceName      = "premium"
	EmailAliasesServiceName = "email-aliases"

	KeyServiceSecretEnv    = "KEY_SERVICE_SECRET"
	KeyServiceSecretHeader = "key-service-secret"
	KeyServiceURLEnv       = "KEY_SERVICE_URL"
)

Variables

View Source
var (
	// Account errors, prefixed with '11'
	ErrEmailNotVerified            = NewExposedError(11003, "email not verified")
	ErrIncorrectVerificationIntent = NewExposedError(11004, "incorrect verification intent")
	ErrNewAccountEmailRequired     = NewExposedError(11005, "newAccountEmail is required when no verification token is provided")

	// Key errors, prefixed with '12'
	ErrKeyNotFound         = NewExposedError(12001, "key not found")
	ErrMaxUserKeysExceeded = NewExposedError(12002, "maximum number of keys exceeded for this service")

	// Verification errors, prefixed with '13'
	ErrTooManyVerifications           = NewExposedError(13001, "too many pending verification requests for email")
	ErrVerificationNotFound           = NewExposedError(13002, "verification not found or invalid id/code")
	ErrIntentNotAllowed               = NewExposedError(13003, "intent not allowed")
	ErrAccountExists                  = NewExposedError(13004, "account already exists")
	ErrAccountDoesNotExist            = NewExposedError(13005, "account does not exist")
	ErrEmailDomainNotSupported        = NewExposedError(13006, "email domain is not supported")
	ErrFailedToSendEmailInvalidFormat = NewExposedError(13007, "failed to send email due to invalid format")

	// Auth errors, prefixed with '14'
	ErrInterimPasswordStateNotFound = NewExposedError(14001, "interim password state not found")
	ErrInterimPasswordStateExpired  = NewExposedError(14002, "interim password state has expired")
	ErrOutdatedSession              = NewExposedError(14003, "outdated session")
	ErrIncorrectCredentials         = NewExposedError(14004, "incorrect credentials")
	ErrIncorrectEmail               = NewExposedError(14005, "incorrect email")
	ErrIncorrectPassword            = NewExposedError(14006, "incorrect password")
	ErrInvalidTokenAudience         = NewExposedError(14007, "invalid token audience")
	ErrBadTOTPCode                  = NewExposedError(14008, "invalid TOTP code")
	ErrInterimPasswordStateMismatch = NewExposedError(14009, "interim password state mismatch")
	ErrBadRecoveryKey               = NewExposedError(14010, "invalid recovery key")
	ErrTOTPAlreadyEnabled           = NewExposedError(14011, "TOTP authentication is already enabled for this account")
	ErrTOTPCodeAlreadyUsed          = NewExposedError(14012, "TOTP code has already been used")

	// Misc errors, prefixed with '15'
	ErrInvalidServicesKey = NewExposedError(15001, "invalid services key")
)
View Source
var (
	TestKeyServiceRouter *chi.Mux = nil
)

Functions

func AssertErrorResponseCode

func AssertErrorResponseCode(t *testing.T, resp *httptest.ResponseRecorder, expectedCode int)

func CanonicalizeEmail

func CanonicalizeEmail(email string) string

Canonicalize email for general email address storage.

func CreateI18nBundle

func CreateI18nBundle() (*i18n.Bundle, error)

func CreateJSONTestRequest

func CreateJSONTestRequest(path string, body interface{}) *http.Request

func DecodeJSONAndValidate

func DecodeJSONAndValidate(w http.ResponseWriter, r *http.Request, data interface{}) bool

func DecodeJSONTestResponse

func DecodeJSONTestResponse(t *testing.T, body *bytes.Buffer, parsed interface{})

func ExecuteTestRequest

func ExecuteTestRequest(req *http.Request, handler http.Handler) *httptest.ResponseRecorder

func ExtractAuthToken

func ExtractAuthToken(r *http.Request) (string, error)

func GenerateRandomString

func GenerateRandomString(length int) string

func GetRequestLocale

func GetRequestLocale(explicitLocale string, r *http.Request) string

func HashRecoveryKey

func HashRecoveryKey(recoveryKey string) ([]byte, error)

func IsEmailAllowed

func IsEmailAllowed(email string, checkStrictCountries bool) bool

func ListenOnPGChannel

func ListenOnPGChannel(ctx context.Context, conn *pgxpool.Conn, channelName string) error

func RenderErrorResponse

func RenderErrorResponse(w http.ResponseWriter, r *http.Request, status int, err error)

func SimplifyEmail

func SimplifyEmail(email string) *string

Simplify email address according to provider-specific rules. To be used for recovery/login assistance flows only.

func StartPrometheusServer

func StartPrometheusServer(registry *prometheus.Registry, listen string)

func VerifyRecoveryKeyHash

func VerifyRecoveryKeyHash(recoveryKey string, storedHash []byte) bool

Types

type ErrorResponse

type ErrorResponse struct {
	// Error code
	Code *int `json:"code"`
	// HTTP status code
	Status int `json:"status"`
	// Error message
	Error string `json:"error"`
}

@Description Standard error response

type ExposedError

type ExposedError struct {
	// Error code
	Code int
	// Details of the error
	Details string
}

ExposedError represents an error that is safe to expose to API clients

func NewExposedError

func NewExposedError(code int, details string) *ExposedError

func (*ExposedError) Error

func (e *ExposedError) Error() string

type KeyServiceClient

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

KeyServiceClient is used to communicate with the key service

func NewKeyServiceClient

func NewKeyServiceClient() *KeyServiceClient

NewKeyServiceClient creates a new client for interacting with the key service

func (*KeyServiceClient) MakeRequest

func (k *KeyServiceClient) MakeRequest(method string, path string, body interface{}, response interface{}) error

MakeRequest sends a request to the key service with the given path and body, unmarshaling the response into the provided response interface