Basic Auth
HTTP Basic Authentication using a username/password validator function.
Usage
import "github.com/Pavan-Silva/go-zen/auth"
basicAuth := &auth.BasicAuth{
Validate: func(username, password string) (*auth.User, error) {
if username == "admin" && password == "secret" {
return &auth.User{
ID: "1",
Username: "admin",
Authorities: auth.Authorities("ROLE_ADMIN"),
}, nil
}
return nil, fmt.Errorf("invalid credentials")
},
}
r.Use(auth.RequireAuth(basicAuth))
How It Works
- Reads the
Authorization: Basic <base64>header - Decodes and extracts username/password
- Calls the validator function
- Returns
401 Unauthorizedon failure - Stores the returned
Userin the context on success