Skip to content

Basic Auth ​

Added: v1.1.0

HTTP Basic Authentication using a username/password validator function.

Usage ​

go
import "github.com/Pavan-Silva/go-zen/auth"

basicAuth := &auth.BasicAuth{
    Validate: func(username, password string) (*zen.User, error) {
        if username == "admin" && password == "secret" {
            return &zen.User{
                ID:     "1",
                Username: "admin",
                Roles:  []string{"admin"},
            }, nil
        }
        return nil, fmt.Errorf("invalid credentials")
    },
}

r.EnableAuth(basicAuth)

How It Works ​

  • Reads the Authorization: Basic <base64> header
  • Decodes and extracts username/password
  • Calls the validator function
  • Returns 401 Unauthorized on failure
  • Stores the returned User in the context on success

Released under the MIT License.