provider

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package provider defines what a provider adapter implements: a declarative Manifest, validation with account discovery, and authentication of outgoing requests. It also holds the Registry a host fills at startup.

Index

Constants

This section is empty.

Variables

View Source
var ErrBadMaterial = errors.New("provider: not credential material")

ErrBadMaterial is returned for plaintext that is not encoded material.

View Source
var ErrUnknownProvider = errors.New("provider: unknown provider")

ErrUnknownProvider is returned for a provider key that is not registered.

Functions

func Classify

func Classify(providerKey string, resp *http.Response) error

Classify returns nil for a 2xx response and a *StatusError otherwise: 401 is an invalid credential, 403 an insufficient scope, 429 rate limiting, 5xx transient, and any other status an invalid credential.

func EncodeMaterial

func EncodeMaterial(m Material) ([]byte, error)

EncodeMaterial returns the plaintext to seal. The caller seals it and clears it.

Types

type AuthMethod

type AuthMethod struct {
	// Key identifies the method, for example "api_token".
	Key    string  `json:"key"`
	Fields []Field `json:"fields"`
	Scopes []Scope `json:"scopes,omitempty"`
	// AccountSelection says how the account is chosen: "discovered" from the
	// credential, or "chosen" by the person from a list.
	AccountSelection string `json:"accountSelection,omitempty"`
	HelpURL          string `json:"helpUrl,omitempty"`
}

AuthMethod is one way to connect to a provider.

type Field

type Field struct {
	Name     string `json:"name"`
	Label    string `json:"label"`
	Secret   bool   `json:"secret"`
	Required bool   `json:"required"`
	Help     string `json:"help,omitempty"`
}

Field is an input a method needs.

type Manifest

type Manifest struct {
	// Key identifies the provider, for example "cloudflare".
	Key         string `json:"key"`
	DisplayName string `json:"displayName"`
	// BaseURLs are the only origins a request for this provider may reach.
	BaseURLs    []string     `json:"baseUrls"`
	AuthMethods []AuthMethod `json:"authMethods"`
	HelpURL     string       `json:"helpUrl,omitempty"`
}

Manifest describes a provider so a user interface can render a connection form and an agent can read what a connection needs. It contains no code.

func (Manifest) Hosts

func (m Manifest) Hosts() ([]string, error)

Hosts returns the host (with port, if any) of each base URL.

func (Manifest) Method

func (m Manifest) Method(key string) (AuthMethod, bool)

Method returns the auth method with key.

func (Manifest) Validate

func (m Manifest) Validate() error

Validate checks that the manifest is complete.

type Material

type Material struct {
	// Kind names the structure, for example "api_token".
	Kind string
	// Fields holds the credential's values by field name.
	Fields map[string]cf.Secret
	// ExpiresAt is when an access token expires, if it does.
	ExpiresAt *time.Time
}

Material is a credential's plaintext, as a provider adapter sees it. It exists only between unsealing and use; every value is a cf.Secret.

func DecodeMaterial

func DecodeMaterial(b []byte) (Material, error)

DecodeMaterial parses unsealed plaintext and clears it.

func (Material) Clear

func (m Material) Clear()

Clear overwrites every value, on a best-effort basis.

func (Material) Field

func (m Material) Field(name string) cf.Secret

Field returns a field's value, or a zero secret.

type Provider

type Provider interface {
	// Manifest describes the provider. It is data and never changes.
	Manifest() Manifest
	// Validate checks the credential with the provider, using hc, which the
	// caller has restricted to the manifest's hosts, and returns the account
	// it belongs to. A rejection wraps a connectionforge provider error.
	Validate(ctx context.Context, hc *http.Client, m Material) (Result, error)
	// Authenticate adds the credential to an outgoing request. The request
	// is the caller's copy.
	Authenticate(r *http.Request, m Material) error
}

Provider is an adapter for one external service.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry holds the providers a host supports. It is safe for concurrent use.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns an empty registry.

func (*Registry) Get

func (r *Registry) Get(key string) (Provider, error)

Get returns the provider with key.

func (*Registry) Manifests

func (r *Registry) Manifests() []Manifest

Manifests returns every registered manifest, ordered by key.

func (*Registry) Register

func (r *Registry) Register(p Provider) error

Register adds a provider. Its manifest must be valid and its key unused.

type Result

type Result struct {
	AccountID    string
	AccountLabel string
	// Scopes are the permissions the provider reports for the credential,
	// if it reports them.
	Scopes []string
}

Result is what validation discovered.

type Scope

type Scope struct {
	Name        string `json:"name"`
	Description string `json:"description,omitempty"`
	Required    bool   `json:"required,omitempty"`
}

Scope is a permission a method asks for or expects.

type StatusError

type StatusError struct {
	Provider  string
	Status    int
	RequestID string
	Class     error
}

StatusError is a provider's refusal, classified. It carries the status and the provider's request ID, never a body.

func (*StatusError) Error

func (e *StatusError) Error() string

func (*StatusError) Unwrap

func (e *StatusError) Unwrap() error

Unwrap returns the class, so errors.Is works with connectionforge's errors.

Directories

Path Synopsis
Package apikey is a provider for services that issue a static API key or token, driven by configuration: where the token goes in a request, which endpoint verifies it, and where the account is in the response.
Package apikey is a provider for services that issue a static API key or token, driven by configuration: where the token goes in a request, which endpoint verifies it, and where the account is in the response.

Jump to

Keyboard shortcuts

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