Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(sdk): improve present credential and custom scope api. #665

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions cmd/wallet-sdk-gomobile/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -1469,14 +1469,13 @@ interaction.presentCredentialUnsafe(preferredVC)

```kotlin

val scope = interaction.scope()
val scope = interaction.customScope()

interaction.presentCredentialWithOpts(selectedCredentials, PresentCredentialOpts()
.addScopeClaim("registration", """{"email", "[email protected]"}"""))
interaction.PresentCredentialOpts(PresentCredentialOpts().addScopeClaim(
scope.atIndex(0), """{"email":"[email protected]"}"""), selectedCredentials)

```


#### Swift (iOS)

```swift
Expand Down Expand Up @@ -1530,11 +1529,12 @@ interaction.presentCredentialUnsafe(preferredVC)

```swift

val scope = interaction.scope()

let opts = Openid4vpNewPresentCredentialOpts()?.addScopeClaim("registration", #"{"email", "[email protected]"}"#)
val scope = interaction.customScope()

try interaction.presentCredentialWithOpts(selectedCredentials, opts)
try interaction.presentCredentialOpts(
selectedCreds,
opts: Openid4vpNewPresentCredentialOpts()?.addScopeClaim(scope.atIndex(0), claimJSON:#"{"email":"[email protected]"}"#)
)

```

Expand Down
12 changes: 6 additions & 6 deletions cmd/wallet-sdk-gomobile/openid4vp/interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

type goAPIOpenID4VP interface {
GetQuery() *presexch.PresentationDefinition
Scope() []string
CustomScope() []string
PresentCredential(credentials []*afgoverifiable.Credential, customClaims openid4vp.CustomClaims) error
PresentCredentialUnsafe(credential *afgoverifiable.Credential, customClaims openid4vp.CustomClaims) error
VerifierDisplayData() *openid4vp.VerifierDisplayData
Expand Down Expand Up @@ -135,9 +135,9 @@ func (o *Interaction) GetQuery() ([]byte, error) {
return pdBytes, nil
}

// Scope returns vp integration scope.
func (o *Interaction) Scope() *Scope {
return NewScope(o.goAPIOpenID4VP.Scope())
// CustomScope returns vp integration scope.
func (o *Interaction) CustomScope() *Scope {
return NewScope(o.goAPIOpenID4VP.CustomScope())
}

// VerifierDisplayData returns display information about verifier.
Expand All @@ -157,8 +157,8 @@ func (o *Interaction) PresentCredential(credentials *verifiable.CredentialsArray
return wrapper.ToMobileErrorWithTrace(o.goAPIOpenID4VP.PresentCredential(vcs, openid4vp.CustomClaims{}), o.oTel)
}

// PresentCredentialWithOpts presents credentials to redirect uri from request object.
func (o *Interaction) PresentCredentialWithOpts(
// PresentCredentialOpts presents credentials to redirect uri from request object.
func (o *Interaction) PresentCredentialOpts(
credentials *verifiable.CredentialsArray,
opts *PresentCredentialOpts,
) error {
Expand Down
13 changes: 6 additions & 7 deletions cmd/wallet-sdk-gomobile/openid4vp/interaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ func TestNewInteraction(t *testing.T) {
instance, err := NewInteraction(requiredArgs, opts)
require.NoError(t, err)
require.NotNil(t, instance)
require.Equal(t, 1, instance.Scope().Length())
require.Equal(t, "openid", instance.Scope().AtIndex(0))
require.Equal(t, 0, instance.CustomScope().Length())
})
t.Run("All other options invoked", func(t *testing.T) {
resolver, err := gomobdid.NewResolver(gomobdid.NewResolverOpts())
Expand Down Expand Up @@ -187,15 +186,15 @@ func TestOpenID4VP_PresentCredential(t *testing.T) {
t.Run("Success With Opts", func(t *testing.T) {
instance := makeInteraction()

err := instance.PresentCredentialWithOpts(credentials, NewPresentCredentialOpts().
err := instance.PresentCredentialOpts(credentials, NewPresentCredentialOpts().
AddScopeClaim("claim1", `{"key" : "val"}`))
require.NoError(t, err)
})

t.Run("Success With nil Opts", func(t *testing.T) {
instance := makeInteraction()

err := instance.PresentCredentialWithOpts(credentials, nil)
err := instance.PresentCredentialOpts(credentials, nil)
require.NoError(t, err)
})

Expand Down Expand Up @@ -224,15 +223,15 @@ func TestOpenID4VP_PresentCredential(t *testing.T) {
PresentCredentialErr: errors.New("present credentials failed"),
}

err := instance.PresentCredentialWithOpts(credentials, NewPresentCredentialOpts().
err := instance.PresentCredentialOpts(credentials, NewPresentCredentialOpts().
AddScopeClaim("claim1", `{"key" : "val"}`))
require.Contains(t, err.Error(), "present credentials failed")
})

t.Run("Present credentials with invalid scope value", func(t *testing.T) {
instance := makeInteraction()

err := instance.PresentCredentialWithOpts(credentials, NewPresentCredentialOpts().
err := instance.PresentCredentialOpts(credentials, NewPresentCredentialOpts().
AddScopeClaim("claim1", `"key" : "val"`))
require.ErrorContains(t, err, `fail to parse "claim1" claim json`)
})
Expand Down Expand Up @@ -350,7 +349,7 @@ func (o *mockGoAPIInteraction) GetQuery() *presexch.PresentationDefinition {
return o.GetQueryResult
}

func (o *mockGoAPIInteraction) Scope() []string {
func (o *mockGoAPIInteraction) CustomScope() []string {
return o.ScopeResult
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class IntegrationTest {
selectedCreds.add(requirementDescriptor.matchedVCs.atIndex(0))

// Presenting from selected credentials.
vpInteraction.presentCredentialWithOpts(selectedCreds, PresentCredentialOpts().addScopeClaim(
vpInteraction.presentCredentialOpts(selectedCreds, PresentCredentialOpts().addScopeClaim(
"registration", """{"email":"[email protected]"}"""))
}

Expand Down
3 changes: 2 additions & 1 deletion demo/app/ios/Runner/Tests/IntegrationTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ class IntegrationTest: XCTestCase {
selectedCreds.add(requirementDescriptor.matchedVCs!.atIndex(0))

// Presenting from selected credentials.
try vpInteraction.presentCredential(withOpts: selectedCreds,
try vpInteraction.presentCredentialOpts(
selectedCreds,
opts: Openid4vpNewPresentCredentialOpts()?.addScopeClaim("registration", claimJSON:#"{"email":"[email protected]"}"#)
)
}
Expand Down
14 changes: 11 additions & 3 deletions pkg/openid4vp/openid4vp.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,17 @@ func (o *Interaction) GetQuery() *presexch.PresentationDefinition {
return o.requestObject.Claims.VPToken.PresentationDefinition
}

// Scope returns vp integration scope.
func (o *Interaction) Scope() []string {
return strings.Split(o.requestObject.Scope, "+")
// CustomScope returns vp integration scope.
func (o *Interaction) CustomScope() []string {
var customScopes []string

for _, scope := range strings.Split(o.requestObject.Scope, "+") {
if scope != "openid" {
customScopes = append(customScopes, scope)
}
}

return customScopes
}

// VerifierDisplayData returns display information about verifier.
Expand Down
6 changes: 3 additions & 3 deletions pkg/openid4vp/openid4vp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,9 +794,9 @@ func TestInteraction_Scope(t *testing.T) {
Scope: "openid+msregistration",
}}

require.Len(t, interaction.Scope(), 2)
require.Contains(t, interaction.Scope(), "openid")
require.Contains(t, interaction.Scope(), "msregistration")
require.Len(t, interaction.CustomScope(), 1)
require.NotContains(t, interaction.CustomScope(), "openid")
require.Contains(t, interaction.CustomScope(), "msregistration")
})
}

Expand Down
23 changes: 15 additions & 8 deletions test/integration/openid4vp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestOpenID4VPFullFlow(t *testing.T) {
verifierProfileID string
signingKeyType string
matchedDisplayData *display.Data
customScope *customScope
customScopes []customScope
}

tests := []test{
Expand Down Expand Up @@ -141,10 +141,10 @@ func TestOpenID4VPFullFlow(t *testing.T) {
claimData: []claimData{verifiableEmployeeClaims, verifiableEmployeeClaims},
walletDIDMethod: "ion",
verifierProfileID: "v_myprofile_jwt_verified_employee",
customScope: &customScope{
customScopes: []customScope{{
name: "registration",
customClaims: `{"email": "[email protected]"}`,
},
}},
},
}

Expand Down Expand Up @@ -173,8 +173,15 @@ func TestOpenID4VPFullFlow(t *testing.T) {
require.NoError(t, err)

var customScopeName *string
if tc.customScope != nil {
customScopeName = &tc.customScope.name
if len(tc.customScopes) > 0 {
name := ""
for i, scope := range tc.customScopes {
if i != 0 {
name += "+"
}
name += scope.name
}
customScopeName = &name
}

initiateURL, err := setup.InitiateInteraction(tc.verifierProfileID, "test purpose.", customScopeName)
Expand Down Expand Up @@ -263,10 +270,10 @@ func TestOpenID4VPFullFlow(t *testing.T) {
require.Equal(t, serializedIssuedVC, serializedMatchedVC)

presentOps := openid4vp.NewPresentCredentialOpts()
if tc.customScope != nil {
presentOps.AddScopeClaim(tc.customScope.name, tc.customScope.customClaims)
for _, scope := range tc.customScopes {
presentOps.AddScopeClaim(scope.name, scope.customClaims)
}
err = interaction.PresentCredentialWithOpts(selectedCreds, presentOps)
err = interaction.PresentCredentialOpts(selectedCreds, presentOps)
require.NoError(t, err)

testHelper.CheckActivityLogAfterOpenID4VPFlow(t, activityLogger, tc.verifierProfileID)
Expand Down
Loading