Skip to content

Commit

Permalink
Added user check on list and get handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
catttam committed Jan 29, 2024
1 parent 9e1a744 commit b568d5f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
25 changes: 24 additions & 1 deletion pkg/handlers/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package handlers

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
Expand All @@ -32,6 +33,28 @@ func MakeListHandler(back types.ServerlessBackend) gin.HandlerFunc {
return
}

c.JSON(http.StatusOK, services)
uidOrigin, uidExists := c.Get("uidOrigin")
if !uidExists {
c.String(http.StatusInternalServerError, fmt.Sprintln("Missing EGI user uid"))
}

uid, uidParsed := uidOrigin.(string)

if !uidParsed {
c.String(http.StatusInternalServerError, fmt.Sprintf("Error parsing uid origin: %v", uidParsed))
return
}

var allowedServicesForUser []*types.Service
for _, service := range services {
for _, id := range service.AllowedUsers {
if uid == id {
allowedServicesForUser = append(allowedServicesForUser, service)
break
}
}
}

c.JSON(http.StatusOK, allowedServicesForUser)
}
}
27 changes: 27 additions & 0 deletions pkg/handlers/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package handlers

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
Expand All @@ -28,6 +29,7 @@ import (
func MakeReadHandler(back types.ServerlessBackend) gin.HandlerFunc {
return func(c *gin.Context) {
service, err := back.ReadService(c.Param("serviceName"))

if err != nil {
// Check if error is caused because the service is not found
if errors.IsNotFound(err) || errors.IsGone(err) {
Expand All @@ -38,6 +40,31 @@ func MakeReadHandler(back types.ServerlessBackend) gin.HandlerFunc {
return
}

uidOrigin, uidExists := c.Get("uidOrigin")
if !uidExists {
c.String(http.StatusInternalServerError, fmt.Sprintln("Missing EGI user uid"))
}

uid, uidParsed := uidOrigin.(string)

if !uidParsed {
c.String(http.StatusInternalServerError, fmt.Sprintf("Error parsing uid origin: %v", uidParsed))
return
}

var isAllowed bool
for _, id := range service.AllowedUsers {
if uid == id {
isAllowed = true
break
}
}

if !isAllowed {
c.String(http.StatusForbidden, "User %s doesn't have permision to get this service", uid)
return
}

c.JSON(http.StatusOK, service)
}
}

0 comments on commit b568d5f

Please sign in to comment.