Skip to content

Commit

Permalink
Set custom document loader (#54)
Browse files Browse the repository at this point in the history
* Set custom document loader

* Add new WithDIDResolver option for Verifier
  • Loading branch information
olomix authored Jul 11, 2023
1 parent 17a795e commit 699ad39
Show file tree
Hide file tree
Showing 17 changed files with 884 additions and 510 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/ci-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ on:
- main
- develop
pull_request:
branches:
- main
- develop
jobs:
test:
strategy:
Expand Down
102 changes: 87 additions & 15 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,26 @@ import (
"github.com/iden3/go-iden3-auth/v2/pubsignals"
"github.com/iden3/go-iden3-auth/v2/state"
"github.com/iden3/go-jwz/v2"
"github.com/iden3/go-schema-processor/merklize"
"github.com/iden3/go-schema-processor/v2/merklize"
"github.com/iden3/go-schema-processor/v2/verifiable"
"github.com/iden3/iden3comm/v2"
"github.com/iden3/iden3comm/v2/packers"
"github.com/iden3/iden3comm/v2/protocol"
shell "github.com/ipfs/go-ipfs-api"
"github.com/piprate/json-gold/ld"
"github.com/pkg/errors"
)

var defaultSchemaLoader ld.DocumentLoader

// SetDocumentLoader sets the default schema loader that would be used if
// other is not set with WithDocumentLoader option. Also, this document loader
// is set for go-schema-processor library to use it for merklize.
func SetDocumentLoader(schemaLoader ld.DocumentLoader) {
defaultSchemaLoader = schemaLoader
merklize.SetDocumentLoader(schemaLoader)
}

// UniversalResolverURL is a url for universal resolver
const UniversalResolverURL = "https://dev.uniresolver.io/1.0/identifiers"

Expand Down Expand Up @@ -86,39 +97,86 @@ var UniversalDIDResolver = packers.DIDResolverHandlerFunc(func(did string) (*ver
// Verifier is a struct for auth instance
type Verifier struct {
verificationKeyLoader loaders.VerificationKeyLoader
claimSchemaLoader loaders.SchemaLoader
documentLoader ld.DocumentLoader
stateResolver map[string]pubsignals.StateResolver
packageManager iden3comm.PackageManager
}

// VerifierOption is a function to set options for Verifier instance
type VerifierOption func(opts *verifierOpts)

// WithDocumentLoader sets the document loader for Verifier instance
func WithDocumentLoader(docLoader ld.DocumentLoader) VerifierOption {
return func(opts *verifierOpts) {
opts.docLoader = docLoader
}
}

// WithIPFSClient sets the IPFS client for document loader of Verifier instance.
// If document loader is set with WithDocumentLoader function, this option is
// ignored.
func WithIPFSClient(ipfsCli *shell.Shell) VerifierOption {
return func(opts *verifierOpts) {
opts.ipfsCli = ipfsCli
}
}

// WithIPFSGateway sets the IPFS gateway for document loader of Verifier
// instance. If document loader is set with WithDocumentLoader function, this
// option is ignored. If WithIPFSClient is set, this option is ignored also.
func WithIPFSGateway(ipfsGW string) VerifierOption {
return func(opts *verifierOpts) {
opts.ipfsGW = ipfsGW
}
}

// WithDIDResolver sets the DID resolver for Verifier instance. The default
// value is UniversalDIDResolver.
func WithDIDResolver(resolver packers.DIDResolverHandlerFunc) VerifierOption {
return func(opts *verifierOpts) {
opts.didResolver = resolver
}
}

type verifierOpts struct {
docLoader ld.DocumentLoader
ipfsCli *shell.Shell
ipfsGW string
didResolver packers.DIDResolverHandlerFunc
}

func newOpts() verifierOpts {
return verifierOpts{
didResolver: UniversalDIDResolver,
}
}

// NewVerifier returns setup instance of auth library
func NewVerifier(
keyLoader loaders.VerificationKeyLoader,
claimSchemaLoader loaders.SchemaLoader,
resolver map[string]pubsignals.StateResolver,
opts ...VerifierOption,
) (*Verifier, error) {
vOpts := newOpts()
for _, optFn := range opts {
optFn(&vOpts)
}

docLoader := getDocumentLoader(vOpts.docLoader, vOpts.ipfsCli,
vOpts.ipfsGW)
v := &Verifier{
verificationKeyLoader: keyLoader,
claimSchemaLoader: claimSchemaLoader,
documentLoader: docLoader,
stateResolver: resolver,
packageManager: *iden3comm.NewPackageManager(),
}

// try to extract IPFS_URL if the schema loader is the default one
if impl, ok := claimSchemaLoader.(loaders.DefaultSchemaLoader); ok &&
impl.IpfsURL != "" {

ipfsCli := shell.NewShell(impl.IpfsURL)
documentLoader := merklize.NewDocumentLoader(ipfsCli, "")
merklize.SetDocumentLoader(documentLoader)
}

err := v.SetupAuthV2ZKPPacker()
if err != nil {
return nil, err
}

err = v.SetupJWSPacker(UniversalDIDResolver)
err = v.SetupJWSPacker(vOpts.didResolver)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -276,7 +334,7 @@ func (v *Verifier) VerifyAuthResponse(
rawMessage = nil
}

err = cv.VerifyQuery(ctx, query, v.claimSchemaLoader, rawMessage)
err = cv.VerifyQuery(ctx, query, v.documentLoader, rawMessage)
if err != nil {
return err
}
Expand Down Expand Up @@ -411,3 +469,17 @@ func findProofByRequestID(arr []protocol.ZeroKnowledgeProofResponse, id uint32)
}
return nil
}

func getDocumentLoader(docLoader ld.DocumentLoader, ipfsCli *shell.Shell,
ipfsGW string) ld.DocumentLoader {

if docLoader != nil {
return docLoader
}

if ipfsCli == nil && ipfsGW == "" && defaultSchemaLoader != nil {
return defaultSchemaLoader
}

return merklize.NewDocumentLoader(ipfsCli, ipfsGW)
}
Loading

0 comments on commit 699ad39

Please sign in to comment.