Skip to content

Commit 822d58c

Browse files
RubenVerborghdmitrizagidulin
authored andcommitted
Add authProxy option.
1 parent 70f8716 commit 822d58c

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

bin/lib/options.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ module.exports = [
155155
default: '/proxy',
156156
prompt: true
157157
},
158+
{
159+
name: 'authProxy',
160+
help: 'Object with path/server pairs to reverse proxy',
161+
default: {},
162+
prompt: false,
163+
hide: true
164+
},
158165
{
159166
name: 'file-browser',
160167
help: 'Type the URL of default app to use for browsing files (or use default)',

lib/create-app.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const cors = require('cors')
88
const LDP = require('./ldp')
99
const LdpMiddleware = require('./ldp-middleware')
1010
const corsProxy = require('./handlers/cors-proxy')
11+
const authProxy = require('./handlers/auth-proxy')
1112
const SolidHost = require('./models/solid-host')
1213
const AccountManager = require('./models/account-manager')
1314
const vhost = require('vhost')
@@ -52,7 +53,7 @@ function createApp (argv = {}) {
5253
// Serve the public 'common' directory (for shared CSS files, etc)
5354
app.use('/common', express.static('common'))
5455

55-
// Adding proxy
56+
// Add CORS proxy
5657
if (argv.proxy) {
5758
console.error('The proxy configuration option has been renamed to corsProxy.')
5859
argv.corsProxy = argv.proxy
@@ -65,14 +66,21 @@ function createApp (argv = {}) {
6566
// Options handler
6667
app.options('/*', options)
6768

69+
// Set up API
6870
if (argv.apiApps) {
6971
app.use('/api/apps', express.static(argv.apiApps))
7072
}
7173

74+
// Authenticate the user
7275
if (argv.webid) {
7376
initWebId(argv, app, ldp)
7477
}
78+
// Add Auth proxy (requires authentication)
79+
if (argv.authProxy) {
80+
authProxy(app, argv.authProxy)
81+
}
7582

83+
// Attach the LDP middleware
7684
app.use('/', LdpMiddleware(corsSettings))
7785

7886
// Errors

test/integration/auth-proxy.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const ldnode = require('../../index')
2+
const path = require('path')
3+
const nock = require('nock')
4+
const request = require('supertest')
5+
const { expect } = require('chai')
6+
const rm = require('../test-utils').rm
7+
8+
const HOST = 'solid.org'
9+
const USER = 'https://ruben.verborgh.org/profile/#me'
10+
11+
describe('Auth Proxy', () => {
12+
describe('A Solid server with the authProxy option', () => {
13+
let server
14+
before(() => {
15+
// Set up test back-end server
16+
nock('http://server-a.org').persist()
17+
.get(/./).reply(200, function () { return this.req.headers })
18+
19+
// Set up Solid server
20+
server = ldnode({
21+
root: path.join(__dirname, '../resources'),
22+
authProxy: {
23+
'/server/a': 'http://server-a.org'
24+
},
25+
forceUser: USER
26+
})
27+
})
28+
29+
after(() => {
30+
// Release back-end server
31+
nock.cleanAll()
32+
// Remove created index files
33+
rm('index.html')
34+
rm('index.html.acl')
35+
})
36+
37+
describe('responding to /server/a', () => {
38+
let response
39+
before(() => {
40+
return request(server).get('/server/a')
41+
.set('Host', HOST)
42+
.then(res => { response = res })
43+
})
44+
45+
it('sets the User header on the proxy request', () => {
46+
expect(response.body).to.have.property('user', USER)
47+
})
48+
49+
it('returns status code 200', () => {
50+
expect(response).to.have.property('statusCode', 200)
51+
})
52+
})
53+
})
54+
})

test/unit/auth-proxy.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ const USER = 'https://ruben.verborgh.org/profile/#me'
99

1010
describe('Auth Proxy', () => {
1111
describe('An auth proxy with 2 destinations', () => {
12-
let app
1312
let loggedIn = true
13+
14+
let app
1415
before(() => {
16+
// Set up test back-end servers
1517
nock('http://server-a.org').persist()
1618
.get(/./).reply(200, addRequestDetails('a'))
1719
nock('https://server-b.org').persist()
1820
.get(/./).reply(200, addRequestDetails('b'))
1921

22+
// Set up proxy server
2023
app = express()
2124
app.use((req, res, next) => {
2225
if (loggedIn) {
@@ -30,6 +33,11 @@ describe('Auth Proxy', () => {
3033
})
3134
})
3235

36+
after(() => {
37+
// Release back-end servers
38+
nock.cleanAll()
39+
})
40+
3341
describe('responding to /server/a', () => {
3442
let response
3543
before(() => {

0 commit comments

Comments
 (0)