From 62edcb3745f414212ee102c13429056f02cf31bd Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Mon, 2 May 2022 16:03:32 -0700 Subject: admin login working --- go.mod | 1 + go.sum | 2 + main.go | 6 +- routes/admin.go | 99 ++++++++++++++++++++++++++-- static/verify.html | 17 ----- views/css/themes/default.css | 154 +++++++++++++++++++++++-------------------- views/layouts/main.html | 39 +++++------ views/verify.html | 17 +++++ 8 files changed, 217 insertions(+), 118 deletions(-) delete mode 100644 static/verify.html create mode 100644 views/verify.html diff --git a/go.mod b/go.mod index 9ad275c..e07b19b 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.15 require ( github.com/gofiber/fiber/v2 v2.20.2 github.com/gofiber/template v1.6.18 + github.com/gofrs/uuid v4.2.0+incompatible github.com/gomodule/redigo v2.0.0+incompatible github.com/lib/pq v1.9.0 github.com/simia-tech/crypt v0.5.0 diff --git a/go.sum b/go.sum index fe9140e..be333e7 100644 --- a/go.sum +++ b/go.sum @@ -90,6 +90,8 @@ github.com/gofiber/fiber/v2 v2.20.2 h1:dqizbjO1pCmH6K+b+kBk7TCJK4rmgjJXvX8/MZDbK github.com/gofiber/fiber/v2 v2.20.2/go.mod h1:/LdZHMUXZvTTo7gU4+b1hclqCAdoQphNQ9bi9gutPyI= github.com/gofiber/template v1.6.18 h1:nrDaRKJWS1vyuMLqijbiP+ryT2CIFYOr+jZnPmVf0Io= github.com/gofiber/template v1.6.18/go.mod h1:HfYYaUgBhj9nMknxczh3U2LtZ88Avd1IPThD3GTUtd8= +github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0= +github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= diff --git a/main.go b/main.go index c83ba10..ec1e61f 100644 --- a/main.go +++ b/main.go @@ -56,9 +56,9 @@ func main() { app.Get("/followers", routes.Followers) // Admin routes - app.Get("/verify", routes.AdminVerify) - app.Get("/auth", routes.AdminAuth) - app.Get("/"+config.Key+"/", routes.AdminIndex) + app.Post("/verify", routes.AdminVerify) + app.Post("/auth", routes.AdminAuth) + app.All("/"+config.Key+"/", routes.AdminIndex) app.Get("/"+config.Key+"/addboard", routes.AdminAddBoard) app.Get("/"+config.Key+"/postnews", routes.AdminPostNews) app.Get("/"+config.Key+"/newsdelete", routes.AdminNewsDelete) diff --git a/routes/admin.go b/routes/admin.go index a2f7cd2..a6978f4 100644 --- a/routes/admin.go +++ b/routes/admin.go @@ -1,26 +1,113 @@ package routes import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" + "time" + + "github.com/FChannel0/FChannel-Server/activitypub" "github.com/FChannel0/FChannel-Server/config" "github.com/FChannel0/FChannel-Server/db" "github.com/FChannel0/FChannel-Server/util" "github.com/FChannel0/FChannel-Server/webfinger" "github.com/gofiber/fiber/v2" + "github.com/gofrs/uuid" ) -func AdminVerify(c *fiber.Ctx) error { - // STUB +func AdminVerify(ctx *fiber.Ctx) error { + identifier := ctx.FormValue("id") + code := ctx.FormValue("code") + + var verify db.Verify + verify.Identifier = identifier + verify.Code = code + + j, _ := json.Marshal(&verify) - return c.SendString("admin verify") + req, err := http.NewRequest("POST", config.Domain+"/auth", bytes.NewBuffer(j)) + + if err != nil { + log.Println("error making verify req") + return err + } + + req.Header.Set("Content-Type", config.ActivityStreams) + + resp, err := http.DefaultClient.Do(req) + + if err != nil { + log.Println("error getting verify resp") + return err + } + + defer resp.Body.Close() + + rBody, _ := ioutil.ReadAll(resp.Body) + + body := string(rBody) + + if resp.StatusCode != 200 { + return ctx.Redirect("/"+config.Key, http.StatusPermanentRedirect) + } + + //TODO remove redis dependency + sessionToken, _ := uuid.NewV4() + + _, err = db.Cache.Do("SETEX", sessionToken, "86400", body+"|"+verify.Code) + if err != nil { + return ctx.Redirect("/"+config.Key, http.StatusPermanentRedirect) + } + + ctx.Cookie(&fiber.Cookie{ + Name: "session_token", + Value: sessionToken.String(), + Expires: time.Now().UTC().Add(60 * 60 * 48 * time.Second), + }) + + return ctx.Redirect("/", http.StatusSeeOther) } -func AdminAuth(c *fiber.Ctx) error { - // STUB +// TODO remove this route it is mostly unneeded +func AdminAuth(ctx *fiber.Ctx) error { + var verify db.Verify + + err := json.Unmarshal(ctx.Body(), &verify) + + if err != nil { + log.Println("error get verify from json") + return err + } - return c.SendString("admin auth") + v, _ := db.GetVerificationByCode(verify.Code) + + if v.Identifier == verify.Identifier { + _, err := ctx.Write([]byte(v.Board)) + return err + } + + ctx.Response().Header.SetStatusCode(http.StatusBadRequest) + _, err = ctx.Write([]byte("")) + + return err } func AdminIndex(ctx *fiber.Ctx) error { + fmt.Println("admin index") + id, _ := db.GetPasswordFromSession(ctx) + actor, _ := webfinger.GetActorFromPath(ctx.Path(), "/"+config.Key+"/") + + if actor.Id == "" { + actor, _ = activitypub.GetActorByNameFromDB(config.Domain) + } + + if id == "" || (id != actor.Id && id != config.Domain) { + return ctx.Render("verify", fiber.Map{}) + } + actor, err := webfinger.GetActor(config.Domain) if err != nil { diff --git a/static/verify.html b/static/verify.html deleted file mode 100644 index fb3fb3d..0000000 --- a/static/verify.html +++ /dev/null @@ -1,17 +0,0 @@ - - - -
- - -