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

Basic Auth Implementation in Retail React App Take 2️⃣ #732

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 4 additions & 2 deletions packages/template-retail-react-app/app/commerce-api/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,9 @@ class Auth {
const authorization = `Basic ${btoa(`${credentials.email}:${credentials.password}`)}`
const options = {
headers: {
Authorization: authorization,
// @@@
// Authorization: authorization,
'X-Authorization': authorization,
'Content-Type': `application/x-www-form-urlencoded`
},
body: {
Expand Down Expand Up @@ -352,7 +354,7 @@ class Auth {

const options = {
headers: {
Authorization: '',
// Authorization: '',
'Content-Type': `application/x-www-form-urlencoded`
},
parameters: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ class CommerceAPI {
const [fetchOptions, ...restParams] = params
const newFetchOptions = {
...fetchOptions,
headers: {...fetchOptions.headers, Authorization: this.auth.authToken}
// @@@
// headers: {...fetchOptions.headers, Authorization: this.auth.authToken}
headers: {...fetchOptions.headers, 'X-Authorization': this.auth.authToken}
}
return [newFetchOptions, ...restParams]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ export const createOcapiFetch = (commerceAPIConfig) => async (
methodName,
body
) => {
const proxy = `/mobify/proxy/ocapi`
// @@@
// const proxy = `/mobify/proxy/ocapi`
const proxy = `/proxy/ocapi`

// The api config will only have `ocapiHost` during testing to workaround localhost proxy
const host = commerceAPIConfig.ocapiHost
Expand Down
80 changes: 80 additions & 0 deletions packages/template-retail-react-app/app/ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {getRuntime} = require('pwa-kit-runtime/ssr/server/express')
const {isRemote} = require('pwa-kit-runtime/utils/ssr-server')
const {getConfig} = require('pwa-kit-runtime/utils/ssr-config')
const helmet = require('helmet')
const {createProxyMiddleware} = require('http-proxy-middleware')

const options = {
// The build directory (an absolute path)
Expand All @@ -32,7 +33,86 @@ const options = {

const runtime = getRuntime()

// @@@
const AUTH = {
username: 'storefront',
password: 'password'
}

function basicAuthMiddleware(req, res, next) {
const shouldSkipAuth =
req.path.startsWith('/proxy') ||
req.path.startsWith('/mobify') ||
req.path.startsWith('/callback')
if (shouldSkipAuth) {
return next()
}

const authorization = (req.get('authorization') || '').split(' ')[1] || ''
const [username, password] = Buffer.from(authorization, 'base64')
.toString()
.split(':')

const hasValidAuth = username == AUTH.username && password == AUTH.password
if (hasValidAuth) {
return next()
}

res.set('WWW-Authenticate', 'Basic realm="Storefront"')
res.status(401).send('Auth Required! :woman-gesturing-no:')
}

// Basic auth credentials are in the `authorization` header, while
// SCAPI/OCAPI JWTs are in `x-authorization`.
function swapAuthHeader(proxyReq, req, res) {
proxyReq.removeHeader('Authorization')

const authorization = proxyReq.getHeader('X-Authorization')
proxyReq.removeHeader('X-Authorization')
// Avoid OCAPI origin protection.
proxyReq.removeHeader('Origin')
if (authorization) {
proxyReq.setHeader('Authorization', authorization)
}
}

// Useful for testing!
const proxyHTTPBin = createProxyMiddleware({
target: 'https://httpbin.org/anything',
secure: true,
changeOrigin: true,
onProxyReq: swapAuthHeader,
pathRewrite: {
'.*': ''
}
})

const proxySCAPI = createProxyMiddleware({
target: 'https://kv7kzm78.api.commercecloud.salesforce.com',
secure: true,
changeOrigin: true,
onProxyReq: swapAuthHeader,
pathRewrite: {
'^/proxy/scapi/': '/'
}
})

const proxyOCAPI = createProxyMiddleware({
target: 'https://zzte-053.sandbox.us02.dx.commercecloud.salesforce.com',
secure: true,
changeOrigin: true,
onProxyReq: swapAuthHeader,
pathRewrite: {
'^/proxy/ocapi/': '/'
}
})

const {handler} = runtime.createHandler(options, (app) => {
app.use(basicAuthMiddleware)
app.all('/proxy/httpbin/*', proxyHTTPBin)
app.all('/proxy/scapi/*', proxySCAPI)
app.all('/proxy/ocapi/*', proxyOCAPI)

// Set HTTP security headers
app.use(
helmet({
Expand Down
4 changes: 3 additions & 1 deletion packages/template-retail-react-app/config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ module.exports = {
},
sites,
commerceAPI: {
proxyPath: `/mobify/proxy/api`,
// @@@
// proxyPath: `/mobify/proxy/api`,
proxyPath: `/proxy/api`,
parameters: {
clientId: 'c9c45bfd-0ed3-4aa2-9971-40f88962b836',
organizationId: 'f_ecom_zzrf_001',
Expand Down
67 changes: 54 additions & 13 deletions packages/template-retail-react-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion packages/template-retail-react-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,8 @@
"iOS >= 9.0",
"Android >= 4.4.4",
"last 4 ChromeAndroid versions"
]
],
"dependencies": {
"http-proxy-middleware": "^2.0.6"
}
}