55import os
66import logging
77from contextvars import ContextVar
8+ from string import ascii_letters , digits
89from typing import List , Optional
910
1011from urllib .parse import urlparse
1718
1819_LOGGER = logging .getLogger (__name__ )
1920
21+ VALID_TENANT_ID_CHARACTERS = frozenset (ascii_letters + digits + "-." )
22+ VALID_SCOPE_CHARACTERS = frozenset (ascii_letters + digits + "_-.:/" )
23+
2024
2125def normalize_authority (authority : str ) -> str :
2226 """Ensure authority uses https, strip trailing spaces and /.
@@ -43,19 +47,28 @@ def get_default_authority() -> str:
4347 return normalize_authority (authority )
4448
4549
46- VALID_TENANT_ID_CHARACTERS = frozenset ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + "0123456789" + "-." )
50+ def validate_scope (scope : str ) -> None :
51+ """Raise ValueError if scope is empty or contains a character invalid for a scope
52+
53+ :param str scope: scope to validate
54+ :raises: ValueError if scope is empty or contains a character invalid for a scope.
55+ """
56+ if not scope or any (c not in VALID_SCOPE_CHARACTERS for c in scope ):
57+ raise ValueError (
58+ "An invalid scope was provided. Only alphanumeric characters, '.', '-', '_', ':', and '/' are allowed."
59+ )
4760
4861
4962def validate_tenant_id (tenant_id : str ) -> None :
5063 """Raise ValueError if tenant_id is empty or contains a character invalid for a tenant ID.
5164
52- :param str tenant_id: tenant id to validate
65+ :param str tenant_id: tenant ID to validate
5366 :raises: ValueError if tenant_id is empty or contains a character invalid for a tenant ID.
5467 """
5568 if not tenant_id or any (c not in VALID_TENANT_ID_CHARACTERS for c in tenant_id ):
5669 raise ValueError (
57- "Invalid tenant id provided. You can locate your tenant id by following the instructions here: "
58- + "https://docs .microsoft.com/partner-center/find-ids-and-domain-names"
70+ "Invalid tenant ID provided. You can locate your tenant ID by following the instructions here: "
71+ + "https://learn .microsoft.com/partner-center/find-ids-and-domain-names"
5972 )
6073
6174
0 commit comments