Skip to content

Commit

Permalink
changes for brp obo
Browse files Browse the repository at this point in the history
  • Loading branch information
NvdLaan committed Dec 17, 2024
1 parent cdff444 commit 1a014a3
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ HOST=http://172.17.0.1:8080
USE_DECOS_MOCK_DATA=False
SESSION_COOKIE_AGE=25200
AXES_ENABLED=False
BRP_CLIENT_ID = client_id
BRP_CLIENT_SECRET = client_secret
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ __pycache__/
app/.env
app/.coverage
private_media/
.local.env
3 changes: 3 additions & 0 deletions .local.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Set these in your .local.env file
BRP_CLIENT_ID=
BRP_CLIENT_SECRET=
8 changes: 4 additions & 4 deletions app/apps/addresses/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class AddressViewSet(
serializer_class = AddressSerializer
queryset = Address.objects.all()
lookup_field = "bag_id"
http_method_names = ["get", "patch"]
http_method_names = ["get", "patch", "post"]

def update(self, request, bag_id, *args, **kwargs):
address_instance = Address.objects.get(bag_id=bag_id)
Expand All @@ -56,7 +56,7 @@ def update(self, request, bag_id, *args, **kwargs):

@action(
detail=True,
methods=["get"],
methods=["post"],
serializer_class=ResidentsSerializer,
url_path="residents",
permission_classes=[permissions.CanAccessBRP],
Expand All @@ -81,9 +81,9 @@ def residents_by_bag_id(self, request, bag_id):
# nummeraanduiding_id should have been retrieved, so get BRP data
if address.nummeraanduiding_id:
try:
brp_access_token = request.GET.get("brp_access_token", None)
obo_acces_stoken = request.data.get("obo_acces_stoken")
brp_data, status_code = get_brp_by_nummeraanduiding_id(
request, address.nummeraanduiding_id, brp_access_token
request, address.nummeraanduiding_id, obo_acces_stoken
)
serialized_residents = ResidentsSerializer(data=brp_data)
serialized_residents.is_valid(raise_exception=True)
Expand Down
2 changes: 2 additions & 0 deletions app/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ def filter_traces(envelope):
]
)

BRP_CLIENT_ID = os.getenv("BRP_CLIENT_ID", "BRP_CLIENT_ID")
BRP_CLIENT_SECRET = os.getenv("BRP_CLIENT_SECRET", "BRP_CLIENT_SECRET")
# Secret keys which can be used to access certain parts of the API
SECRET_KEY_TOP_ZAKEN = os.getenv("SECRET_KEY_TOP_ZAKEN", None)
SECRET_KEY_TON_ZAKEN = os.getenv("SECRET_KEY_TON_ZAKEN", None)
Expand Down
30 changes: 23 additions & 7 deletions app/utils/api_queries_brp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

import requests
from django.conf import settings
from tenacity import after_log, retry, stop_after_attempt
from tenacity import retry, stop_after_attempt
from utils.exceptions import MKSPermissionsError

logger = logging.getLogger(__name__)


def get_brp_by_nummeraanduiding_id(request, nummeraanduiding_id, brp_access_token):
def get_brp_by_nummeraanduiding_id(request, nummeraanduiding_id, obo_acces_stoken):
"""Returns BRP data by bag_"""

queryParams = {
"verblijfplaats__identificatiecodenummeraanduiding": f"{nummeraanduiding_id}",
"inclusiefoverledenpersonen": "true",
"expand": "partners,ouders,kinderen",
}
return get_brp(request, queryParams, brp_access_token)
return get_brp(queryParams, obo_acces_stoken)


def get_brp_by_address(request, postal_code, number, suffix, suffix_letter):
Expand All @@ -43,12 +43,11 @@ def get_brp_by_address(request, postal_code, number, suffix, suffix_letter):
return get_brp(request, queryParams)


@retry(stop=stop_after_attempt(3), after=after_log(logger, logging.ERROR))
def get_brp(request, queryParams, brp_access_token):
@retry(stop=stop_after_attempt(3))
def get_brp(queryParams, obo_acces_stoken):
"""Returns BRP data"""

url = f"{settings.BRP_API_URL}"

brp_access_token = get_brp_access_token(obo_acces_stoken)
response = requests.get(
url,
params=queryParams,
Expand All @@ -57,12 +56,29 @@ def get_brp(request, queryParams, brp_access_token):
"Authorization": f"Bearer {brp_access_token}",
},
)
print(response.text)
if response.status_code == 403:
raise MKSPermissionsError()

return response.json(), response.status_code


def get_brp_access_token(obo_acces_stoken):
url = settings.OIDC_OP_TOKEN_ENDPOINT
payload = {
"grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer",
"client_id": settings.BRP_CLIENT_ID,
"client_secret": settings.BRP_CLIENT_SECRET,
"assertion": obo_acces_stoken,
"scope": f"{settings.BRP_CLIENT_ID}/.default",
"requested_token_use": "on_behalf_of",
}

response = requests.request("POST", url, data=payload)
print(response.text)
return response.json().get("access_token")


def get_mock_brp():
return {
"message": "mocked data",
Expand Down
1 change: 1 addition & 0 deletions docker-compose.local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ services:
- zaak-redis
env_file:
- .env
- .local.env
entrypoint: /app/deploy/docker-entrypoint.development.sh
command: python -m debugpy --listen 0.0.0.0:5678 ./manage.py runserver 0.0.0.0:8000
volumes:
Expand Down

0 comments on commit 1a014a3

Please sign in to comment.