1111from ..base import BaseDIDResolver , DIDNotFound , ResolverError , ResolverType
1212
1313LOGGER = logging .getLogger (__name__ )
14- DEFAULT_ENDPOINT = "https://dev.uniresolver.io"
15-
16-
17- async def _fetch_resolver_props (endpoint : str ) -> dict :
18- """Retrieve universal resolver properties."""
19- async with aiohttp .ClientSession () as session :
20- async with session .get (f"{ endpoint } /1.0/properties/" ) as resp :
21- if resp .status >= 200 and resp .status < 400 :
22- return await resp .json ()
23- raise ResolverError (
24- "Failed to retrieve resolver properties: " + await resp .text ()
25- )
26-
27-
28- async def _get_supported_did_regex (endpoint : str ) -> Pattern :
29- props = await _fetch_resolver_props (endpoint )
30- return _compile_supported_did_regex (
31- driver ["http" ]["pattern" ] for driver in props .values ()
32- )
14+ DEFAULT_ENDPOINT = "https://dev.uniresolver.io/1.0"
3315
3416
3517def _compile_supported_did_regex (patterns : Iterable [Union [str , Pattern ]]):
@@ -54,25 +36,37 @@ def __init__(
5436 * ,
5537 endpoint : Optional [str ] = None ,
5638 supported_did_regex : Optional [Pattern ] = None ,
39+ bearer_token : Optional [str ] = None ,
5740 ):
5841 """Initialize UniversalResolver."""
5942 super ().__init__ (ResolverType .NON_NATIVE )
6043 self ._endpoint = endpoint
6144 self ._supported_did_regex = supported_did_regex
6245
46+ self .__default_headers = (
47+ {"Authorization" : f"Bearer { bearer_token } " } if bearer_token else {}
48+ )
49+
6350 async def setup (self , context : InjectionContext ):
64- """Preform setup, populate supported method list, configuration."""
51+ """Perform setup, populate supported method list, configuration."""
52+
53+ # configure endpoint
6554 endpoint = context .settings .get_str ("resolver.universal" )
6655 if endpoint == "DEFAULT" or not endpoint :
6756 endpoint = DEFAULT_ENDPOINT
57+ self ._endpoint = endpoint
58+
59+ # configure authorization
60+ token = context .settings .get_str ("resolver.universal.token" )
61+ self .__default_headers = {"Authorization" : f"Bearer { token } " } if token else {}
6862
63+ # configure supported methods
6964 supported = context .settings .get ("resolver.universal.supported" )
7065 if supported is None :
71- supported_did_regex = await _get_supported_did_regex (endpoint )
66+ supported_did_regex = await self . _get_supported_did_regex ()
7267 else :
7368 supported_did_regex = _compile_supported_did_regex (supported )
7469
75- self ._endpoint = endpoint
7670 self ._supported_did_regex = supported_did_regex
7771
7872 @property
@@ -91,8 +85,8 @@ async def _resolve(
9185 ) -> dict :
9286 """Resolve DID through remote universal resolver."""
9387
94- async with aiohttp .ClientSession () as session :
95- async with session .get (f"{ self ._endpoint } /1.0/ identifiers/{ did } " ) as resp :
88+ async with aiohttp .ClientSession (headers = self . __default_headers ) as session :
89+ async with session .get (f"{ self ._endpoint } /identifiers/{ did } " ) as resp :
9690 if resp .status == 200 :
9791 doc = await resp .json ()
9892 did_doc = doc ["didDocument" ]
@@ -103,5 +97,21 @@ async def _resolve(
10397
10498 text = await resp .text ()
10599 raise ResolverError (
106- f"Unexecpted status from universal resolver ({ resp .status } ): { text } "
100+ f"Unexpected status from universal resolver ({ resp .status } ): { text } "
101+ )
102+
103+ async def _fetch_resolver_props (self ) -> dict :
104+ """Retrieve universal resolver properties."""
105+ async with aiohttp .ClientSession (headers = self .__default_headers ) as session :
106+ async with session .get (f"{ self ._endpoint } /properties/" ) as resp :
107+ if 200 <= resp .status < 400 :
108+ return await resp .json ()
109+ raise ResolverError (
110+ "Failed to retrieve resolver properties: " + await resp .text ()
107111 )
112+
113+ async def _get_supported_did_regex (self ) -> Pattern :
114+ props = await self ._fetch_resolver_props ()
115+ return _compile_supported_did_regex (
116+ driver ["http" ]["pattern" ] for driver in props .values ()
117+ )
0 commit comments