-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(middleware): Add customAuthorization middleware
- Loading branch information
1 parent
f1b460d
commit 0a41072
Showing
8 changed files
with
187 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import request from 'request'; | ||
import { get, pick } from '../../../base'; | ||
|
||
const normalizeUrl = url => url.replace(/^\//, ''); | ||
|
||
const checkUrl = (url, targetUrl) => normalizeUrl(url).indexOf(targetUrl) === 0; | ||
|
||
const extractAuthorization = (body, path) => { | ||
try { | ||
const json = JSON.parse(body); | ||
return get(json, path); | ||
} catch (error) { | ||
return false; | ||
} | ||
}; | ||
|
||
const processRequest = (req, res, next, config) => { | ||
const options = { | ||
url: config.authorizationUrl, | ||
headers: pick(req.headers, config.headers) | ||
}; | ||
request.get(options, (error, response, body) => { | ||
const authorization = extractAuthorization(body, config.authPath); | ||
if (authorization) { | ||
const options = { | ||
uri: normalizeUrl(req.url), | ||
method: req.method, | ||
headers: { | ||
Authorization: authorization | ||
} | ||
}; | ||
request(options).pipe(res); | ||
} else { | ||
next(error); | ||
} | ||
}); | ||
}; | ||
|
||
export default (req, res, next) => { | ||
const config = get(req, 'proxy.rule.customAuthorization'); | ||
if (config && checkUrl(req.url, config.targetUrl)) { | ||
return processRequest(req, res, next, config); | ||
} | ||
return next(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import proxyquire from 'proxyquire'; | ||
import reqres from 'reqresnext'; | ||
|
||
const stubRequest = request => | ||
proxyquire( | ||
'../../../../src/servlet/corsproxy/middlewares/customAuthorization', | ||
{ request: request } | ||
).default; | ||
|
||
describe('corsproxy.middleware.customAuthorization', () => { | ||
const authBody = '{"key1":{"key2":"SuchSecretMuchSecurity"}}'; | ||
const rule = { | ||
customAuthorization: { | ||
targetUrl: 'http://target', | ||
authorizationUrl: 'http://authorization', | ||
headers: ['authorization', 'additionalHeader'], | ||
authPath: 'key1.key2' | ||
} | ||
}; | ||
const proxy = { rule }; | ||
const headers = { | ||
authorization: '1', | ||
additionalHeader: '2', | ||
badHeader: '3' | ||
}; | ||
const expectedHeaders = { | ||
authorization: '1', | ||
additionalHeader: '2' | ||
}; | ||
const targetUrl = '/http://target'; | ||
const otherUrl = '/http://other'; | ||
|
||
it('exchanges headers to Authorization', () => { | ||
let authReqOpts = {}; | ||
let proxyedReqOpts = {}; | ||
const request = sinon.stub().callsFake((opts, cb) => { | ||
proxyedReqOpts = opts; | ||
return { pipe: sinon.stub() }; | ||
}); | ||
sinon.stub(request, 'get').callsFake((opts, cb) => { | ||
authReqOpts = opts; | ||
cb(null, {}, authBody); | ||
}); | ||
const customAuthorization = stubRequest(request); | ||
|
||
const { req, res, next } = reqres( | ||
{ proxy, url: targetUrl, headers }, | ||
{} | ||
); | ||
customAuthorization(req, res, next); | ||
|
||
expect(authReqOpts.headers).to.deep.equal(expectedHeaders); | ||
expect(proxyedReqOpts.headers.Authorization).to.be.equal( | ||
'SuchSecretMuchSecurity' | ||
); | ||
}); | ||
|
||
it('does nothing to urls not from config', () => { | ||
const request = sinon.stub(); | ||
const customAuthorization = stubRequest(request); | ||
const pass = sinon.stub(); | ||
|
||
const { req, res, next } = reqres( | ||
{ proxy, url: otherUrl, headers }, | ||
{} | ||
); | ||
customAuthorization(req, res, pass); | ||
|
||
expect(pass.called).to.be.true; | ||
expect(request.called).to.be.false; | ||
}); | ||
}); |