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

PGOV-440: Basic auth for frontend application and fix for broken images #55

Merged
merged 4 commits into from
Feb 3, 2025
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
3 changes: 2 additions & 1 deletion src/frontend/lib/drupal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ export const drupal = new DrupalClient(
{
auth: {
clientId: process.env.DRUPAL_CLIENT_ID,
clientSecret: process.env.DRUPAL_CLIENT_SECRET,
clientSecret: process.env.DRUPAL_CLIENT_SECRET
},
headers: {
"Content-Type": "application/json",
'Authorization': `Basic ${btoa(process.env.BASIC_AUTH_USER + ':' + process.env.BASIC_AUTH_PASSWORD)}`
},
},
);
22 changes: 22 additions & 0 deletions src/frontend/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { NextRequest, NextResponse } from 'next/server'

export const config = {
matcher: ['/', '/index'],
}

export default function middleware(req: NextRequest) {
const basicAuth = req.headers.get('authorization')
const url = req.nextUrl

if (basicAuth) {
const authValue = basicAuth.split(' ')[1]
const [user, pwd] = atob(authValue).split(':')

if (user === process.env.BASIC_AUTH_USER && pwd === process.env.BASIC_AUTH_PASSWORD) {
return NextResponse.next()
}
}
url.pathname = '/api/basic-auth'

return NextResponse.rewrite(url)
}
29 changes: 29 additions & 0 deletions src/frontend/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
const path = require("path");


const cspHeader = `
default-src 'self';
script-src 'self' 'unsafe-eval' 'unsafe-inline';
style-src 'self' 'unsafe-inline';
img-src 'self' data: https://pgov-cms.app.cloud.gov;
font-src 'self';
object-src 'none';
base-uri 'self';
form-action 'self';
frame-ancestors 'self' https://pgov-cms.app.cloud.gov;
upgrade-insecure-requests
`;

/** @type {import('next').NextConfig} */
const nextConfig = {
images: {
Expand All @@ -11,6 +25,21 @@ const nextConfig = {
path.join(__dirname, "node_modules", "@uswds", "uswds", "packages"),
],
},
async headers() {
return [
{
source: '/(.*)',
headers: [
{
key: 'Content-Security-Policy',
value: cspHeader
.replace(/\s{2,}/g, " ")
.trim(),
},
],
},
]
}
};

module.exports = nextConfig;
8 changes: 7 additions & 1 deletion src/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
"@types/react": "^18.2.79",
"@types/react-dom": "^18.2.25",
"autoprefixer": "^10.4.19",
"eslint": "^8.57.0",
"eslint": "^8.57.1",
"eslint-config-next": "^14.2.2",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
"husky": "^9.1.7",
"lint-staged": "^15.2.10",
"postcss": "^8.4.19",
Expand All @@ -41,5 +43,9 @@
},
"lint-staged": {
"**/*": "prettier --write --ignore-unknown"
},
"engines": {
"node": "22.11.0",
"npm": "10.9.2"
}
}
7 changes: 7 additions & 0 deletions src/frontend/pages/api/basic-auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { NextApiRequest, NextApiResponse } from 'next'

export default function handler(_: NextApiRequest, res: NextApiResponse) {
res.setHeader('WWW-authenticate', 'Basic realm="Secure Area"')
res.statusCode = 401
res.end(`Auth Required.`)
}