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

improve did parser for port and path #24

Merged
merged 2 commits into from
Oct 16, 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
4 changes: 2 additions & 2 deletions integration/app/integration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function genDidWebs() {
# if [ "${prompt}" == "y" ]; then
# read -p "Name the identity [searcher]: " runGenDid
# fi
dkr did webs generate --name=wan --did=did:webs:127.0.0.1:7676:BBilc4-L3tFUnfM_wJr4S4OJanAv_VmF_dJNN6vkf2Ha --oobi http://127.0.0.1:5642/oobi/BBilc4-L3tFUnfM_wJr4S4OJanAv_VmF_dJNN6vkf2Ha/controller
dkr did webs generate --name=wan --did=did:webs:127.0.0.1%3a7676:BBilc4-L3tFUnfM_wJr4S4OJanAv_VmF_dJNN6vkf2Ha --oobi http://127.0.0.1:5642/oobi/BBilc4-L3tFUnfM_wJr4S4OJanAv_VmF_dJNN6vkf2Ha/controller
fi
}

Expand Down Expand Up @@ -120,7 +120,7 @@ function resolveDIDAndKeriEvents() {
# if [ "${prompt}" == "y" ]; then
# read -p "Name the identity [searcher]: " runGenDid
# fi
dkr did webs resolve --name wan --did did:webs:127.0.0.1:7676:BBilc4-L3tFUnfM_wJr4S4OJanAv_VmF_dJNN6vkf2Ha
dkr did webs resolve --name wan --did did:webs:127.0.0.1%3a7676:BBilc4-L3tFUnfM_wJr4S4OJanAv_VmF_dJNN6vkf2Ha
fi
}

Expand Down
2 changes: 1 addition & 1 deletion src/dkr/app/cli/commands/did/webs/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def generate(self, tymth, tock=0.0, **opts):
self.tock = tock
_ = (yield self.tock)

domain, path, aid = didding.parseDIDWebs(self.did)
domain, port, path, aid = didding.parseDIDWebs(self.did)
obr = basing.OobiRecord(date=helping.nowIso8601())
obr.cid = aid
self.hby.db.oobis.pin(keys=(self.oobi,), val=obr)
Expand Down
4 changes: 2 additions & 2 deletions src/dkr/app/cli/commands/did/webs/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ def resolve(self, tymth, tock=0.0, **opts):
self.tock = tock
_ = (yield self.tock)

domain, path, aid = didding.parseDIDWebs(self.did)
domain, port, path, aid = didding.parseDIDWebs(self.did)

base_url = f"http://{domain}/{path}/{aid}"
base_url = f"http://{domain}{(f':{port}' if port is not None else '')}{(f'/{path}' if path is not None else '')}/{aid}"

# Load the did doc
dd_url = f"{base_url}/{webbing.DID_JSON}"
Expand Down
14 changes: 6 additions & 8 deletions src/dkr/core/didding.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
from keri.app import oobiing
from keri.core import coring

DID_KERI_RE = re.compile('\\Adid:keri:(?P<aid>[^:]+)\\Z', re.IGNORECASE)
DID_WEBS_RE = re.compile('\\Adid:webs:(?P<domain>[^:]+):((?P<path>.+):)?(?P<aid>[^:]+)\\Z', re.IGNORECASE)

DID_KERI_RE = re.compile(r'\Adid:keri:(?P<aid>[^:]+)\Z', re.IGNORECASE)
# DID_WEBS_RE = re.compile(r'\Adid:webs:(?P<domain>[^%:]+)(?:%3a(?P<port>\d+))?:(?P<path>.+?):(?P<aid>[^:]+)\Z', re.IGNORECASE)
DID_WEBS_RE = re.compile(r'\Adid:webs:(?P<domain>[^%:]+)(?:%3a(?P<port>\d+))?(?::(?P<path>.+?))?(?::(?P<aid>[^:]+))\Z', re.IGNORECASE)
def parseDIDKeri(did):
match = DID_KERI_RE.match(did)
if match is None:
raise ValueError(f"{did} is not a valid did:webs DID")
raise ValueError(f"{did} is not a valid did:keri DID")

aid = match.group("aid")

Expand All @@ -37,16 +37,14 @@ def parseDIDWebs(did):
if match is None:
raise ValueError(f"{did} is not a valid did:webs DID")

domain = match.group("domain")
path = match.group("path")
aid = match.group("aid")
domain, port, path, aid = match.group("domain", "port", "path", "aid")

try:
_ = coring.Prefixer(qb64=aid)
except Exception as e:
raise ValueError(f"{aid} is an invalid AID")

return domain, path, aid
return domain, port, path, aid


def generateDIDDoc(hby, did, aid, oobi=None, metadata=None):
Expand Down
165 changes: 108 additions & 57 deletions tests/app/core/test_didding.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,69 +17,120 @@
from keri.peer import exchanging


def test_parse_did():
def test_parse_keri_did():

# Valid did:keri DID
did = "did:keri:EKW4IEkAZ8VQ_ADXbtRsSOQ_Gk0cRxp6U4qKSr4Eb8zg:http://example.com/oobi" \
"/EKW4IEkAZ8VQ_ADXbtRsSOQ_Gk0cRxp6U4qKSr4Eb8zg/witness/BIYCp_nGrWF_UR0IDtEFlHGmj_nAx2I3DzbfXxAgc0DC"
did = "did:keri:EKW4IEkAZ8VQ_ADXbtRsSOQ_Gk0cRxp6U4qKSr4Eb8zg"
# ":http://example.com/oobi/EKW4IEkAZ8VQ_ADXbtRsSOQ_Gk0cRxp6U4qKSr4Eb8zg/witness/BIYCp_nGrWF_UR0IDtEFlHGmj_nAx2I3DzbfXxAgc0DC"

aid, oobi = didding.parseDID(did)
aid = didding.parseDIDKeri(did)
assert aid == "EKW4IEkAZ8VQ_ADXbtRsSOQ_Gk0cRxp6U4qKSr4Eb8zg"
assert oobi == "http://example.com/oobi" \
"/EKW4IEkAZ8VQ_ADXbtRsSOQ_Gk0cRxp6U4qKSr4Eb8zg/witness/BIYCp_nGrWF_UR0IDtEFlHGmj_nAx2I3DzbfXxAgc0DC"
# assert oobi == "http://example.com/oobi" \
# "/EKW4IEkAZ8VQ_ADXbtRsSOQ_Gk0cRxp6U4qKSr4Eb8zg/witness/BIYCp_nGrWF_UR0IDtEFlHGmj_nAx2I3DzbfXxAgc0DC"

# Invalid AID in did:keri
did = "did:keri:Gk0cRxp6U4qKSr4Eb8zg:http://example.com/oobi" \
"/EKW4IEkAZ8VQ_ADXbtRsSOQ_Gk0cRxp6U4qKSr4Eb8zg/witness/BIYCp_nGrWF_UR0IDtEFlHGmj_nAx2I3DzbfXxAgc0DC"
did = "did:keri:Gk0cRxp6U4qKSr4Eb8zg"
# :http://example.com/oobi/EKW4IEkAZ8VQ_ADXbtRsSOQ_Gk0cRxp6U4qKSr4Eb8zg/witness/BIYCp_nGrWF_UR0IDtEFlHGmj_nAx2I3DzbfXxAgc0DC"

with pytest.raises(ValueError):
_, _ = didding.parseDID(did)

def test_generate_did_doc():

# Valid did:keri DID
pre = "EBNaNu-M9P5cgrnfl2Fvymy4E_jvxxyjb70PRtiANlJy"
oobi = f"http://127.0.0.1:7723/oobi/{pre}"
did = f"did:webs:example.com:{pre}/path/to/file"
domain, aid, path = didding.parseDIDWebs(did)

with habbing.openHby(name="oobi") as hby:
hab = hby.makeHab(name="oobi")
msgs = bytearray()
msgs.extend(hab.makeEndRole(eid=hab.pre,
role=kering.Roles.controller,
stamp=help.nowIso8601()))

msgs.extend(hab.makeLocScheme(url='http://127.0.0.1:5555',
scheme=kering.Schemes.http,
stamp=help.nowIso8601()))
hab.psr.parse(ims=msgs)

oobiery = keri.app.oobiing.Oobiery(hby=hby)

# Insert some that will fail
url = 'http://127.0.0.1:5644/oobi/EADqo6tHmYTuQ3Lope4mZF_4hBoGJl93cBHRekr_iD_A/witness' \
'/BAyRFMideczFZoapylLIyCjSdhtqVb31wZkRKvPfNqkw?name=jim'
obr = basing.OobiRecord(date=helping.nowIso8601())
hby.db.oobis.pin(keys=(url,), val=obr)
url = 'http://127.0.0.1:5644/oobi/EBRzmSCFmG2a5U2OqZF-yUobeSYkW-a3FsN82eZXMxY0'
obr = basing.OobiRecord(date=helping.nowIso8601())
hby.db.oobis.pin(keys=(url,), val=obr)
url = 'http://127.0.0.1:5644/oobi?name=Blind'
obr = basing.OobiRecord(date=helping.nowIso8601())
hby.db.oobis.pin(keys=(url,), val=obr)

# Configure the MOOBI rpy URL and the controller URL
curl = f'http://127.0.0.1:5644/oobi/{hab.pre}/controller'
murl = f'http://127.0.0.1:5644/.well-known/keri/oobi/{hab.pre}?name=Root'
obr = basing.OobiRecord(date=helping.nowIso8601())
hby.db.oobis.pin(keys=(murl,), val=obr)

# app = falcon.App() # falcon.App instances are callable WSGI apps
# ending.loadEnds(app, hby=hby)
# moobi = MOOBIEnd(hab=hab, url=curl)
# app.add_route(f"/.well-known/keri/oobi/{hab.pre}", moobi)

didDoc = didding.generateDIDDoc(hby, did, aid, oobi=oobi, metadata=None)
_, _ = didding.parseDIDKeri(did)

def test_parse_webs_did():
with pytest.raises(ValueError):
did = "did:webs:127.0.0.1:1234567"
domain, port, path, aid = didding.parseDIDWebs(did)

did = "did:webs:127.0.0.1:BBilc4-L3tFUnfM_wJr4S4OJanAv_VmF_dJNN6vkf2Ha"
domain, port, path, aid = didding.parseDIDWebs(did)
assert "127.0.0.1" == domain
assert None == port
assert None == path
assert aid == "BBilc4-L3tFUnfM_wJr4S4OJanAv_VmF_dJNN6vkf2Ha"

# port url should be url encoded with %3a according to the spec
did_port_bad = "did:webs:127.0.0.1:7676:BBilc4-L3tFUnfM_wJr4S4OJanAv_VmF_dJNN6vkf2Ha"
domain, port, path, aid = didding.parseDIDWebs(did_port_bad)
assert "127.0.0.1" == domain
assert None == port
assert "7676" == path
assert aid == "BBilc4-L3tFUnfM_wJr4S4OJanAv_VmF_dJNN6vkf2Ha"

did_port = "did:webs:127.0.0.1%3a7676:BBilc4-L3tFUnfM_wJr4S4OJanAv_VmF_dJNN6vkf2Ha"
domain, port, path, aid = didding.parseDIDWebs(did_port)
assert "127.0.0.1" == domain
assert "7676" == port
assert None == path
assert aid == "BBilc4-L3tFUnfM_wJr4S4OJanAv_VmF_dJNN6vkf2Ha"

# port should be url encoded with %3a according to the spec
did_port_path_bad = "did:webs:127.0.0.1:7676:my:path:BBilc4-L3tFUnfM_wJr4S4OJanAv_VmF_dJNN6vkf2Ha"
domain, port, path, aid = didding.parseDIDWebs(did_port_path_bad)
assert "127.0.0.1" == domain
assert None == port
assert "7676:my:path" == path
assert aid == "BBilc4-L3tFUnfM_wJr4S4OJanAv_VmF_dJNN6vkf2Ha"

# port is properly url encoded with %3a according to the spec
did_port_path = "did:webs:127.0.0.1%3a7676:my:path:BBilc4-L3tFUnfM_wJr4S4OJanAv_VmF_dJNN6vkf2Ha"
domain, port, path, aid = didding.parseDIDWebs(did_port_path)
assert "127.0.0.1" == domain
assert "7676" == port
assert "my:path" == path
assert aid == "BBilc4-L3tFUnfM_wJr4S4OJanAv_VmF_dJNN6vkf2Ha"

did_path = "did:webs:127.0.0.1:my:path:BBilc4-L3tFUnfM_wJr4S4OJanAv_VmF_dJNN6vkf2Ha"
domain, port, path, aid = didding.parseDIDWebs(did_path)
assert "127.0.0.1" == domain
assert None == port
assert "my:path" == path
assert aid,"BBilc4-L3tFUnfM_wJr4S4OJanAv_VmF_dJNN6vkf2Ha"


# def test_generate_did_doc():

# # Valid did:keri DID
# pre = "EBNaNu-M9P5cgrnfl2Fvymy4E_jvxxyjb70PRtiANlJy"
# oobi = f"http://127.0.0.1:7723/oobi/{pre}"
# did = f"did:webs:example.com:{pre}/path/to/file"
# domain, aid, path = didding.parseDIDWebs(did)

# with habbing.openHby(name="oobi") as hby:
# hab = hby.makeHab(name="oobi")
# msgs = bytearray()
# msgs.extend(hab.makeEndRole(eid=hab.pre,
# role=kering.Roles.controller,
# stamp=help.nowIso8601()))

# msgs.extend(hab.makeLocScheme(url='http://127.0.0.1:5555',
# scheme=kering.Schemes.http,
# stamp=help.nowIso8601()))
# hab.psr.parse(ims=msgs)

# oobiery = keri.app.oobiing.Oobiery(hby=hby)

# # Insert some that will fail
# url = 'http://127.0.0.1:5644/oobi/EADqo6tHmYTuQ3Lope4mZF_4hBoGJl93cBHRekr_iD_A/witness' \
# '/BAyRFMideczFZoapylLIyCjSdhtqVb31wZkRKvPfNqkw?name=jim'
# obr = basing.OobiRecord(date=helping.nowIso8601())
# hby.db.oobis.pin(keys=(url,), val=obr)
# url = 'http://127.0.0.1:5644/oobi/EBRzmSCFmG2a5U2OqZF-yUobeSYkW-a3FsN82eZXMxY0'
# obr = basing.OobiRecord(date=helping.nowIso8601())
# hby.db.oobis.pin(keys=(url,), val=obr)
# url = 'http://127.0.0.1:5644/oobi?name=Blind'
# obr = basing.OobiRecord(date=helping.nowIso8601())
# hby.db.oobis.pin(keys=(url,), val=obr)

# # Configure the MOOBI rpy URL and the controller URL
# curl = f'http://127.0.0.1:5644/oobi/{hab.pre}/controller'
# murl = f'http://127.0.0.1:5644/.well-known/keri/oobi/{hab.pre}?name=Root'
# obr = basing.OobiRecord(date=helping.nowIso8601())
# hby.db.oobis.pin(keys=(murl,), val=obr)

# # app = falcon.App() # falcon.App instances are callable WSGI apps
# # ending.loadEnds(app, hby=hby)
# # moobi = MOOBIEnd(hab=hab, url=curl)
# # app.add_route(f"/.well-known/keri/oobi/{hab.pre}", moobi)

# didDoc = didding.generateDIDDoc(hby, did, aid, oobi=oobi, metadata=None)

assert didDoc is not None
# assert didDoc is not None