mallard

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2023 License: MIT Imports: 11 Imported by: 0

README

Mallard

MITS' authentication middleware built on top of Firebase Authentication

Usage

Installation

go get github.com/mits-ee/mallard

Perms

Function Perms can be used to protect routes by enforcing users to have custom claims passed into the function arguments.

Usage
package main

import (
  "firebase.google.com/go/v4/auth"
mallard "github.com/mits-ee/mallard"
  "github.com/gofiber/fiber/v2"
)

func privateRoute(c *fiber.Ctx) error {
  if status := mallard.Perms(c, "private.has"); status != 0 {
    return c.SendStatus(status)
  }

  return c.SendString("Hello, private route!")
}

func main() {
  // Aquire auth client from Firebase
  var authClient *auth.Client

  app := fiber.New()
  app.Use(mallard.New(mallard.Config{
    AuthClient: authClient,
  }))
  app.Get("/private", privateRoute)
  app.Listen(":8080")
}
GetFirestore

Function GetFirestore can be used to initialize all needed Firebase services.

Usage
package main

import (
  "context"
  "github.com/mits-ee/mallard"
  "log"
)

func main() {
  ctx := context.Background()
  bundle, err := mallard.GetFirebase(ctx, mallard.Authentication)
  if err != nil {
    log.Fatalf("Could not initialize Firebase services: %v\n", err)
  }
  // Now Firebase Authentication can be used
  auth := bundle.Authentication
  // ...
}

Documentation

Index

Constants

View Source
const PermissionsKey = "permissions"

Variables

View Source
var ErrorAlreadyInitialized = errors.New("service is already initialized")
View Source
var ErrorUnsupportedService = errors.New("unsupported service")

Functions

func New added in v0.2.0

func New(config Config) fiber.Handler

func Perms

func Perms(c *fiber.Ctx, permissions ...string) int

Types

type Config added in v0.2.0

type Config struct {
	AuthClient *auth.Client
}

type FirebaseServiceType added in v0.0.4

type FirebaseServiceType int
const (
	Authentication FirebaseServiceType = iota
	Firestore
)

type ServiceBundle added in v0.0.4

type ServiceBundle struct {
	App            *firebase.App
	Authentication *auth.Client
	Firestore      *firestore.Client
}

func GetFirebase added in v0.0.4

func GetFirebase(ctx context.Context, services ...FirebaseServiceType) (*ServiceBundle, error)

Jump to

Keyboard shortcuts

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