requests

package
v1.25.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 9 Imported by: 2

Documentation

Index

Constants

View Source
const DEFAULT_TIMEOUT = 15 * time.Second

Variables

This section is empty.

Functions

func ParseBearer added in v1.18.0

func ParseBearer(header http.Header) (string, error)

ParseBearer returns the token of a well-formed "Bearer <token>" Authorization header. It is the single parser for the module; mcp uses it too. The scheme compares case-insensitively and is separated from the non-empty token by one or more spaces or tabs, as RFC 7235 allows. The token itself contains no whitespace. An absent header is ENOTAUTHENTICATED; a present but malformed one, or more than one, is EINVALID, so callers can tell "no credentials" from "bad credentials" with ez.ErrorCode.

func RequestID added in v1.17.0

func RequestID(ctx context.Context) string

RequestID returns the request ID New stored in ctx, or "" when ctx does not come from a request.

Types

type AuthOption added in v1.18.0

type AuthOption func(*authConfig)

AuthOption configures where Authenticate looks for credentials besides the Authorization header.

func WithCookie added in v1.18.0

func WithCookie(name string, allowedOrigins ...string) AuthOption

WithCookie reads the token from the named cookie when no Authorization header is present. Requests authenticated by cookie must pass the CSRF rule: a method other than GET, HEAD or OPTIONS needs an Origin header listed in allowedOrigins (exact match). With no allowedOrigins, cookies authenticate only GET, HEAD and OPTIONS. The method comes from WithMethod; a request without one is treated as unsafe.

type AuthenticatedRequest

type AuthenticatedRequest struct {
	StandardRequest
	// Token is the bearer token from the Authorization header, or the cookie
	// named by WithCookie when the header is absent.
	Token string
	// FromCookie is true when Token came from a cookie rather than the
	// Authorization header.
	FromCookie bool
}

type Body

type Body interface {
	Validate() error
}

type Option

type Option func(*StandardRequest)

Option is a function that modifies the Request.

func WithMethod added in v1.18.0

func WithMethod(method string) Option

WithMethod returns an Option that records the HTTP method of the request, for example c.Request().Method. Pass it when Authenticate may accept a cookie: the CSRF rule treats a request without a method as unsafe, so a cookie then needs an allowed Origin on every method.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout returns an Option that sets the timeout for the request context. It replaces the previous deadline and releases the timer of the context it replaces, keeping the context values.

type Request

type Request interface {
	GetID() string
	GetIP() string
	GetClient() string
	GetHeader() http.Header
	GetBody() Body
	SetBody(Body)
	GetCreatedAt() time.Time
	GetContext() context.Context
	SetContext(context.Context)
	GetLocale() string
	SetLocale(string)
}

type StandardRequest

type StandardRequest struct {
	ID        string
	IP        string
	Client    string
	Header    http.Header
	Body      Body
	CreatedAt time.Time
	Context   context.Context
	Cancel    context.CancelFunc
	Locale    string
	// Method is the HTTP method, set by WithMethod. Authenticate needs it to
	// apply the CSRF rule of WithCookie; empty counts as unsafe.
	Method string
}

func New

func New(header http.Header, ip string, opts ...Option) *StandardRequest

func (StandardRequest) Authenticate

func (r StandardRequest) Authenticate(opts ...AuthOption) (*AuthenticatedRequest, error)

Authenticate reads the credentials of the request, in order: the Authorization bearer token, then the WithCookie cookie. A malformed Authorization header is EINVALID and the cookie is not tried. A request with no credentials is ENOTAUTHENTICATED. Authenticate only extracts credentials; the caller verifies them.

func (*StandardRequest) GetBody

func (r *StandardRequest) GetBody() Body

func (*StandardRequest) GetClient

func (r *StandardRequest) GetClient() string

func (*StandardRequest) GetContext

func (r *StandardRequest) GetContext() context.Context

func (*StandardRequest) GetCreatedAt

func (r *StandardRequest) GetCreatedAt() time.Time

func (*StandardRequest) GetHeader

func (r *StandardRequest) GetHeader() http.Header

func (*StandardRequest) GetID

func (r *StandardRequest) GetID() string

func (*StandardRequest) GetIP

func (r *StandardRequest) GetIP() string

func (*StandardRequest) GetLocale added in v1.9.2

func (r *StandardRequest) GetLocale() string

GetLocale returns the locale used to localize the response, e.g. "es-MX". New negotiates it from the Accept-Language header, so it is always set.

func (StandardRequest) LegacyCredentials added in v1.18.0

func (r StandardRequest) LegacyCredentials(keyHeader, secretHeader string) (string, string, error)

LegacyCredentials returns the legacy header pair, for REST APIs that keep API-key clients working while they move to Authorization: Bearer. It reads nothing else. Both headers absent is ez.ENOTAUTHENTICATED; only one present is ez.EINVALID. Compose it after Authenticate and fall through only on ez.ENOTAUTHENTICATED, so a malformed Authorization header or a cookie that fails the CSRF rule is still refused rather than retried with the pair:

authReq, err := req.Authenticate()
if ez.ErrorCode(err) == ez.ENOTAUTHENTICATED {
	key, secret, err := req.LegacyCredentials("API-KEY", "API-SECRET")
	if err != nil {
		return ez.Wrap(err)
	}
	return resolveLegacy(key, secret)
}
if err != nil {
	return ez.Wrap(err)
}

func (*StandardRequest) SetBody

func (r *StandardRequest) SetBody(body Body)

func (*StandardRequest) SetContext

func (r *StandardRequest) SetContext(ctx context.Context)

func (*StandardRequest) SetLocale added in v1.9.2

func (r *StandardRequest) SetLocale(locale string)

SetLocale overrides the negotiated locale, e.g. with an authenticated user's saved preference.

Jump to

Keyboard shortcuts

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