Skip to content

Commit

Permalink
Merge pull request #772 from MetaCell/feature/ch-45
Browse files Browse the repository at this point in the history
CH-45 Update django main file template to handle both auth header and cookie
  • Loading branch information
filippomc authored Sep 19, 2024
2 parents a0982f1 + 60b02ca commit 0bc0a4b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 31 deletions.
19 changes: 12 additions & 7 deletions application-templates/django-app/api/templates/main.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ app.add_middleware(
allow_headers=["*"],
)

from cloudharness.middleware import set_authentication_token
from cloudharness.middleware import set_authentication_token, get_authentication_token
@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
# retrieve the bearer token from the header
# and save it for use in the AuthClient
authorization = request.headers.get('Authorization')
authorization = request.headers.get('Authorization') or request.cookies.get('kc-access')

if authorization:
if 'Bearer ' in authorization:
authorization = authorization.split('Bearer ')[1]

set_authentication_token(authorization)

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

# enable the Bearer Authentication
security = HTTPBearer()

async def has_access(credentials: HTTPBasicCredentials = Depends(security)):
async def has_access():
"""
Function that is used to validate the token in the case that it requires it
"""
if not os.environ.get('KUBERNETES_SERVICE_HOST', None):
return {}
token = credentials.credentials

token = get_authentication_token()

if not token:
raise HTTPException(status_code=401)

try:
payload = get_auth_service().get_auth_client().decode_token(token)
Expand Down
16 changes: 8 additions & 8 deletions application-templates/webapp/frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import Version from './components/Version';


const Main = () => (
<>
<img src="/logo.png" width="800" />
<h1>__APP_NAME__ React application is working!</h1>
<Version />
<RestTest />
<p>See api documentation <a href="/api/ui/">here</a></p>
</>
)
<>
<img src="/logo.png" width="800" />
<h1>__APP_NAME__ React application is working!</h1>
<Version />
<RestTest />
<p>See api documentation <a href="/api/ui/">here</a></p>
</>
)


export default Main;
34 changes: 18 additions & 16 deletions application-templates/webapp/frontend/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '')

const theDomain = env && env.DOMAIN ? env.DOMAIN : 'localhost:5000';

console.log('Dev server address: ', theDomain);

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


return {
plugins: [react()],
server: {
port: 9000,
proxy: {
'/api/': {
target: replaceHost( proxyTarget, 'samples'),
secure: false,
changeOrigin: true,
},
'/proxy/common/api': {
target: replaceHost( proxyTarget, 'common'),
secure: false,
changeOrigin: true,
rewrite: (path) => path.replace(/^\/proxy\/common\/api/, '/api')
plugins: [react()],
server: {
port: 9000,
proxy: {
'/api/': {
target: replaceHost(proxyTarget, 'samples'),
secure: false,
changeOrigin: true,
},
'/proxy/common/api': {
target: replaceHost(proxyTarget, 'common'),
secure: false,
changeOrigin: true,
rewrite: (path) => path.replace(/^\/proxy\/common\/api/, '/api')
}
}
}
}
}}}
}
)

0 comments on commit 0bc0a4b

Please sign in to comment.