-
Notifications
You must be signed in to change notification settings - Fork 1
fix(attestation): verify attested signatures from did:web authorities #43
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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,74 @@ | ||
| package attestation_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/fil-forge/ucantone/did" | ||
| "github.com/fil-forge/ucantone/did/key" | ||
| "github.com/fil-forge/ucantone/ucan/command" | ||
| "github.com/fil-forge/ucantone/ucan/delegation" | ||
| "github.com/fil-forge/ucantone/validator" | ||
|
|
||
| "github.com/fil-forge/libforge/attestation" | ||
| "github.com/fil-forge/libforge/attestation/didmailto" | ||
| "github.com/fil-forge/libforge/identity" | ||
| "github.com/fil-forge/libforge/testutil" | ||
| ) | ||
|
|
||
| // TestSigner_WebAuthority exercises an attestation whose authority is a did:web | ||
| // service (e.g. did:web:upload), resolved via its DID document — the real | ||
| // service flow. The existing TestSigner uses a did:key authority, which the | ||
| // default key.Resolver handles, so it never caught that the attestation verifier | ||
| // re-resolved the authority with the did:key-only default resolver. With a | ||
| // did:web authority that path fails ("signature mismatch"); this test guards the | ||
| // fix that verifies with the already-resolved authority verifier. | ||
| func TestSigner_WebAuthority(t *testing.T) { | ||
| authority, err := identity.New("", "did:web:example.com") | ||
| require.NoError(t, err) | ||
|
|
||
| doc, err := authority.DIDDocument() | ||
| require.NoError(t, err) | ||
|
|
||
| alice, err := did.Parse("did:mailto:example.com:alice") | ||
| require.NoError(t, err) | ||
|
|
||
| issuer := attestation.Attest(t.Context(), alice, authority) | ||
|
|
||
| del, err := delegation.Delegate( | ||
| issuer, | ||
| testutil.RandomDID(t), | ||
| issuer.DID(), | ||
| command.MustParse("/example/command"), | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| encoded, err := delegation.Encode(del) | ||
| require.NoError(t, err) | ||
| decoded, err := delegation.Decode(encoded) | ||
| require.NoError(t, err) | ||
|
|
||
| // Serve the authority's generated did:web document. | ||
| webResolver := did.ResolverFunc(func(_ context.Context, d did.DID) (did.Document, error) { | ||
| if d == authority.DID() { | ||
| return doc, nil | ||
| } | ||
| return did.Document{}, fmt.Errorf("unexpected did %s", d) | ||
| }) | ||
| resolver := did.ResolverMap{ | ||
| "key": key.Resolver, | ||
| "web": webResolver, | ||
| "mailto": didmailto.NewResolver(authority.DID()), | ||
| } | ||
| factories := validator.DefaultFactories() | ||
| factories[attestation.Type] = attestation.NewVerifierFactory(resolver, factories) | ||
|
|
||
| err = validator.ValidateToken(t.Context(), decoded, | ||
| validator.WithDIDResolver(resolver), | ||
| validator.WithVerifierFactories(factories), | ||
| ) | ||
| require.NoError(t, err) | ||
| } |
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.
This comment isn't quite right. This didn't re-validate the (outer) token, it validated the signature, as an invocation, for the first time.
Technically, only verifying the signature leaves out a few ways the invocation could be invalid, like expiration, but we're not supposed to use any of those ways for these invocations. I think ideally this eventually change from using invocations to using assertions (or maybe they'll be called "attestations", which would certainly be appropriate), which would be a different kind of token and would specifically not have expiration dates and other things that aren't relevant.
More to the point,
v.authorityVerifierisn't used for anything yet, so I'm pretty sure this is exactly why I added it, I just failed to do exactly this part. So good catch, thank you! 😅