Skip to content

Commit

Permalink
Fix #470 Add validation to scopes field in apikey
Browse files Browse the repository at this point in the history
  • Loading branch information
albinpa authored and georgepadayatti committed Nov 7, 2023
1 parent 8831b3e commit df50ffb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
19 changes: 19 additions & 0 deletions internal/apikey/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,22 @@ func Decode(apiKey string) (claims Claims, err error) {
}
return claims, nil
}

func ValidateScopes(scopes []string) bool {
allowedScopes := []string{"service", "audit", "config", "onboard"}

for _, scope := range scopes {
found := false
for _, allowed := range allowedScopes {
if scope == allowed {
found = true
break
}
}
if !found {
return false
}
}

return true
}
8 changes: 8 additions & 0 deletions internal/handler/v2/config/apikey/config_create_apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ func ConfigCreateApiKey(w http.ResponseWriter, r *http.Request) {
return
}

// validate scopes
validScopes := apikey.ValidateScopes(apiKeyReq.Apikey.Scopes)
if !validScopes {
m := "Invalid scopes provided for creating api key"
common.HandleErrorV2(w, http.StatusBadRequest, m, err)
return
}

// Repository
apiKeyRepo := apikey.ApiKeyRepository{}
apiKeyRepo.Init(organisationId)
Expand Down
8 changes: 8 additions & 0 deletions internal/handler/v2/config/apikey/config_update_apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ func ConfigUpdateApiKey(w http.ResponseWriter, r *http.Request) {
return
}

// validate scopes
validScopes := apikey.ValidateScopes(apiKeyReq.Apikey.Scopes)
if !validScopes {
m := "Invalid scopes provided for updating api key"
common.HandleErrorV2(w, http.StatusBadRequest, m, err)
return
}

// Repository
apiKeyRepo := apikey.ApiKeyRepository{}
apiKeyRepo.Init(organisationId)
Expand Down

0 comments on commit df50ffb

Please sign in to comment.