Browse Source

Submit simple form.

master
Robert Jacob 2 years ago
parent
commit
480add377c
4 changed files with 79 additions and 11 deletions
  1. +4
    -4
      assets/index.html
  2. +18
    -0
      config.go
  3. +11
    -0
      ldap.go
  4. +46
    -7
      main.go

+ 4
- 4
assets/index.html View File

@ -30,19 +30,19 @@
<form method="POST" action="/api/change-password">
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" placeholder="Enter username">
<input type="text" class="form-control" id="username" name="username" placeholder="Enter username">
</div>
<div class="form-group">
<label for="password">Current password</label>
<input type="password" class="form-control" id="password" placeholder="Enter your password">
<input type="password" class="form-control" id="password" name="password" placeholder="Enter your password">
</div>
<div class="form-group">
<label for="new-password">New password</label>
<input type="password" class="form-control" id="new-password" placeholder="Enter new password">
<input type="password" class="form-control" id="new-password" name="new-password" placeholder="Enter new password">
</div>
<div class="form-group">
<label for="repeat-password">New password (repeat)</label>
<input type="password" class="form-control" id="repeat-password" placeholder="Repeat new password">
<input type="password" class="form-control" id="repeat-password" name="repeat-password" placeholder="Repeat new password">
</div>
<button type="submit" class="btn btn-primary">Change password</button>
</form>


+ 18
- 0
config.go View File

@ -0,0 +1,18 @@
package main
import "github.com/spf13/pflag"
type config struct {
ListenAddr string
}
func parseConfig() (config, error) {
c := config{
ListenAddr: ":8080",
}
pflag.StringVarP(&c.ListenAddr, "listen-addr", "l", c.ListenAddr, "Network address to listen on.")
pflag.Parse()
return c, nil
}

+ 11
- 0
ldap.go View File

@ -0,0 +1,11 @@
package main
type ldapConnection struct{}
func newLDAP(cfg config) (*ldapConnection, error) {
return &ldapConnection{}, nil
}
func (l *ldapConnection) ChangePassword(username, password, newPassword string) (bool, error) {
return false, nil
}

+ 46
- 7
main.go View File

@ -1,12 +1,12 @@
package main
import (
"fmt"
"net/http"
"github.com/gobuffalo/packr/v2"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
)
var (
@ -16,21 +16,60 @@ var (
)
func main() {
pflag.StringVarP(&addr, "listen-addr", "l", addr, "Network address to listen on.")
pflag.Parse()
cfg, err := parseConfig()
if err != nil {
log.Fatal(err)
}
ldap, err := newLDAP(cfg)
if err != nil {
log.Fatalf("Error initializing LDAP connection: %s", err)
}
assetBox := packr.New("Assets", "./assets/")
r := mux.NewRouter()
r.Path("/api/change-password").Handler(changePasswordHandler())
r.Path("/api/change-password").Handler(changePasswordHandler(ldap))
r.PathPrefix("/").Handler(http.FileServer(assetBox))
log.Infof("Listening on %s...", addr)
log.Error(http.ListenAndServe(addr, r))
log.Infof("Listening on %s...", cfg.ListenAddr)
log.Error(http.ListenAndServe(cfg.ListenAddr, r))
}
func changePasswordHandler() http.HandlerFunc {
func changePasswordHandler(ldap *ldapConnection) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, fmt.Sprintf("Error parsing form: %s", err), http.StatusBadRequest)
return
}
username := r.Form.Get("username")
password := r.Form.Get("password")
newPassword := r.Form.Get("new-password")
newPasswordRepeat := r.Form.Get("repeat-password")
if username == "" || password == "" || newPassword == "" || newPasswordRepeat == "" {
http.Error(w, "All fields are needed!", http.StatusBadRequest)
return
}
if newPassword != newPasswordRepeat {
http.Error(w, "New passwords do not match!", http.StatusBadRequest)
return
}
ok, err := ldap.ChangePassword(username, password, newPassword)
if err != nil {
http.Error(w, fmt.Sprintf("Error changing password: %s", err), http.StatusInternalServerError)
return
}
if !ok {
http.Error(w, "Login not valid!", http.StatusForbidden)
return
}
http.Redirect(w, r, "/", http.StatusFound)
}
}

Loading…
Cancel
Save