firebaseauth

package module
v0.0.0-...-43c18ae Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2021 License: MIT Imports: 9 Imported by: 0

README

fork from: modora/firebase-auth-echo-middleware

firebase-authecho-middleware-fork Go Report Card

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

install

go get github.com/7sedam7/firebase-authecho-middleware-fork

example

package main

import (
    "github.com/labstack/echo/v4"
    firebaseauth "github.com/7edam7/firebase-authecho-middleware-fork"
)

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

This section is empty.

Variables

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

Errors

View Source
var (
	// nolint
	// 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

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

func GetContextValueMap

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

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

Jump to

Keyboard shortcuts

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