From 90b69bbdcc967a4c3ecc1aadf9447b90b321b2ca Mon Sep 17 00:00:00 2001 From: Braeden Sowinski Date: Fri, 11 Feb 2022 13:49:45 -0800 Subject: [PATCH] update/fix/admin-and-student-retrieve-student-data --- controllers/authController.go | 51 ++++++++++++++++++++++++++--------- go.mod | 1 + go.sum | 2 ++ 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/controllers/authController.go b/controllers/authController.go index eb06090..3f86304 100644 --- a/controllers/authController.go +++ b/controllers/authController.go @@ -636,24 +636,51 @@ func AdminLogin(c *fiber.Ctx) error { } func Student(c *fiber.Ctx) error { - cookie := c.Cookies("jwt") + var sid string + if AuthAdmin(c) { + var data map[string]string - token, err := jwt.ParseWithClaims(cookie, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) { - return []byte(SecretKey), nil - }) - if err != nil { - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ - "success": false, - "message": "not authorized", + if err := c.BodyParser(&data); err != nil { + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "success": false, + "message": "Failed to parse body", + "error": err, + }) + } + + // Check required fields are included + if data["sid"] == "" { + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ + "success": false, + "message": "missing required fields", + }) + } + sid = data["sid"] + } else { + cookie := c.Cookies("jwt") + + token, err := jwt.ParseWithClaims(cookie, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) { + return []byte(SecretKey), nil }) + // This returns not authorized for both admin and student + if err != nil { + return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ + "success": false, + "message": "not authorized", + }) + } + + claims := token.Claims.(*jwt.StandardClaims) + sid = claims.Issuer } - claims := token.Claims.(*jwt.StandardClaims) - - var responceData map[string]interface{} + responceData := make(map[string]interface{}) + responceData["student"] = nil + responceData["locker"] = nil + responceData["contacts"] = nil var student models.Student - findErr := studentCollection.FindOne(context.TODO(), bson.M{"schooldata.sid": claims.Issuer}).Decode(&student) + findErr := studentCollection.FindOne(context.TODO(), bson.M{"schooldata.sid": sid}).Decode(&student) if findErr != nil { return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, diff --git a/go.mod b/go.mod index 1485216..310af92 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.16 require ( github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/gofiber/fiber/v2 v2.19.0 + github.com/google/uuid v1.3.0 github.com/joho/godotenv v1.3.0 go.mongodb.org/mongo-driver v1.7.2 golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 diff --git a/go.sum b/go.sum index 342c1ca..eafaefc 100644 --- a/go.sum +++ b/go.sum @@ -39,6 +39,8 @@ github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA= github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=