Go API Documentation

github.com/TykTechnologies/tyk/ee/middleware/upstreambasicauth

No package summary is available.

Package

Files: 3. Third party imports: 1. Imports from organisation: 0. Tests: 0. Benchmarks: 0.

Constants

const (
	// ExtensionTykStreaming is the OAS extension for Tyk streaming.
	ExtensionTykStreaming	= "x-tyk-streaming"
	StreamGCInterval	= 1 * time.Minute
)

Vars

Middleware implements model.Middleware.

var _ model.Middleware = &Middleware{}

Types

APISpec

APISpec is a subset of gateway.APISpec for the values the middleware consumes.

Field name Field type Comment
APIID

string

No comment on field.
Name

string

No comment on field.
IsOAS

bool

No comment on field.
OAS

oas.OAS

No comment on field.
UpstreamAuth

apidef.UpstreamAuth

No comment on field.
type APISpec struct {
	APIID	string
	Name	string
	IsOAS	bool
	OAS	oas.OAS

	UpstreamAuth	apidef.UpstreamAuth
}

BaseMiddleware

BaseMiddleware is the subset of BaseMiddleware APIs that the middleware uses.

Field name Field type Comment
type

any

No comment on field.
type BaseMiddleware interface {
	model.LoggerProvider
}

Gateway

Gateway is the subset of Gateway APIs that the middleware uses.

Field name Field type Comment
type

any

No comment on field.
type Gateway interface {
	model.ConfigProvider
	model.ReplaceTykVariables
}

Middleware

Middleware implements upstream basic auth middleware.

Field name Field type Comment
Spec

*APISpec

No comment on field.
Gw

Gateway

No comment on field.
base

BaseMiddleware

No comment on field.
type Middleware struct {
	Spec	*APISpec
	Gw	Gateway

	base	BaseMiddleware
}

Provider

Provider implements upstream auth provider.

Field name Field type Comment
Logger

*logrus.Entry

Logger is the logger to be used.

HeaderName

string

HeaderName is the header name to be used to fill upstream auth with.

AuthValue

string

AuthValue is the value of auth header.

type Provider struct {
	// Logger is the logger to be used.
	Logger	*logrus.Entry
	// HeaderName is the header name to be used to fill upstream auth with.
	HeaderName	string
	// AuthValue is the value of auth header.
	AuthValue	string
}

Functions

func NewAPISpec

NewAPISpec creates a new APISpec object based on the required inputs. The resulting object is a subset of *gateway.APISpec.

func NewAPISpec(id string, name string, isOasDef bool, oasDef oas.OAS, upstreamAuth apidef.UpstreamAuth) *APISpec {
	return &APISpec{
		APIID:		id,
		Name:		name,
		IsOAS:		isOasDef,
		OAS:		oasDef,
		UpstreamAuth:	upstreamAuth,
	}
}

Cognitive complexity: 1, Cyclomatic complexity: 1

func NewMiddleware

NewMiddleware returns a new instance of Middleware.

func NewMiddleware(gw Gateway, mw BaseMiddleware, spec *APISpec) *Middleware {
	return &Middleware{
		base:	mw,
		Gw:	gw,
		Spec:	spec,
	}
}

Cognitive complexity: 1, Cyclomatic complexity: 1

func (*Middleware) EnabledForSpec

EnabledForSpec checks if streaming is enabled on the config.

func (m *Middleware) EnabledForSpec() bool {
	if !m.Spec.UpstreamAuth.IsEnabled() {
		return false
	}

	if !m.Spec.UpstreamAuth.BasicAuth.Enabled {
		return false
	}

	return true
}

Cognitive complexity: 4, Cyclomatic complexity: 3

func (*Middleware) Init

Init initializes the middleware.

func (m *Middleware) Init() {
	m.Logger().Debug("Initializing Upstream basic auth Middleware")
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (*Middleware) Logger

Logger returns a logger with middleware filled out.

func (m *Middleware) Logger() *logrus.Entry {
	return m.base.Logger().WithField("mw", m.Name())
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (*Middleware) Name

Name returns the name for the middleware.

func (m *Middleware) Name() string {
	return "UpstreamBasicAuthMiddleware"
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (*Middleware) ProcessRequest

ProcessRequest will handle upstream basic auth.

func (m *Middleware) ProcessRequest(_ http.ResponseWriter, r *http.Request, _ interface{}) (error, int) {
	basicAuthConfig := m.Spec.UpstreamAuth.BasicAuth

	upstreamBasicAuthProvider := Provider{
		Logger:		m.Logger(),
		HeaderName:	header.Authorization,
	}

	if basicAuthConfig.Header.AuthKeyName() != "" {
		upstreamBasicAuthProvider.HeaderName = basicAuthConfig.Header.AuthKeyName()
	}

	upstreamBasicAuthProvider.AuthValue = httputil.AuthHeader(basicAuthConfig.Username, basicAuthConfig.Password)

	core.SetUpstreamAuth(r, upstreamBasicAuthProvider)
	return nil, http.StatusOK
}

Cognitive complexity: 4, Cyclomatic complexity: 2

Uses: core.SetUpstreamAuth, header.Authorization, http.StatusOK, httputil.AuthHeader.

func (*Middleware) Unload

func (m *Middleware) Unload() {
	// nothing to do here
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (Provider) Fill

Fill sets the request's HeaderName with AuthValue

func (u Provider) Fill(r *http.Request) {
	if r.Header.Get(u.HeaderName) != "" {
		u.Logger.WithFields(logrus.Fields{
			"header": u.HeaderName,
		}).Info("Authorization header conflict detected: Client header overwritten by Gateway upstream authentication header.")
	}
	r.Header.Set(u.HeaderName, u.AuthValue)
}

Cognitive complexity: 3, Cyclomatic complexity: 2

Uses: logrus.Fields.