mirror of
https://github.com/Permissionless-Software-Foundation/psf-bch-api-base.git
synced 2026-09-21 16:52:00 -07:00
fix(basic auth): Rejecting API calls that do not include basic auth header
This commit is contained in:
+7
-6
@@ -6,13 +6,13 @@ RPC_USERNAME=bitcoin
|
|||||||
RPC_PASSWORD=password
|
RPC_PASSWORD=password
|
||||||
|
|
||||||
# Fulcrum Indexer
|
# Fulcrum Indexer
|
||||||
FULCRUM_API=http://192.168.2.127:3001/v1
|
FULCRUM_API=http://172.17.0.1:3001/v1
|
||||||
|
|
||||||
# SLP Indexer
|
# SLP Indexer
|
||||||
SLP_INDEXER_API=http://192.168.2.127:5010
|
SLP_INDEXER_API=http://localhost:5010
|
||||||
|
|
||||||
# REST API URL for wallet operations
|
# REST API URL for wallet operations
|
||||||
LOCAL_RESTURL=http://localhost:5942/v6/
|
LOCAL_RESTURL=http://localhost:5942/v6
|
||||||
|
|
||||||
# END INFRASTRUCTURE SETUP
|
# END INFRASTRUCTURE SETUP
|
||||||
|
|
||||||
@@ -23,7 +23,8 @@ LOCAL_RESTURL=http://localhost:5942/v6/
|
|||||||
X402_ENABLED=false
|
X402_ENABLED=false
|
||||||
|
|
||||||
# Basic Authentication required to access this API?
|
# Basic Authentication required to access this API?
|
||||||
USE_BASIC_AUTH=false
|
USE_BASIC_AUTH=true
|
||||||
#BASIC_AUTH_TOKEN=some-random-token
|
BASIC_AUTH_TOKEN=some-random-token
|
||||||
|
|
||||||
|
# END ACCESS CONTROL
|
||||||
|
|
||||||
# END ACCESS CONTROL
|
|
||||||
+28
-3
@@ -86,7 +86,7 @@ class Server {
|
|||||||
// - If X402_ENABLED=false OR USE_BASIC_AUTH=false: Don't apply x402 (no rate limits)
|
// - If X402_ENABLED=false OR USE_BASIC_AUTH=false: Don't apply x402 (no rate limits)
|
||||||
// - If X402_ENABLED=true AND USE_BASIC_AUTH=true: Apply x402 conditionally (bypass if basic auth valid)
|
// - If X402_ENABLED=true AND USE_BASIC_AUTH=true: Apply x402 conditionally (bypass if basic auth valid)
|
||||||
|
|
||||||
// Only apply x402 if both are enabled
|
// Apply access control middleware based on configuration
|
||||||
if (x402Settings.enabled && basicAuthSettings.enabled) {
|
if (x402Settings.enabled && basicAuthSettings.enabled) {
|
||||||
// X402_ENABLED=true AND USE_BASIC_AUTH=true: Apply x402 conditionally
|
// X402_ENABLED=true AND USE_BASIC_AUTH=true: Apply x402 conditionally
|
||||||
const routes = buildX402Routes(this.config.apiPrefix)
|
const routes = buildX402Routes(this.config.apiPrefix)
|
||||||
@@ -112,9 +112,34 @@ class Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
app.use(conditionalX402Middleware)
|
app.use(conditionalX402Middleware)
|
||||||
|
} else if (basicAuthSettings.enabled && !x402Settings.enabled) {
|
||||||
|
// USE_BASIC_AUTH=true AND X402_ENABLED=false: Require basic auth, reject unauthenticated requests
|
||||||
|
wlogger.info('Basic auth enforcement enabled (x402 disabled)')
|
||||||
|
|
||||||
|
// Middleware that rejects requests without valid basic auth
|
||||||
|
const requireBasicAuthMiddleware = (req, res, next) => {
|
||||||
|
// Skip auth check for health endpoint and root
|
||||||
|
if (req.path === '/health' || req.path === '/') {
|
||||||
|
return next()
|
||||||
|
}
|
||||||
|
|
||||||
|
// If basic auth is valid, allow the request
|
||||||
|
if (req.locals?.basicAuthValid === true) {
|
||||||
|
return next()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reject unauthenticated requests
|
||||||
|
wlogger.warn(`Unauthenticated request rejected: ${req.method} ${req.path}`)
|
||||||
|
return res.status(401).json({
|
||||||
|
error: 'Unauthorized',
|
||||||
|
message: 'Valid Bearer token required in Authorization header'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
app.use(requireBasicAuthMiddleware)
|
||||||
} else {
|
} else {
|
||||||
// X402_ENABLED=false OR USE_BASIC_AUTH=false: No x402 middleware
|
// X402_ENABLED=false AND USE_BASIC_AUTH=false: No access control middleware
|
||||||
wlogger.info('x402 middleware disabled via configuration')
|
wlogger.info('No access control middleware enabled')
|
||||||
}
|
}
|
||||||
|
|
||||||
// Endpoint logging middleware
|
// Endpoint logging middleware
|
||||||
|
|||||||
Reference in New Issue
Block a user