Skip to content

Commit

Permalink
feat: add possibility to override sub
Browse files Browse the repository at this point in the history
  • Loading branch information
skynet2 committed Mar 22, 2024
1 parent 6a61141 commit d3b95d3
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 184 deletions.
338 changes: 169 additions & 169 deletions api/spec/openapi.gen.go

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions docs/v1/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,10 @@ components:
type: boolean
description: Override issuer.
nullable: true
override_subject_did:
type: boolean
description: Override credential subject did.
nullable: true
credential:
type: object
description: Raw Complete credential for sign and customization
Expand Down
7 changes: 4 additions & 3 deletions pkg/restapi/v1/issuer/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,10 @@ func (c *Controller) initiateIssuance(

if multiCredentialIssuance.Compose != nil {
credConfig.ComposeCredential = &oidc4ci.InitiateIssuanceComposeCredential{
Credential: multiCredentialIssuance.Compose.Credential,
IDTemplate: lo.FromPtr(multiCredentialIssuance.Compose.IdTemplate),
OverrideIssuer: lo.FromPtr(multiCredentialIssuance.Compose.OverrideIssuer),
Credential: multiCredentialIssuance.Compose.Credential,
IDTemplate: lo.FromPtr(multiCredentialIssuance.Compose.IdTemplate),
OverrideIssuer: lo.FromPtr(multiCredentialIssuance.Compose.OverrideIssuer),
OverrideSubjectDID: lo.FromPtr(multiCredentialIssuance.Compose.OverrideSubjectDid),
}
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/restapi/v1/issuer/openapi.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions pkg/service/oidc4ci/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ type TxCredentialConfiguration struct {
}

type CredentialComposeConfiguration struct {
IDTemplate string `json:"id_template"`
OverrideIssuer bool `json:"override_issuer"`
IDTemplate string `json:"id_template"`
OverrideIssuer bool `json:"override_issuer"`
OverrideSubjectDID bool `json:"override_subject_did"`
}

// AuthorizationDetails represents the domain model for Authorization Details request.
Expand Down Expand Up @@ -194,9 +195,10 @@ type InitiateIssuanceCredentialConfiguration struct {
}

type InitiateIssuanceComposeCredential struct {
Credential *map[string]interface{} `json:"credential,omitempty"`
IDTemplate string `json:"id_template"`
OverrideIssuer bool `json:"override_issuer"`
Credential *map[string]interface{} `json:"credential,omitempty"`
IDTemplate string `json:"id_template"`
OverrideIssuer bool `json:"override_issuer"`
OverrideSubjectDID bool `json:"override_subject_did"`
}

// InitiateIssuanceResponse is the response from the Issuer to the Wallet with initiate issuance URL.
Expand Down
13 changes: 12 additions & 1 deletion pkg/service/oidc4ci/composer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (c *CredentialComposer) Compose(
credential *verifiable.Credential,
tx *Transaction,
txCredentialConfiguration *TxCredentialConfiguration,
_ *PrepareCredentialRequest,
prepRequest *PrepareCredentialRequest,
) (*verifiable.Credential, error) {
if txCredentialConfiguration == nil || txCredentialConfiguration.CredentialComposeConfiguration == nil {
return credential, nil
Expand All @@ -46,6 +46,17 @@ func (c *CredentialComposer) Compose(
credential = credential.WithModifiedIssuer(&verifiable.Issuer{ID: tx.DID})
}

if txCredentialConfiguration.CredentialComposeConfiguration.OverrideSubjectDID {
var newSubjects []verifiable.Subject
for _, s := range credential.Contents().Subject {
s.ID = prepRequest.DID

newSubjects = append(newSubjects, s)
}

credential = credential.WithModifiedSubject(newSubjects)
}

return credential, nil
}

Expand Down
14 changes: 10 additions & 4 deletions pkg/service/oidc4ci/composer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ func TestComposer(t *testing.T) {
t.Run("success", func(t *testing.T) {
srv := oidc4ci.NewCredentialComposer()

cred, err := verifiable.CreateCredential(verifiable.CredentialContents{}, verifiable.CustomFields{})
cred, err := verifiable.CreateCredential(verifiable.CredentialContents{
Subject: []verifiable.Subject{{ID: "xxx:yyy"}},
}, verifiable.CustomFields{})
assert.NoError(t, err)

resp, err := srv.Compose(
Expand All @@ -28,18 +30,22 @@ func TestComposer(t *testing.T) {
},
&oidc4ci.TxCredentialConfiguration{
CredentialComposeConfiguration: &oidc4ci.CredentialComposeConfiguration{
IDTemplate: "hardcoded:{{.TxID}}:suffix",
OverrideIssuer: true,
IDTemplate: "hardcoded:{{.TxID}}:suffix",
OverrideIssuer: true,
OverrideSubjectDID: true,
},
},
&oidc4ci.PrepareCredentialRequest{},
&oidc4ci.PrepareCredentialRequest{
DID: "some-awesome-did",
},
)

assert.NoError(t, err)
assert.NotNil(t, resp)

assert.EqualValues(t, "hardcoded:some-awesome-id:suffix", resp.Contents().ID)
assert.EqualValues(t, "did:example:123", resp.Contents().Issuer.ID)
assert.EqualValues(t, "some-awesome-did", resp.Contents().Subject[0].ID)
})

t.Run("invalid template", func(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions pkg/service/oidc4ci/oidc4ci_service_initiate_issuance.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,9 @@ func (s *Service) applyPreAuthFlowModifications(
txCredentialConfiguration.ClaimDataType = ClaimDataTypeVC

txCredentialConfiguration.CredentialComposeConfiguration = &CredentialComposeConfiguration{
IDTemplate: req.ComposeCredential.IDTemplate,
OverrideIssuer: req.ComposeCredential.OverrideIssuer,
IDTemplate: req.ComposeCredential.IDTemplate,
OverrideIssuer: req.ComposeCredential.OverrideIssuer,
OverrideSubjectDID: req.ComposeCredential.OverrideSubjectDID,
}
}

Expand Down

0 comments on commit d3b95d3

Please sign in to comment.