Improve overall SCIM req. handling
This commit is contained in:
@@ -2,6 +2,7 @@ package scim
|
||||
|
||||
import (
|
||||
"github.com/cortezaproject/corteza-server/system/types"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -11,6 +12,17 @@ type (
|
||||
Created time.Time `json:"created"`
|
||||
LastModified *time.Time `json:"lastModified,omitempty"`
|
||||
}
|
||||
|
||||
errorResponse struct {
|
||||
Schemas []string `json:"schemas"`
|
||||
SCIMType string `json:"scimType,omitempty"`
|
||||
Detail string `json:"detail,omitempty"`
|
||||
Status int `json:"status,string"`
|
||||
}
|
||||
)
|
||||
|
||||
const (
|
||||
urnError = "urn:ietf:params:scim:api:messages:2.0:Error"
|
||||
)
|
||||
|
||||
func newUserMetaResponse(u *types.User) *metaResponse {
|
||||
@@ -22,3 +34,30 @@ func newUserMetaResponse(u *types.User) *metaResponse {
|
||||
|
||||
return rsp
|
||||
}
|
||||
|
||||
func newGroupMetaResponse(u *types.Role) *metaResponse {
|
||||
rsp := &metaResponse{
|
||||
ResourceType: "Group",
|
||||
Created: u.CreatedAt,
|
||||
LastModified: u.UpdatedAt,
|
||||
}
|
||||
|
||||
return rsp
|
||||
}
|
||||
|
||||
func newErrorResonse(httpStatus int, err error) *errorResponse {
|
||||
if httpStatus == 0 {
|
||||
httpStatus = http.StatusInternalServerError
|
||||
}
|
||||
|
||||
er := &errorResponse{
|
||||
Schemas: []string{urnError},
|
||||
Status: httpStatus,
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
er.Detail = err.Error()
|
||||
}
|
||||
|
||||
return er
|
||||
}
|
||||
|
||||
+6
-1
@@ -6,9 +6,14 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func send(w http.ResponseWriter, payload interface{}) {
|
||||
func send(w http.ResponseWriter, status int, payload interface{}) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
if err := json.NewEncoder(w).Encode(payload); err != nil {
|
||||
log.Error("could not encode payload", zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
func sendError(w http.ResponseWriter, err *errorResponse) {
|
||||
send(w, err.Status, err)
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package scim
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/cortezaproject/corteza-server/pkg/api"
|
||||
"github.com/cortezaproject/corteza-server/pkg/auth"
|
||||
"github.com/cortezaproject/corteza-server/pkg/errors"
|
||||
"github.com/cortezaproject/corteza-server/system/service"
|
||||
@@ -33,13 +32,13 @@ func (h usersHandler) get(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if u, err := svc.FindByID(id); err != nil {
|
||||
errors.ServeHTTP(w, r, err, !api.DebugFromContext(r.Context()))
|
||||
sendError(w, newErrorResonse(http.StatusBadRequest, err))
|
||||
return
|
||||
} else {
|
||||
send(w, newUserResourceResponse(u))
|
||||
send(w, http.StatusOK, newUserResourceResponse(u))
|
||||
}
|
||||
|
||||
w.WriteHeader(200)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func (h usersHandler) create(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -50,10 +49,9 @@ func (h usersHandler) create(w http.ResponseWriter, r *http.Request) {
|
||||
)
|
||||
|
||||
if u, err := h.createFromJSON(ctx, r.Body); err != nil {
|
||||
errors.ServeHTTP(w, r, err, !api.DebugFromContext(r.Context()))
|
||||
sendError(w, newErrorResonse(http.StatusBadRequest, err))
|
||||
} else {
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
send(w, newUserResourceResponse(u))
|
||||
send(w, http.StatusCreated, newUserResourceResponse(u))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,10 +97,9 @@ func (h usersHandler) replace(w http.ResponseWriter, r *http.Request) {
|
||||
)
|
||||
|
||||
if u, err := h.updateFromJSON(ctx, userID, r.Body); err != nil {
|
||||
errors.ServeHTTP(w, r, err, !api.DebugFromContext(r.Context()))
|
||||
sendError(w, newErrorResonse(http.StatusBadRequest, err))
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
send(w, newUserResourceResponse(u))
|
||||
send(w, http.StatusOK, newUserResourceResponse(u))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,7 +134,7 @@ func (h usersHandler) delete(w http.ResponseWriter, r *http.Request) {
|
||||
)
|
||||
|
||||
if err := svc.Delete(userID); err != nil {
|
||||
send(w, err)
|
||||
sendError(w, newErrorResonse(http.StatusBadRequest, err))
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user