Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CH-45 Update django main file template to handle both auth header and cookie #772

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
}
}
}
}
}}}
}
)
Loading