From 93fa10df03f1d46c177ae01d55fb83c6d47e61e6 Mon Sep 17 00:00:00 2001 From: pinak dhir Date: Sat, 9 Dec 2023 13:39:50 +0530 Subject: [PATCH 1/7] code_of_conduct.md file added --- code_of_conduct.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 code_of_conduct.md diff --git a/code_of_conduct.md b/code_of_conduct.md new file mode 100644 index 0000000..82e03d3 --- /dev/null +++ b/code_of_conduct.md @@ -0,0 +1,33 @@ +

Code Of Conduct-KWOC For Frontened

+ +Our Pledge +In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to make participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. + +Our Standards +Examples of behaviour that contributes to a positive environment for our community include: + +Demonstrating empathy and kindness toward other people +Being respectful of differing opinions, viewpoints, and experiences +Giving and gracefully accepting constructive feedback +Accepting responsibility and apologising to those affected by our mistakes, and learning from the experience +Focusing on what is best not just for us as individuals, but for the overall community +Examples of unacceptable behaviour include: + +The use of sexualised language or imagery, and sexual attention or advances +Trolling, insulting or derogatory comments, and personal or political attacks +Public or private harassment +Publishing others' private information, such as a physical or email address, without their explicit permission +Other conduct which could reasonably be considered inappropriate in a professional setting +Our Responsibilities +Project maintainers are responsible for clarifying and enforcing our standards of acceptable behaviour and will take appropriate and fair corrective action in response to any instances of unacceptable behaviour. + +Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviours that they deem inappropriate, threatening, offensive, or harmful. + +Scope +This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. + +Enforcement +Instances of abusive, harassing, or otherwise unacceptable behaviour may be reported to the community leaders responsible for enforcement at <>. All complaints will be reviewed and investigated promptly and fairly. + +Attribution +This Code of Conduct is adapted from the Contributor Covenant, version 1.4, available at http://contributor-covenant.org/version/1/4 \ No newline at end of file From de535fbb83629c38ff8b1fc35f4875f10b7d6241 Mon Sep 17 00:00:00 2001 From: Pinak Dhir Date: Wed, 13 Dec 2023 21:33:30 +0530 Subject: [PATCH 2/7] Update code_of_conduct.md as ask to make changes Signed-off-by: Pinak Dhir --- code_of_conduct.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/code_of_conduct.md b/code_of_conduct.md index 82e03d3..f3d2adf 100644 --- a/code_of_conduct.md +++ b/code_of_conduct.md @@ -1,9 +1,9 @@ -

Code Of Conduct-KWOC For Frontened

+## Code Of Conduct-KWOC For Frontened -Our Pledge +## Our Pledge In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to make participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. -Our Standards +## Our Standards Examples of behaviour that contributes to a positive environment for our community include: Demonstrating empathy and kindness toward other people @@ -18,16 +18,16 @@ Trolling, insulting or derogatory comments, and personal or political attacks Public or private harassment Publishing others' private information, such as a physical or email address, without their explicit permission Other conduct which could reasonably be considered inappropriate in a professional setting -Our Responsibilities +## Our Responsibilities Project maintainers are responsible for clarifying and enforcing our standards of acceptable behaviour and will take appropriate and fair corrective action in response to any instances of unacceptable behaviour. Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviours that they deem inappropriate, threatening, offensive, or harmful. -Scope +## Scope This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. -Enforcement +## Enforcement Instances of abusive, harassing, or otherwise unacceptable behaviour may be reported to the community leaders responsible for enforcement at <>. All complaints will be reviewed and investigated promptly and fairly. Attribution -This Code of Conduct is adapted from the Contributor Covenant, version 1.4, available at http://contributor-covenant.org/version/1/4 \ No newline at end of file +This Code of Conduct is adapted from the Contributor Covenant, version 1.4, available at http://contributor-covenant.org/version/1/4 From 216134bedec6a25937f02836e6ee2bee147a883f Mon Sep 17 00:00:00 2001 From: Pinak Dhir Date: Thu, 14 Dec 2023 02:04:27 +0530 Subject: [PATCH 3/7] Update code_of_conduct.md Signed-off-by: Pinak Dhir --- code_of_conduct.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/code_of_conduct.md b/code_of_conduct.md index f3d2adf..7e9529b 100644 --- a/code_of_conduct.md +++ b/code_of_conduct.md @@ -1,4 +1,4 @@ -## Code Of Conduct-KWOC For Frontened +# Code Of Conduct-KWOC For Backened ## Our Pledge In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to make participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. @@ -6,18 +6,18 @@ In the interest of fostering an open and welcoming environment, we as contributo ## Our Standards Examples of behaviour that contributes to a positive environment for our community include: -Demonstrating empathy and kindness toward other people -Being respectful of differing opinions, viewpoints, and experiences -Giving and gracefully accepting constructive feedback -Accepting responsibility and apologising to those affected by our mistakes, and learning from the experience -Focusing on what is best not just for us as individuals, but for the overall community +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologising to those affected by our mistakes, and learning from the experience +* Focusing on what is best not just for us as individuals, but for the overall community Examples of unacceptable behaviour include: -The use of sexualised language or imagery, and sexual attention or advances -Trolling, insulting or derogatory comments, and personal or political attacks -Public or private harassment -Publishing others' private information, such as a physical or email address, without their explicit permission -Other conduct which could reasonably be considered inappropriate in a professional setting +* The use of sexualised language or imagery, and sexual attention or advances +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email address, without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a professional setting ## Our Responsibilities Project maintainers are responsible for clarifying and enforcing our standards of acceptable behaviour and will take appropriate and fair corrective action in response to any instances of unacceptable behaviour. From 281aa259ccc93cc59ce82d7141aa48acdaf2684e Mon Sep 17 00:00:00 2001 From: Pinak Dhir Date: Thu, 14 Dec 2023 02:10:15 +0530 Subject: [PATCH 4/7] Update code_of_conduct.md Signed-off-by: Pinak Dhir --- code_of_conduct.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code_of_conduct.md b/code_of_conduct.md index 7e9529b..d11ae22 100644 --- a/code_of_conduct.md +++ b/code_of_conduct.md @@ -1,4 +1,4 @@ -# Code Of Conduct-KWOC For Backened +# Code Of Conduct ## Our Pledge In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to make participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. @@ -27,7 +27,7 @@ Project maintainers have the right and responsibility to remove, edit, or reject This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. ## Enforcement -Instances of abusive, harassing, or otherwise unacceptable behaviour may be reported to the community leaders responsible for enforcement at <>. All complaints will be reviewed and investigated promptly and fairly. +Instances of abusive, harassing, or otherwise unacceptable behaviour may be reported to the community leaders responsible for enforcement at . All complaints will be reviewed and investigated promptly and fairly. Attribution This Code of Conduct is adapted from the Contributor Covenant, version 1.4, available at http://contributor-covenant.org/version/1/4 From f20344267b50bd9c687e70ce6b1e7b22b490d19a Mon Sep 17 00:00:00 2001 From: Pinak Dhir Date: Thu, 14 Dec 2023 02:11:32 +0530 Subject: [PATCH 5/7] Update code_of_conduct.md Signed-off-by: Pinak Dhir --- code_of_conduct.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/code_of_conduct.md b/code_of_conduct.md index d11ae22..429503c 100644 --- a/code_of_conduct.md +++ b/code_of_conduct.md @@ -26,8 +26,12 @@ Project maintainers have the right and responsibility to remove, edit, or reject ## Scope This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. + ## Enforcement Instances of abusive, harassing, or otherwise unacceptable behaviour may be reported to the community leaders responsible for enforcement at . All complaints will be reviewed and investigated promptly and fairly. +## Attribution +This Code of Conduct is adapted from the Contributor Covenant, version 1.4, available at http://contributor-covenant.org/version/1/4 + Attribution This Code of Conduct is adapted from the Contributor Covenant, version 1.4, available at http://contributor-covenant.org/version/1/4 From becb3c27137db9e525da81047c1af2247eb8f213 Mon Sep 17 00:00:00 2001 From: Harsh Khandeparkar Date: Sat, 16 Dec 2023 00:28:01 +0530 Subject: [PATCH 6/7] feat: turned off registration --- server/routes.go | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/server/routes.go b/server/routes.go index a2dd202..ee358a2 100644 --- a/server/routes.go +++ b/server/routes.go @@ -40,12 +40,12 @@ func getRoutes(app *middleware.App) []Route { "/mentor/", middleware.WithLogin(middleware.WrapApp(app, controllers.GetMentorDetails)), }, - { - "Student Registration", - "POST", - "/student/form/", - middleware.WithLogin(middleware.WrapApp(app, controllers.RegisterStudent)), - }, + // { + // "Student Registration", + // "POST", + // "/student/form/", + // middleware.WithLogin(middleware.WrapApp(app, controllers.RegisterStudent)), + // }, { "Update Student Details", "PUT", @@ -64,12 +64,12 @@ func getRoutes(app *middleware.App) []Route { "/student/dashboard/", middleware.WithLogin(middleware.WrapApp(app, controllers.FetchStudentDashboard)), }, - { - "Mentor Registration", - "POST", - "/mentor/form/", - middleware.WithLogin(middleware.WrapApp(app, controllers.RegisterMentor)), - }, + // { + // "Mentor Registration", + // "POST", + // "/mentor/form/", + // middleware.WithLogin(middleware.WrapApp(app, controllers.RegisterMentor)), + // }, { "Update Mentor Details", "PUT", @@ -100,12 +100,12 @@ func getRoutes(app *middleware.App) []Route { "/healthcheck/ping/", controllers.Ping, }, - { - "Project Registration", - "POST", - "/project/", - middleware.WithLogin(middleware.WrapApp(app, controllers.RegisterProject)), - }, + // { + // "Project Registration", + // "POST", + // "/project/", + // middleware.WithLogin(middleware.WrapApp(app, controllers.RegisterProject)), + // }, { "Fetch All Projects", "GET", From 3f93fd420e05a1e450a06ea2975b4f5b211d68c4 Mon Sep 17 00:00:00 2001 From: Harsh Khandeparkar Date: Sat, 16 Dec 2023 17:03:44 +0530 Subject: [PATCH 7/7] feat: added fetch profile route --- controllers/mentor_test.go | 4 +- controllers/profile.go | 86 ++++++++++++++++++++++++++++++++++++++ controllers/student.go | 2 +- middleware/login.go | 5 +++ server/routes.go | 6 +++ utils/jwt.go | 8 +++- 6 files changed, 107 insertions(+), 4 deletions(-) create mode 100644 controllers/profile.go diff --git a/controllers/mentor_test.go b/controllers/mentor_test.go index 3428656..5661524 100644 --- a/controllers/mentor_test.go +++ b/controllers/mentor_test.go @@ -386,10 +386,10 @@ func TestMentorDashboardOK(t *testing.T) { var resMentor controllers.MentorDashboard _ = json.NewDecoder(res.Body).Decode(&resMentor) - fmt.Printf("%+v %+v", testMentor, resMentor) - expectStatusCodeToBe(t, res, http.StatusOK) if !reflect.DeepEqual(testMentor, resMentor) { t.Fatalf("Incorrect data returned from /mentor/dashboard/") + fmt.Printf("Expected mentor dashboard: %#v\n\n", testMentor) + fmt.Printf("Received mentor dashboard: %#v\n", resMentor) } } diff --git a/controllers/profile.go b/controllers/profile.go new file mode 100644 index 0000000..2d59f2e --- /dev/null +++ b/controllers/profile.go @@ -0,0 +1,86 @@ +package controllers + +import ( + "net/http" + + "github.com/kossiitkgp/kwoc-backend/v2/middleware" + "github.com/kossiitkgp/kwoc-backend/v2/utils" + "gorm.io/gorm" + + "github.com/kossiitkgp/kwoc-backend/v2/models" +) + +type ProfileResBodyFields struct { + Username string `json:"username"` + Name string `json:"name"` + Email string `json:"email"` + // `mentor` or `student` + Type string `json:"type"` +} + +// FetchProfile godoc +// @Summary Fetches user profile +// @Description Fetches the user's profile from the JWT, if it is valid. If invalid, returns an error. +// @Accept plain +// @Produce json +// @Success 200 {object} ProfileResBodyFields "Succesfully authenticated." +// @Failure 400 {object} utils.HTTPMessage "User is not registered." +// @Failure 401 {object} utils.HTTPMessage "JWT session token invalid." +// @Failure 500 {object} utils.HTTPMessage "Error parsing JWT string." +// +// @Security JWT +// +// @Router /profile [get] +func FetchProfile(w http.ResponseWriter, r *http.Request) { + app := r.Context().Value(middleware.APP_CTX_KEY).(*middleware.App) + db := app.Db + + username := r.Context().Value(middleware.LOGIN_CTX_USERNAME_KEY).(string) + + // Check if the student already exists in the db + student := models.Student{} + tx := db. + Table("students"). + Where("username = ?", username). + First(&student) + + if tx.Error != nil && tx.Error != gorm.ErrRecordNotFound { + utils.LogErrAndRespond(r, w, tx.Error, "Database error.", http.StatusInternalServerError) + return + } + + student_exists := student.Username == username + if student_exists { + utils.RespondWithJson(r, w, ProfileResBodyFields{ + Username: student.Username, + Name: student.Name, + Email: student.Email, + Type: "student", + }) + return + } + + // Check if a mentor of the same username exists + mentor := models.Mentor{} + tx = db. + Table("mentors"). + Where("username = ?", username). + First(&mentor) + if tx.Error != nil && tx.Error != gorm.ErrRecordNotFound { + utils.LogErrAndRespond(r, w, tx.Error, "Database error.", http.StatusInternalServerError) + return + } + mentor_exists := mentor.Username == username + + if mentor_exists { + utils.RespondWithJson(r, w, ProfileResBodyFields{ + Username: mentor.Username, + Name: mentor.Name, + Email: mentor.Email, + Type: "mentor", + }) + return + } + + utils.RespondWithHTTPMessage(r, w, http.StatusBadRequest, "User is not registered.") +} diff --git a/controllers/student.go b/controllers/student.go index 420ad03..b1948b0 100644 --- a/controllers/student.go +++ b/controllers/student.go @@ -100,7 +100,7 @@ func RegisterStudent(w http.ResponseWriter, r *http.Request) { First(&student) if tx.Error != nil && tx.Error != gorm.ErrRecordNotFound { - utils.LogErrAndRespond(r, w, err, "Database error.", http.StatusInternalServerError) + utils.LogErrAndRespond(r, w, tx.Error, "Database error.", http.StatusInternalServerError) return } diff --git a/middleware/login.go b/middleware/login.go index 67dbde3..0134cde 100644 --- a/middleware/login.go +++ b/middleware/login.go @@ -29,6 +29,11 @@ func WithLogin(inner http.HandlerFunc) http.HandlerFunc { return } + if err == utils.ErrJwtTokenExpired { + utils.LogErrAndRespond(r, w, err, "Error: JWT session token expired.", http.StatusUnauthorized) + return + } + utils.LogErrAndRespond(r, w, err, "Error parsing JWT string.", http.StatusInternalServerError) return } diff --git a/server/routes.go b/server/routes.go index ee358a2..0968b3b 100644 --- a/server/routes.go +++ b/server/routes.go @@ -28,6 +28,12 @@ func getRoutes(app *middleware.App) []Route { "/oauth/", middleware.WrapApp(app, controllers.OAuth), }, + { + "Profile", + "GET", + "/profile/", + middleware.WithLogin(middleware.WrapApp(app, controllers.FetchProfile)), + }, { "Fetch Student Details", "GET", diff --git a/utils/jwt.go b/utils/jwt.go index cb14515..86679af 100644 --- a/utils/jwt.go +++ b/utils/jwt.go @@ -2,6 +2,7 @@ package utils import ( "errors" + "fmt" "os" "strconv" "time" @@ -11,6 +12,7 @@ import ( ) var ErrJwtSecretKeyNotFound = errors.New("ERROR: JWT SECRET KEY NOT FOUND") +var ErrJwtTokenExpired = errors.New("ERROR: JWT TOKEN EXPIRED") var ErrJwtTokenInvalid = errors.New("ERROR: JWT TOKEN INVALID") func getJwtKey() (string, error) { @@ -46,6 +48,10 @@ func ParseLoginJwtString(tokenString string) (*jwt.Token, *LoginJwtClaims, error var loginClaims = LoginJwtClaims{} token, err := jwt.ParseWithClaims(tokenString, &loginClaims, jwtKeyFunc) + if err.Error() == fmt.Sprintf("%s: %s", jwt.ErrTokenInvalidClaims.Error(), jwt.ErrTokenExpired.Error()) { + return nil, nil, ErrJwtTokenExpired + } + if err != nil { return nil, nil, err } @@ -66,7 +72,7 @@ func GenerateLoginJwtString(loginJwtFields LoginJwtFields) (string, error) { if err != nil { // Default of 30 days - jwtValidityTime = 30 * 24 + jwtValidityTime = 0 log.Warn().Msgf("Could not parse JWT validity time from the environment. Set to default of %d hours.", jwtValidityTime) }