Skip to content

Commit 0bc0a4b

Browse files
authored
Merge pull request #772 from MetaCell/feature/ch-45
CH-45 Update django main file template to handle both auth header and cookie
2 parents a0982f1 + 60b02ca commit 0bc0a4b

3 files changed

Lines changed: 38 additions & 31 deletions

File tree

application-templates/django-app/api/templates/main.jinja2

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,17 @@ app.add_middleware(
4949
allow_headers=["*"],
5050
)
5151

52-
from cloudharness.middleware import set_authentication_token
52+
from cloudharness.middleware import set_authentication_token, get_authentication_token
5353
@app.middleware("http")
5454
async def add_process_time_header(request: Request, call_next):
5555
# retrieve the bearer token from the header
5656
# and save it for use in the AuthClient
57-
authorization = request.headers.get('Authorization')
57+
authorization = request.headers.get('Authorization') or request.cookies.get('kc-access')
58+
5859
if authorization:
60+
if 'Bearer ' in authorization:
61+
authorization = authorization.split('Bearer ')[1]
62+
5963
set_authentication_token(authorization)
6064

6165
return await call_next(request)
@@ -67,16 +71,17 @@ if os.environ.get('KUBERNETES_SERVICE_HOST', None):
6771
# start the kafka event listener when running in/for k8s
6872
import cloudharness_django.services.events
6973

70-
# enable the Bearer Authentication
71-
security = HTTPBearer()
72-
73-
async def has_access(credentials: HTTPBasicCredentials = Depends(security)):
74+
async def has_access():
7475
"""
7576
Function that is used to validate the token in the case that it requires it
7677
"""
7778
if not os.environ.get('KUBERNETES_SERVICE_HOST', None):
7879
return {}
79-
token = credentials.credentials
80+
81+
token = get_authentication_token()
82+
83+
if not token:
84+
raise HTTPException(status_code=401)
8085

8186
try:
8287
payload = get_auth_service().get_auth_client().decode_token(token)

application-templates/webapp/frontend/src/App.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import Version from './components/Version';
33

44

55
const Main = () => (
6-
<>
7-
<img src="/logo.png" width="800" />
8-
<h1>__APP_NAME__ React application is working!</h1>
9-
<Version />
10-
<RestTest />
11-
<p>See api documentation <a href="/api/ui/">here</a></p>
12-
</>
13-
)
6+
<>
7+
<img src="/logo.png" width="800" />
8+
<h1>__APP_NAME__ React application is working!</h1>
9+
<Version />
10+
<RestTest />
11+
<p>See api documentation <a href="/api/ui/">here</a></p>
12+
</>
13+
)
1414

1515

1616
export default Main;

application-templates/webapp/frontend/vite.config.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default defineConfig(({ mode }) => {
1010
const env = loadEnv(mode, process.cwd(), '')
1111

1212
const theDomain = env && env.DOMAIN ? env.DOMAIN : 'localhost:5000';
13-
13+
1414
console.log('Dev server address: ', theDomain);
1515

1616
const proxyTarget = theDomain;
@@ -22,21 +22,23 @@ export default defineConfig(({ mode }) => {
2222

2323

2424
return {
25-
plugins: [react()],
26-
server: {
27-
port: 9000,
28-
proxy: {
29-
'/api/': {
30-
target: replaceHost( proxyTarget, 'samples'),
31-
secure: false,
32-
changeOrigin: true,
33-
},
34-
'/proxy/common/api': {
35-
target: replaceHost( proxyTarget, 'common'),
36-
secure: false,
37-
changeOrigin: true,
38-
rewrite: (path) => path.replace(/^\/proxy\/common\/api/, '/api')
25+
plugins: [react()],
26+
server: {
27+
port: 9000,
28+
proxy: {
29+
'/api/': {
30+
target: replaceHost(proxyTarget, 'samples'),
31+
secure: false,
32+
changeOrigin: true,
33+
},
34+
'/proxy/common/api': {
35+
target: replaceHost(proxyTarget, 'common'),
36+
secure: false,
37+
changeOrigin: true,
38+
rewrite: (path) => path.replace(/^\/proxy\/common\/api/, '/api')
39+
}
3940
}
41+
}
4042
}
41-
}}}
43+
}
4244
)

0 commit comments

Comments
 (0)