firebaseauth

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2023 License: MIT Imports: 10 Imported by: 0

README

firebase-auth-echo-middleware Go Report Card

define Golang firebase auth middleware for echo web framework https://echo.labstack.com/

install

go get github.com/mondora/firebase-auth-echo-middleware

example

package main

import (
    "github.com/labstack/echo/v4"
    firebaseauth "github.com/mondora/firebase-auth-echo-middleware"
)

type H map[string]interface{}

func testApiHandler(c echo.Context) error {
    user := c.Get("user")
    authProvider := c.Get("auth-provider")
    return c.JSON(200, H{
        "user": user,
        "authProvider": authProvider,
        "OK":      true,
    })
}

func optionsSkipper(c echo.Context) bool {
    return c.Request().Method == "OPTIONS"
}

func main() {
    credentialJSON := `{
      "type": "service_account",
      "project_id": "...",
      "private_key_id": "...",
      "private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
      "client_email": "firebase-adminsdk-...@....iam.gserviceaccount.com",
      "client_id": "...",
      "auth_uri": "https://accounts.google.com/o/oauth2/auth",
      "token_uri": "https://oauth2.googleapis.com/token",
      "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
      "client_x509_cert_url": "..."
    }`
    firebaseAuth := firebaseauth.WithConfig(firebaseauth.Config{
        Skipper:        optionsSkipper,
        CredentialJSON: []byte(credentialJSON),
    })
    router := echo.New()
    apiV1 := router.Group("/api/v1")
    apiV1.Use(firebaseAuth)
    apiV1.GET("/test-api", testApiHandler)
    router.Logger.Fatal(router.Start(":8080"))
}

Documentation

Index

Constants

View Source
const (
	ContextKeyRoles = "roles"
)

Variables

View Source
var (
	// Errors
	ErrTokenMissing = echo.NewHTTPError(http.StatusBadRequest, "Missing or malformed Firebase AuthID TOKEN")
	ErrTokenInvalid = echo.NewHTTPError(http.StatusUnauthorized, "Invalid or expired Firebase AuthID TOKEN")

	// DefaultFirebaseAuthConfig is the default auth middleware config.
	DefaultFirebaseAuthConfig = Config{
		Skipper:          middleware.DefaultSkipper,
		ContextIDKey:     "id-key",
		ContextUserKey:   "user",
		ContextUserIDKey: "userID",
		TokenLookup:      "header:" + echo.HeaderAuthorization,
		AuthScheme:       "Bearer",
	}
)

Functions

func FirebaseAuth

func FirebaseAuth() echo.MiddlewareFunc

FirebaseAuth 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 GetContextValue added in v0.2.1

func GetContextValue(c echo.Context, key string) string

func GetContextValueMap added in v0.2.2

func GetContextValueMap(c echo.Context, key string) map[string]interface{}

func WithConfig

func WithConfig(config Config) echo.MiddlewareFunc

WithConfig returns a FirebaseAuth middleware with config. See: `FirebaseAuth()`.

Types

type Config

type Config struct {
	// Skipper defines a function to skip middleware.
	Skipper middleware.Skipper

	// ID key to store user information from the token into context.
	// Optional. Default value "id-key".
	ContextIDKey string

	// Context key to store user information from the user into context.
	// Optional. Default value "user".
	ContextUserKey string

	ContextUserIDKey string

	GetRoles GetRolesFunc

	// 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

	CredentialJSON []byte
}

Config defines the config for Firebase Auth JWT middleware.

type GetRolesFunc added in v0.2.4

type GetRolesFunc func(email string) []string

GetRolesFunc is an external closure function that can retrieve roles by email.

Jump to

Keyboard shortcuts

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