File size: 10,548 Bytes
7475f0d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | import json
import warnings
from functools import cached_property
from hashlib import md5
from typing import Any
from typing import Dict
from typing import MutableMapping
from typing import Optional
from typing import Union
from typing import cast
from urllib.parse import urljoin
import requests
from requests import PreparedRequest
from requests import Response
from requests.auth import AuthBase
from anaconda_auth import __version__ as version
from anaconda_auth.adapters import HTTPAdapter
from anaconda_auth.config import AnacondaAuthConfig
from anaconda_auth.config import AnacondaAuthSite
from anaconda_auth.exceptions import TokenExpiredError
from anaconda_auth.exceptions import TokenNotFoundError
from anaconda_auth.token import TokenInfo
from anaconda_auth.utils import get_hostname
from anaconda_cli_base.exceptions import AnacondaConfigValidationError
# VersionInfo was renamed and is deprecated in semver>=3
try:
from semver.version import Version
except ImportError:
# In semver<3, it's called VersionInfo
from semver import VersionInfo as Version
def login_required(response: Response, *args: Any, **kwargs: Any) -> Response:
has_auth_header = response.request.headers.get("Authorization", False)
if response.status_code in [401, 403]:
try:
error_code = response.json().get("error", {}).get("code", "")
except requests.JSONDecodeError:
error_code = ""
if error_code == "auth_required":
if has_auth_header:
response.reason = "Your API key or login token is invalid."
else:
response.reason = (
"You must login before using this API endpoint"
" or provide an api_key to your client."
)
return response
class BearerAuth(AuthBase):
def __init__(
self, domain: Optional[str] = None, api_key: Optional[str] = None
) -> None:
self.api_key = api_key
if domain is None:
domain = AnacondaAuthConfig().domain
self._token_info = TokenInfo(domain=domain)
def __call__(self, r: PreparedRequest) -> PreparedRequest:
if not self.api_key:
try:
r.headers["Authorization"] = (
f"Bearer {self._token_info.get_access_token()}"
)
except (TokenNotFoundError, TokenExpiredError):
pass
else:
r.headers["Authorization"] = f"Bearer {self.api_key}"
return r
class BaseClient(requests.Session):
_user_agent: str = f"anaconda-auth/{version}"
_api_version: Optional[str] = None
def __init__(
self,
site: Optional[Union[str, AnacondaAuthSite]] = None,
base_uri: Optional[str] = None,
domain: Optional[str] = None,
auth_domain_override: Optional[str] = None,
api_key: Optional[str] = None,
user_agent: Optional[str] = None,
api_version: Optional[str] = None,
ssl_verify: Optional[Union[bool, str]] = None,
extra_headers: Optional[Union[str, dict]] = None,
hash_hostname: Optional[bool] = None,
proxy_servers: Optional[MutableMapping[str, str]] = None,
client_cert: Optional[str] = None,
client_cert_key: Optional[str] = None,
):
super().__init__()
# Prepare the requested or default site config
if isinstance(site, AnacondaAuthSite):
config = site
elif site:
config = AnacondaAuthConfig(site=site)
else:
config = AnacondaAuthConfig()
# Prepare site overrides
if base_uri and domain:
raise ValueError("Can only specify one of `domain` or `base_uri` argument")
kwargs: Dict[str, Any] = {}
if domain is not None:
kwargs["domain"] = domain
if auth_domain_override is not None:
kwargs["auth_domain_override"] = auth_domain_override
if api_key is not None:
kwargs["api_key"] = api_key
if ssl_verify is not None:
kwargs["ssl_verify"] = ssl_verify
if extra_headers is not None:
kwargs["extra_headers"] = extra_headers
if hash_hostname is not None:
kwargs["hash_hostname"] = hash_hostname
if proxy_servers is not None:
kwargs["proxy_servers"] = proxy_servers
if client_cert_key is not None:
kwargs["client_cert"] = client_cert
kwargs["client_cert_key"] = client_cert_key
if client_cert is not None:
kwargs["client_cert"] = client_cert
self.config = config.model_copy(update=kwargs, deep=True)
self.proxies = self.config.proxy_servers or {}
self.configure_ssl()
# base_url overrides domain
self._base_uri = base_uri or f"https://{self.config.domain}"
self.headers["User-Agent"] = user_agent or self._user_agent
self.headers["X-Client-Hostname"] = get_hostname(hash=self.config.hash_hostname)
self.api_version = api_version or self._api_version
if self.api_version:
self.headers["Api-Version"] = self.api_version
if self.config.extra_headers is not None:
if isinstance(self.config.extra_headers, str):
try:
self.config.extra_headers = cast(
dict, json.loads(self.config.extra_headers)
)
except json.decoder.JSONDecodeError:
raise ValueError(
f"{repr(self.config.extra_headers)} is not valid JSON."
)
keys_to_add = self.config.extra_headers.keys() - self.headers.keys()
for k in keys_to_add:
self.headers[k] = self.config.extra_headers[k]
self.auth = BearerAuth(domain=self.config.domain, api_key=self.config.api_key)
self.hooks["response"].append(login_required)
def configure_ssl(self) -> None:
ssl_context = None
if self.config.ssl_verify == "truststore":
try:
import ssl
import truststore # type: ignore
ssl_context = truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
except ImportError:
raise AnacondaConfigValidationError(
"The `ssl_verify: truststore` setting is only supported on Python 3.10 or later."
)
self.verify = True
else:
self.verify = self.config.ssl_verify
http_adapter = HTTPAdapter(ssl_context=ssl_context)
self._ssl = ssl_context
self.mount("http://", http_adapter)
self.mount("https://", http_adapter)
if self.config.client_cert_key and self.config.client_cert:
self.cert = (self.config.client_cert, self.config.client_cert_key)
elif self.config.client_cert:
self.cert = self.config.client_cert
def urljoin(self, url: str) -> str:
return urljoin(self._base_uri, url)
def prepare_request(self, request: requests.Request) -> PreparedRequest:
request.url = self.urljoin(str(request.url))
return super().prepare_request(request)
def request(
self,
method: Union[str, bytes],
url: Union[str, bytes],
*args: Any,
**kwargs: Any,
) -> Response:
joined_url = self.urljoin(str(url))
# Ensure we don't set `verify` twice. If it is passed as a kwarg to this method,
# that becomes the value. Otherwise, we use the value in `self.config.ssl_verify`.
kwargs.setdefault("verify", self.config.ssl_verify)
response = super().request(method, joined_url, *args, **kwargs)
min_api_version_string = response.headers.get("Min-Api-Version")
self._validate_api_version(min_api_version_string)
return response
@cached_property
def account(self) -> dict:
res = self.get("/api/account")
res.raise_for_status()
account = res.json()
return account
@property
def name(self) -> str:
user = self.account.get("user", {})
first_name = user.get("first_name", "")
last_name = user.get("last_name", "")
if not first_name and not last_name:
return self.email
else:
return f"{first_name} {last_name}".strip()
@property
def email(self) -> str:
value = self.account.get("user", {}).get("email")
if value is None:
raise ValueError(
"Something is wrong with your account. An email address could not be found."
)
else:
return value
@cached_property
def avatar(self) -> Union[bytes, None]:
hashed = md5(self.email.encode("utf-8")).hexdigest()
res = self.get(
f"https://gravatar.com/avatar/{hashed}.png?size=120&d=404",
verify=self.config.ssl_verify,
auth=False, # type: ignore
)
if res.ok:
return res.content
else:
return None
def _validate_api_version(self, min_api_version_string: Optional[str]) -> None:
"""Validate that the client API version against the min API version from the service."""
if min_api_version_string is None or self.api_version is None:
return None
# Convert to optional Version objects
api_version = _parse_semver_string(self.api_version)
min_api_version = _parse_semver_string(min_api_version_string)
if api_version is None or min_api_version is None:
return None
if api_version < min_api_version:
warnings.warn(
f"Client API version is {self.api_version}, minimum supported API version is {min_api_version_string}. "
"You may need to update your client.",
DeprecationWarning,
)
def _parse_semver_string(version: str) -> Optional[Version]:
"""Parse a version string into a semver Version object, stripping off any leading zeros from the components.
If the version string is invalid, returns None.
"""
norm_version = ".".join(s.lstrip("0") for s in version.split("."))
try:
return Version.parse(norm_version)
except ValueError:
return None
def client_factory(
user_agent: Optional[str], api_version: Optional[str] = None
) -> BaseClient:
return BaseClient(user_agent=user_agent, api_version=api_version)
|