-
Notifications
You must be signed in to change notification settings - Fork 520
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
Feature/native did web resolver #1218
Merged
andrewwhitehead
merged 5 commits into
openwallet-foundation:main
from
boschresearch:feature/native-did-web-resolver
Jun 4, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"""Test did:web Resolver.""" | ||
|
||
import pytest | ||
from ..web import WebDIDResolver | ||
|
||
|
||
@pytest.fixture | ||
def resolver(): | ||
yield WebDIDResolver() | ||
|
||
|
||
def test_transformation_domain_only(resolver): | ||
did = "did:web:example.com" | ||
url = resolver._WebDIDResolver__transform_to_url(did) | ||
assert url == "https://example.com/.well-known/did.json" | ||
|
||
|
||
def test_transformation_domain_with_path(resolver): | ||
did = "did:web:example.com:department:example" | ||
url = resolver._WebDIDResolver__transform_to_url(did) | ||
assert url == "https://example.com/department/example/did.json" | ||
|
||
|
||
def test_transformation_domain_with_port(resolver): | ||
did = "did:web:localhost%3A443" | ||
url = resolver._WebDIDResolver__transform_to_url(did) | ||
assert url == "https://localhost:443/.well-known/did.json" |
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,83 @@ | ||
"""Web DID Resolver.""" | ||
|
||
import urllib.parse | ||
|
||
from typing import Sequence, Pattern | ||
|
||
import aiohttp | ||
|
||
from pydid import DID, DIDDocument | ||
|
||
from ...config.injection_context import InjectionContext | ||
from ...core.profile import Profile | ||
from ...messaging.valid import DIDWeb | ||
|
||
from ..base import ( | ||
BaseDIDResolver, | ||
DIDNotFound, | ||
ResolverError, | ||
ResolverType, | ||
) | ||
|
||
|
||
class WebDIDResolver(BaseDIDResolver): | ||
"""Web DID Resolver.""" | ||
|
||
def __init__(self): | ||
"""Initialize Web DID Resolver.""" | ||
super().__init__(ResolverType.NATIVE) | ||
|
||
async def setup(self, context: InjectionContext): | ||
"""Perform required setup for Web DID resolution.""" | ||
|
||
@property | ||
def supported_did_regex(self) -> Pattern: | ||
"""Return supported_did_regex of Web DID Resolver.""" | ||
return DIDWeb.PATTERN | ||
|
||
def supported_methods(self) -> Sequence[str]: | ||
"""Return list of supported methods.""" | ||
return ["web"] | ||
|
||
def __transform_to_url(self, did): | ||
""" | ||
Transform did to url. | ||
|
||
according to | ||
https://w3c-ccg.github.io/did-method-web/#read-resolve | ||
""" | ||
|
||
as_did = DID(did) | ||
method_specific_id = as_did.method_specific_id | ||
if ":" in method_specific_id: | ||
# contains path | ||
url = method_specific_id.replace(":", "/") | ||
else: | ||
# bare domain needs /.well-known path | ||
url = method_specific_id + "/.well-known" | ||
|
||
# Support encoded ports (See: https://github.com/w3c-ccg/did-method-web/issues/7) | ||
url = urllib.parse.unquote(url) | ||
|
||
return "https://" + url + "/did.json" | ||
|
||
async def _resolve(self, profile: Profile, did: str) -> dict: | ||
"""Resolve did:web DIDs.""" | ||
|
||
url = self.__transform_to_url(did) | ||
async with aiohttp.ClientSession() as session: | ||
async with session.get(url) as response: | ||
if response.status == 200: | ||
try: | ||
# Validate DIDDoc with pyDID | ||
did_doc = DIDDocument.from_json(await response.text()) | ||
return did_doc.serialize() | ||
except Exception as err: | ||
raise ResolverError( | ||
"Response was incorrectly formatted" | ||
) from err | ||
if response.status == 404: | ||
raise DIDNotFound(f"No document found for {did}") | ||
raise ResolverError( | ||
"Could not find doc for {}: {}".format(did, await response.text()) | ||
) |
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 |
---|---|---|
|
@@ -16,4 +16,4 @@ pyld~=2.0.3 | |
pyyaml~=5.4.0 | ||
ConfigArgParse~=1.2.3 | ||
pyjwt~=1.7.1 | ||
pydid~=0.2.3 | ||
pydid~=0.2.6 |
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 method should no longer be required, technically.