Skip to content

Commit

Permalink
Merge pull request #252 from canonical/css-7621/add-validation-follow-up
Browse files Browse the repository at this point in the history
feat: add validation for remaining endpoints
  • Loading branch information
babakks authored Mar 28, 2024
2 parents 6b3eb80 + 1e8dcb0 commit 6d7434e
Show file tree
Hide file tree
Showing 14 changed files with 1,414 additions and 751 deletions.
76 changes: 45 additions & 31 deletions rebac-admin-backend/v1/identities.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package v1

import (
"encoding/json"
"net/http"

"github.com/canonical/identity-platform-admin-ui/rebac-admin-backend/v1/resources"
Expand Down Expand Up @@ -36,21 +35,25 @@ func (h handler) GetIdentities(w http.ResponseWriter, req *http.Request, params
func (h handler) PostIdentities(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()

identity := new(resources.Identity)
defer req.Body.Close()
body, err := getRequestBodyFromContext(req.Context())
if err != nil {
writeErrorResponse(w, err)
return
}

if err := json.NewDecoder(req.Body).Decode(identity); err != nil {
writeErrorResponse(w, NewValidationError("request doesn't match the expected schema"))
identity, ok := body.(*resources.Identity)
if !ok {
writeErrorResponse(w, NewMissingRequestBodyError(""))
return
}

identity, err := h.Identities.CreateIdentity(ctx, identity)
result, err := h.Identities.CreateIdentity(ctx, identity)
if err != nil {
writeServiceErrorResponse(w, h.IdentitiesErrorMapper, err)
return
}

writeResponse(w, http.StatusCreated, identity)
writeResponse(w, http.StatusCreated, result)
}

// DeleteIdentitiesItem deletes the specified identity.
Expand Down Expand Up @@ -86,26 +89,25 @@ func (h handler) GetIdentitiesItem(w http.ResponseWriter, req *http.Request, id
func (h handler) PutIdentitiesItem(w http.ResponseWriter, req *http.Request, id string) {
ctx := req.Context()

identity := new(resources.Identity)
defer req.Body.Close()

if err := json.NewDecoder(req.Body).Decode(identity); err != nil {
writeErrorResponse(w, NewValidationError("request doesn't match the expected schema"))
body, err := getRequestBodyFromContext(req.Context())
if err != nil {
writeErrorResponse(w, err)
return
}

if id != *identity.Id {
writeErrorResponse(w, NewValidationError("identity ID from path does not match the Identity object"))
identity, ok := body.(*resources.Identity)
if !ok {
writeErrorResponse(w, NewMissingRequestBodyError(""))
return
}

identity, err := h.Identities.UpdateIdentity(ctx, identity)
result, err := h.Identities.UpdateIdentity(ctx, identity)
if err != nil {
writeServiceErrorResponse(w, h.IdentitiesErrorMapper, err)
return
}

writeResponse(w, http.StatusOK, identity)
writeResponse(w, http.StatusOK, result)
}

// GetIdentitiesItemEntitlements returns the list of entitlements for an identity identified by the provided ID.
Expand Down Expand Up @@ -134,15 +136,19 @@ func (h handler) GetIdentitiesItemEntitlements(w http.ResponseWriter, req *http.
func (h handler) PatchIdentitiesItemEntitlements(w http.ResponseWriter, req *http.Request, id string) {
ctx := req.Context()

patchesRequest := new(resources.IdentityEntitlementsPatchRequestBody)
defer req.Body.Close()
body, err := getRequestBodyFromContext(req.Context())
if err != nil {
writeErrorResponse(w, err)
return
}

if err := json.NewDecoder(req.Body).Decode(patchesRequest); err != nil {
writeErrorResponse(w, NewValidationError("request doesn't match the expected schema"))
identityEntitlements, ok := body.(*resources.IdentityEntitlementsPatchRequestBody)
if !ok {
writeErrorResponse(w, NewMissingRequestBodyError(""))
return
}

_, err := h.Identities.PatchIdentityEntitlements(ctx, id, patchesRequest.Patches)
_, err = h.Identities.PatchIdentityEntitlements(ctx, id, identityEntitlements.Patches)
if err != nil {
writeServiceErrorResponse(w, h.IdentitiesErrorMapper, err)
return
Expand Down Expand Up @@ -177,15 +183,19 @@ func (h handler) GetIdentitiesItemGroups(w http.ResponseWriter, req *http.Reques
func (h handler) PatchIdentitiesItemGroups(w http.ResponseWriter, req *http.Request, id string) {
ctx := req.Context()

patchesRequest := new(resources.IdentityGroupsPatchRequestBody)
defer req.Body.Close()
body, err := getRequestBodyFromContext(req.Context())
if err != nil {
writeErrorResponse(w, err)
return
}

if err := json.NewDecoder(req.Body).Decode(patchesRequest); err != nil {
writeErrorResponse(w, NewValidationError("request doesn't match the expected schema"))
identityGroups, ok := body.(*resources.IdentityGroupsPatchRequestBody)
if !ok {
writeErrorResponse(w, NewMissingRequestBodyError(""))
return
}

_, err := h.Identities.PatchIdentityGroups(ctx, id, patchesRequest.Patches)
_, err = h.Identities.PatchIdentityGroups(ctx, id, identityGroups.Patches)
if err != nil {
writeServiceErrorResponse(w, h.IdentitiesErrorMapper, err)
return
Expand Down Expand Up @@ -220,15 +230,19 @@ func (h handler) GetIdentitiesItemRoles(w http.ResponseWriter, req *http.Request
func (h handler) PatchIdentitiesItemRoles(w http.ResponseWriter, req *http.Request, id string) {
ctx := req.Context()

patchesRequest := new(resources.IdentityRolesPatchRequestBody)
defer req.Body.Close()
body, err := getRequestBodyFromContext(req.Context())
if err != nil {
writeErrorResponse(w, err)
return
}

if err := json.NewDecoder(req.Body).Decode(patchesRequest); err != nil {
writeErrorResponse(w, NewValidationError("request doesn't match the expected schema"))
identityRoles, ok := body.(*resources.IdentityRolesPatchRequestBody)
if !ok {
writeErrorResponse(w, NewMissingRequestBodyError(""))
return
}

_, err := h.Identities.PatchIdentityRoles(ctx, id, patchesRequest.Patches)
_, err = h.Identities.PatchIdentityRoles(ctx, id, identityRoles.Patches)
if err != nil {
writeServiceErrorResponse(w, h.IdentitiesErrorMapper, err)
return
Expand Down
Loading

0 comments on commit 6d7434e

Please sign in to comment.