Skip to content

Commit 03a5827

Browse files
committed
Interim; testing an idea.
1 parent a56d698 commit 03a5827

File tree

5 files changed

+50
-8
lines changed

5 files changed

+50
-8
lines changed

apps/authorization/permissions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class DataAccessGrantPermission(permissions.BasePermission):
99
"""
1010
Permission check for a Grant related to the token used.
1111
"""
12+
1213
def has_permission(self, request, view) -> bool: # type: ignore
1314
dag = None
1415
try:

apps/capabilities/permissions.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
from .models import ProtectedCapability
1010

11+
import apps.logging.request_logger as logging
12+
13+
logger = logging.getLogger(logging.DEBUG_GENERAL)
14+
1115

1216
class BBCapabilitiesPermissionTokenScopeMissingException(APIException):
1317
# BB2-237 custom exception
@@ -17,6 +21,7 @@ class BBCapabilitiesPermissionTokenScopeMissingException(APIException):
1721
class TokenHasProtectedCapability(permissions.BasePermission):
1822

1923
def has_permission(self, request, view) -> bool: # type: ignore
24+
logger.warning({"has_permission": "start"})
2025
token = request.auth
2126
access_token_query_param = request.GET.get("access_token", None)
2227

@@ -27,9 +32,11 @@ def has_permission(self, request, view) -> bool: # type: ignore
2732
)
2833

2934
if not token:
35+
logger.warning("has_permission: not token")
3036
return False
3137

3238
if not switch_is_active("require-scopes"):
39+
logger.warning("has_permission: switch_is_active('require-scopes')")
3340
return True
3441

3542
if hasattr(token, "scope"): # OAuth 2
@@ -43,17 +50,33 @@ def has_permission(self, request, view) -> bool: # type: ignore
4350
slug__in=token_scopes
4451
).values_list('protected_resources', flat=True).all())
4552

53+
logger.warning({"token_scopes": token_scopes, "scopes": scopes})
54+
4655
for scope in scopes:
4756
for method, path in json.loads(scope):
57+
logger.warning({"scope in scopes": scope,
58+
"method": method,
59+
"path": path,
60+
"request.method": request.method,
61+
"request.path": request.path})
4862
if method != request.method:
63+
logger.warning({"A": 1})
64+
logger.warning({"request_method": request.method})
4965
continue
5066
if path == request.path:
67+
logger.warning({"A": 2})
68+
logger.warning({"path == request.path": (path == request.path)})
5169
return True
5270
if re.fullmatch(path, request.path) is not None:
71+
logger.warning({"A": 3})
5372
return True
73+
logger.warning({"end-of-scope-in-scopes loop": "here"})
74+
75+
logger.warning("has_permission: scope not matched/found")
5476
return False
5577
else:
5678
# BB2-237: Replaces ASSERT with exception. We should never reach here.
5779
mesg = ("TokenHasScope requires the `oauth2_provider.rest_framework.OAuth2Authentication`"
5880
" authentication class to be used.")
81+
logger.warning("has_permission: end of line scope missing exception")
5982
raise BBCapabilitiesPermissionTokenScopeMissingException(mesg)

apps/fhir/bluebutton/views/insurancecard.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
ResourcePermission,
44
ApplicationActivePermission)
55
from apps.authorization.permissions import DataAccessGrantPermission
6-
from apps.capabilities.permissions import TokenHasProtectedCapability
6+
# FIXME: removed for local testing
7+
# from apps.capabilities.permissions import TokenHasProtectedCapability
78
from django.http import JsonResponse
89

910
from rest_framework import permissions # pyright: ignore[reportMissingImports]
1011

12+
from apps.versions import noisy_has_permission
13+
1114

1215
def _is_not_empty(s: set) -> bool:
1316
if len(s) > 0:
@@ -56,12 +59,12 @@ class DigitalInsuranceCardView(FhirDataView):
5659

5760
permission_classes = [
5861
permissions.IsAuthenticated,
59-
ApplicationActivePermission,
60-
ResourcePermission,
61-
SearchCrosswalkPermission,
62-
DataAccessGrantPermission,
63-
TokenHasProtectedCapability,
64-
HasDigitalInsuranceCardScope,
62+
noisy_has_permission(ApplicationActivePermission),
63+
noisy_has_permission(ResourcePermission),
64+
noisy_has_permission(SearchCrosswalkPermission),
65+
noisy_has_permission(DataAccessGrantPermission),
66+
# noisy_has_permission(TokenHasProtectedCapability),
67+
noisy_has_permission(HasDigitalInsuranceCardScope),
6568
]
6669

6770
# FIXME: Are these required here? Or, can I put them in the permission class?
@@ -103,11 +106,13 @@ def has_permission(self, request, view):
103106
# if required_scopes is None:
104107
# return False
105108
# return request.user.is_authenticated and hasattr(request.user, 'crosswalk')
109+
print("HAS_PERMISSION IN DIGITALINSURANCECARD")
106110
return True
107111

108112
def build_parameters(self, request):
113+
print("BUILD_PARAMETERS IN DIGITALINSURANCECARD")
109114
return {
110-
'_format': 'application/json'
115+
'_format': 'application/fhir+json'
111116
}
112117

113118
def build_url(self, fhir_settings, resource_type, resource_id=None, *args, **kwargs):

apps/logging/request_logger.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
AUDIT_CREDS_REQUEST_LOGGER = "audit.creds.request"
2929
AUDIT_APPLICATION_TYPE_CHANGE = "audit.application.type.change"
3030
PERFORMANCE_LOGGER = 'performance'
31+
DEBUG_GENERAL = "debug.general_logger"
3132

3233
LOGGER_NAMES = [
3334
AUDIT_BASIC_LOGGER,

apps/versions.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22
# we should use this class as opposed to interned strings.
33
# e.g. A use of 'v1' should become Versions.V1.
44

5+
def noisy_has_permission(original_class):
6+
orig_has_perm = original_class.has_permission
7+
8+
def has_permission(self, request, view):
9+
class_obj = self.__class__
10+
class_name = class_obj.__name__
11+
print(f" --> has_permission: {class_name} <--")
12+
return orig_has_perm(self, request, view)
13+
14+
original_class.has_permission = has_permission
15+
return original_class
16+
517

618
class VersionNotMatched(Exception):
719
"""

0 commit comments

Comments
 (0)