Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authenticate username, password passed from frontend #111

Open
ianrussel opened this issue Aug 18, 2021 · 2 comments
Open

Authenticate username, password passed from frontend #111

ianrussel opened this issue Aug 18, 2021 · 2 comments

Comments

@ianrussel
Copy link

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
Copy link
Owner

shaj13 commented Sep 8, 2021

@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
Copy link

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.Minute
10)
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)

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants