Skip to content

Commit 89b6133

Browse files
RubenVerborghdmitrizagidulin
authored andcommitted
Set User header on proxied requests.
1 parent bf3b95c commit 89b6133

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

lib/handlers/auth-proxy.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,18 @@ function addAuthProxyHandler (app, sourcePath, target) {
2525
const sourcePathLength = sourcePath.length
2626
const settings = Object.assign({
2727
target,
28+
onProxyReq: addUserHeader,
29+
onProxyReqWs: addUserHeader,
2830
pathRewrite: path => path.substr(sourcePathLength)
2931
}, PROXY_SETTINGS)
3032

3133
// Activate the proxy
3234
app.use(`${sourcePath}*`, proxy(settings))
3335
}
36+
37+
// Adds a User header with the user's ID if the user is logged in
38+
function addUserHeader (proxyReq, req) {
39+
if (req.session && req.session.userId) {
40+
proxyReq.setHeader('User', req.session.userId)
41+
}
42+
}

test/unit/auth-proxy.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,25 @@ const express = require('express')
44
const request = require('supertest')
55
const { expect } = require('chai')
66

7+
const USER = 'https://ruben.verborgh.org/profile/#me'
8+
79
describe('Auth Proxy', () => {
810
describe('An auth proxy with 2 destinations', () => {
911
let app
12+
let loggedIn = true
1013
before(() => {
1114
nock('http://server-a.org').persist()
1215
.get(/./).reply(200, addRequestDetails('a'))
1316
nock('https://server-b.org').persist()
1417
.get(/./).reply(200, addRequestDetails('b'))
1518

1619
app = express()
20+
app.use((req, res, next) => {
21+
if (loggedIn) {
22+
req.session = { userId: USER }
23+
}
24+
next()
25+
})
1726
authProxy(app, {
1827
'/server/a': 'http://server-a.org',
1928
'/server/b': 'https://server-b.org/foo/bar'
@@ -33,6 +42,11 @@ describe('Auth Proxy', () => {
3342
expect(path).to.equal('/')
3443
})
3544

45+
it('sets the User header on the proxy request', () => {
46+
const { headers } = response.body
47+
expect(headers).to.have.property('user', USER)
48+
})
49+
3650
it('returns status code 200', () => {
3751
expect(response).to.have.property('statusCode', 200)
3852
})
@@ -51,6 +65,11 @@ describe('Auth Proxy', () => {
5165
expect(path).to.equal('/my/path?query=string')
5266
})
5367

68+
it('sets the User header on the proxy request', () => {
69+
const { headers } = response.body
70+
expect(headers).to.have.property('user', USER)
71+
})
72+
5473
it('returns status code 200', () => {
5574
expect(response).to.have.property('statusCode', 200)
5675
})
@@ -69,6 +88,11 @@ describe('Auth Proxy', () => {
6988
expect(path).to.equal('/foo/bar')
7089
})
7190

91+
it('sets the User header on the proxy request', () => {
92+
const { headers } = response.body
93+
expect(headers).to.have.property('user', USER)
94+
})
95+
7296
it('returns status code 200', () => {
7397
expect(response).to.have.property('statusCode', 200)
7498
})
@@ -87,6 +111,38 @@ describe('Auth Proxy', () => {
87111
expect(path).to.equal('/foo/bar/my/path?query=string')
88112
})
89113

114+
it('sets the User header on the proxy request', () => {
115+
const { headers } = response.body
116+
expect(headers).to.have.property('user', USER)
117+
})
118+
119+
it('returns status code 200', () => {
120+
expect(response).to.have.property('statusCode', 200)
121+
})
122+
})
123+
124+
describe('responding to /server/a without a logged-in user', () => {
125+
let response
126+
before(() => {
127+
loggedIn = false
128+
return request(app).get('/server/a')
129+
.then(res => { response = res })
130+
})
131+
after(() => {
132+
loggedIn = true
133+
})
134+
135+
it('proxies to http://server-a.org/', () => {
136+
const { server, path } = response.body
137+
expect(server).to.equal('a')
138+
expect(path).to.equal('/')
139+
})
140+
141+
it('does not set the User header on the proxy request', () => {
142+
const { headers } = response.body
143+
expect(headers).to.not.have.property('user')
144+
})
145+
90146
it('returns status code 200', () => {
91147
expect(response).to.have.property('statusCode', 200)
92148
})

0 commit comments

Comments
 (0)