File tree Expand file tree Collapse file tree 1 file changed +46
-3
lines changed Expand file tree Collapse file tree 1 file changed +46
-3
lines changed Original file line number Diff line number Diff line change 45
45
EXIT_CODE_WHEN_ADDRESS_ALREADY_IN_USE = 13
46
46
47
47
CA_CONTEXT = None
48
+ CODEX_LOGIN_TRACE = os .environ .get ("CODEX_LOGIN_TRACE" , "false" ) in ["true" , "1" ]
49
+
48
50
try :
49
- import ssl
50
- import certifi as _certifi
51
51
52
- CA_CONTEXT = ssl .create_default_context (cafile = _certifi .where ())
52
+ def trace (msg : str ) -> None :
53
+ if CODEX_LOGIN_TRACE :
54
+ print (msg )
55
+
56
+ def attempt_request (method : str ) -> bool :
57
+ try :
58
+ with urllib .request .urlopen (
59
+ urllib .request .Request (
60
+ f"{ DEFAULT_ISSUER } /.well-known/openid-configuration" ,
61
+ method = "GET" ,
62
+ ),
63
+ context = CA_CONTEXT ,
64
+ ) as resp :
65
+ if resp .status != 200 :
66
+ trace (f"Request using { method } failed: { resp .status } " )
67
+ return False
68
+
69
+ trace (f"Request using { method } succeeded" )
70
+ return True
71
+ except Exception as e :
72
+ trace (f"Request using { method } failed: { e } " )
73
+ return False
74
+
75
+ status = attempt_request ("default settings" )
76
+ if not status :
77
+ try :
78
+ import truststore
79
+
80
+ truststore .inject_into_ssl ()
81
+ status = attempt_request ("truststore" )
82
+ except Exception as e :
83
+ trace (f"Failed to use truststore: { e } " )
84
+
85
+ if not status :
86
+ try :
87
+ import ssl
88
+ import certifi as _certifi
89
+
90
+ CA_CONTEXT = ssl .create_default_context (cafile = _certifi .where ())
91
+ status = attempt_request ("certify" )
92
+ except Exception as e :
93
+ trace (f"Failed to use certify: { e } " )
94
+
95
+
53
96
except Exception :
54
97
pass
55
98
You can’t perform that action at this time.
0 commit comments