-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathlink.go
432 lines (373 loc) · 12.9 KB
/
link.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package services
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/url"
"time"
"github.com/google/uuid"
"github.com/iden3/go-iden3-core/v2/w3c"
"github.com/iden3/go-schema-processor/v2/verifiable"
"github.com/iden3/iden3comm/v2/packers"
"github.com/iden3/iden3comm/v2/protocol"
"github.com/jackc/pgx/v4"
"github.com/polygonid/sh-id-platform/internal/common"
"github.com/polygonid/sh-id-platform/internal/core/domain"
"github.com/polygonid/sh-id-platform/internal/core/event"
"github.com/polygonid/sh-id-platform/internal/core/ports"
"github.com/polygonid/sh-id-platform/internal/db"
"github.com/polygonid/sh-id-platform/internal/jsonschema"
"github.com/polygonid/sh-id-platform/internal/loader"
"github.com/polygonid/sh-id-platform/internal/log"
"github.com/polygonid/sh-id-platform/internal/repositories"
linkState "github.com/polygonid/sh-id-platform/pkg/link"
"github.com/polygonid/sh-id-platform/pkg/pubsub"
)
var (
// ErrLinkAlreadyActive link is already active
ErrLinkAlreadyActive = errors.New("link is already active")
// ErrLinkAlreadyInactive link is already inactive
ErrLinkAlreadyInactive = errors.New("link is already inactive")
// ErrLinkAlreadyExpired - link already expired
ErrLinkAlreadyExpired = errors.New("cannot issue a credential for an expired link")
// ErrLinkMaxExceeded - link max exceeded
ErrLinkMaxExceeded = errors.New("cannot issue a credential for an expired link")
// ErrLinkInactive - link inactive
ErrLinkInactive = errors.New("cannot issue a credential for an inactive link")
// ErrClaimAlreadyIssued - claim already issued
ErrClaimAlreadyIssued = errors.New("the claim was already issued for the user")
)
// Link - represents a link in the issuer node
type Link struct {
storage *db.Storage
claimsService ports.ClaimsService
qrService ports.QrStoreService
claimRepository ports.ClaimsRepository
linkRepository ports.LinkRepository
schemaRepository ports.SchemaRepository
loader loader.DocumentLoader
sessionManager ports.SessionRepository
publisher pubsub.Publisher
ipfsGateway string
}
// NewLinkService - constructor
func NewLinkService(storage *db.Storage, claimsService ports.ClaimsService, qrService ports.QrStoreService, claimRepository ports.ClaimsRepository, linkRepository ports.LinkRepository, schemaRepository ports.SchemaRepository, ld loader.DocumentLoader, sessionManager ports.SessionRepository, publisher pubsub.Publisher, ipfsGatewayURL string) ports.LinkService {
return &Link{
storage: storage,
claimsService: claimsService,
qrService: qrService,
claimRepository: claimRepository,
linkRepository: linkRepository,
schemaRepository: schemaRepository,
loader: ld,
sessionManager: sessionManager,
publisher: publisher,
ipfsGateway: ipfsGatewayURL,
}
}
// Save - save a new credential
func (ls *Link) Save(
ctx context.Context,
did w3c.DID,
maxIssuance *int,
validUntil *time.Time,
schemaID uuid.UUID,
credentialExpiration *time.Time,
credentialSignatureProof bool,
credentialMTPProof bool,
credentialSubject domain.CredentialSubject,
refreshService *verifiable.RefreshService,
displayMethod *verifiable.DisplayMethod,
) (*domain.Link, error) {
schemaDB, err := ls.schemaRepository.GetByID(ctx, did, schemaID)
if err != nil {
return nil, err
}
if err := ls.validateCredentialSubjectAgainstSchema(ctx, credentialSubject, schemaDB); err != nil {
log.Error(ctx, "validating credential subject", "err", err)
return nil, ErrParseClaim
}
if err = ls.validateRefreshService(refreshService, credentialExpiration); err != nil {
log.Error(ctx, "validating refresh service", "err", err)
return nil, err
}
if err = ls.validateDisplayMethod(displayMethod); err != nil {
log.Error(ctx, "validating display method", "err", err)
return nil, err
}
link := domain.NewLink(did, maxIssuance, validUntil, schemaID, credentialExpiration, credentialSignatureProof, credentialMTPProof, credentialSubject, refreshService, displayMethod)
_, err = ls.linkRepository.Save(ctx, ls.storage.Pgx, link)
if err != nil {
return nil, err
}
link.Schema = schemaDB
return link, nil
}
// Activate - activates or deactivates a credential link
func (ls *Link) Activate(ctx context.Context, issuerID w3c.DID, linkID uuid.UUID, active bool) error {
link, err := ls.linkRepository.GetByID(ctx, issuerID, linkID)
if err != nil {
return err
}
if link.Active && active {
return ErrLinkAlreadyActive
}
if !link.Active && !active {
return ErrLinkAlreadyInactive
}
link.Active = active
_, err = ls.linkRepository.Save(ctx, ls.storage.Pgx, link)
return err
}
// GetByID returns a link by id and issuerDID
func (ls *Link) GetByID(ctx context.Context, issuerID w3c.DID, id uuid.UUID) (*domain.Link, error) {
link, err := ls.linkRepository.GetByID(ctx, issuerID, id)
if err != nil {
if errors.Is(err, repositories.ErrLinkDoesNotExist) {
return nil, ErrLinkNotFound
}
return nil, err
}
return link, nil
}
// GetAll returns all links from issueDID of type lType filtered by query string
func (ls *Link) GetAll(ctx context.Context, issuerDID w3c.DID, status ports.LinkStatus, query *string) ([]domain.Link, error) {
return ls.linkRepository.GetAll(ctx, issuerDID, status, query)
}
// Delete - delete a link by id
func (ls *Link) Delete(ctx context.Context, id uuid.UUID, did w3c.DID) error {
return ls.linkRepository.Delete(ctx, id, did)
}
// CreateQRCode - generates a qr code for a link
func (ls *Link) CreateQRCode(ctx context.Context, issuerDID w3c.DID, linkID uuid.UUID, serverURL string) (*ports.CreateQRCodeResponse, error) {
link, err := ls.GetByID(ctx, issuerDID, linkID)
if err != nil {
return nil, err
}
err = ls.validate(ctx, link)
if err != nil {
return nil, err
}
sessionID := uuid.New().String()
reqID := uuid.New().String()
qrCode := &protocol.AuthorizationRequestMessage{
From: issuerDID.String(),
ID: reqID,
ThreadID: reqID,
Typ: packers.MediaTypePlainMessage,
Type: protocol.AuthorizationRequestMessageType,
Body: protocol.AuthorizationRequestMessageBody{
CallbackURL: fmt.Sprintf("%s/v1/credentials/links/callback?sessionID=%s&linkID=%s", serverURL, sessionID, linkID.String()),
Reason: authReason,
Scope: make([]protocol.ZeroKnowledgeProofRequest, 0),
},
}
err = ls.sessionManager.Set(ctx, sessionID, *qrCode)
if err != nil {
return nil, err
}
err = ls.sessionManager.SetLink(ctx, linkState.CredentialStateCacheKey(linkID.String(), sessionID), *linkState.NewStatePending())
if err != nil {
return nil, err
}
raw, err := json.Marshal(qrCode)
if err != nil {
return nil, err
}
id, err := ls.qrService.Store(ctx, raw, DefaultQRBodyTTL)
if err != nil {
return nil, err
}
return &ports.CreateQRCodeResponse{
SessionID: sessionID,
QrCode: ls.qrService.ToURL(serverURL, id),
QrID: id,
Link: link,
}, nil
}
// IssueClaim - Create a new claim
func (ls *Link) IssueClaim(ctx context.Context, sessionID string, issuerDID w3c.DID, userDID w3c.DID, linkID uuid.UUID, hostURL string, credentialStatusType verifiable.CredentialStatusType) error {
link, err := ls.linkRepository.GetByID(ctx, issuerDID, linkID)
if err != nil {
log.Error(ctx, "cannot fetch the link", "err", err)
return err
}
issuedByUser, err := ls.claimRepository.GetClaimsIssuedForUser(ctx, ls.storage.Pgx, issuerDID, userDID, linkID)
if err != nil {
log.Error(ctx, "cannot fetch the claims issued for the user", "err", err, "issuerDID", issuerDID, "userDID", userDID)
return err
}
if err := ls.validate(ctx, link); err != nil {
setLinkError := ls.sessionManager.SetLink(ctx, linkState.CredentialStateCacheKey(linkID.String(), sessionID), *linkState.NewStateError(err))
if setLinkError != nil {
log.Error(ctx, "cannot set the state", "err", setLinkError)
return setLinkError
}
return err
}
var credentialIssuedID uuid.UUID
var credentialIssued *domain.Claim
schema, err := ls.schemaRepository.GetByID(ctx, issuerDID, link.SchemaID)
if err != nil {
log.Error(ctx, "cannot fetch the schema", "err", err)
return err
}
if len(issuedByUser) == 0 {
link.CredentialSubject["id"] = userDID.String()
claimReq := ports.NewCreateClaimRequest(&issuerDID,
schema.URL,
link.CredentialSubject,
link.CredentialExpiration,
schema.Type,
nil, nil, nil,
common.ToPointer(link.CredentialSignatureProof),
common.ToPointer(link.CredentialMTPProof),
&linkID,
true,
credentialStatusType,
link.RefreshService,
nil,
link.DisplayMethod,
)
credentialIssued, err = ls.claimsService.CreateCredential(ctx, claimReq)
if err != nil {
log.Error(ctx, "cannot create the claim", "err", err.Error())
return err
}
err = ls.storage.Pgx.BeginFunc(ctx,
func(tx pgx.Tx) error {
link.IssuedClaims += 1
_, err := ls.linkRepository.Save(ctx, ls.storage.Pgx, link)
if err != nil {
return err
}
credentialIssuedID, err = ls.claimRepository.Save(ctx, ls.storage.Pgx, credentialIssued)
if err != nil {
return err
}
return nil
})
if err != nil {
return err
}
} else {
credentialIssuedID = issuedByUser[0].ID
credentialIssued = issuedByUser[0]
}
credentialIssued.ID = credentialIssuedID
if link.CredentialSignatureProof {
err = ls.publisher.Publish(ctx, event.CreateCredentialEvent, &event.CreateCredential{CredentialIDs: []string{credentialIssued.ID.String()}, IssuerID: issuerDID.String()})
if err != nil {
log.Error(ctx, "publish CreateCredentialEvent", "err", err.Error(), "credential", credentialIssued.ID.String())
}
}
r := &linkState.QRCodeMessage{
ID: uuid.NewString(),
Typ: "application/iden3comm-plain-json",
Type: linkState.CredentialOfferMessageType,
ThreadID: uuid.NewString(),
Body: linkState.CredentialsLinkMessageBody{
URL: fmt.Sprintf("%s/v1/agent", hostURL),
Credentials: []linkState.CredentialLink{{
ID: credentialIssued.ID.String(),
Description: schema.Type,
}},
},
From: issuerDID.String(),
To: userDID.String(),
}
qrCodeBytes, err := json.Marshal(r)
if err != nil {
log.Error(ctx, "cannot marshal the qr code", "err", err)
return err
}
id, err := ls.qrService.Store(ctx, qrCodeBytes, DefaultQRBodyTTL)
if err != nil {
log.Error(ctx, "cannot store the qr code", "err", err)
return err
}
if link.CredentialSignatureProof {
err = ls.sessionManager.SetLink(ctx, linkState.CredentialStateCacheKey(linkID.String(), sessionID), *linkState.NewStateDone(ls.qrService.ToURL(hostURL, id)))
} else {
err = ls.sessionManager.SetLink(ctx, linkState.CredentialStateCacheKey(linkID.String(), sessionID), *linkState.NewStatePendingPublish())
}
if err != nil {
log.Error(ctx, "cannot set the sate", "err", err)
return err
}
return nil
}
// GetQRCode - return the link qr code.
func (ls *Link) GetQRCode(ctx context.Context, sessionID uuid.UUID, issuerID w3c.DID, linkID uuid.UUID) (*ports.GetQRCodeResponse, error) {
link, err := ls.GetByID(ctx, issuerID, linkID)
if err != nil {
log.Error(ctx, "error fetching the link from the database", "err", err)
return nil, err
}
linkStateInCache, err := ls.sessionManager.GetLink(ctx, linkState.CredentialStateCacheKey(linkID.String(), sessionID.String()))
if err != nil {
log.Error(ctx, "error fetching the link state from the cache", "err", err)
return nil, err
}
return &ports.GetQRCodeResponse{
State: &linkStateInCache,
Link: link,
}, nil
}
func (ls *Link) validate(ctx context.Context, link *domain.Link) error {
if link.ValidUntil != nil && time.Now().UTC().After(*link.ValidUntil) {
log.Debug(ctx, "cannot issue a credential for an expired link")
return ErrLinkAlreadyExpired
}
if link.MaxIssuance != nil && *link.MaxIssuance <= link.IssuedClaims {
log.Debug(ctx, "cannot dispatch more credentials for this link")
return ErrLinkMaxExceeded
}
if !link.Active {
log.Debug(ctx, "cannot dispatch credentials for inactive link")
return ErrLinkInactive
}
return nil
}
func (ls *Link) validateCredentialSubjectAgainstSchema(ctx context.Context, cSubject domain.CredentialSubject, schemaDB *domain.Schema) error {
return jsonschema.ValidateCredentialSubject(ctx, ls.loader, schemaDB.URL, schemaDB.Type, cSubject)
}
func (ls *Link) validateRefreshService(rs *verifiable.RefreshService, expiration *time.Time) error {
if rs == nil {
return nil
}
if expiration == nil {
return ErrRefreshServiceLacksExpirationTime
}
if rs.ID == "" {
return ErrRefreshServiceLacksURL
}
_, err := url.ParseRequestURI(rs.ID)
if err != nil {
return ErrRefreshServiceLacksURL
}
switch rs.Type {
case verifiable.Iden3RefreshService2023:
return nil
default:
return ErrUnsupportedRefreshServiceType
}
}
func (ls *Link) validateDisplayMethod(dm *verifiable.DisplayMethod) error {
if dm == nil {
return nil
}
if dm.ID == "" {
return ErrDisplayMethodLacksURL
}
if _, err := url.ParseRequestURI(dm.ID); err != nil {
return ErrDisplayMethodLacksURL
}
switch dm.Type {
case verifiable.Iden3BasicDisplayMethodV1:
return nil
default:
return ErrUnsupportedDisplayMethodType
}
}