auth

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: MIT Imports: 10 Imported by: 0

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 APIKey

type APIKey struct {
	In    string // header | query | cookie
	Name  string
	Value string
}

APIKey puts the key where the API expects it: header, query or cookie.

func (APIKey) Apply

func (k APIKey) Apply(_ context.Context, req *http.Request) error

type Applier

type Applier interface {
	Apply(ctx context.Context, req *http.Request) error
}

Applier stamps identity onto a request that is about to go out.

type Basic

type Basic struct{ User, Password string }

Basic sets `Authorization: Basic <base64(user:password)>`.

func (Basic) Apply

func (b Basic) Apply(_ context.Context, req *http.Request) error

type Bearer

type Bearer struct{ Token string }

Bearer sets `Authorization: Bearer <token>`.

func (Bearer) Apply

func (b Bearer) Apply(_ context.Context, req *http.Request) error

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) Apply

func (f *Flow) Apply(ctx context.Context, req *http.Request) error

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 None

type None struct{}

None authenticates nothing — public APIs exist.

func (None) Apply

func (None) Apply(context.Context, *http.Request) error

Jump to

Keyboard shortcuts

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