-
Notifications
You must be signed in to change notification settings - Fork 1
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
add AgentResolver #40
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
d801561
add AgentResolver
volodymyr-basiuk acd7549
add comments
volodymyr-basiuk 882c510
update schema-processor
volodymyr-basiuk 685f4cd
update go-schema-processor
volodymyr-basiuk 1df1699
refactor & add unit test
volodymyr-basiuk 332c21d
fix linter
volodymyr-basiuk 82684e8
rm options
volodymyr-basiuk b75d0d2
add NewAgentResolver
volodymyr-basiuk a2e694e
add Resolve Options
volodymyr-basiuk 78abb1a
use CredentialStatusResolveOpt...
volodymyr-basiuk c84e85a
Upgrade to new version of go-schema-processor, add user/issuser DID g…
olomix aac3cbd
Extract issuer setter/getter to go-schema-processor. Rename user DID …
olomix 8a3fe41
Merge pull request #41 from iden3/feature/agent-resolver-2
volodymyr-basiuk a9d810a
resolve comments
volodymyr-basiuk b62f007
fix linter
volodymyr-basiuk 609e668
accept all 200 codes
volodymyr-basiuk e72b4f4
errors.WithStack
volodymyr-basiuk 5af1576
make customHTTPClient public
volodymyr-basiuk bec4dff
rewrite body close err only if proof err nil
volodymyr-basiuk 67db65e
upgrae go-schema-processor/v2 to 2.3.0
volodymyr-basiuk ad9a4c3
rm gofrs package
volodymyr-basiuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package resolvers | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/google/uuid" | ||
"github.com/iden3/go-iden3-core/v2/w3c" | ||
"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" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type ctxKeySenderDID struct{} | ||
|
||
// WithSenderDID puts the user DID in the context | ||
func WithSenderDID(ctx context.Context, userDID *w3c.DID) context.Context { | ||
return context.WithValue(ctx, ctxKeySenderDID{}, userDID) | ||
} | ||
|
||
// GetSenderDID extract the sender's DID from the context. | ||
// Returns nil if nothing is found. | ||
func GetSenderDID(ctx context.Context) *w3c.DID { | ||
v := ctx.Value(ctxKeySenderDID{}) | ||
if v == nil { | ||
return nil | ||
} | ||
return v.(*w3c.DID) | ||
} | ||
|
||
// AgentResolverConfig options for credential status verification | ||
type AgentResolverConfig struct { | ||
PackageManager *iden3comm.PackageManager | ||
CustomHTTPClient *http.Client | ||
} | ||
|
||
// AgentResolver is a struct that allows to interact with the issuer's agent to get revocation status. | ||
type AgentResolver struct { | ||
config AgentResolverConfig | ||
} | ||
|
||
// NewAgentResolver returns new agent resolver | ||
func NewAgentResolver(config AgentResolverConfig) *AgentResolver { | ||
return &AgentResolver{config} | ||
} | ||
|
||
// Resolve is a method to resolve a credential status from an agent. | ||
func (r AgentResolver) Resolve(ctx context.Context, | ||
status verifiable.CredentialStatus) (out verifiable.RevocationStatus, err error) { | ||
|
||
vmidyllic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if status.Type != verifiable.Iden3commRevocationStatusV1 { | ||
return out, errors.New("invalid status type") | ||
} | ||
revocationBody := protocol.RevocationStatusRequestMessageBody{ | ||
RevocationNonce: status.RevocationNonce, | ||
} | ||
rawBody, err := json.Marshal(revocationBody) | ||
if err != nil { | ||
return out, errors.WithStack(err) | ||
} | ||
|
||
idUUID, err := uuid.NewV7() | ||
if err != nil { | ||
return out, errors.WithStack(err) | ||
} | ||
threadUUID, err := uuid.NewV7() | ||
if err != nil { | ||
return out, errors.WithStack(err) | ||
} | ||
|
||
senderDID := GetSenderDID(ctx) | ||
if senderDID == nil { | ||
return out, errors.New("sender DID not found in the context") | ||
} | ||
issuerDID := verifiable.GetIssuerDID(ctx) | ||
if issuerDID == nil { | ||
return out, errors.New("issuer DID not found in the context") | ||
} | ||
msg := iden3comm.BasicMessage{ | ||
ID: idUUID.String(), | ||
ThreadID: threadUUID.String(), | ||
From: senderDID.String(), | ||
To: issuerDID.String(), | ||
Type: protocol.RevocationStatusRequestMessageType, | ||
Body: rawBody, | ||
} | ||
bytesMsg, err := json.Marshal(msg) | ||
if err != nil { | ||
return out, errors.WithStack(err) | ||
} | ||
|
||
iden3commMsg, err := r.config.PackageManager.Pack(packers.MediaTypePlainMessage, bytesMsg, nil) | ||
if err != nil { | ||
return out, errors.WithStack(err) | ||
} | ||
|
||
httpClient := http.DefaultClient | ||
if r.config.CustomHTTPClient != nil { | ||
httpClient = r.config.CustomHTTPClient | ||
} | ||
|
||
resp, err := httpClient.Post(status.ID, "application/json", bytes.NewBuffer(iden3commMsg)) | ||
if err != nil { | ||
return out, errors.WithStack(err) | ||
} | ||
defer func() { | ||
err2 := resp.Body.Close() | ||
if err == nil { | ||
err = errors.WithStack(err2) | ||
} | ||
}() | ||
|
||
statusOK := resp.StatusCode >= 200 && resp.StatusCode < 300 | ||
if !statusOK { | ||
return out, errors.Errorf("bad status code: %d", resp.StatusCode) | ||
} | ||
|
||
b, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return out, errors.WithStack(err) | ||
} | ||
|
||
basicMessage, _, err := r.config.PackageManager.Unpack(b) | ||
if err != nil { | ||
return out, errors.WithStack(err) | ||
} | ||
|
||
if basicMessage.Type != protocol.RevocationStatusResponseMessageType { | ||
return out, errors.Errorf("unexpected message type: %s", basicMessage.Type) | ||
} | ||
|
||
var revocationStatus protocol.RevocationStatusResponseMessageBody | ||
if err = json.Unmarshal(basicMessage.Body, &revocationStatus); err != nil { | ||
return out, errors.WithStack(err) | ||
} | ||
|
||
return revocationStatus.RevocationStatus, err | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use an options pattern? Or if it required field pass the PackageManager directly to
NewAgentResolver
constructor?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, it looks like you missed our discussions