55from apps .authorization .permissions import DataAccessGrantPermission
66from apps .capabilities .permissions import TokenHasProtectedCapability
77
8- from rest_framework import permissions
8+ from rest_framework import permissions # pyright: ignore[reportMissingImports]
9+
10+
11+ def _is_not_empty (s : set ):
12+ if len (s ) > 0 :
13+ return True
14+ else :
15+ return False
916
1017
1118class HasDigitalInsuranceCardScope (permissions .BasePermission ):
19+
20+ required_coverage_search_scopes = ['patient/Coverage.rs' , 'patient/Coverage.s' , 'patient/Coverage.read' ]
21+ required_patient_read_scopes = ['patient/Patient.r' , 'patient/Patient.rs' , 'patient/Patient.read' ]
22+
1223 def has_permission (self , request , view ) -> bool : # type: ignore
13- # TODO - implement scope checking logic
14- return True
24+ # Is this an authorized request? If not, exit.
25+ if request .get ('auth' , None ) is None :
26+ return False
27+
28+ # If we're authenticated, then we can check the scopes from the token.
29+ token_scopes = request .auth .scope
30+ # Two things need to be true:
31+ # 1. At least one of the scopes in the token needs to be one of the above coverage scopes.
32+ # 2. At leaset one of the scopes in the token needs to be one of the above read scopes.
33+ coverage_set = set (HasDigitalInsuranceCardScope .required_coverage_search_scopes )
34+ patient_set = set (HasDigitalInsuranceCardScope .required_patient_read_scopes )
35+ token_set = set (token_scopes )
36+ return (_is_not_empty (coverage_set .intersection (token_set ))
37+ and _is_not_empty (patient_set .intersection (token_set )))
1538
1639
1740class DigitalInsuranceCardSearchView (FhirDataView ):
@@ -27,9 +50,11 @@ class DigitalInsuranceCardSearchView(FhirDataView):
2750 HasDigitalInsuranceCardScope ,
2851 ]
2952
30- required_coverage_search_scopes = ['patient/Coverage.rs' , 'patient/Coverage.s' , 'patient/Coverage.read' ]
31- required_patient_read_scopes = ['patient/Patient.r' , 'patient/Patient.rs' , 'patient/Patient.read' ]
53+ # FIXME: Are these required here? Or, can I put them in the permission class?
54+ # required_coverage_search_scopes = ['patient/Coverage.rs', 'patient/Coverage.s', 'patient/Coverage.read']
55+ # required_patient_read_scopes = ['patient/Patient.r', 'patient/Patient.rs', 'patient/Patient.read']
3256
57+ # TODO/FIXME: What are the version=1? doing? Check/look into.
3358 def __init__ (self , version = 1 ):
3459 super ().__init__ (version )
3560 self .resource_type = 'Bundle'
0 commit comments