gofiberauth0middleware

package module
v0.0.0-...-13c4ee2 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2022 License: MIT Imports: 8 Imported by: 0

README

go-fiber-auth0-middleware

Auth0 middleware for Go Fiber

package main

import (
	"log"
	"net/http"
	"os"

	"github.com/auth0/go-jwt-middleware/v2/validator"
	"github.com/gofiber/fiber/v2"
	auth0 "github.com/satishbabariya/go-fiber-auth0-middleware"
)

func main() {
	app := fiber.New()

	app.Use(auth0.New(auth0.Config{
		Issuer:   os.Getenv("AUTH0_ISSUER"),
		Audience: []string{os.Getenv("AUTH0_AUDIENCE")},
		ErrorHandler: func(c *fiber.Ctx, err error) error {
			return fiber.NewError(http.StatusUnauthorized, err.Error())
		},
	}))

	app.Get("/", func(c *fiber.Ctx) error {
		claims := c.Locals("claims").(*validator.ValidatedClaims)
		return c.JSON(claims)
	})

	log.Fatal(app.Listen(":1203"))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(config ...Config) fiber.Handler

Types

type Config

type Config struct {
	// Filter defines a function to skip middleware.
	// Optional. Default: nil
	Filter func(*fiber.Ctx) bool

	// SuccessHandler defines a function which is executed for a valid token.
	// Optional. Default: nil
	SuccessHandler fiber.Handler

	// ErrorHandler defines a function which is executed for an invalid token.
	// It may be used to define a custom JWT error.
	// Optional. Default: 401 Invalid or expired JWT
	ErrorHandler fiber.ErrorHandler

	Issuer             string        `yaml:"issuer"`
	Audience           []string      `yaml:"audience"`
	SignatureAlgorithm string        `yaml:"signature_algorithm"`
	CacheDuration      time.Duration `yaml:"cache_duration"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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