Help us shape the future of Go! Take the 2026 Go Developer Survey

httpauth

package
v1.3.104-alpha1 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: AGPL-3.0 Imports: 19 Imported by: 0

Documentation

Overview

Package httpauth pkg/httpauth/auth.go c0-com-http

Package httpauth pkg/httpauth/handler.go c0-com-http

Package httpauth pkg/httpauth/memory_store.go c0-com-http

Package httpauth pkg/httpauth/nonce-storer.go c0-com-http

Package httpauth pkg/httpauth/redis-store.go c0-com-http

Package httpauth pkg/httpauth/whitelist.go c0-com-http

Index

Constants

This section is empty.

Variables

View Source
var (

	// ContextAuthKey stores authenticated PubKey in Context .
	ContextAuthKey = struct{}{}
	// LogAuthKey stores authentication PubKey in log entry
	LogAuthKey = "PK"
)
View Source
var (
	// ErrNotWhitelisted means the caller's key is not in the Whitelist.
	ErrNotWhitelisted = errors.New("caller is not whitelisted")
	// ErrNotDmsg means the request did not arrive over dmsg or skynet, so it has
	// no authenticated caller.
	ErrNotDmsg = errors.New("whitelisted endpoints are served over dmsg only")
)

Functions

func GetRemoteAddr

func GetRemoteAddr(r *http.Request) string

GetRemoteAddr gets the remote address from the request in case of dmsghttp the RemoteAddress is a pk so it gets the RemoteAddr from the header instead

func GetRemoteAddrLB

func GetRemoteAddrLB(r *http.Request) string

GetRemoteAddrLB gets the remote address from the request behind LB in case of dmsghttp the RemoteAddress is a pk so it gets the RemoteAddr from the header instead

func MakeLoadTestingMiddleware

func MakeLoadTestingMiddleware(store NonceStore) func(next http.Handler) http.Handler

MakeLoadTestingMiddleware is the same as `MakeMiddleware` but omits auth checks to simplify load testing.

func MakeMiddleware

func MakeMiddleware(store NonceStore) func(next http.Handler) http.Handler

MakeMiddleware is a convenience function that calls WithAuth.

func PKFromCtx

func PKFromCtx(ctx context.Context) cipher.PubKey

PKFromCtx is a convenience function to obtain PK from ctx.

func PayloadWithNonce

func PayloadWithNonce(payload []byte, nonce Nonce) []byte

PayloadWithNonce returns the concatenation of payload and nonce.

func ReadKeyList

func ReadKeyList(r *http.Request) ([]cipher.PubKey, error)

ReadKeyList reads a request body holding a JSON array of public keys.

func Sign

func Sign(payload []byte, nonce Nonce, sec cipher.SecKey) (cipher.Sig, error)

Sign signs the Hash of payload and nonce

func Verify

func Verify(payload []byte, nonce Nonce, pub cipher.PubKey, sig cipher.Sig) error

Verify verifies the signature of the hash of payload and nonce

func WithAuth

func WithAuth(store NonceStore, original http.Handler, shouldVerifyAuth bool) http.Handler

WithAuth wraps a http.Handler and adds authentication logic. The original http.Handler is responsible for setting the status code. The middleware logic should only increment the security nonce if the status code from the original http.Handler is of 2xx value (representing success). Any http.Handler that is wrapped with this function will have available the authenticated public key from it's context, stored in the value ContextAuthKey.

Types

type Auth

type Auth struct {
	Key   cipher.PubKey
	Nonce Nonce
	Sig   cipher.Sig
}

Auth holds authentication mandatory values

func AuthFromHeaders

func AuthFromHeaders(hdr http.Header, shouldVerifyAuth bool) (*Auth, error)

AuthFromHeaders attempts to extract auth from request header

func (*Auth) Verify

func (a *Auth) Verify(in []byte) error

Verify verifies signature of a payload.

type HTTPError

type HTTPError struct {
	Message string `json:"message"`
	Code    int    `json:"code"`
}

HTTPError is included in an HTTPResponse

type HTTPResponse

type HTTPResponse struct {
	Error *HTTPError  `json:"error,omitempty"`
	Data  interface{} `json:"data,omitempty"`
}

HTTPResponse represents the http response struct

func NewHTTPErrorResponse

func NewHTTPErrorResponse(code int, msg string) HTTPResponse

NewHTTPErrorResponse returns an HTTPResponse with the Error field populated

type NextNonceResponse

type NextNonceResponse struct {
	Edge      cipher.PubKey `json:"edge"`
	NextNonce Nonce         `json:"next_nonce"`
}

NextNonceResponse represents a ServeHTTP response for json encoding

type Nonce

type Nonce uint64

Nonce is used to sign requests in order to avoid replay attack

func (Nonce) String

func (n Nonce) String() string

type NonceHandler

type NonceHandler struct {
	Store NonceStore
}

NonceHandler provides server-side logic for Skywire-related RESTFUL authorization and authentication.

func (*NonceHandler) ServeHTTP

func (as *NonceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http Handler Use this in endpoint: mux.Handle("/security/nonces/", &NonceHandler{Store})

type NonceStore

type NonceStore interface {

	// IncrementNonce increments the nonce associated with the specified remote entity.
	// It returns the next expected nonce after it has been incremented and returns error on failure.
	IncrementNonce(ctx context.Context, remotePK cipher.PubKey) (nonce Nonce, err error)

	// Nonce obtains the next expected nonce for a given remote entity (represented by public key).
	// It returns error on failure.
	Nonce(ctx context.Context, remotePK cipher.PubKey) (nonce Nonce, err error)

	// Count obtains the number of entries stored in the underlying database.
	Count(ctx context.Context) (n int, err error)
}

NonceStore stores Incrementing Security Nonces.

func NewNonceStore

func NewNonceStore(ctx context.Context, config storeconfig.Config, prefix string) (NonceStore, error)

NewNonceStore returns a new nonce storer of the given kind that connects to given Store's url. Nonce count should not be shared between services, so it should be stored in a unique key for every service.

type Whitelist

type Whitelist map[string]struct{}

Whitelist holds the keys, in hex, allowed to call the bulk deregistration endpoints of the deployment services.

func NewWhitelist

func NewWhitelist() Whitelist

NewWhitelist returns an empty Whitelist.

func (Whitelist) Authorize

func (wl Whitelist) Authorize(r *http.Request) (cipher.PubKey, int, error)

Authorize returns the key of a caller allowed by the Whitelist, or the HTTP status to answer with. The caller is the authenticated remote key of a dmsg stream, which includes a stream a host visor hands over from skynet. A plain HTTP request has no such key and is refused.

func (Whitelist) Get

func (wl Whitelist) Get(pk string) bool

Get reports whether the key pk, in hex, is listed.

func (Whitelist) Set

func (wl Whitelist) Set(pk string)

Set adds the key pk, in hex.

Jump to

Keyboard shortcuts

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