Skip to content

Commit 25189cf

Browse files
authored
Merge pull request #32 from sirosfoundation/fix/correct-fields-in-mongodb-indexes
fix: make sure tenant_id is present in verifiers and issuers mongodb collection instances
2 parents a8cb40e + 8822164 commit 25189cf

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

internal/api/admin_handlers.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package api
22

33
import (
4+
"errors"
45
"fmt"
56
"net/http"
67
"time"
@@ -666,6 +667,10 @@ func (h *AdminHandlers) CreateVerifier(c *gin.Context) {
666667
}
667668

668669
if err := h.store.Verifiers().Create(c.Request.Context(), verifier); err != nil {
670+
if errors.Is(err, storage.ErrAlreadyExists) {
671+
c.JSON(http.StatusConflict, gin.H{"error": "Verifier with this URL already exists in tenant"})
672+
return
673+
}
669674
h.logger.Error("Failed to create verifier", zap.Error(err))
670675
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create verifier"})
671676
return

internal/storage/mongodb/mongodb.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,30 @@ func (s *Store) createIndexes(ctx context.Context) error {
115115
return fmt.Errorf("failed to create challenge indexes: %w", err)
116116
}
117117

118+
// Drop old incorrect issuers collection indexes
119+
_, _ = s.issuers.collection.Indexes().DropOne(ctx, "identifier_1")
120+
118121
// Issuers collection indexes
119122
_, err = s.issuers.collection.Indexes().CreateOne(ctx, mongo.IndexModel{
120-
Keys: bson.D{{Key: "identifier", Value: 1}},
123+
Keys: bson.D{
124+
{Key: "credential_issuer_identifier", Value: 1},
125+
{Key: "tenant_id", Value: 1},
126+
},
121127
Options: options.Index().SetUnique(true),
122128
})
123129
if err != nil {
124130
return fmt.Errorf("failed to create issuer indexes: %w", err)
125131
}
126132

133+
// Drop old incorrect verifiers collection indexes
134+
_, _ = s.verifiers.collection.Indexes().DropOne(ctx, "did_1")
135+
127136
// Verifiers collection indexes
128137
_, err = s.verifiers.collection.Indexes().CreateOne(ctx, mongo.IndexModel{
129-
Keys: bson.D{{Key: "did", Value: 1}},
138+
Keys: bson.D{
139+
{Key: "url", Value: 1},
140+
{Key: "tenant_id", Value: 1},
141+
},
130142
Options: options.Index().SetUnique(true),
131143
})
132144
if err != nil {

0 commit comments

Comments
 (0)