forked from longtao.wu/droboticflow
680 lines
24 KiB
Go
680 lines
24 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"crypto/subtle"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"net/http"
|
|
"sort"
|
|
"strings"
|
|
|
|
"droboticflow/internal/domain"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
type externalBusinessWriteTokenCreateRequest struct {
|
|
Name string `json:"name"`
|
|
Description string `json:"description,omitempty"`
|
|
Enabled *bool `json:"enabled"`
|
|
Scopes []domain.ExternalBusinessWriteScope `json:"scopes"`
|
|
}
|
|
|
|
type externalBusinessWriteTokenUpdateRequest struct {
|
|
Name *string `json:"name"`
|
|
Description *string `json:"description"`
|
|
Enabled *bool `json:"enabled"`
|
|
Scopes *[]domain.ExternalBusinessWriteScope `json:"scopes"`
|
|
}
|
|
|
|
type externalBusinessWriteCreateRequest struct {
|
|
ConnectionID string `json:"connectionId"`
|
|
Collection string `json:"collection"`
|
|
Document map[string]any `json:"document"`
|
|
}
|
|
|
|
type externalBusinessWritePatchRequest struct {
|
|
ConnectionID string `json:"connectionId"`
|
|
Collection string `json:"collection"`
|
|
Patch map[string]any `json:"patch"`
|
|
}
|
|
|
|
type externalBusinessWriteUpsertRequest struct {
|
|
ConnectionID string `json:"connectionId"`
|
|
Collection string `json:"collection"`
|
|
Match map[string]any `json:"match"`
|
|
Patch map[string]any `json:"patch"`
|
|
SetOnInsert map[string]any `json:"setOnInsert,omitempty"`
|
|
}
|
|
|
|
type externalBusinessWriteTokenMutationResult struct {
|
|
Token domain.ExternalBusinessWriteTokenRecord `json:"token"`
|
|
APIKey string `json:"apiKey,omitempty"`
|
|
}
|
|
|
|
type externalBusinessWriteTokensResult struct {
|
|
Items []domain.ExternalBusinessWriteTokenRecord `json:"items"`
|
|
}
|
|
|
|
type externalBusinessWriteDocumentResult struct {
|
|
ConnectionID string `json:"connectionId"`
|
|
Collection string `json:"collection"`
|
|
Operation string `json:"operation"`
|
|
Document map[string]any `json:"document"`
|
|
}
|
|
|
|
type externalBusinessWriteTokenContextKey struct{}
|
|
|
|
func externalBusinessWriteAPIKeyHash(value string) string {
|
|
sum := sha256.Sum256([]byte(strings.TrimSpace(value)))
|
|
return hex.EncodeToString(sum[:])
|
|
}
|
|
|
|
func generateExternalBusinessWriteAPIKey() (string, string, string, error) {
|
|
randomBytes := make([]byte, 24)
|
|
if _, err := rand.Read(randomBytes); err != nil {
|
|
return "", "", "", err
|
|
}
|
|
apiKey := "dbw_" + hex.EncodeToString(randomBytes)
|
|
return apiKey, externalBusinessWriteAPIKeyHash(apiKey), maskedSecretPreview(apiKey), nil
|
|
}
|
|
|
|
func sanitizeExternalBusinessWriteToken(item domain.ExternalBusinessWriteTokenRecord) domain.ExternalBusinessWriteTokenRecord {
|
|
sanitized := item
|
|
sanitized.TokenValue = ""
|
|
sanitized.TokenHash = ""
|
|
if sanitized.Scopes == nil {
|
|
sanitized.Scopes = []domain.ExternalBusinessWriteScope{}
|
|
}
|
|
return sanitized
|
|
}
|
|
|
|
func sanitizeExternalBusinessWriteTokens(items []domain.ExternalBusinessWriteTokenRecord) []domain.ExternalBusinessWriteTokenRecord {
|
|
sanitized := make([]domain.ExternalBusinessWriteTokenRecord, 0, len(items))
|
|
for _, item := range items {
|
|
sanitized = append(sanitized, sanitizeExternalBusinessWriteToken(item))
|
|
}
|
|
sort.Slice(sanitized, func(left int, right int) bool {
|
|
return sanitized[left].CreatedAt > sanitized[right].CreatedAt
|
|
})
|
|
return sanitized
|
|
}
|
|
|
|
func normalizeExternalBusinessWriteScopes(items []domain.ExternalBusinessWriteScope) []domain.ExternalBusinessWriteScope {
|
|
if len(items) == 0 {
|
|
return []domain.ExternalBusinessWriteScope{}
|
|
}
|
|
normalized := make([]domain.ExternalBusinessWriteScope, 0, len(items))
|
|
seenConnections := map[string]int{}
|
|
for _, item := range items {
|
|
connectionID := strings.TrimSpace(item.ConnectionID)
|
|
if connectionID == "" {
|
|
continue
|
|
}
|
|
collections := make([]string, 0, len(item.Collections))
|
|
seenCollections := map[string]struct{}{}
|
|
for _, collection := range item.Collections {
|
|
trimmed := strings.TrimSpace(collection)
|
|
if trimmed == "" {
|
|
continue
|
|
}
|
|
if _, exists := seenCollections[trimmed]; exists {
|
|
continue
|
|
}
|
|
seenCollections[trimmed] = struct{}{}
|
|
collections = append(collections, trimmed)
|
|
}
|
|
sort.Strings(collections)
|
|
if len(collections) == 0 {
|
|
continue
|
|
}
|
|
if index, exists := seenConnections[connectionID]; exists {
|
|
existingCollections := normalized[index].Collections
|
|
for _, collection := range collections {
|
|
if slicesContains(existingCollections, collection) {
|
|
continue
|
|
}
|
|
existingCollections = append(existingCollections, collection)
|
|
}
|
|
sort.Strings(existingCollections)
|
|
normalized[index].Collections = existingCollections
|
|
continue
|
|
}
|
|
seenConnections[connectionID] = len(normalized)
|
|
normalized = append(normalized, domain.ExternalBusinessWriteScope{
|
|
ConnectionID: connectionID,
|
|
Collections: collections,
|
|
})
|
|
}
|
|
return normalized
|
|
}
|
|
|
|
func slicesContains(items []string, target string) bool {
|
|
for _, item := range items {
|
|
if item == target {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (server *server) validateExternalBusinessWriteScopes(ctx context.Context, scopes []domain.ExternalBusinessWriteScope) []validationIssue {
|
|
issues := make([]validationIssue, 0)
|
|
normalized := normalizeExternalBusinessWriteScopes(scopes)
|
|
if len(normalized) == 0 {
|
|
return []validationIssue{{Field: "scopes", Message: "at least one connectionId and collection whitelist is required"}}
|
|
}
|
|
for index, scope := range normalized {
|
|
if _, err := server.getMongoConnection(ctx, scope.ConnectionID); err != nil {
|
|
issues = append(issues, validationIssue{
|
|
Field: fmt.Sprintf("scopes[%d].connectionId", index),
|
|
Message: "connectionId must reference an existing mongo connection",
|
|
})
|
|
}
|
|
if len(scope.Collections) == 0 {
|
|
issues = append(issues, validationIssue{
|
|
Field: fmt.Sprintf("scopes[%d].collections", index),
|
|
Message: "at least one collection is required",
|
|
})
|
|
}
|
|
for collectionIndex, collection := range scope.Collections {
|
|
if issue := validateDatabaseCollectionName(
|
|
fmt.Sprintf("scopes[%d].collections[%d]", index, collectionIndex),
|
|
collection,
|
|
); issue != nil {
|
|
issues = append(issues, *issue)
|
|
}
|
|
}
|
|
}
|
|
return issues
|
|
}
|
|
|
|
func validateExternalBusinessWriteObject(field string, value map[string]any, requireNonEmpty bool) []validationIssue {
|
|
if value == nil {
|
|
if requireNonEmpty {
|
|
return []validationIssue{{Field: field, Message: field + " is required"}}
|
|
}
|
|
return nil
|
|
}
|
|
if requireNonEmpty && len(value) == 0 {
|
|
return []validationIssue{{Field: field, Message: field + " is required"}}
|
|
}
|
|
return validateExternalBusinessWriteValue(field, value)
|
|
}
|
|
|
|
func validateExternalBusinessWriteValue(field string, value any) []validationIssue {
|
|
issues := make([]validationIssue, 0)
|
|
switch typed := value.(type) {
|
|
case map[string]any:
|
|
for key, item := range typed {
|
|
trimmedKey := strings.TrimSpace(key)
|
|
itemField := field
|
|
if trimmedKey != "" {
|
|
itemField = field + "." + trimmedKey
|
|
}
|
|
if trimmedKey == "" {
|
|
issues = append(issues, validationIssue{
|
|
Field: field,
|
|
Message: field + " cannot contain empty field names",
|
|
})
|
|
continue
|
|
}
|
|
if strings.ContainsRune(trimmedKey, '\x00') {
|
|
issues = append(issues, validationIssue{
|
|
Field: itemField,
|
|
Message: field + " cannot contain NUL bytes",
|
|
})
|
|
continue
|
|
}
|
|
if strings.HasPrefix(trimmedKey, "$") {
|
|
issues = append(issues, validationIssue{
|
|
Field: itemField,
|
|
Message: field + " cannot contain Mongo operator keys",
|
|
})
|
|
continue
|
|
}
|
|
issues = append(issues, validateExternalBusinessWriteValue(itemField, item)...)
|
|
}
|
|
case []any:
|
|
for index, item := range typed {
|
|
issues = append(issues, validateExternalBusinessWriteValue(fmt.Sprintf("%s[%d]", field, index), item)...)
|
|
}
|
|
case []map[string]any:
|
|
for index, item := range typed {
|
|
issues = append(issues, validateExternalBusinessWriteValue(fmt.Sprintf("%s[%d]", field, index), item)...)
|
|
}
|
|
}
|
|
return issues
|
|
}
|
|
|
|
func externalBusinessWriteTokenEnabled(value *bool) bool {
|
|
if value == nil {
|
|
return true
|
|
}
|
|
return *value
|
|
}
|
|
|
|
func externalBusinessWriteSettings(settings *domain.SettingsRecord) *domain.ExternalBusinessWriteSettings {
|
|
if settings == nil {
|
|
return &domain.ExternalBusinessWriteSettings{Tokens: []domain.ExternalBusinessWriteTokenRecord{}}
|
|
}
|
|
if settings.ExternalBusinessWrite.Tokens == nil {
|
|
settings.ExternalBusinessWrite.Tokens = []domain.ExternalBusinessWriteTokenRecord{}
|
|
}
|
|
return &settings.ExternalBusinessWrite
|
|
}
|
|
|
|
func findExternalBusinessWriteTokenByID(settings *domain.SettingsRecord, tokenID string) (int, *domain.ExternalBusinessWriteTokenRecord) {
|
|
config := externalBusinessWriteSettings(settings)
|
|
for index := range config.Tokens {
|
|
if config.Tokens[index].ID == tokenID {
|
|
return index, &config.Tokens[index]
|
|
}
|
|
}
|
|
return -1, nil
|
|
}
|
|
|
|
func findExternalBusinessWriteTokenByAPIKey(settings *domain.SettingsRecord, apiKey string) *domain.ExternalBusinessWriteTokenRecord {
|
|
config := externalBusinessWriteSettings(settings)
|
|
hash := externalBusinessWriteAPIKeyHash(apiKey)
|
|
for index := range config.Tokens {
|
|
item := &config.Tokens[index]
|
|
if !item.Enabled || strings.TrimSpace(item.TokenHash) == "" {
|
|
continue
|
|
}
|
|
if subtle.ConstantTimeCompare([]byte(item.TokenHash), []byte(hash)) == 1 {
|
|
return item
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func currentExternalBusinessWriteToken(request *http.Request) *domain.ExternalBusinessWriteTokenRecord {
|
|
item, _ := request.Context().Value(externalBusinessWriteTokenContextKey{}).(*domain.ExternalBusinessWriteTokenRecord)
|
|
return item
|
|
}
|
|
|
|
func externalBusinessWriteAllowed(token *domain.ExternalBusinessWriteTokenRecord, connectionID string, collection string) bool {
|
|
if token == nil {
|
|
return false
|
|
}
|
|
targetConnectionID := strings.TrimSpace(connectionID)
|
|
targetCollection := strings.TrimSpace(collection)
|
|
for _, scope := range token.Scopes {
|
|
if strings.TrimSpace(scope.ConnectionID) != targetConnectionID {
|
|
continue
|
|
}
|
|
for _, allowedCollection := range scope.Collections {
|
|
if strings.TrimSpace(allowedCollection) == targetCollection {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (server *server) touchExternalBusinessWriteToken(ctx context.Context, tokenID string) {
|
|
settings, err := server.store.GetSettings(ctx)
|
|
if err != nil {
|
|
return
|
|
}
|
|
index, item := findExternalBusinessWriteTokenByID(settings, tokenID)
|
|
if item == nil {
|
|
return
|
|
}
|
|
now := nowISO()
|
|
settings.ExternalBusinessWrite.Tokens[index].LastUsedAt = now
|
|
settings.ExternalBusinessWrite.Tokens[index].UpdatedAt = now
|
|
_ = server.store.UpsertSettings(ctx, *settings)
|
|
}
|
|
|
|
func (server *server) externalBusinessWriteAuth(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
|
apiKey := strings.TrimSpace(request.Header.Get("X-API-Key"))
|
|
limitKey := server.clientRateLimitKey(request, "external-write:"+externalBusinessWriteAPIKeyHash(apiKey))
|
|
if !server.externalWriteLimiter.allow(limitKey) {
|
|
renderJSON(writer, http.StatusTooManyRequests, map[string]string{"error": "too many external write requests"})
|
|
return
|
|
}
|
|
if apiKey == "" {
|
|
renderJSON(writer, http.StatusUnauthorized, map[string]string{"error": "X-API-Key header is required"})
|
|
return
|
|
}
|
|
settings, err := server.store.GetSettings(request.Context())
|
|
if err != nil {
|
|
renderError(writer, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
token := findExternalBusinessWriteTokenByAPIKey(settings, apiKey)
|
|
if token == nil {
|
|
renderJSON(writer, http.StatusUnauthorized, map[string]string{"error": "invalid API key"})
|
|
return
|
|
}
|
|
tokenCopy := *token
|
|
ctx := context.WithValue(request.Context(), externalBusinessWriteTokenContextKey{}, &tokenCopy)
|
|
next.ServeHTTP(writer, request.WithContext(ctx))
|
|
})
|
|
}
|
|
|
|
func (server *server) handleExternalBusinessWriteTokensList(writer http.ResponseWriter, request *http.Request) {
|
|
settings, err := server.store.GetSettings(request.Context())
|
|
if err != nil {
|
|
renderError(writer, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
tokens := server.filterExternalBusinessWriteTokensForRequest(request, settings.ExternalBusinessWrite.Tokens)
|
|
renderJSON(writer, http.StatusOK, externalBusinessWriteTokensResult{
|
|
Items: sanitizeExternalBusinessWriteTokens(tokens),
|
|
})
|
|
}
|
|
|
|
func (server *server) handleExternalBusinessWriteTokensCreate(writer http.ResponseWriter, request *http.Request) {
|
|
var payload externalBusinessWriteTokenCreateRequest
|
|
if !decodeJSON(writer, request, &payload) {
|
|
return
|
|
}
|
|
normalizedScopes := normalizeExternalBusinessWriteScopes(payload.Scopes)
|
|
issues := make([]validationIssue, 0)
|
|
if strings.TrimSpace(payload.Name) == "" {
|
|
issues = append(issues, validationIssue{Field: "name", Message: "name is required"})
|
|
}
|
|
issues = append(issues, server.validateExternalBusinessWriteScopes(request.Context(), normalizedScopes)...)
|
|
if len(issues) > 0 {
|
|
renderValidationError(writer, issues...)
|
|
return
|
|
}
|
|
|
|
apiKey, tokenHash, tokenPreview, err := generateExternalBusinessWriteAPIKey()
|
|
if err != nil {
|
|
renderError(writer, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
settings, err := server.store.GetSettings(request.Context())
|
|
if err != nil {
|
|
renderError(writer, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
now := nowISO()
|
|
item := domain.ExternalBusinessWriteTokenRecord{
|
|
ID: domain.NewID("exttoken"),
|
|
OrganizationID: server.requestOrganizationID(request),
|
|
Name: strings.TrimSpace(payload.Name),
|
|
Description: strings.TrimSpace(payload.Description),
|
|
Enabled: externalBusinessWriteTokenEnabled(payload.Enabled),
|
|
TokenHash: tokenHash,
|
|
TokenPreview: tokenPreview,
|
|
Scopes: normalizedScopes,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
settings.ExternalBusinessWrite.Tokens = append(settings.ExternalBusinessWrite.Tokens, item)
|
|
if err := server.store.UpsertSettings(request.Context(), *settings); err != nil {
|
|
renderError(writer, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
renderJSON(writer, http.StatusCreated, externalBusinessWriteTokenMutationResult{
|
|
Token: sanitizeExternalBusinessWriteToken(item),
|
|
APIKey: apiKey,
|
|
})
|
|
}
|
|
|
|
func (server *server) handleExternalBusinessWriteTokensUpdate(writer http.ResponseWriter, request *http.Request) {
|
|
settings, err := server.store.GetSettings(request.Context())
|
|
if err != nil {
|
|
renderError(writer, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
index, item := findExternalBusinessWriteTokenByID(settings, chi.URLParam(request, "id"))
|
|
if item == nil {
|
|
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "External write token not found"})
|
|
return
|
|
}
|
|
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "External write token") {
|
|
return
|
|
}
|
|
var payload externalBusinessWriteTokenUpdateRequest
|
|
if !decodeJSON(writer, request, &payload) {
|
|
return
|
|
}
|
|
updated := *item
|
|
if payload.Name != nil {
|
|
updated.Name = strings.TrimSpace(*payload.Name)
|
|
}
|
|
if payload.Description != nil {
|
|
updated.Description = strings.TrimSpace(*payload.Description)
|
|
}
|
|
if payload.Enabled != nil {
|
|
updated.Enabled = *payload.Enabled
|
|
}
|
|
if payload.Scopes != nil {
|
|
updated.Scopes = normalizeExternalBusinessWriteScopes(*payload.Scopes)
|
|
}
|
|
issues := make([]validationIssue, 0)
|
|
if strings.TrimSpace(updated.Name) == "" {
|
|
issues = append(issues, validationIssue{Field: "name", Message: "name is required"})
|
|
}
|
|
issues = append(issues, server.validateExternalBusinessWriteScopes(request.Context(), updated.Scopes)...)
|
|
if len(issues) > 0 {
|
|
renderValidationError(writer, issues...)
|
|
return
|
|
}
|
|
updated.UpdatedAt = nowISO()
|
|
settings.ExternalBusinessWrite.Tokens[index] = updated
|
|
if err := server.store.UpsertSettings(request.Context(), *settings); err != nil {
|
|
renderError(writer, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
renderJSON(writer, http.StatusOK, externalBusinessWriteTokenMutationResult{
|
|
Token: sanitizeExternalBusinessWriteToken(updated),
|
|
})
|
|
}
|
|
|
|
func (server *server) handleExternalBusinessWriteTokensReveal(writer http.ResponseWriter, request *http.Request) {
|
|
settings, err := server.store.GetSettings(request.Context())
|
|
if err != nil {
|
|
renderError(writer, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
_, item := findExternalBusinessWriteTokenByID(settings, chi.URLParam(request, "id"))
|
|
if item == nil {
|
|
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "External write token not found"})
|
|
return
|
|
}
|
|
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "External write token") {
|
|
return
|
|
}
|
|
renderJSON(writer, http.StatusGone, map[string]string{"error": "API key is only shown once during create or rotate"})
|
|
}
|
|
|
|
func (server *server) handleExternalBusinessWriteTokensRotate(writer http.ResponseWriter, request *http.Request) {
|
|
settings, err := server.store.GetSettings(request.Context())
|
|
if err != nil {
|
|
renderError(writer, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
index, item := findExternalBusinessWriteTokenByID(settings, chi.URLParam(request, "id"))
|
|
if item == nil {
|
|
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "External write token not found"})
|
|
return
|
|
}
|
|
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "External write token") {
|
|
return
|
|
}
|
|
apiKey, tokenHash, tokenPreview, err := generateExternalBusinessWriteAPIKey()
|
|
if err != nil {
|
|
renderError(writer, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
updated := *item
|
|
updated.TokenValue = ""
|
|
updated.TokenHash = tokenHash
|
|
updated.TokenPreview = tokenPreview
|
|
updated.UpdatedAt = nowISO()
|
|
settings.ExternalBusinessWrite.Tokens[index] = updated
|
|
if err := server.store.UpsertSettings(request.Context(), *settings); err != nil {
|
|
renderError(writer, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
renderJSON(writer, http.StatusOK, externalBusinessWriteTokenMutationResult{
|
|
Token: sanitizeExternalBusinessWriteToken(updated),
|
|
APIKey: apiKey,
|
|
})
|
|
}
|
|
|
|
func (server *server) handleExternalBusinessWriteTokensDelete(writer http.ResponseWriter, request *http.Request) {
|
|
settings, err := server.store.GetSettings(request.Context())
|
|
if err != nil {
|
|
renderError(writer, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
index, item := findExternalBusinessWriteTokenByID(settings, chi.URLParam(request, "id"))
|
|
if item == nil {
|
|
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "External write token not found"})
|
|
return
|
|
}
|
|
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "External write token") {
|
|
return
|
|
}
|
|
settings.ExternalBusinessWrite.Tokens = append(settings.ExternalBusinessWrite.Tokens[:index], settings.ExternalBusinessWrite.Tokens[index+1:]...)
|
|
if err := server.store.UpsertSettings(request.Context(), *settings); err != nil {
|
|
renderError(writer, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
writer.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func validateExternalBusinessWriteTarget(connectionID string, collection string) []validationIssue {
|
|
issues := make([]validationIssue, 0, 2)
|
|
if strings.TrimSpace(connectionID) == "" {
|
|
issues = append(issues, validationIssue{Field: "connectionId", Message: "connectionId is required"})
|
|
}
|
|
if issue := validateDatabaseCollectionName("collection", collection); issue != nil {
|
|
issues = append(issues, *issue)
|
|
}
|
|
return issues
|
|
}
|
|
|
|
func (server *server) renderExternalBusinessWriteForbidden(writer http.ResponseWriter) {
|
|
renderJSON(writer, http.StatusForbidden, map[string]string{"error": "token is not allowed to write to the requested connection or collection"})
|
|
}
|
|
|
|
func (server *server) handleExternalBusinessWriteCreate(writer http.ResponseWriter, request *http.Request) {
|
|
var payload externalBusinessWriteCreateRequest
|
|
if !decodeJSON(writer, request, &payload) {
|
|
return
|
|
}
|
|
issues := validateExternalBusinessWriteTarget(payload.ConnectionID, payload.Collection)
|
|
issues = append(issues, validateExternalBusinessWriteObject("document", payload.Document, true)...)
|
|
if len(issues) > 0 {
|
|
renderValidationError(writer, issues...)
|
|
return
|
|
}
|
|
if !externalBusinessWriteAllowed(currentExternalBusinessWriteToken(request), payload.ConnectionID, payload.Collection) {
|
|
server.renderExternalBusinessWriteForbidden(writer)
|
|
return
|
|
}
|
|
browserStore, _, cleanup, err := server.resolveBusinessDatabaseStore(request.Context(), payload.ConnectionID)
|
|
if err != nil {
|
|
renderValidationError(writer, validationIssue{Field: "connectionId", Message: err.Error()})
|
|
return
|
|
}
|
|
defer cleanup()
|
|
document, err := browserStore.InsertDatabaseDocument(request.Context(), payload.Collection, payload.Document)
|
|
if err != nil {
|
|
renderDatabaseBrowserWriteError(writer, err)
|
|
return
|
|
}
|
|
if token := currentExternalBusinessWriteToken(request); token != nil {
|
|
server.touchExternalBusinessWriteToken(request.Context(), token.ID)
|
|
}
|
|
renderJSON(writer, http.StatusCreated, externalBusinessWriteDocumentResult{
|
|
ConnectionID: strings.TrimSpace(payload.ConnectionID),
|
|
Collection: strings.TrimSpace(payload.Collection),
|
|
Operation: "created",
|
|
Document: document,
|
|
})
|
|
}
|
|
|
|
func (server *server) handleExternalBusinessWritePatch(writer http.ResponseWriter, request *http.Request) {
|
|
var payload externalBusinessWritePatchRequest
|
|
if !decodeJSON(writer, request, &payload) {
|
|
return
|
|
}
|
|
issues := validateExternalBusinessWriteTarget(payload.ConnectionID, payload.Collection)
|
|
issues = append(issues, validateExternalBusinessWriteObject("patch", payload.Patch, true)...)
|
|
if len(issues) > 0 {
|
|
renderValidationError(writer, issues...)
|
|
return
|
|
}
|
|
if !externalBusinessWriteAllowed(currentExternalBusinessWriteToken(request), payload.ConnectionID, payload.Collection) {
|
|
server.renderExternalBusinessWriteForbidden(writer)
|
|
return
|
|
}
|
|
browserStore, _, cleanup, err := server.resolveBusinessDatabaseStore(request.Context(), payload.ConnectionID)
|
|
if err != nil {
|
|
renderValidationError(writer, validationIssue{Field: "connectionId", Message: err.Error()})
|
|
return
|
|
}
|
|
defer cleanup()
|
|
document, err := browserStore.PatchDatabaseDocument(request.Context(), payload.Collection, chi.URLParam(request, "documentId"), payload.Patch)
|
|
if err != nil {
|
|
renderDatabaseBrowserWriteError(writer, err)
|
|
return
|
|
}
|
|
if token := currentExternalBusinessWriteToken(request); token != nil {
|
|
server.touchExternalBusinessWriteToken(request.Context(), token.ID)
|
|
}
|
|
renderJSON(writer, http.StatusOK, externalBusinessWriteDocumentResult{
|
|
ConnectionID: strings.TrimSpace(payload.ConnectionID),
|
|
Collection: strings.TrimSpace(payload.Collection),
|
|
Operation: "updated",
|
|
Document: document,
|
|
})
|
|
}
|
|
|
|
func (server *server) handleExternalBusinessWriteUpsert(writer http.ResponseWriter, request *http.Request) {
|
|
var payload externalBusinessWriteUpsertRequest
|
|
if !decodeJSON(writer, request, &payload) {
|
|
return
|
|
}
|
|
issues := validateExternalBusinessWriteTarget(payload.ConnectionID, payload.Collection)
|
|
issues = append(issues, validateExternalBusinessWriteObject("match", payload.Match, true)...)
|
|
issues = append(issues, validateExternalBusinessWriteObject("patch", payload.Patch, true)...)
|
|
issues = append(issues, validateExternalBusinessWriteObject("setOnInsert", payload.SetOnInsert, false)...)
|
|
if len(issues) > 0 {
|
|
renderValidationError(writer, issues...)
|
|
return
|
|
}
|
|
if !externalBusinessWriteAllowed(currentExternalBusinessWriteToken(request), payload.ConnectionID, payload.Collection) {
|
|
server.renderExternalBusinessWriteForbidden(writer)
|
|
return
|
|
}
|
|
browserStore, _, cleanup, err := server.resolveBusinessDatabaseStore(request.Context(), payload.ConnectionID)
|
|
if err != nil {
|
|
renderValidationError(writer, validationIssue{Field: "connectionId", Message: err.Error()})
|
|
return
|
|
}
|
|
defer cleanup()
|
|
document, created, err := browserStore.UpsertDatabaseDocument(request.Context(), payload.Collection, payload.Match, payload.Patch, payload.SetOnInsert)
|
|
if err != nil {
|
|
renderDatabaseBrowserWriteError(writer, err)
|
|
return
|
|
}
|
|
if token := currentExternalBusinessWriteToken(request); token != nil {
|
|
server.touchExternalBusinessWriteToken(request.Context(), token.ID)
|
|
}
|
|
operation := "updated"
|
|
if created {
|
|
operation = "created"
|
|
}
|
|
renderJSON(writer, http.StatusOK, externalBusinessWriteDocumentResult{
|
|
ConnectionID: strings.TrimSpace(payload.ConnectionID),
|
|
Collection: strings.TrimSpace(payload.Collection),
|
|
Operation: operation,
|
|
Document: document,
|
|
})
|
|
}
|