shaj13 / go-guardian

Go-Guardian is a golang library that provides a simple, clean, and idiomatic way to create powerful modern API and web authentication.
MIT License
559 stars 56 forks source link

Authenticate username, password passed from frontend #111

Open ianrussel opened 3 years ago

ianrussel commented 3 years ago

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

       return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    log.Println("Executing Auth Middleware")
    for k, v := range r.URL.Query() {
        log.Printf("%s: %s\n", k, v)
    }
    user, err := authenticator.Authenticate(r)

What I want to do is to check the user by cheking the username/password passed from frontend form Like

         func SetupGoGuardian(u, p string) (*authentication.User, error) {
                   // u here is username from form
                   // p here is password from form
               log.Printf("User username %s", u)
               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.Minute*10)
              strategy := ldap.NewCached(cfg, cache)
              authenticator.EnableStrategy(ldap.StrategyKey, strategy)
                  user, err := authenticator.Authenticate(u, p) // this what I want to check my username password,
          if err != nil {
                      return &authentication.User{}, nil
                  }
                   return (///////////////////you are now allowed)

Any ideas ?

shaj13 commented 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.

ljluestc commented 1 year ago

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)

}