Skip to content

Commit

Permalink
Added necessary RBACs for creating and fetching jwt token (#4619)
Browse files Browse the repository at this point in the history
* Added necessary RBACs for creating and fetching jwt token

Signed-off-by: Saranya-jena <saranya.jena@harness.io>

* fixed UTs

Signed-off-by: Saranya-jena <saranya.jena@harness.io>

* Added rbac in get user api

Signed-off-by: Saranya-jena <saranya.jena@harness.io>

* fixed UTs

Signed-off-by: Saranya-jena <saranya.jena@harness.io>

---------

Signed-off-by: Saranya-jena <saranya.jena@harness.io>
Co-authored-by: Namkyu Park <53862866+namkyu1999@users.noreply.github.com>
  • Loading branch information
Saranya-jena and namkyu1999 committed May 7, 2024
1 parent 197dd60 commit 8e87c1e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
31 changes: 31 additions & 0 deletions chaoscenter/authentication/api/handlers/rest/user_handlers.go
Expand Up @@ -152,6 +152,18 @@ func UpdateUser(service services.ApplicationService) gin.HandlerFunc {
func GetUser(service services.ApplicationService) gin.HandlerFunc {
return func(c *gin.Context) {
uid := c.Param("uid")

// Validating logged in user
// Must be either requesting info from the logged in user
// or any user if it has the admin role
role := c.MustGet("role").(string)
if c.MustGet("uid").(string) != uid && role != string(entities.RoleAdmin) {
log.Error("auth error: unauthorized")
c.JSON(utils.ErrorStatusCodes[utils.ErrUnauthorized],
presenter.CreateErrorResponse(utils.ErrUnauthorized))
return
}

user, err := service.GetUser(uid)
if err != nil {
log.Error(err)
Expand Down Expand Up @@ -559,6 +571,15 @@ func CreateApiToken(service services.ApplicationService) gin.HandlerFunc {
return
}

// Validating logged in user
// Requesting info must be from the logged in user
if c.MustGet("uid").(string) != apiTokenRequest.UserID {
log.Error("auth error: unauthorized")
c.JSON(utils.ErrorStatusCodes[utils.ErrUnauthorized],
presenter.CreateErrorResponse(utils.ErrUnauthorized))
return
}

// Checking if user exists
user, err := service.GetUser(apiTokenRequest.UserID)
if err != nil {
Expand Down Expand Up @@ -594,6 +615,16 @@ func CreateApiToken(service services.ApplicationService) gin.HandlerFunc {
func GetApiTokens(service services.ApplicationService) gin.HandlerFunc {
return func(c *gin.Context) {
uid := c.Param("uid")

// Validating logged in user
// Requesting info must be from the logged in user
if c.MustGet("uid").(string) != uid {
log.Error("auth error: unauthorized")
c.JSON(utils.ErrorStatusCodes[utils.ErrUnauthorized],
presenter.CreateErrorResponse(utils.ErrUnauthorized))
return
}

apiTokens, err := service.GetApiTokensByUserID(uid)
if err != nil {
log.Error(err)
Expand Down
Expand Up @@ -142,12 +142,14 @@ func TestGetUser(t *testing.T) {
tests := []struct {
name string
uid string
role string
given func()
expectedCode int
}{
{
name: "Successfully retrieve user",
uid: "testUID",
role: "user",
given: func() {
user := &entities.User{
ID: "testUID",
Expand All @@ -167,7 +169,8 @@ func TestGetUser(t *testing.T) {
c.Params = gin.Params{
{"uid", tt.uid},
}

c.Set("uid", tt.uid)
c.Set("role", tt.role)
tt.given()

rest.GetUser(service)(c)
Expand Down Expand Up @@ -637,7 +640,7 @@ func TestCreateApiToken(t *testing.T) {
bodyBytes, _ := json.Marshal(tt.inputBody)
c.Request = httptest.NewRequest(http.MethodPost, "/api/token", bytes.NewReader(bodyBytes))
c.Request.Header.Set("Content-Type", "application/json")

c.Set("uid", tt.inputBody.UserID)
tt.given()

rest.CreateApiToken(service)(c)
Expand Down Expand Up @@ -682,7 +685,7 @@ func TestGetApiTokens(t *testing.T) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Params = []gin.Param{{Key: "uid", Value: tt.uid}}

c.Set("uid", tt.uid)
tt.given()

rest.GetApiTokens(service)(c)
Expand Down

0 comments on commit 8e87c1e

Please sign in to comment.