urlx

package
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package urlx provides shared URL parsing, normalization, and validation helpers, along with token URL construction and shortening utilities for embedding signed tokens in email action links

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTokenCreationFailed indicates that the token pair could not be created
	ErrTokenCreationFailed = errors.New("urlx: token creation failed")
	// ErrURLConstructionFailed indicates that the URL could not be constructed
	ErrURLConstructionFailed = errors.New("urlx: URL construction failed")
	// ErrEmptyURL indicates the input URL is empty or whitespace
	ErrEmptyURL = errors.New("urlx: url is required")
	// ErrInvalidURL indicates the input could not be parsed as a URL
	ErrInvalidURL = errors.New("urlx: invalid url")
	// ErrMissingHost indicates the parsed URL does not contain a host
	ErrMissingHost = errors.New("urlx: url host is required")
	// ErrUnsupportedScheme indicates the URL scheme is not http or https
	ErrUnsupportedScheme = errors.New("urlx: unsupported url scheme")
	// ErrSizeLimitExceeded indicates a response body exceeded the configured size limit
	ErrSizeLimitExceeded = errors.New("urlx: response size limit exceeded")
)

Functions

func BuildTokenURL

func BuildTokenURL(ctx context.Context, sl *shortlinks.Client, baseURL url.URL, token string) (string, error)

BuildTokenURL appends a token query parameter to the base URL and optionally shortens the result via the shortlinks client. If sl is nil or shortening fails, the full-length URL is returned (graceful degradation)

func MaxSizeValidator

func MaxSizeValidator(maxBytes int64) httpsling.ValidationFunc

MaxSizeValidator returns a validator that rejects payloads whose size exceeds maxBytes

func NewHTTPClient

func NewHTTPClient(opts ...httpclient.Option) (*http.Client, error)

NewHTTPClient returns an *http.Client built on the httpclient default transport with the org default timeout, overridable via opts

func NewRequester

func NewRequester(opts ...httpsling.Option) (*httpsling.Requester, error)

NewRequester returns an httpsling.Requester backed by NewHTTPClient so callers never fall back to http.DefaultClient, with opts applied after the defaults

func NormalizeHostname

func NormalizeHostname(rawURL string) (string, error)

NormalizeHostname extracts the hostname from a URL or raw host input, lowercasing it and stripping any trailing dots

func Parse

func Parse(rawURL string) (*url.URL, error)

Parse parses rawURL after trimming surrounding whitespace, applying the https scheme when rawURL has none, and requires the result to contain a hostname and an http or https scheme

func ParseAbsolute

func ParseAbsolute(rawURL string) (*url.URL, error)

ParseAbsolute parses rawURL after trimming surrounding whitespace, requiring an explicit http or https scheme and a host

func ReadBody

func ReadBody(resp *http.Response, vf httpsling.ValidationFunc) ([]byte, error)

ReadBody reads and closes resp.Body, invoking vf with the response content type and the accumulated size as data arrives so a failing validator aborts the read mid-stream; the advertised Content-Length is validated before anything is read

func WithoutQuery

func WithoutQuery(rawURL string) string

WithoutQuery returns rawURL with its query string and fragment removed, returning rawURL unchanged when it cannot be parsed

Types

type AnonTokenRequest

type AnonTokenRequest struct {
	// Prefix is prepended to SubjectID to form the JWT subject (e.g. "anon-tc-")
	Prefix string
	// SubjectID is the domain-specific identifier (e.g. request ID, ULID)
	SubjectID string
	// OrgID is the organization the token is scoped to
	OrgID string
	// Email is the recipient email address embedded in the token
	Email string
	// Duration is the access token lifetime
	Duration time.Duration
	// ExtraClaims is an optional callback to set domain-specific claim fields
	// (e.g. AssessmentID, TrustCenterID) on the token before signing
	ExtraClaims func(*tokens.Claims)
}

AnonTokenRequest holds the parameters for generating an anonymous JWT and embedding it in a URL

type AnonTokenResult

type AnonTokenResult struct {
	// AccessToken is the signed JWT string
	AccessToken string
	// URL is the full URL with the token embedded as a query parameter
	URL string
}

AnonTokenResult holds the generated access token and the final URL containing it

func GenerateAnonTokenURL

func GenerateAnonTokenURL(ctx context.Context, tm *tokens.TokenManager, sl *shortlinks.Client, baseURL url.URL, req AnonTokenRequest) (*AnonTokenResult, error)

GenerateAnonTokenURL creates a short-lived anonymous JWT from the request parameters, appends it to baseURL as a query parameter, and optionally shortens the result

Jump to

Keyboard shortcuts

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