middleware

package
v0.0.0-...-1612f9a Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2025 License: GPL-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package middleware contains application specific middleware.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthMiddleware

func AuthMiddleware(auth Authentication) func(http.Handler) http.Handler

AuthMiddleware creates an http middleware that validates auth tokens in requests.

It takes an Authentication interface and returns a middleware function that checks for valid Authorization header tokens, responding with 401 Unauthorized if validation fails.

Types

type Authentication

type Authentication interface {
	ValidateToken(ctx context.Context, token string) (bool, error)
}

type AuthenticationMock

type AuthenticationMock struct {
	// ValidateTokenFunc mocks the ValidateToken method.
	ValidateTokenFunc func(ctx context.Context, token string) (bool, error)
	// contains filtered or unexported fields
}

AuthenticationMock is a mock implementation of Authentication.

func TestSomethingThatUsesAuthentication(t *testing.T) {

	// make and configure a mocked Authentication
	mockedAuthentication := &AuthenticationMock{
		ValidateTokenFunc: func(ctx context.Context, token string) (bool, error) {
			panic("mock out the ValidateToken method")
		},
	}

	// use mockedAuthentication in code that requires Authentication
	// and then make assertions.

}

func (*AuthenticationMock) ValidateToken

func (mock *AuthenticationMock) ValidateToken(ctx context.Context, token string) (bool, error)

ValidateToken calls ValidateTokenFunc.

func (*AuthenticationMock) ValidateTokenCalls

func (mock *AuthenticationMock) ValidateTokenCalls() []struct {
	Ctx   context.Context
	Token string
}

ValidateTokenCalls gets all the calls that were made to ValidateToken. Check the length with:

len(mockedAuthentication.ValidateTokenCalls())

Jump to

Keyboard shortcuts

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