go-auth-middleware
is a small Go library that provides middleware functionalities for authentication and authorization in HTTP handlers.
- 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.
You can install the package using go get
:
go get github.com/Eyal-Shalev/go-auth-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
// 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
// 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
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)
}
}
Feel free to contribute by submitting issues or pull requests!
This library is licensed under the MIT License. See the LICENSE file for details.