From bbf360d57064ed559aa0f2e19a2ac94519bbe4ce Mon Sep 17 00:00:00 2001 From: sanine-a Date: Tue, 2 May 2023 16:28:00 -0500 Subject: add basic login logic --- phlox/go.mod | 15 ++++++++++++--- phlox/go.sum | 7 ++++++- phlox/login.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ phlox/main.go | 21 ++++++++++++++++++--- phlox/phlox.conf | Bin 0 -> 32768 bytes phlox/phlox.exe | Bin 0 -> 23151815 bytes 6 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 phlox/login.go create mode 100644 phlox/phlox.conf create mode 100644 phlox/phlox.exe diff --git a/phlox/go.mod b/phlox/go.mod index cd9243f..6ea0cc1 100644 --- a/phlox/go.mod +++ b/phlox/go.mod @@ -1,7 +1,16 @@ module sanine.net/git/phlox/phlox -go 1.20 +go 1.19 -require github.com/sirupsen/logrus v1.9.0 +replace sanine.net/git/phlox/db => ../db -require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect +require ( + github.com/sirupsen/logrus v1.9.0 + sanine.net/git/phlox/db v0.0.0-00010101000000-000000000000 +) + +require ( + github.com/mattn/go-sqlite3 v1.14.16 // indirect + golang.org/x/crypto v0.8.0 // indirect + golang.org/x/sys v0.7.0 // indirect +) diff --git a/phlox/go.sum b/phlox/go.sum index ed65537..b99b4dc 100644 --- a/phlox/go.sum +++ b/phlox/go.sum @@ -1,6 +1,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= +github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= @@ -8,8 +10,11 @@ github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVs github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ= +golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= +golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/phlox/login.go b/phlox/login.go new file mode 100644 index 0000000..27decf4 --- /dev/null +++ b/phlox/login.go @@ -0,0 +1,56 @@ +package main + +import ( + "fmt" + "net/http" + db "sanine.net/git/phlox/db" +) + + +func LoginUser(username, password string) (bool, db.Session, error) { + auth, user, err := p.AuthenticateUser(username, password) + if err != nil { + return false, db.Session{}, err + } + if auth == false { + return false, db.Session{}, nil + } + // auth was successful! + session, err := p.CreateSession(user) + if err != nil { + return false, db.Session{}, err + } + return true, session, nil +} + + +func LoginPostHandler(w http.ResponseWriter, r *http.Request) { + r.ParseForm() + username := r.Form.Get("username") + password := r.Form.Get("password") + redirect := r.Form.Get("redirect") + + auth, session, err := LoginUser(username, password) + if err != nil { + // respond with error page + w.WriteHeader(http.StatusInternalServerError) + fmt.Fprintf(w, "an error was encountered when processing the request") + log.Error(err) + } + + if !auth { + // not allowed! + w.WriteHeader(http.StatusUnauthorized) + fmt.Fprintf(w, "bad username or password") + log.Errorf("failed login for %v", username) + } + + http.SetCookie(w, &http.Cookie{ + Name: "phlox-session-id", + Value: session.Id, + SameSite: http.SameSiteLaxMode, + } + + http.Header.Add("Location", redirect) + http.WriteHeader(http.StatusTemporaryRedirect) +} diff --git a/phlox/main.go b/phlox/main.go index ee9416c..3f80724 100644 --- a/phlox/main.go +++ b/phlox/main.go @@ -13,7 +13,7 @@ import ( ) -var p *db.Plhox +var p *db.Phlox func main() { @@ -28,16 +28,31 @@ func main() { } defer p.Close() - const addr = "localhost:3333" + addr, err := p.GetHostAddress() + if err != nil { + log.Fatal(err) + } + endpoints, err := p.AllEndpoints() + if err != nil { + log.Fatal(err) + } log.Info("configuring reverse proxy...") - configureEndpoint("/proxy1", "http://localhost:8000") + for _, endpoint := range endpoints { + configureEndpoint(endpoint.Path, endpoint.Address) + } log.Infof("serving on %v", addr) log.Fatal(http.ListenAndServe(addr, nil)) } +type Endpoint struct { + Path string + Origin *url.URL +} + + func configureEndpoint(path, address string) { log.Infof("proxying endpoint %v to %v", path, address) origin, err := url.Parse(address) diff --git a/phlox/phlox.conf b/phlox/phlox.conf new file mode 100644 index 0000000..f41d0b1 Binary files /dev/null and b/phlox/phlox.conf differ diff --git a/phlox/phlox.exe b/phlox/phlox.exe new file mode 100644 index 0000000..8a4412b Binary files /dev/null and b/phlox/phlox.exe differ -- cgit v1.2.1