File tree 5 files changed +13
-6
lines changed
5 files changed +13
-6
lines changed Original file line number Diff line number Diff line change 1
1
"""Common components."""
2
2
import re
3
3
4
- DID_PATTERN = re .compile ("did:([a-z0-9]+):((?:[a-zA-Z0-9._-]*:)*[a-zA-Z0-9._-]+)" )
4
+ DID_REGEX = "did:([a-z0-9]+):((?:[a-zA-Z0-9._-]*:)*[a-zA-Z0-9._-]+)"
5
+ DID_PATTERN = re .compile (f"^{ DID_REGEX } $" )
6
+ DID_URL_DID_PART_PATTERN = re .compile (f"^({ DID_REGEX } )[?/#]" )
5
7
6
8
7
9
class DIDError (Exception ):
Original file line number Diff line number Diff line change @@ -25,7 +25,7 @@ class DID:
25
25
def __init__ (self , did : str ):
26
26
"""Validate and parse raw DID str."""
27
27
self ._raw = did
28
- matched = DID_PATTERN .fullmatch (did )
28
+ matched = DID_PATTERN .match (did )
29
29
if not matched :
30
30
raise InvalidDIDError ("Unable to parse DID {}" .format (did ))
31
31
self ._method = matched .group (1 )
@@ -73,7 +73,7 @@ def ref(self, ident: str) -> DIDUrl:
73
73
@classmethod
74
74
def is_valid (cls , did : str ):
75
75
"""Return if the passed string is a valid DID."""
76
- return DID_PATTERN .fullmatch (did )
76
+ return DID_PATTERN .match (did )
77
77
78
78
@classmethod
79
79
def validate (cls , did : str ):
Original file line number Diff line number Diff line change 4
4
5
5
from voluptuous import Invalid
6
6
7
- from .common import DID_PATTERN , DIDError
7
+ from .common import DID_URL_DID_PART_PATTERN , DIDError
8
8
9
9
10
10
class InvalidDIDUrlError (DIDError , Invalid ):
@@ -78,12 +78,12 @@ def __hash__(self):
78
78
@classmethod
79
79
def parse (cls , url : str ):
80
80
"""Parse DID URL from string."""
81
- matches = DID_PATTERN .match (url )
81
+ matches = DID_URL_DID_PART_PATTERN .match (url )
82
82
83
83
if not matches :
84
84
raise InvalidDIDUrlError ("DID could not be parsed from URL {}" .format (url ))
85
85
86
- did = matches .group (0 )
86
+ did = matches .group (1 )
87
87
_ , url_component = url .split (did )
88
88
89
89
if not url_component :
Original file line number Diff line number Diff line change @@ -111,6 +111,7 @@ def test_validate(did):
111
111
"did:nomethodspecificidentifier" ,
112
112
"did:invalid-chars-in-method:method-specific-id" ,
113
113
"bad-prefix:method:method-specific-id" ,
114
+ "did:bad:char'@='acters:example:1234abcd" ,
114
115
* TEST_DID_URLS ,
115
116
],
116
117
)
Original file line number Diff line number Diff line change @@ -53,3 +53,7 @@ def test_is_valid(url):
53
53
@pytest .mark .parametrize ("bad_url" , [TEST_DID0 , "not a did url" ])
54
54
def test_is_valid_x (bad_url ):
55
55
assert not DIDUrl .is_valid (bad_url )
56
+
57
+
58
+ def test_partial_match_url ():
59
+ assert not DIDUrl .is_valid ("did:bad:char'@='acters:example:1234abcd#4" )
You can’t perform that action at this time.
0 commit comments