Open ianrussel opened 3 years ago
@ianrussel
what version you are using ?
unfortunately seems you are using an old version of this package since authenticator
only exist in v1.
please describe your use case.
package main
import ( "log" "net/http" "time"
"github.com/shaj13/go-guardian/auth"
"github.com/shaj13/go-guardian/auth/ldap"
"github.com/shaj13/go-guardian/auth/strategies"
"github.com/shaj13/go-guardian/store"
)
var ( authenticator auth.Authenticator cache store.Cache )
func SetupGoGuardian(username, password string) (auth.User, error) { log.Printf("User username %s", username) cfg := &ldap.Config{ Port: "389", Host: "ldapadmin.test", BindDN: "cn=admin,dc=ldapadmin,dc=test", BindPassword: "root", BaseDN: "dc=ldapadmin, dc=test", Filter: "(uid=%s)", } authenticator = auth.New() cache = store.NewFIFO(context.Background(), time.Minute10) strategy := ldap.NewCached(cfg, cache) authenticator.EnableStrategy(ldap.StrategyKey, strategy) user, err := authenticator.Authenticate(username, password) if err != nil { return nil, err } return user, nil }
func main() { http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPost { r.ParseForm() username := r.FormValue("username") password := r.FormValue("password")
user, err := SetupGoGuardian(username, password)
if err != nil {
http.Error(w, "Authentication failed", http.StatusUnauthorized)
return
}
// Now you can use the authenticated user for authorization or other purposes.
// user.Username and user.Groups can be used to determine access levels.
http.Redirect(w, r, "/dashboard", http.StatusSeeOther)
return
}
// Handle GET request (display login form)
// Render your HTML form here
})
http.ListenAndServe(":8080", nil)
}
Hi, I would like to ask how to use authenticator to verify username/pass instead of request
Here is the authenticator from the go guardian package
What I want to do is to check the user by cheking the username/password passed from frontend form Like
Any ideas ?