gofiberfirebaseauth

package module
v0.0.0-...-0096650 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2023 License: MIT Imports: 6 Imported by: 1

README

Go Fiber Firebase Auth Middleware

Note: This is a fork of the original gofiber-firebaseauth middlware for the RALF project. The API changed in this fork.

Authenticate your endpoints with Firebase Authentication.

gofiber-firebaseauth is inspired by npm package express-firebase-auth .

$ go get -u github.com/gofiber/fiber/v2
$ go get github.com/ralf-life/gofiber-firebaseauth

Features

  • Authenticate the user using Firebase before running the function.
  • Ability to skip authentication on public API endpoints.

Usage

Configuration

app.Use(gofiberfirebaseauth.New(firebaseApp, gofiberfirebaseauth.Config{
	// ...
}))

Note: You can find all available configuration parameters here.

Accessing the User

func Handler(ctx *fiber.Ctx) error {
    currentUser := ctx.Locals("user").(gofiberfirebaseauth.User)
	return ctx.SendString(fmt.Sprintf("Hello, %s!", currentUser.UserID))
}

Customizing the User object

You can specify a TokenCallback function in the config, to create a custom user object.

cfg := gofiberfirebaseauth.Config{
    TokenCallback: func(ctx *fiber.Ctx, token *auth.Token) error {
        ctx.Locals("raw-token", token)
        return nil
    }
}

Extracting the Token

By default, the middleware checks the Authorization header for tokens.

If you have a custom token format (e.g. a Bearer ... prefix), or if you want to read token from cookies, you can specify a custom TokenExtractor in the config.

Extract Token from Headers

cfg := gofiberfirebaseauth.Config{
	TokenExtractor: gofiberfirebaseauth.NewHeaderExtractor("Bearer ")
}

Extract Token from Cookies

cfg := gofiberfirebaseauth.Config{
    TokenExtractor: gofiberfirebaseauth.NewCookieExtractor("my-cookie-name", "Bearer ")
}

Extract Token from Header or Cookie

cfg := gofiberfirebaseauth.Config{
    TokenExtractor: gofiberfirebaseauth.NewMultiExtractor(
        gofiberfirebaseauth.NewHeaderExtractor("Bearer "),
        gofiberfirebaseauth.NewCookieExtractor("my-cookie-name", "Bearer "),
    )
}

License

MIT licensed.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConfigDefault = Config{
	ErrorHandler: func(c *fiber.Ctx, err error) error {
		return c.Status(fiber.StatusBadRequest).SendString(err.Error())
	},
	SuccessHandler: func(c *fiber.Ctx) error {
		return c.Next()
	},
	ContextKey:     "user",
	TokenExtractor: NewHeaderExtractor(),
}

ConfigDefault is the default config

View Source
var ErrEmailNotVerified = errors.New("email not verified")
View Source
var ErrTokenMissingInHeader = errors.New("token missing")

Functions

func New

func New(app *firebase.App, config Config) fiber.Handler

New - Signature Function

Types

type Config

type Config struct {
	// Skip Email Check.
	// Optional. Default: nil
	CheckEmailVerified bool

	// Ignore email verification for these routes
	// Optional. Default: nil
	CheckEmailVerifiedIgnoredUrls []string

	// Filter defines a function to skip middleware.
	// Optional. Default: nil
	Next func(c *fiber.Ctx) bool

	// Authorizer defines a function which authenticate the Authorization token and return the authenticated token
	// Optional. Default: nil
	Authorizer func(string) (*auth.Token, error)

	// 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: nil
	ErrorHandler fiber.ErrorHandler

	// Context key to store user information from the token into context.
	// Optional. Default: "user".
	ContextKey string

	TokenExtractor ExtractorFun
	TokenCallback  func(ctx *fiber.Ctx, token *auth.Token) error
}

Config defines the config for middleware

type ExtractorFun

type ExtractorFun func(ctx *fiber.Ctx) string

func NewCookieExtractor

func NewCookieExtractor(cookieKey string, stripLeft ...string) ExtractorFun

func NewHeaderExtractor

func NewHeaderExtractor(stripLeft ...string) ExtractorFun

func NewMultiExtractor

func NewMultiExtractor(extractors ...ExtractorFun) ExtractorFun

type User

type User struct {
	EmailVerified bool
	UserID, Email string
}

Jump to

Keyboard shortcuts

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