basicauth

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2020 License: MIT Imports: 3 Imported by: 8

README

Basic Authentication

Release Discord Test Security Linter

Basic auth middleware provides an HTTP basic authentication. It calls the next handler for valid credentials and 401 Unauthorized for missing or invalid credentials.

Install
go get -u github.com/gofiber/fiber
go get -u github.com/gofiber/basicauth
Signature
basicauth.New(config ...basicauth.Config) func(*fiber.Ctx)
Config
Property Type Description Default
Filter func(*fiber.Ctx) bool Defines a function to skip middleware nil
Users map[string][string] Users defines the allowed credentials nil
Realm string Realm is a string to define the realm attribute Restricted
Authorizer func(string, string) bool A function you can pass to check the credentials however you want. nil
Unauthorized func(*fiber.Ctx) Custom response body for unauthorized responses nil
Example
package main

import (
  "fmt"

  "github.com/gofiber/fiber"
  "github.com/gofiber/basicauth"
)

func main() {
  app := fiber.New()

  cfg := basicauth.Config{
    Users: map[string]string{
      "john":   "doe",
      "admin":  "123456",
    },
  }
  app.Use(basicauth.New(cfg))

  app.Get("/", func(c *fiber.Ctx) {
    username := c.Locals("username").(string)
    password := c.Locals("password").(string)
    fmt.Println(username, password)
    c.Send("Welcome!")
  })

  app.Listen(3000)
}
Test
curl --user john:doe http://localhost:3000

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(config ...Config) func(*fiber.Ctx)

Types

type Config

type Config struct {
	// Filter defines a function to skip middleware.
	// Optional. Default: nil
	Filter func(*fiber.Ctx) bool
	// Users defines the allowed credentials
	// Required. Default: map[string]string{}
	Users map[string]string
	// Realm is a string to define realm attribute of BasicAuth.
	// the realm identifies the system to authenticate against
	// and can be used by clients to save credentials
	// Optional. Default: "Restricted".
	Realm string
	// Authorizer defines a function you can pass
	// to check the credentials however you want.
	// It will be called with a username and password
	// and is expected to return true or false to indicate
	// that the credentials were approved or not.
	// Optional. Default: nil.
	Authorizer func(string, string) bool
	// Unauthorized defines the response body for unauthorized responses.
	// Optional. Default: nil
	Unauthorized func(*fiber.Ctx)
}

Config defines the config for BasicAuth middleware

Jump to

Keyboard shortcuts

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