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

make target optional #316

Merged
merged 4 commits into from
Mar 22, 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
43 changes: 41 additions & 2 deletions credential/exchange/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,54 @@ const (
// JWT key values

PresentationDefinitionKey string = "presentation_definition"

// Presentation Request Option types

TargetOption PresentationRequestOptionType = "target"
)

type PresentationRequestOptionType string

type PresentationRequestOption struct {
Type PresentationRequestOptionType
Value any
}

// BuildPresentationRequest https://identity.foundation/presentation-exchange/#presentation-request
// used for transmitting a Presentation Definition from a holder to a verifier. Target is who the request is intended for.
// TODO(gabe) expand to other presentation types and signers https://github.com/TBD54566975/ssi-sdk/issues/57
func BuildPresentationRequest(signer crypto.JWTSigner, pt PresentationRequestType, def PresentationDefinition, target string) ([]byte, error) {
func BuildPresentationRequest(signer any, pt PresentationRequestType, def PresentationDefinition, opts ...PresentationRequestOption) ([]byte, error) {
if signer == nil {
return nil, fmt.Errorf("cannot build presentation request with nil signer")
}

// process options
if len(opts) > 1 {
return nil, fmt.Errorf("only one option supported")
}
var target string
if len(opts) == 1 {
opt := opts[0]
if opt.Type != TargetOption {
return nil, fmt.Errorf("unsupported option type: %s", opt.Type)
}
var ok bool
target, ok = opt.Value.(string)
if !ok {
return nil, fmt.Errorf("target option value must be a string")
}
}

if !IsSupportedPresentationRequestType(pt) {
return nil, fmt.Errorf("unsupported presentation request type: %s", pt)
}
switch pt {
case JWTRequest:
return BuildJWTPresentationRequest(signer, def, target)
jwtSigner, ok := signer.(crypto.JWTSigner)
if !ok {
return nil, errors.New("signer is not a JWTSigner")
}
return BuildJWTPresentationRequest(jwtSigner, def, target)
default:
return nil, fmt.Errorf("presentation request type <%s> is not implemented", pt)
}
Expand All @@ -46,6 +82,9 @@ func BuildJWTPresentationRequest(signer crypto.JWTSigner, def PresentationDefini
jwt.AudienceKey: target,
PresentationDefinitionKey: def,
}
if target != "" {
jwtValues[jwt.AudienceKey] = target
}
return signer.SignWithDefaults(jwtValues)
}

Expand Down
7 changes: 5 additions & 2 deletions credential/exchange/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestBuildPresentationRequest(t *testing.T) {
assert.NoError(t, err)

testDef := getDummyPresentationDefinition()
requestJWTBytes, err := BuildPresentationRequest(*signer, JWTRequest, testDef, "did:test")
requestJWTBytes, err := BuildPresentationRequest(*signer, JWTRequest, testDef)
assert.NoError(t, err)
assert.NotEmpty(t, requestJWTBytes)

Expand All @@ -63,7 +63,10 @@ func TestBuildPresentationRequest(t *testing.T) {
assert.NoError(t, err)

testDef := getDummyPresentationDefinition()
_, err = BuildPresentationRequest(*signer, "bad", testDef, "did:test")
_, err = BuildPresentationRequest(*signer, "bad", testDef, PresentationRequestOption{
Type: TargetOption,
Value: "did:test:abcd",
})
assert.Error(t, err)
assert.Contains(t, err.Error(), "unsupported presentation request type")
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ func main() {
example.HandleExampleError(err, "Failed to make presentation definition")
example.HandleExampleError(presentationDefinition.IsValid(), "Presentation definition is not valid")

presentationRequestBytes, err := exchange.BuildPresentationRequest(*aptSigner, exchange.JWTRequest, *presentationDefinition, string(*holderDIDKey))
presentationRequestBytes, err := exchange.BuildPresentationRequest(*aptSigner, exchange.JWTRequest, *presentationDefinition, exchange.PresentationRequestOption{
Type: exchange.TargetOption,
Value: holderDIDKey.String(),
})
example.HandleExampleError(err, "Failed to make presentation request")

_, _ = fmt.Print("\n\nStep 3: The apartment creates a presentation request that confirms which information is required from the tenant\n\n")
Expand Down
Binary file modified wasm/static/main.wasm
Binary file not shown.