authMiddleware

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2024 License: MIT Imports: 2 Imported by: 0

README

go-auth-middleware

go-auth-middleware is a small Go library that provides middleware functionalities for authentication and authorization in HTTP handlers.

Features

  • Authentication Middleware: Allows authenticating incoming requests using custom authentication functions.
  • Authorization Middleware: Enables authorization by validating request context against provided authorization functions.
  • Basic Authentication Middleware: Offers basic authentication support using HTTP Basic Auth headers.

Installation

You can install the package using go get:

go get github.com/Eyal-Shalev/go-auth-middleware

Usage

Authentication Middleware
// Example authentication function
func authenticate(r *http.Request) (value T, ok bool, err error) {
// Your authentication logic here
}

// Create authentication middleware
authMiddleware := authMiddleware.NewAuthenticateMiddleware(authenticate)

// Use `authMiddleware` in your HTTP handlers
Authorization Middleware
// Example authorization function
func authorize(r *http.Request, value T) bool {
// Your authorization logic here
}

// Create authorization middleware
authzMiddleware := authMiddleware.NewAuthorizationMiddleware(authorize)

// Use `authzMiddleware` in your HTTP handlers after authentication
Basic Authentication Middleware
// Example basic authentication function
func basicAuthenticator(ctx context.Context, username, password string) (T, bool, error) {
// Your basic authentication logic here
}

// Create basic authentication middleware
basicAuthMiddleware := authMiddleware.NewBasicAuthenticateMiddleware(basicAuthenticator)

// Use `basicAuthMiddleware` in your HTTP handlers

Example

Here's a simple example demonstrating how to use the library:

package main

import (
	authMiddleware "github.com/Eyal-Shalev/go-auth-middleware"
	"net/http"
	"strings"
)

type httpStringHandler string

func (h httpStringHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	_, _ = w.Write([]byte(h))
}


const helloWorld httpStringHandler = "Hello, World!"
const helloAdmin httpStringHandler = "Hello, Admin!"

func basicAuthenticate(ctx context.Context, userName, password string) (string, bool, error) {
	if password == strings.ToUpper(userName) {
		return userName, true, nil
	}
	return "", false, nil
}

func main() {
	authenticate := authMiddleware.NewBasicAuthenticateMiddleware(basicAuthenticate)
	authorizeAdmin := authMiddleware.NewAuthorizationMiddleware(func(r *http.Request, value string) bool {
		return value == "admin"
	})
	http.Handle("/", helloWorld)
	http.Handle("/admin", authenticate(authorizeAdmin(helloAdmin)))
	err := http.ListenAndServe("localhost:9090", nil)
	if err != nil {
		panic(err)
	}
}

Contributing

Feel free to contribute by submitting issues or pull requests!

License

This library is licensed under the MIT License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetValue

func GetValue[T any](ctx context.Context) (T, bool)

func SetValue

func SetValue[T any](ctx context.Context, value T) context.Context

Types

type AuthenticateFunc

type AuthenticateFunc[T any] func(r *http.Request) (value T, ok bool, err error)

func (AuthenticateFunc[T]) Wrap added in v0.0.2

func (fn AuthenticateFunc[T]) Wrap(next http.Handler) http.Handler

type AuthorizeFunc

type AuthorizeFunc[T any] func(r *http.Request, value T) bool

func (AuthorizeFunc[T]) Wrap added in v0.0.2

func (fn AuthorizeFunc[T]) Wrap(next http.Handler) http.Handler

type BasicAuthFunc added in v0.0.2

type BasicAuthFunc[T any] func(ctx context.Context, userName, password string) (T, bool, error)

func (BasicAuthFunc[T]) Wrap added in v0.0.2

func (fn BasicAuthFunc[T]) Wrap(next http.Handler) http.Handler

type Middleware

type Middleware func(next http.Handler) http.Handler

func Authenticate added in v0.0.3

func Authenticate[T any](fn AuthenticateFunc[T]) Middleware

func Authorize added in v0.0.3

func Authorize[T any](fn AuthorizeFunc[T]) Middleware

func BasicAuth added in v0.0.3

func BasicAuth[T any](fn BasicAuthFunc[T]) Middleware

type Middlewarer added in v0.0.2

type Middlewarer interface {
	Wrap(r http.Handler) http.Handler
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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