Skip to content

Commit

Permalink
feat: add credentialIDs to interaction-succeeded (#1749)
Browse files Browse the repository at this point in the history
* feat: add credentialIDs to interaction-succeeded

* fix: lint
  • Loading branch information
skynet2 authored Aug 20, 2024
1 parent c293c0a commit 57404f6
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
5 changes: 3 additions & 2 deletions pkg/service/oidc4ci/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,9 @@ type EventPayload struct {
// Deprecated: use Credentials instead.
CredentialTemplateID string `json:"credentialTemplateID,omitempty"`
// Deprecated: use Credentials instead.
Format vcsverifiable.OIDCFormat `json:"format,omitempty"`
Credentials map[string]vcsverifiable.OIDCFormat `json:"credentials"`
Format vcsverifiable.OIDCFormat `json:"format,omitempty"`
Credentials map[string]vcsverifiable.OIDCFormat `json:"credentials"`
CredentialIDs []string `json:"credentialIDs"`
}

type AuthorizationCodeGrant struct {
Expand Down
30 changes: 24 additions & 6 deletions pkg/service/oidc4ci/oidc4ci_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,12 @@ func (s *Service) ValidatePreAuthorizedCodeRequest( //nolint:gocognit,nolintlint
return nil, err
}

if errSendEvent := s.sendTransactionEvent(ctx, tx, spi.IssuerOIDCInteractionQRScanned); errSendEvent != nil {
if errSendEvent := s.sendTransactionEvent(
ctx,
tx,
spi.IssuerOIDCInteractionQRScanned,
nil,
); errSendEvent != nil {
return nil, errSendEvent
}

Expand Down Expand Up @@ -752,6 +757,8 @@ func (s *Service) PrepareCredential( //nolint:funlen

requestedTxCredentialConfigurationIDs := make(map[string]struct{})

var credentialIDs []string

for _, requestedCredential := range req.CredentialRequests {
if err = s.validateRequestAudienceClaim(
tx.ProfileID, tx.ProfileVersion, requestedCredential.AudienceClaim); err != nil {
Expand Down Expand Up @@ -794,6 +801,7 @@ func (s *Service) PrepareCredential( //nolint:funlen
NotificationID: ackID,
}

credentialIDs = append(credentialIDs, cred.Contents().ID)
prepareCredentialResult.Credentials = append(prepareCredentialResult.Credentials, prepareCredentialResultData)
}

Expand All @@ -806,7 +814,12 @@ func (s *Service) PrepareCredential( //nolint:funlen
return nil, e
}

if errSendEvent := s.sendTransactionEvent(ctx, tx, spi.IssuerOIDCInteractionSucceeded); errSendEvent != nil {
if errSendEvent := s.sendTransactionEvent(
ctx,
tx,
spi.IssuerOIDCInteractionSucceeded,
credentialIDs,
); errSendEvent != nil {
return nil, errSendEvent
}

Expand Down Expand Up @@ -1005,8 +1018,9 @@ func (s *Service) sendTransactionEvent(
ctx context.Context,
tx *issuecredential.Transaction,
eventType spi.EventType,
credentialIDs []string,
) error {
return s.sendEvent(ctx, eventType, tx.ID, createTxEventPayload(tx))
return s.sendEvent(ctx, eventType, tx.ID, createTxEventPayload(tx, credentialIDs))
}

func (s *Service) sendFailedTransactionEvent(
Expand All @@ -1028,7 +1042,10 @@ func (s *Service) sendFailedTransactionEvent(
}
}

func createTxEventPayload(tx *issuecredential.Transaction) *EventPayload {
func createTxEventPayload(
tx *issuecredential.Transaction,
credentialIDs []string,
) *EventPayload {
var (
credentialTemplateID string
credentialFormat vcsverifiable.OIDCFormat
Expand Down Expand Up @@ -1063,6 +1080,7 @@ func createTxEventPayload(tx *issuecredential.Transaction) *EventPayload {
PinRequired: tx.UserPin != "",
PreAuthFlow: tx.IsPreAuthFlow,
Credentials: credentialsData,
CredentialIDs: credentialIDs,
}
}

Expand All @@ -1071,7 +1089,7 @@ func (s *Service) sendInitiateIssuanceEvent(
tx *issuecredential.Transaction,
initiateURL string,
) error {
payload := createTxEventPayload(tx)
payload := createTxEventPayload(tx, nil)
payload.InitiateIssuanceURL = initiateURL

return s.sendEvent(ctx, spi.IssuerOIDCInteractionInitiated, tx.ID, payload)
Expand All @@ -1081,7 +1099,7 @@ func (s *Service) sendIssuanceAuthRequestPreparedTxEvent(
ctx context.Context,
tx *issuecredential.Transaction,
) error {
payload := createTxEventPayload(tx)
payload := createTxEventPayload(tx, nil)
payload.AuthorizationEndpoint = tx.AuthorizationEndpoint

return s.sendEvent(ctx, spi.IssuerOIDCInteractionAuthorizationRequestPrepared, tx.ID, payload)
Expand Down
7 changes: 6 additions & 1 deletion pkg/service/oidc4ci/oidc4ci_service_exchange_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ func (s *Service) ExchangeAuthorizationCode(
return nil, err
}

if err = s.sendTransactionEvent(ctx, tx, spi.IssuerOIDCInteractionAuthorizationCodeExchanged); err != nil {
if err = s.sendTransactionEvent(
ctx,
tx,
spi.IssuerOIDCInteractionAuthorizationCodeExchanged,
nil,
); err != nil {
return nil, err
}

Expand Down
7 changes: 6 additions & 1 deletion pkg/service/oidc4ci/oidc4ci_service_store_auth_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ func (s *Service) StoreAuthorizationCode(
return "", err
}

if err = s.sendTransactionEvent(ctx, tx, spi.IssuerOIDCInteractionAuthorizationCodeStored); err != nil {
if err = s.sendTransactionEvent(
ctx,
tx,
spi.IssuerOIDCInteractionAuthorizationCodeStored,
nil,
); err != nil {
return "", err
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/service/oidc4ci/oidc4ci_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4149,6 +4149,12 @@ func TestService_PrepareCredential(t *testing.T) {
assert.Len(t, messages, 1)
assert.Equal(t, messages[0].Type, spi.IssuerOIDCInteractionSucceeded)

credIDs := messages[0].Data.(map[string]interface{})["credentialIDs"].([]any) //nolint
assert.Len(t, credIDs, 2)

assert.Contains(t, credIDs[0], "urn:uuid:")
assert.Contains(t, credIDs[1], "urn:uuid:")

return nil
})

Expand Down

0 comments on commit 57404f6

Please sign in to comment.