httpclient

package
v1.6.8 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package httpclient is an HTTP client, with go-client, of a resource which may need to be authorized, such as an MCP server's: a request which isn't authorized returns an AuthError, with the server's challenge, and the resource's metadata, and that of its authorization servers, is discovered, which says how a token for it is got.

Index

Constants

View Source
const (
	ResourceMetadataPath = ".well-known/oauth-protected-resource"
	OAuthMetadataPath    = ".well-known/oauth-authorization-server"
	OIDCMetadataPath     = ".well-known/openid-configuration"
)

The well-known paths of a resource's metadata, and of an authorization server's, as OAuth, and OpenID Connect, have them

View Source
const AuthenticateHeader = "WWW-Authenticate"

AuthenticateHeader is the header of a server's challenges

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthError

type AuthError struct {
	Scheme string     `json:"scheme"`
	Params url.Values `json:"params,omitempty"`
}

AuthError is a server's challenge, as a request to it isn't authorized: its scheme, such as Bearer, and its parameters, by their names in lower case, such as realm, and resource_metadata, the URL of the resource's metadata

func AsAuthError

func AsAuthError(err error) *AuthError

AsAuthError returns the AuthError the error is, or wraps, or nil

func ParseChallenge

func ParseChallenge(header http.Header) *AuthError

ParseChallenge returns the challenge of the header, of its challenges, which is the Bearer one, when it has one, as that's the scheme a token is for, or otherwise its first, or nil, when it hasn't any

func (*AuthError) Error

func (e *AuthError) Error() string

Error returns the challenge's error, and its description, when it has them, or that it isn't authorized

func (*AuthError) Get

func (e *AuthError) Get(key string) string

Get returns the value of the challenge's parameter, whatever the case of its name, or nothing

type Client

type Client struct {
	*client.Client
	// contains filtered or unexported fields
}

Client is a client of a resource, at its endpoint, with go-client, whose requests which aren't authorized return an AuthError, and which discovers how a token for the resource is got

func New

func New(endpoint string, opts ...client.ClientOpt) (*Client, error)

New returns a client of the resource at the endpoint, with go-client's options, such as a token, which the endpoint is applied after, so an option can't change it

func (*Client) Discover

func (c *Client) Discover(ctx context.Context, resource string) (*Metadata, error)

Discover returns how a token for the resource at the URL, or the client's endpoint, when it's empty, is got: its metadata, at its well-known path, with its path, or without, and that of its authorization servers, or of its origin, as an authorization server, when it says none. Returns ErrNotFound when no authorization server's metadata is found.

func (*Client) DiscoverResource

func (c *Client) DiscoverResource(ctx context.Context, err error) (*ResourceMetadata, error)

DiscoverResource returns the metadata of the resource, alone, without that of its authorization servers, such as for its name: from the URL of its metadata the error has, when it's an AuthError which has one, or otherwise at its well-known path, of the client's endpoint, with its path, or without. Returns ErrNotFound when it has none.

func (*Client) DiscoverWithError

func (c *Client) DiscoverWithError(ctx context.Context, err error) (*Metadata, error)

DiscoverWithError returns how a token is got, as the error, which is an AuthError, says, from the URL of the resource's metadata it has, when it has one, or otherwise as Discover does, for the client's endpoint. Returns the error, when it isn't an AuthError.

func (*Client) DoAuthWithContext

func (c *Client) DoAuthWithContext(ctx context.Context, req client.Payload, v any, opts ...client.RequestOpt) error

DoAuthWithContext makes the request, as DoWithContext does, and returns an AuthError, with the server's challenge, when it isn't authorized, which is a 401 Unauthorized response with a WWW-Authenticate header, so how a token is got for it can be discovered, with DiscoverWithError

func (*Client) Endpoint

func (c *Client) Endpoint() string

Endpoint returns the endpoint of the resource

func (*Client) Register

func (c *Client) Register(ctx context.Context, server ServerMetadata, name string, redirectURIs []string, scopes ...string) (*Registration, error)

Register registers a client, with the name, and the URIs it's redirected to, and the scopes, with the authorization server, which has a registration endpoint, and returns it, without the resource's credentials, as it's another's. A server which says the request isn't valid, as some don't register a client with scopes, even those they say they support, is asked again, without them, as they're optional, and asked for as it's authorized. Returns ErrNotImplemented when the server doesn't register clients, so one's identifier is needed.

type Metadata

type Metadata struct {
	Resource             ResourceMetadata `json:"resource,omitzero"`
	AuthorizationServers []ServerMetadata `json:"authorization_servers,omitempty"`
}

Metadata is how a token for a resource is got: the resource's metadata, when it has it, and that of its authorization servers

type Registration

type Registration struct {
	ClientID     string   `json:"client_id"`
	ClientSecret string   `json:"client_secret,omitempty"`
	RedirectURIs []string `json:"redirect_uris,omitempty"`
}

Registration is a client, as an authorization server registered it: its identifier, its secret, when it has one, and the URIs it's redirected to

type ResourceMetadata

type ResourceMetadata struct {
	Resource               string   `json:"resource,omitempty"`
	AuthorizationServers   []string `json:"authorization_servers,omitempty"`
	ScopesSupported        []string `json:"scopes_supported,omitempty"`
	BearerMethodsSupported []string `json:"bearer_methods_supported,omitempty"`
	ResourceName           string   `json:"resource_name,omitempty"`
}

ResourceMetadata is a protected resource's metadata, which says which authorization servers issue tokens for it https://datatracker.ietf.org/doc/html/rfc9728

type ServerMetadata

type ServerMetadata struct {
	Issuer                            string   `json:"issuer"`
	AuthorizationEndpoint             string   `json:"authorization_endpoint,omitempty"`
	TokenEndpoint                     string   `json:"token_endpoint,omitempty"`
	RegistrationEndpoint              string   `json:"registration_endpoint,omitempty"`
	RevocationEndpoint                string   `json:"revocation_endpoint,omitempty"`
	ScopesSupported                   []string `json:"scopes_supported,omitempty"`
	ResponseTypesSupported            []string `json:"response_types_supported,omitempty"`
	GrantTypesSupported               []string `json:"grant_types_supported,omitempty"`
	CodeChallengeMethodsSupported     []string `json:"code_challenge_methods_supported,omitempty"`
	TokenEndpointAuthMethodsSupported []string `json:"token_endpoint_auth_methods_supported,omitempty"`
}

ServerMetadata is an authorization server's metadata, which says where a person authorizes a client, a token is got, and a client is registered https://datatracker.ietf.org/doc/html/rfc8414

func (ServerMetadata) Config

func (server ServerMetadata) Config(registration Registration, redirectURI string, scopes ...string) *oauth2.Config

Config returns the configuration of the authorization code grant, with the server, for the client, its redirect URI, and the scopes, which its code is exchanged, and its token refreshed, with

func (ServerMetadata) SupportsS256

func (server ServerMetadata) SupportsS256() bool

SupportsS256 returns true when the authorization server says it supports PKCE's S256, which a client needs, as MCP has it

Jump to

Keyboard shortcuts

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