middleware

package module
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2024 License: MIT Imports: 5 Imported by: 0

README

connectrpc-middleware

Go Reference

auth middleware

default jwt middleware

	//a default jwt handler
	jwtHandler := handler.NewJWTHandler(
		//add skip, before, success, error to shim()
		handler.NewShim(),
		//no extractor specified, it default to extract token from "Authorization": Bearer <token>
		//default jwt parser, the default result is jwt.MapClaims which can be retrieved by middleware.FromContext[jwt.MapClaims](ctx)
		handler.WithJwtMapClaimsParser([]byte("secret_key")),
	)
	//create new middleware
	authMiddleware, err := middleware.NewAuthMiddleware(middleware.WithHandler(jwtHandler))
	if err != nil {
		panic(err)
	}

	//...

	http.ListenAndServe(
		"localhost:8080",
		// Use h2c so we can serve HTTP/2 without TLS.
		h2c.NewHandler(authMiddleware.Wrap(mux), &http2.Server{}),
	)

TODO

  • tests for cookie
  • test for kid field

refs

Documentation

Index

Constants

View Source
const (
	UnaryAndStreamHandler = UnaryHandler | StreamHandler
)

Variables

This section is empty.

Functions

func DefaultSkipper

func DefaultSkipper(context.Context, *Request) bool

DefaultSkipper returns false which processes the middleware.

func FromContext

func FromContext[T any](ctx context.Context) (T, bool)

FromContext is used to get the payload from context, T is the returned type from parser

func IsExtractTokenErr

func IsExtractTokenErr(err error) bool

IsExtractTokenErr checks if err is from Extractor

func IsParseTokenErr

func IsParseTokenErr(err error) bool

IsParseTokenErr checks if err is from Parser It can be used in ErrorHandle to determine where to err come from

func NewAuthInterceptor

func NewAuthInterceptor(opts ...authInterceptorOpt) (*authInterceptor, error)

func NewAuthMiddleware added in v0.2.0

func NewAuthMiddleware(opts ...authMiddlewareOpt) (*authMiddleware, error)

func NewContext added in v0.2.0

func NewContext(ctx context.Context, payload any) context.Context

func WithClientHandler added in v0.6.1

func WithClientHandler(h ClientTokenGetter) authInterceptorOpt

func WithErrorWriterOpts added in v0.2.1

func WithErrorWriterOpts(opts ...connect.HandlerOption) authMiddlewareOpt

func WithHandler added in v0.6.1

func WithHandler(h AuthHandler) authMiddlewareOpt

func WithServiceHandler added in v0.6.1

func WithServiceHandler(h AuthHandler) authInterceptorOpt

func WithStreamServiceHandler added in v0.6.1

func WithStreamServiceHandler(h AuthHandler) authInterceptorOpt

func WithUnaryServiceHandler added in v0.6.1

func WithUnaryServiceHandler(h AuthHandler) authInterceptorOpt

Types

type AuthHandler

type AuthHandler interface {
	//Skip defines a function to skip the middleware
	Skip(ctx context.Context, req *Request) bool
	// Before defines a function which is executed before Extracor and Parser
	// If this function return an error, the middleware will return the error and skip the rest of the process
	// HandleError will be ignored if Before return an error
	Before(ctx context.Context, req *Request) error
	// Extract defines a function which is used to extract data from request, and may return a new context to be used in parse
	Extract(ctx context.Context, req *Request) (context.Context, error)
	// Parse is used to parse data from context
	Parse(ctx context.Context) (any, error)
	// Success defines a function which is executed after Extracor and Parser when they return no error.
	// This function accepts the modified context, and the request.
	// If this function return an error, the middleware will return with the error and skip error handler.
	Success(ctx context.Context, req *Request) error
	// HandleError defines a function which is executed when Extractor or Parser return error.
	HandleError(ctx context.Context, req *Request, err error) error
}

AuthHandler is used in unary and streaming service handler The order of execution of handler's functions are: Skip(if this function return true, skip the rest process) -> Before(if this function return err, skip the rest process) -> Extract(if this function return err, jump to HandleError) -> Parse (if this function return err, jump to HandleError) -> Success(if this function return err, the error is returned) -> HandleError

type ClientTokenGetter

type ClientTokenGetter interface {
	Get() (string, string)
}

ClientTokenGetter is used to get token for client request

type Middleware added in v0.2.3

type Middleware interface {
	Wrap(http.Handler) http.Handler
}

type Request added in v0.2.0

type Request struct {
	Procedure  string // for example, "/acme.foo.v1.FooService/Bar"
	ClientAddr string // client address, in IP:port format
	Protocol   string // connect.ProtocolConnect, connect.ProtocolGRPC, or connect.ProtocolGRPCWeb
	Header     http.Header
}

Request describes a single RPC invocation.

type ServiceHandlerType added in v0.2.0

type ServiceHandlerType int
const (
	UnaryHandler ServiceHandlerType = 1 << iota
	StreamHandler
)

Directories

Path Synopsis
gen/ping/v1/pingv1connect
The connect.ping.v1 package contains an echo service designed to test the connect-go implementation.
The connect.ping.v1 package contains an echo service designed to test the connect-go implementation.

Jump to

Keyboard shortcuts

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