Skip to content

Commit 627773c

Browse files
authored
all endpoints now work with and without slash. They will also set the correct location. (#6)
1 parent fe3e7ef commit 627773c

File tree

8 files changed

+73
-16
lines changed

8 files changed

+73
-16
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ on:
66
- 'main'
77
tags:
88
- "v*.*.*"
9+
pull_request:
10+
branches:
11+
- "main"
912

1013
jobs:
1114
push-images:
@@ -44,6 +47,6 @@ jobs:
4447
uses: docker/build-push-action@v3
4548
with:
4649
context: .
47-
push: ${{ github.event_name != 'pull_request' }}
50+
push: true
4851
tags: ${{ steps.meta.outputs.tags }}
4952
labels: ${{ steps.meta.outputs.labels }}

api/base/endpoints/redirect.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
router = APIRouter()
55

6+
67
@router.get("/", include_in_schema=False)
78
async def redirect():
8-
response = RedirectResponse(url='redoc')
9+
response = RedirectResponse(url="redoc")
910
return response

api/v1/endpoints/register.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22

3-
from fastapi import APIRouter
3+
from fastapi import APIRouter, Request
44
from fastapi.responses import JSONResponse
55

66
from config.settings import settings
@@ -17,7 +17,13 @@
1717
responses={400: {"model": response.BadRequest}},
1818
tags=["register"],
1919
)
20-
def create_propagated_user(user: models.User):
20+
@router.post(
21+
"/register",
22+
response_model=response.TokenPropagated,
23+
responses={400: {"model": response.BadRequest}},
24+
tags=["register"],
25+
)
26+
async def create_propagated_user(user: models.User, request: Request):
2127
"""
2228
Create the requested client_ids with different APIs.
2329
@@ -57,7 +63,14 @@ def create_propagated_user(user: models.User):
5763
created = settings.ZGW_CLIENT.autorisatie.create_user(body=body)
5864

5965
if created.status_code != 201:
60-
return JSONResponse(status_code=400, content={"message": created.json()})
66+
if settings.ENV.lower() == "kubernetes":
67+
https_url = request.url.replace(scheme="https")
68+
headers = {"Location": str(https_url)}
69+
return JSONResponse(status_code=400, content={"message": created.json()}, headers=headers)
70+
71+
else:
72+
return JSONResponse(status_code=400, content={"message": created.json()})
73+
6174

6275
logging.debug(f"got a response {str(created.status_code)} when creating new user")
6376
logging.info(
@@ -77,4 +90,21 @@ def create_propagated_user(user: models.User):
7790
logging.info(f"propagated to all apis result: {str(propagated)}")
7891

7992
token = tokens.create_token(user_ids[0], secret)
80-
return {"authorization": f"Bearer {token}", "propagated": propagated}
93+
propagated_list = [
94+
{
95+
"endpoint": propagation.endpoint,
96+
"success": propagation.success,
97+
"client_id": propagation.client_id,
98+
}
99+
for propagation in propagated
100+
]
101+
102+
if settings.ENV.lower() == "kubernetes":
103+
https_url = request.url.replace(scheme="https")
104+
headers = {"Location": str(https_url)}
105+
logging.info(https_url)
106+
logging.info(headers)
107+
return JSONResponse(status_code=200, content={"authorization": f"Bearer {token}", "propagated": propagated_list}, headers=headers)
108+
109+
else:
110+
return JSONResponse(status_code=200, content={"authorization": f"Bearer {token}", "propagated": propagated_list})

api/v1/endpoints/status.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22

3-
from fastapi import APIRouter
3+
from fastapi import APIRouter, Request
44
from fastapi.responses import JSONResponse
55

66
from config.settings import settings
@@ -15,7 +15,13 @@
1515
responses={400: {"model": response.Health}},
1616
tags=["status"],
1717
)
18-
async def check_health():
18+
@router.get(
19+
"/status",
20+
response_model=response.Health,
21+
responses={400: {"model": response.Health}},
22+
tags=["status"],
23+
)
24+
async def check_health(request: Request):
1925
"""
2026
Check health before creating tokens:
2127
@@ -25,4 +31,11 @@ async def check_health():
2531
status = settings.ZGW_CLIENT.check_availability_of_apis()
2632
if status:
2733
code = 200
28-
return JSONResponse(status_code=code, content={"health": status})
34+
35+
if settings.ENV.lower() == "kubernetes":
36+
https_url = request.url.replace(scheme="https")
37+
headers = {"Location": str(https_url)}
38+
return JSONResponse(status_code=code, content={"health": status}, headers=headers)
39+
40+
else:
41+
return JSONResponse(status_code=code, content={"health": status})

api/v1/endpoints/tokens.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
from fastapi import APIRouter
1+
from fastapi import APIRouter, Request
22

33
from models import response
44
from models import token as models
55
from util import tokens
6+
from fastapi.responses import JSONResponse
7+
8+
from config.settings import settings
69

710
router = APIRouter()
811

912

1013
@router.post("/tokens/", response_model=response.TokenCreated, tags=["tokens"])
11-
async def create_token_endpoint(token: models.Token):
14+
@router.post("/tokens", response_model=response.TokenCreated, tags=["tokens"])
15+
async def create_token_endpoint(token: models.Token, request: Request):
1216
"""
1317
Create a token based on an existing set of clientId and secret.
1418
And this path operation will:
@@ -17,4 +21,11 @@ async def create_token_endpoint(token: models.Token):
1721
* Returns the token to be used by the client.
1822
"""
1923
created = tokens.create_token(identifier=token.client_id[0], secret=token.secret)
20-
return {"authorization": f"Bearer {created}"}
24+
resp = {"authorization": f"Bearer {created}"}
25+
if settings.ENV.lower() == "kubernetes":
26+
https_url = request.url.replace(scheme="https")
27+
headers = {"Location": str(https_url)}
28+
return JSONResponse(status_code=200, content=resp, headers=headers)
29+
30+
else:
31+
return JSONResponse(status_code=200, content=resp)

main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@
4747
},
4848
openapi_tags=tags_metadata,
4949
openapi_url="/api/v1/openapi.json",
50+
trusting_proxy=True,
5051
)
5152

52-
5353
# Set all CORS enabled origins
5454
if settings.BACKEND_CORS_ORIGINS:
5555
app.add_middleware(
@@ -60,6 +60,7 @@
6060
allow_headers=["*"],
6161
)
6262

63+
6364
app.add_middleware(
6465
TrustedHostMiddleware, allowed_hosts=settings.HOSTS
6566
)

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ idna==3.4
99
pydantic==1.10.2
1010
PyJWT==2.6.0
1111
python-dotenv==0.21.0
12-
PyYAML==6.0
12+
PyYAML==6.0.1
1313
requests==2.28.1
1414
sniffio==1.3.0
1515
starlette==0.22.0

util/tokens.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ def create_token(identifier: str, secret: str):
99
"iss": identifier,
1010
"iat": int(time.time()),
1111
"client_id": identifier,
12-
"user_id": "",
13-
"user_representation": "",
1412
}
1513

1614
headers = {"client_identifier": identifier}

0 commit comments

Comments
 (0)