apikey

package
v0.0.0-...-af91ace Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package apikey provides authentication context support for APIs that require a pre-generated constant authenticationn key passed via a header, query parameter, or cookie value in each request.

Example
// Initialize the CLI.
cli.Init(&cli.Config{
	AppName:   "example",
	EnvPrefix: "EXAMPLE",
})

// Initialize the API key authentication.
Init("X-API-Key", LocationHeader)

// Mock out a context to be used in the request.
cli.Creds.Set("contexts.default.api_key", "my-secret")

// Make a request.
req := cli.Client.Get().URL("http://example.com/")
if _, err := req.Do(); err != nil {
	panic(err)
}

// Look at the header that was used in the request. It should match the
// context's API key value.
fmt.Println(req.Context.Request.Header.Get("X-API-Key"))
Output:

my-secret

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init(name string, in Location, extra ...string)

Init sets up the API key client authentication. Must be called *after* you have called `cli.Init()`. Passing `extra` values will set additional custom keys to store for each context.

Example (Header)
// Use a custom header for authentication.
Init("X-API-Key", LocationHeader)
Output:

Example (Query)
// Use a query parameter for authentication.
Init("apikey", LocationHeader)
Output:

Types

type Handler

type Handler struct {
	Name string
	In   Location
	Keys []string
}

Handler sets up the API key authentication flow.

func (*Handler) ContextKeys

func (h *Handler) ContextKeys() []string

ContextKeys returns the key names for fields to store in the context.

func (*Handler) OnRequest

func (h *Handler) OnRequest(log *zerolog.Logger, request *http.Request) error

OnRequest gets run before the request goes out on the wire.

type Location

type Location int

Location defines how a parameter is sent.

const (
	LocationHeader Location = iota
	LocationQuery
	LocationCookie
)

API key parameter locations, which correspond to the OpenAPI `in` parameter values for the `apikey` security type.

Jump to

Keyboard shortcuts

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