8
8
import urllib .parse
9
9
10
10
from os .path import commonprefix
11
+ from pathlib import Path
11
12
from typing import TYPE_CHECKING
12
13
from typing import Any
13
14
20
21
21
22
from poetry .config .config import Config
22
23
from poetry .exceptions import PoetryException
23
- from poetry .utils .helpers import get_cert
24
- from poetry .utils .helpers import get_client_cert
25
24
from poetry .utils .password_manager import HTTPAuthCredential
26
25
from poetry .utils .password_manager import PasswordManager
27
26
28
27
29
28
if TYPE_CHECKING :
30
- from pathlib import Path
31
-
32
29
from cleo .io .io import IO
33
30
34
31
35
32
logger = logging .getLogger (__name__ )
36
33
37
34
35
+ @dataclasses .dataclass (frozen = True )
36
+ class RepositoryCertificateConfig :
37
+ cert : Path | None = dataclasses .field (default = None )
38
+ client_cert : Path | None = dataclasses .field (default = None )
39
+ verify : bool = dataclasses .field (default = True )
40
+
41
+ @classmethod
42
+ def create (
43
+ cls , repository : str , config : Config | None
44
+ ) -> RepositoryCertificateConfig :
45
+ config = config if config else Config .create ()
46
+
47
+ verify : str | bool = config .get (
48
+ f"certificates.{ repository } .verify" ,
49
+ config .get (f"certificates.{ repository } .cert" , True ),
50
+ )
51
+ client_cert : str = config .get (f"certificates.{ repository } .client-cert" )
52
+
53
+ return cls (
54
+ cert = Path (verify ) if isinstance (verify , str ) else None ,
55
+ client_cert = Path (client_cert ) if client_cert else None ,
56
+ verify = verify if isinstance (verify , bool ) else True ,
57
+ )
58
+
59
+
38
60
@dataclasses .dataclass
39
61
class AuthenticatorRepositoryConfig :
40
62
name : str
@@ -47,11 +69,8 @@ def __post_init__(self) -> None:
47
69
self .netloc = parsed_url .netloc
48
70
self .path = parsed_url .path
49
71
50
- def certs (self , config : Config ) -> dict [str , Path | None ]:
51
- return {
52
- "cert" : get_client_cert (config , self .name ),
53
- "verify" : get_cert (config , self .name ),
54
- }
72
+ def certs (self , config : Config ) -> RepositoryCertificateConfig :
73
+ return RepositoryCertificateConfig .create (self .name , config )
55
74
56
75
@property
57
76
def http_credential_keys (self ) -> list [str ]:
@@ -91,7 +110,7 @@ def __init__(
91
110
self ._io = io
92
111
self ._sessions_for_netloc : dict [str , requests .Session ] = {}
93
112
self ._credentials : dict [str , HTTPAuthCredential ] = {}
94
- self ._certs : dict [str , dict [ str , Path | None ] ] = {}
113
+ self ._certs : dict [str , RepositoryCertificateConfig ] = {}
95
114
self ._configured_repositories : dict [
96
115
str , AuthenticatorRepositoryConfig
97
116
] | None = None
@@ -186,14 +205,13 @@ def request(
186
205
stream = kwargs .get ("stream" )
187
206
188
207
certs = self .get_certs_for_url (url )
189
- verify = kwargs .get ("verify" ) or certs .get ( " verify" )
190
- cert = kwargs .get ("cert" ) or certs .get ( "cert" )
208
+ verify = kwargs .get ("verify" ) or certs .cert or certs . verify
209
+ cert = kwargs .get ("cert" ) or certs .client_cert
191
210
192
211
if cert is not None :
193
212
cert = str (cert )
194
213
195
- if verify is not None :
196
- verify = str (verify )
214
+ verify = str (verify ) if isinstance (verify , Path ) else verify
197
215
198
216
settings = session .merge_environment_settings ( # type: ignore[no-untyped-call]
199
217
prepared_request .url , proxies , stream , verify , cert
@@ -332,6 +350,11 @@ def get_http_auth(
332
350
repository = repository , username = username
333
351
)
334
352
353
+ def get_certs_for_repository (self , name : str ) -> RepositoryCertificateConfig :
354
+ if name .lower () == "pypi" or name not in self .configured_repositories :
355
+ return RepositoryCertificateConfig ()
356
+ return self .configured_repositories [name ].certs (self ._config )
357
+
335
358
@property
336
359
def configured_repositories (self ) -> dict [str , AuthenticatorRepositoryConfig ]:
337
360
if self ._configured_repositories is None :
@@ -352,7 +375,7 @@ def add_repository(self, name: str, url: str) -> None:
352
375
self .configured_repositories [name ] = AuthenticatorRepositoryConfig (name , url )
353
376
self .reset_credentials_cache ()
354
377
355
- def get_certs_for_url (self , url : str ) -> dict [ str , Path | None ] :
378
+ def get_certs_for_url (self , url : str ) -> RepositoryCertificateConfig :
356
379
if url not in self ._certs :
357
380
self ._certs [url ] = self ._get_certs_for_url (url )
358
381
return self ._certs [url ]
@@ -398,11 +421,11 @@ def _get_repository_config_for_url(
398
421
399
422
return candidates [0 ]
400
423
401
- def _get_certs_for_url (self , url : str ) -> dict [ str , Path | None ] :
424
+ def _get_certs_for_url (self , url : str ) -> RepositoryCertificateConfig :
402
425
selected = self .get_repository_config_for_url (url )
403
426
if selected :
404
427
return selected .certs (config = self ._config )
405
- return { "cert" : None , "verify" : None }
428
+ return RepositoryCertificateConfig ()
406
429
407
430
408
431
_authenticator : Authenticator | None = None
0 commit comments