Documentation
¶
Overview ¶
Package auth decides how a request identifies itself to the API.
There are two families, and the second is why this package exists. STATIC is what every tool does: a bearer, an API key, a basic — a fixed value, from a flag, applied to every call. DYNAMIC is what many production APIs require: trading key+secret for a short-lived JWT, typically valid for an hour or two. With static auth, an MCP server for those APIs works until the token expires and then returns 401 until somebody restarts it.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Basic ¶
type Basic struct{ User, Password string }
Basic sets `Authorization: Basic <base64(user:password)>`.
type Flow ¶
type Flow struct {
// URL of the endpoint that issues the token.
URL string
// Fields sent as form-urlencoded, which is what authentication endpoints usually accept.
Fields map[string]string
// TokenPath says where the token sits in the JSON response, as nested keys.
// E.g. {"data":{"token":"x"}} → []string{"data","token"}.
TokenPath []string
// TTL is how long the token is valid. The renewal margin is derived from it.
TTL time.Duration
// Client is injectable for tests.
Client *http.Client
// contains filtered or unexported fields
}
Flow is dynamic auth: it trades credentials for a short-lived token and renews it on its own.
The token is kept in memory and renewed BEFORE it expires, with a margin. Renewing on a 401 would also work, but it burns one call every couple of hours and turns a predictable event into a visible failure for whoever is on the other side.
func (*Flow) Invalidate ¶
func (f *Flow) Invalidate()
Invalidate throws away the stored token — for when the API answered 401 despite the validity it announced itself. The next call authenticates again.
type Signature ¶ added in v1.3.0
type Signature struct {
// Algo is `sha256` (plain hash of the payload) or `hmac-sha256` (keyed by Secret).
Algo string
// Payload is the template of the string to sign.
Payload string
// Into says where the signature goes: `header:Name=template` or `query:name=template`.
Into string
AppID string
Secret string
}
Signature is per-request authentication: instead of carrying a fixed token, each call is signed over its own content. Shopee's affiliate API and TikTok Shop's both work this way, and no amount of bearer/apikey configuration reaches them — the credential is not a value, it is a computation.
The shapes differ (Shopee hashes appId+timestamp+body+secret into an Authorization header; TikTok HMACs path+query+body into a `sign` query parameter), so what is configurable here is the RECIPE: which algorithm, which string gets signed, and where the result goes.
Placeholders, usable in both Payload and Into:
{app_id} {secret} the credentials
{timestamp} unix seconds, computed per request
{body} {path} {query} the request itself ({query} sorted, `k=v` joined, no separator)
{signature} the result (Into only)