Skip to content

Commit 72eb40e

Browse files
committed
chore: replace bare except, remove duplicate import, consolidate utils
1 parent f8d91c7 commit 72eb40e

4 files changed

Lines changed: 18 additions & 11 deletions

File tree

testbed/core/forms/__init__.py

Whitespace-only changes.

testbed/core/utils/oauth_utils.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import logging
2+
import string
23
import secrets # Python's secure random number generator module
34
import base64 # For encoding binary data as text
45
from oauth2_provider.models import get_application_model
5-
from testbed.core.utils.utils import random_client_id, random_client_secret
66

77
logger = logging.getLogger(__name__)
88
Application = get_application_model()
@@ -20,6 +20,17 @@
2020
TOKEN_EXPIRY_SESSION_KEY = 'lola_token_expiry'
2121
TOKEN_SCOPE_SESSION_KEY = 'lola_token_scope'
2222

23+
24+
def random_client_id(length=10):
25+
"""Generate a random alphanumeric OAuth client ID of the given length."""
26+
return ''.join(secrets.choice(string.ascii_letters + string.digits) for _ in range(length))
27+
28+
29+
def random_client_secret(length=40):
30+
"""Generate a random alphanumeric OAuth client secret of the given length."""
31+
return ''.join(secrets.choice(string.ascii_letters + string.digits) for _ in range(length))
32+
33+
2334
# Get or create the single OAuth Application for a user.
2435
# This enforces the one-application-per-user approach where each user
2536
# represents an ActivityPub service in the LOLA portability flow.

testbed/core/utils/utils.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

testbed/core/views.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from django.urls import reverse
2323
import logging
2424
import requests
25-
from django.urls import reverse
2625
from django.conf import settings
2726
from functools import wraps
2827

@@ -769,7 +768,12 @@ def test_token_exchange_view(request):
769768
context['token_error'] = f"Error: {error_details.get('error', 'Unknown error')}"
770769
if 'error_description' in error_details:
771770
context['token_error'] += f" - {error_details['error_description']}"
772-
except:
771+
except (ValueError, KeyError):
772+
# ValueError catches JSONDecodeError (response body is not valid JSON).
773+
# KeyError is included defensively; in practice it cannot fire here
774+
# because error_details['error_description'] is guarded by the 'in' check above.
775+
# Note: AttributeError/TypeError (non-dict JSON) fall through to the outer
776+
# except Exception handler at the call site with a less precise error message.
773777
logger.warning(f"Response text: {token_response.text}")
774778
context['token_error'] = f"Error: HTTP {token_response.status_code} - {token_response.text}"
775779

0 commit comments

Comments
 (0)