Skip to content

Commit bf3b95c

Browse files
RubenVerborghdmitrizagidulin
authored andcommitted
Add Auth Proxy.
1 parent c2463bc commit bf3b95c

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

lib/handlers/auth-proxy.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// An authentication proxy is a reverse proxy
2+
// that sends a logged-in Solid user's details to a backend
3+
module.exports = addAuthProxyHandlers
4+
5+
const proxy = require('http-proxy-middleware')
6+
const debug = require('../debug')
7+
8+
const PROXY_SETTINGS = {
9+
logLevel: 'silent'
10+
}
11+
12+
// Registers Auth Proxy handlers for each target
13+
function addAuthProxyHandlers (app, targets) {
14+
for (const sourcePath in targets) {
15+
addAuthProxyHandler(app, sourcePath, targets[sourcePath])
16+
}
17+
}
18+
19+
// Registers an Auth Proxy handler for the given target
20+
function addAuthProxyHandler (app, sourcePath, target) {
21+
debug.settings(`Add auth proxy from ${sourcePath} to ${target}`)
22+
23+
// Proxy to the target, removing the source path
24+
// (e.g., /my/proxy/path resolves to http://my.proxy/path)
25+
const sourcePathLength = sourcePath.length
26+
const settings = Object.assign({
27+
target,
28+
pathRewrite: path => path.substr(sourcePathLength)
29+
}, PROXY_SETTINGS)
30+
31+
// Activate the proxy
32+
app.use(`${sourcePath}*`, proxy(settings))
33+
}

test/unit/auth-proxy.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
const authProxy = require('../../lib/handlers/auth-proxy')
2+
const nock = require('nock')
3+
const express = require('express')
4+
const request = require('supertest')
5+
const { expect } = require('chai')
6+
7+
describe('Auth Proxy', () => {
8+
describe('An auth proxy with 2 destinations', () => {
9+
let app
10+
before(() => {
11+
nock('http://server-a.org').persist()
12+
.get(/./).reply(200, addRequestDetails('a'))
13+
nock('https://server-b.org').persist()
14+
.get(/./).reply(200, addRequestDetails('b'))
15+
16+
app = express()
17+
authProxy(app, {
18+
'/server/a': 'http://server-a.org',
19+
'/server/b': 'https://server-b.org/foo/bar'
20+
})
21+
})
22+
23+
describe('responding to /server/a', () => {
24+
let response
25+
before(() => {
26+
return request(app).get('/server/a')
27+
.then(res => { response = res })
28+
})
29+
30+
it('proxies to http://server-a.org/', () => {
31+
const { server, path } = response.body
32+
expect(server).to.equal('a')
33+
expect(path).to.equal('/')
34+
})
35+
36+
it('returns status code 200', () => {
37+
expect(response).to.have.property('statusCode', 200)
38+
})
39+
})
40+
41+
describe('responding to /server/a/my/path?query=string', () => {
42+
let response
43+
before(() => {
44+
return request(app).get('/server/a/my/path?query=string')
45+
.then(res => { response = res })
46+
})
47+
48+
it('proxies to http://server-a.org/my/path?query=string', () => {
49+
const { server, path } = response.body
50+
expect(server).to.equal('a')
51+
expect(path).to.equal('/my/path?query=string')
52+
})
53+
54+
it('returns status code 200', () => {
55+
expect(response).to.have.property('statusCode', 200)
56+
})
57+
})
58+
59+
describe('responding to /server/b', () => {
60+
let response
61+
before(() => {
62+
return request(app).get('/server/b')
63+
.then(res => { response = res })
64+
})
65+
66+
it('proxies to http://server-b.org/foo/bar', () => {
67+
const { server, path } = response.body
68+
expect(server).to.equal('b')
69+
expect(path).to.equal('/foo/bar')
70+
})
71+
72+
it('returns status code 200', () => {
73+
expect(response).to.have.property('statusCode', 200)
74+
})
75+
})
76+
77+
describe('responding to /server/b/my/path?query=string', () => {
78+
let response
79+
before(() => {
80+
return request(app).get('/server/b/my/path?query=string')
81+
.then(res => { response = res })
82+
})
83+
84+
it('proxies to http://server-b.org/foo/bar/my/path?query=string', () => {
85+
const { server, path } = response.body
86+
expect(server).to.equal('b')
87+
expect(path).to.equal('/foo/bar/my/path?query=string')
88+
})
89+
90+
it('returns status code 200', () => {
91+
expect(response).to.have.property('statusCode', 200)
92+
})
93+
})
94+
})
95+
})
96+
97+
function addRequestDetails (server) {
98+
return function (path) {
99+
return { server, path, headers: this.req.headers }
100+
}
101+
}

0 commit comments

Comments
 (0)