From eb065c414224f291a38e60a41fed245025a886e1 Mon Sep 17 00:00:00 2001 From: Braeden Sowinski Date: Tue, 7 Jun 2022 11:12:28 -0700 Subject: [PATCH] generalize authentication in #13 --- controllers/authController.go | 47 ++++++++++----------------- controllers/updateController.go | 56 ++++++++++++++++----------------- models/idModel.go | 5 +-- 3 files changed, 47 insertions(+), 61 deletions(-) diff --git a/controllers/authController.go b/controllers/authController.go index 6ccb7d9..76bba1c 100644 --- a/controllers/authController.go +++ b/controllers/authController.go @@ -113,29 +113,11 @@ var SecretKey = os.Getenv("secret") var systemEmail string = os.Getenv("SYSTEM_EMAIL") var systemPassword string = os.Getenv("SYSTEM_PASSWORD") -func AuthAdmin(c *fiber.Ctx) bool { - cookie := c.Cookies("jwt") - - token, err := jwt.ParseWithClaims(cookie, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) { - return []byte(SecretKey), nil - }) - if err != nil { - return false +func AuthenticateUser(c *fiber.Ctx, userType int) (bool, string) { + if userType < 1 || userType > 3 { + log.Fatal("Invalid userType") } - claims := token.Claims.(*jwt.StandardClaims) - - // Though admin is not used, it's required to prevent findErr - var admin models.Admin - findErr := adminCollection.FindOne(context.TODO(), bson.M{"aid": claims.Issuer}).Decode(&admin) - if findErr != nil { - return false - } - - return true -} - -func AuthStudent(c *fiber.Ctx) (verified bool, sid string) { cookie := c.Cookies("jwt") token, err := jwt.ParseWithClaims(cookie, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) { @@ -147,14 +129,17 @@ func AuthStudent(c *fiber.Ctx) (verified bool, sid string) { claims := token.Claims.(*jwt.StandardClaims) - // Though student is not used, it's required to prevent findErr - var student models.Student - findErr := studentCollection.FindOne(context.TODO(), bson.M{"sid": claims.Issuer}).Decode(&student) + var userID models.Id + findErr := idCollection.FindOne(context.TODO(), bson.M{"cid": claims.Issuer}).Decode(&userID) if findErr != nil { return false, "" } - return true, claims.Issuer + if userID.ParentType != userType { + return false, "" + } + + return true, userID.CID } func Enroll(c *fiber.Ctx) error { @@ -171,7 +156,7 @@ func Enroll(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -312,7 +297,7 @@ func RegisterTeacher(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -429,7 +414,7 @@ func CreateAdmin(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -814,7 +799,7 @@ func AdminLogin(c *fiber.Ctx) error { func Student(c *fiber.Ctx) error { var sid string - if AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { var data map[string]string if err := c.BodyParser(&data); err != nil { @@ -999,7 +984,7 @@ func CreateContact(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -1084,7 +1069,7 @@ func DeleteContact(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, diff --git a/controllers/updateController.go b/controllers/updateController.go index 30317bd..bbab93b 100644 --- a/controllers/updateController.go +++ b/controllers/updateController.go @@ -41,7 +41,7 @@ func UpdateStudentName(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -121,7 +121,7 @@ func UpdateStudentGradeLevel(c *fiber.Ctx) error { } // Ensure Authorized admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -193,7 +193,7 @@ func UpdateStudentHomeroom(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -456,7 +456,7 @@ func UpdateStudentLocker(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -528,7 +528,7 @@ func UpdateStudentAddress(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -593,7 +593,7 @@ func UpdateStudentYOG(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -664,7 +664,7 @@ func RemoveStudentContact(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -738,7 +738,7 @@ func AddStudentContact(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -802,7 +802,7 @@ func UpdateStudentPhoto(c *fiber.Ctx) error { ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) //Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -928,8 +928,8 @@ func UpdateStudentEmail(c *fiber.Ctx) error { }) } - verifiedAdmin := AuthAdmin(c) - verifiedStudent, sid := AuthStudent(c) + verifiedAdmin, _ := AuthenticateUser(c, 3) + verifiedStudent, sid := AuthenticateUser(c, 1) // Ensure Authenticated admin sent request if !verifiedAdmin && !verifiedStudent { cancel() @@ -992,7 +992,7 @@ func RemoveStudentsDisabled(c *fiber.Ctx) error { } // Ensure Authorized admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -1055,7 +1055,7 @@ func RemoveTeachersDisabled(c *fiber.Ctx) error { } // Ensure Authorized admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -1128,7 +1128,7 @@ func UpdateTeacherHomeroom(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -1389,7 +1389,7 @@ func UpdateTeacherAddress(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -1443,7 +1443,7 @@ func UpdateTeacherPhoto(c *fiber.Ctx) error { ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) //Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -1570,7 +1570,7 @@ func UpdateTeacherEmail(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -1631,7 +1631,7 @@ func UpdateTeacherName(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -1703,7 +1703,7 @@ func UpdateContactName(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -1766,7 +1766,7 @@ func UpdateContactAddress(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -1830,7 +1830,7 @@ func UpdateContactHomePhone(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -1891,7 +1891,7 @@ func UpdateContactWorkPhone(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -1952,7 +1952,7 @@ func UpdateContactEmail(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -2013,7 +2013,7 @@ func UpdateContactPriority(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -2074,7 +2074,7 @@ func UpdateLockerCombo(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -2135,7 +2135,7 @@ func RemoveStudent(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -2194,7 +2194,7 @@ func RemoveTeacher(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, @@ -2253,7 +2253,7 @@ func RemoveAdmin(c *fiber.Ctx) error { } // Ensure Authenticated admin sent request - if !AuthAdmin(c) { + if verified, _ := AuthenticateUser(c, 3); !verified { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, diff --git a/models/idModel.go b/models/idModel.go index fd45ad7..8c78e67 100644 --- a/models/idModel.go +++ b/models/idModel.go @@ -15,6 +15,7 @@ import ( */ type Id struct { - ID primitive.ObjectID `bson:"_id"` - CID string `bson:"cid"` // custom id for admin, teahcer or student + ID primitive.ObjectID `bson:"_id"` + CID string `json:"cid"` // custom id for admin, teahcer or student + ParentType int `json:"parenttype"` // A number representing the user (1: student, 2: teacher, 3: admin) }