oauth2-cli-auth 1.1.0__tar.gz → 1.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: oauth2-cli-auth
3
- Version: 1.1.0
3
+ Version: 1.2.0
4
4
  Summary: Authenticate against OAuth2 Provider in Python CLIs
5
5
  Home-page: https://github.com/timo-reymann/python-oauth2-cli-auth
6
6
  License: MIT
@@ -0,0 +1,23 @@
1
+ import signal
2
+
3
+
4
+ class TimeoutException(Exception):
5
+ pass
6
+
7
+
8
+ def _timeout_handler(signum, frame):
9
+ raise TimeoutException("Operation timed out")
10
+
11
+
12
+ def _method_with_timeout(your_method, timeout_seconds=5, *args, **kwargs):
13
+ signal.signal(signal.SIGALRM, _timeout_handler)
14
+ signal.alarm(timeout_seconds)
15
+
16
+ try:
17
+ result = your_method(*args, **kwargs)
18
+ except TimeoutException as te:
19
+ raise te
20
+ finally:
21
+ signal.alarm(0) # Reset the alarm
22
+
23
+ return result
@@ -0,0 +1,25 @@
1
+ import json
2
+ import time
3
+ import urllib
4
+
5
+
6
+ def _urlopen_with_backoff(url, max_retries=3, base_delay=1, timeout=15):
7
+ retries = 0
8
+
9
+ while retries < max_retries:
10
+ try:
11
+ response = urllib.request.urlopen(url, timeout=timeout)
12
+ return response
13
+ except Exception:
14
+ retries += 1
15
+ delay = base_delay * (2 ** retries)
16
+ time.sleep(delay)
17
+
18
+ raise Exception(f"Failed to open URL after {max_retries} retries")
19
+
20
+
21
+ def _load_json(url_or_request: str | urllib.request.Request) -> dict:
22
+ with _urlopen_with_backoff(url_or_request) as response:
23
+ response_data = response.read().decode('utf-8')
24
+ json_response = json.loads(response_data)
25
+ return json_response
@@ -3,7 +3,7 @@ from dataclasses import dataclass
3
3
  import urllib.request
4
4
  import urllib.parse
5
5
  import base64
6
- import json
6
+ from oauth2_cli_auth._urllib_util import _load_json
7
7
 
8
8
 
9
9
  @dataclass
@@ -31,13 +31,6 @@ class OAuth2ClientInfo:
31
31
  )
32
32
 
33
33
 
34
- def _load_json(url_or_request: str | urllib.request.Request) -> dict:
35
- with urllib.request.urlopen(url_or_request) as response:
36
- response_data = response.read().decode('utf-8')
37
- json_response = json.loads(response_data)
38
- return json_response
39
-
40
-
41
34
  def load_oidc_config(odic_well_known_endpoint: str) -> dict:
42
35
  config = _load_json(odic_well_known_endpoint)
43
36
  return config
@@ -0,0 +1,12 @@
1
+ import urllib
2
+ from unittest.mock import patch
3
+
4
+ import pytest
5
+
6
+
7
+ @pytest.fixture
8
+ def create_urlopen_mock():
9
+ def mock(buffer):
10
+ return patch.object(urllib.request, 'urlopen', return_value=buffer)
11
+
12
+ return mock
@@ -2,6 +2,7 @@ from http.server import BaseHTTPRequestHandler, HTTPServer
2
2
  from string import Template
3
3
  from typing import Optional
4
4
  from urllib.parse import parse_qs, urlparse
5
+ from oauth2_cli_auth._timeout import _method_with_timeout, TimeoutException
5
6
 
6
7
 
7
8
  class CallbackPageTemplate:
@@ -150,10 +151,13 @@ class CallbackPageTemplate:
150
151
  class OAuthRedirectHandler(BaseHTTPRequestHandler):
151
152
  callback_template = CallbackPageTemplate()
152
153
 
154
+ def log_message(self, format, *args):
155
+ # silence the log messages
156
+ pass
157
+
153
158
  def do_GET(self):
154
159
  params = parse_qs(urlparse(self.path).query)
155
160
 
156
-
157
161
  has_error = "code" not in params or len(params['code']) != 1 or params['code'][0].strip() == ""
158
162
 
159
163
  if has_error:
@@ -197,9 +201,22 @@ class OAuthCallbackHttpServer(HTTPServer):
197
201
  def callback_url(self):
198
202
  return f"http://localhost:{self.server_port}"
199
203
 
200
- def wait_for_code(self, attempts: int = 3) -> Optional[int]:
204
+ def wait_for_code(self, attempts: int = 3, timeout_per_attempt=10) -> Optional[int]:
205
+ """
206
+ Wait for the server to open the callback page containing the code query parameter.
207
+
208
+ It tries for #attempts with a timeout of #timeout_per_attempts for each attempt.
209
+ This prevents the CLI from getting stuck by unsolved callback URls
210
+
211
+ :param attempts: Amount of attempts
212
+ :param timeout_per_attempt: Timeout for each attempt to be successful
213
+ :return: Code from callback page or None if the callback page is not called successfully
214
+ """
201
215
  for i in range(0, attempts):
202
- self.handle_request()
216
+ try:
217
+ _method_with_timeout(self.handle_request, timeout_seconds=timeout_per_attempt)
218
+ except TimeoutException:
219
+ continue
203
220
  if self.get_code() is not None:
204
221
  return self.get_code()
205
222
 
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "oauth2-cli-auth"
3
- version = "1.1.0"
3
+ version = "1.2.0"
4
4
  description = "Authenticate against OAuth2 Provider in Python CLIs"
5
5
  authors = ["Timo Reymann <mail@timo-reymann.de>"]
6
6
  license = "MIT"
@@ -1,118 +0,0 @@
1
- <html lang="en">
2
-
3
- <head>
4
- <title>bcs</title>
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
6
- <meta name="charset" content="utf-8">
7
- <style>
8
- * {
9
- margin: 0;
10
- padding: 0;
11
- }
12
-
13
- body {
14
- font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
15
- }
16
-
17
- html, body {
18
- display: flex;
19
- align-items: center;
20
- justify-content: center;
21
- height: 100%;
22
- }
23
-
24
- .message {
25
- text-align: center;
26
- padding: 40px;
27
- }
28
-
29
- .animation-ctn {
30
- text-align: center;
31
- margin-top: 5em;
32
- }
33
-
34
- @keyframes checkmark {
35
- 0% {
36
- stroke-dashoffset: 100px
37
- }
38
-
39
- 100% {
40
- stroke-dashoffset: 0px
41
- }
42
- }
43
-
44
- @keyframes checkmark-circle {
45
- 0% {
46
- stroke-dashoffset: 480px
47
- }
48
-
49
- 100% {
50
- stroke-dashoffset: 960px
51
- }
52
- }
53
-
54
- @keyframes colored-circle {
55
- 0% {
56
- opacity: 0
57
- }
58
-
59
- 100% {
60
- opacity: 100
61
- }
62
- }
63
-
64
-
65
- .icon svg polyline {
66
- -webkit-animation: checkmark 0.25s ease-in-out 0.7s backwards;
67
- animation: checkmark 0.25s ease-in-out 0.7s backwards
68
- }
69
-
70
- .icon svg circle {
71
- -webkit-animation: checkmark-circle 0.6s ease-in-out backwards;
72
- animation: checkmark-circle 0.6s ease-in-out backwards;
73
- }
74
-
75
- .icon svg circle#colored {
76
- -webkit-animation: colored-circle 0.6s ease-in-out 0.7s backwards;
77
- animation: colored-circle 0.6s ease-in-out 0.7s backwards;
78
- }
79
- </style>
80
- </head>
81
-
82
- <body>
83
- <div class="message">
84
- <div class="animation-ctn">
85
- <div class="icon">
86
- <svg xmlns="http://www.w3.org/2000/svg" width="154px" height="154px">
87
- <g fill="none" stroke="#22AE73" stroke-width="2">
88
- <circle cx="77" cy="77" r="72"
89
- style="stroke-dasharray:480px, 480px; stroke-dashoffset: 960px;"></circle>
90
- <circle id="colored" fill="#22AE73" cx="77" cy="77" r="72"
91
- style="stroke-dasharray:480px, 480px; stroke-dashoffset: 960px;"></circle>
92
- <polyline class="st0" stroke="#fff" stroke-width="10" points="43.5,77.8 63.7,97.9 112.2,49.4 "
93
- style="stroke-dasharray:100px, 100px; stroke-dashoffset: 200px;"/>
94
- </g>
95
- </svg>
96
- </div>
97
- </div>
98
-
99
- <div class="animation-ctn">
100
- <div class="icon">
101
- <svg xmlns="http://www.w3.org/2000/svg" width="154px" height="154px">
102
- <g fill="none" stroke="#F44812" stroke-width="2">
103
- <circle cx="77" cy="77" r="72"
104
- style="stroke-dasharray:480px, 480px; stroke-dashoffset: 960px;"></circle>
105
- <circle id="colored" fill="#F44812" cx="77" cy="77" r="72"
106
- style="stroke-dasharray:480px, 480px; stroke-dashoffset: 960px;"></circle>
107
- <polyline class="st0" stroke="#fff" stroke-width="10" points="43.5,77.8 112.2,77.8 "
108
- style="stroke-dasharray:100px, 100px; stroke-dashoffset: 200px;"/>
109
- </g>
110
- </svg>
111
- </div>
112
- </div>
113
-
114
- <h1>$title</h1>
115
- <p>$message</p>
116
- </div>
117
- </body>
118
- </html>
File without changes