auth

package module
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2022 License: MIT Imports: 8 Imported by: 0

README

go-basic-auth GocodecovGo Report CardReleaseGoDoc

A go module for simple HTTP basic auth.

Currently a LDAP backend and a simple in memory backend is provided.

package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
	auth "github.com/vogtp/go-basic-auth"
)

func main() {
	allowMap := make(map[string]string)
	allowMap["user"] = "password"
	basicAuth := auth.New(
		auth.WithInMemory(allowMap),                            // authorise users in allowMap (no groups used)
		auth.WithLdap("SERVER_NAME", "BASE_DN", "DOMAIN_NAME"), // authorise users for (AD) LDAP
		auth.WithGroup("group"),                                // One or more groups the user has to be in to be authorised
		auth.Debug(),                                           // enable debug output
		auth.WithFailMsg("Use the email address as user name")) // custom error message
	// use as http middleware
	http.HandleFunc("/", basicAuth.Handler(http.NotFound))
	// or use as GIN middleware
	gin := gin.Default()
	gin.Use(basicAuth.Gin())
}

Documentation

Overview

Package auth provides authentication based on http basic auth differen backend can be used

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Authenticator

type Authenticator interface {
	Handler(http.HandlerFunc) http.HandlerFunc
	GinHandler() gin.HandlerFunc
}

Authenticator provides handler funcs

type Backend

type Backend struct {
	// contains filtered or unexported fields
}

Backend provides basic HTTP Auth against a AuthBackend

func New

func New(opts ...Option) *Backend

New creates a new BasicAuth authenticator with sever and base DN user must be in one of the authGroups to be successfully authenticated

func (Backend) Gin added in v0.2.1

func (b Backend) Gin() gin.HandlerFunc

Gin is a handler func that does the authentification for GIN

func (Backend) Handler

func (b Backend) Handler(next http.HandlerFunc) http.HandlerFunc

Handler is a handler func that does the authentification for stdlib http

type Backender

type Backender interface {
	Authenticate(user string, password string, authGroups []string) (bool, error)
}

Backender defines the interface to the auth backends

type Option

type Option func(*Backend)

Option is a func to configure basic auth

func Debug

func Debug() Option

Debug forces for log output

func WithBackend

func WithBackend(b Backender) Option

WithBackend sets a authentication backend

func WithFailMsg

func WithFailMsg(msg string) Option

WithFailMsg sets a custom message on auth failure

func WithGroup

func WithGroup(authGroups ...string) Option

WithGroup sets groups that the user has to be in to be authorised

func WithInMemory

func WithInMemory(data map[string]string) Option

WithInMemory is a convience function to add a in memory authentication

func WithLdap added in v0.2.0

func WithLdap(server, baseDN, domainName string) Option

WithLdap is a convience function to add a LDAP server

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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