auth0

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2022 License: MIT Imports: 11 Imported by: 0

README

oapi-codegen-auth0

Auth0 JWT Request Validator for deepmap/oapi-codegen router middlewares

Usage

You will need your AUTH0_DOMAIN and AUTH0_AUDIENCE. By default these will be read from environment variable but can also optionally be passed in to NewAuth0Authenticator with auth0.WithDomain() and auth0.WithAudience().

For example with chi, use it as a middleware

func main() {
	mux := chi.NewRouter()

	mux.Use(requestValidator())
	api.HandlerFromMuxWithBaseURL(api.New(), mux, "/api/v1")

	log.Fatal(http.ListenAndServe(":8080", mux))
}

func requestValidator() func(next http.Handler) http.Handler {
	swagger, err := api.GetSwagger()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error loading openapi spec\n: %s", err)
		os.Exit(1)
	}

	return middleware.OapiRequestValidatorWithOptions(swagger, &middleware.Options{
		Options: openapi3filter.Options{
			AuthenticationFunc: func(ctx context.Context, input *openapi3filter.AuthenticationInput) error {
				auth0, err := auth.NewAuth0Authenticator(input.RequestValidationInput.Request, input.Scopes)
				if err != nil {
					return err
				}

				return auth0.Validate()
			},
		},
	})
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Auth0

type Auth0 struct {
	Domain   string
	Audience string
}

type Auth0Authenticator

type Auth0Authenticator struct {
	Request       *http.Request
	AllowedScopes []string
	Validator     func(ctx context.Context, tokenString string) (interface{}, error)
}

func NewAuth0Authenticator

func NewAuth0Authenticator(r *http.Request, allowedScopes []string, opts ...Options) (*Auth0Authenticator, error)

func (*Auth0Authenticator) Validate

func (a *Auth0Authenticator) Validate() error

type CustomClaims

type CustomClaims struct {
	Scopes string `json:"scope"`
}

CustomClaims contains custom data we want from the token.

func (CustomClaims) HasScope

func (c CustomClaims) HasScope(expectedScope string) bool

HasScope checks whether our claims have a specific scope.

func (CustomClaims) Validate

func (c CustomClaims) Validate(ctx context.Context) error

Validate does nothing here, but we need it to satisfy validator.CustomClaims interface.

type Options

type Options func(*Auth0)

func WithAudience

func WithAudience(audience string) Options

func WithDomain

func WithDomain(domain string) Options

Jump to

Keyboard shortcuts

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