Code Monkey home page Code Monkey logo

go-auth-middleware's Introduction

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.

go-auth-middleware's People

Contributors

eyal-shalev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.