firebasejwt

package module
v0.0.0-...-88f3098 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2020 License: MIT Imports: 7 Imported by: 0

README

echo-middleware-firebasejwt

An Echo middleware to authenticate a user through Firebase's JWT functionality, based on Echo's original JWT auth middleware.

Install
go get -u github.com/reedom/echo-middleware-firebasejwt
Usage

Import packages.

import (
    firebase "firebase.google.com/go"
    "github.com/reedom/echo-middleware-firebasejwt"
)

Initialize a Firebase App.

ctx := context.Background()
app, err := firebase.NewApp(ctx, nil)
if err != nil {
    panic(err)
}

Set up the middleware with the default config.

e.Use(firebasejwt.Default(app))

Or with a custom config.

jwtConfig := firebasejwt.JWTConfig{
    App:              app,
    Skipper:          authSkipper,
    ContextSpecifier: contextSpecifier,
}
e.Use(firebasejwt.With(jwtConfig))

////

func authSkipper(e echo.Context) bool {
	path := e.Request().URL.Path
	if path == "/" || path == "/login" {
		return true
	}
	return false
}

type AuthUser struct {
	Token *auth.Token
	Email string
	Name  string
}

func contextSpecifier(token *auth.Token) (string, interface{}) {
	user := AuthUser{
		Token: token,
		Email: token.Claims["email"].(string),
		Name:  token.Claims["name"].(string),
	}
	return "authUser", user
}
License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultJWTConfig is the default JWT auth middleware config.
	DefaultFirebaseJWTConfig = JWTConfig{
		Skipper: middleware.DefaultSkipper,
		ContextSpecifier: func(token *auth.Token) (string, interface{}) {
			return "user", token
		},
		TokenLookup: "header:" + echo.HeaderAuthorization,
		AuthScheme:  "Bearer",
	}
)
View Source
var (
	ErrJWTMissing = echo.NewHTTPError(http.StatusBadRequest, "missing or malformed jwt")
)

Errors

Functions

func Default

func Default(app *firebase.App) echo.MiddlewareFunc

JWT returns a JSON Web Token (JWT) auth middleware.

For valid token, it sets the user in context and calls next handler. For invalid token, it returns "401 - Unauthorized" error. For missing token, it returns "400 - Bad Request" error.

func With

func With(config JWTConfig) echo.MiddlewareFunc

JWTWithConfig returns a JWT auth middleware with config. See: `JWT()`.

Types

type JWTConfig

type JWTConfig struct {
	// Firebase app.
	// Required.
	App *firebase.App

	// Skipper defines a function to skip middleware.
	Skipper middleware.Skipper

	// BeforeFunc defines a function which is executed just before the middleware.
	BeforeFunc middleware.BeforeFunc

	// SuccessHandler defines a function which is executed for a valid token.
	SuccessHandler JWTSuccessHandler

	// ErrorHandler defines a function which is executed for an invalid token.
	// It may be used to define a custom JWT error.
	ErrorHandler JWTErrorHandler

	// JWTContextSpecifier defines a function which returns (context-key, token-value).
	// The token-value will be stored into context with context-key.
	// Optional. Default values are ("user", *auth.Token)
	ContextSpecifier JWTContextSpecifier

	// TokenLookup is a string in the form of "<source>:<name>" that is used
	// to extract token from the request.
	// Optional. Default value "header:Authorization".
	// Possible values:
	// - "header:<name>"
	// - "query:<name>"
	// - "cookie:<name>"
	TokenLookup string

	// AuthScheme to be used in the Authorization header.
	// Optional. Default value "Bearer".
	AuthScheme string
}

JWTConfig defines the config for JWT middleware.

type JWTContextSpecifier

type JWTContextSpecifier func(*auth.Token) (string, interface{})

JWTContextSpecifier defines a function which returns (context-key, token-value). The token-value will be stored into context with context-key. Optional. Default values are ("user", *auth.Token)

type JWTErrorHandler

type JWTErrorHandler func(error) error

JWTErrorHandler defines a function which is executed for an invalid token.

type JWTSuccessHandler

type JWTSuccessHandler func(echo.Context)

JWTSuccessHandler defines a function which is executed for a valid token.

Jump to

Keyboard shortcuts

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