-
-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: documentation authorization (#466)
* feat: ui hooks extension * add docs * Apply suggestions from code review Co-authored-by: Vincent LE GOFF <vince.legoff@gmail.com> * Update README.md Co-authored-by: Frazer Smith <frazer.dev@outlook.com> Co-authored-by: Vincent LE GOFF <vince.legoff@gmail.com> Co-authored-by: Frazer Smith <frazer.dev@outlook.com>
- Loading branch information
1 parent
2202cbc
commit 1a68013
Showing
6 changed files
with
167 additions
and
2 deletions.
There are no files selected for viewing
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
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,108 @@ | ||
'use strict' | ||
|
||
const { test } = require('tap') | ||
const Fastify = require('fastify') | ||
const yaml = require('js-yaml') | ||
|
||
const fastifySwagger = require('../index') | ||
const { swaggerOption, schemaBody } = require('../examples/options') | ||
|
||
const authOptions = { | ||
validate (username, password, req, reply, done) { | ||
if (username === 'admin' && password === 'admin') { | ||
done() | ||
} else { | ||
done(new Error('Winter is coming')) | ||
} | ||
}, | ||
authenticate: true | ||
} | ||
|
||
function basicAuthEncode (username, password) { | ||
return 'Basic ' + Buffer.from(username + ':' + password).toString('base64') | ||
} | ||
|
||
test('hooks on static swagger', async t => { | ||
const fastify = Fastify() | ||
await fastify.register(require('fastify-basic-auth'), authOptions) | ||
fastify.register(fastifySwagger, { | ||
mode: 'static', | ||
specification: { | ||
path: './examples/example-static-specification.yaml' | ||
}, | ||
exposeRoute: true, | ||
uiHooks: { | ||
onRequest: fastify.basicAuth | ||
} | ||
}) | ||
|
||
let res = await fastify.inject('/documentation') | ||
t.equal(res.statusCode, 401, 'root auth required') | ||
|
||
res = await fastify.inject('/documentation/yaml') | ||
t.equal(res.statusCode, 401, 'auth required yaml') | ||
res = await fastify.inject({ | ||
method: 'GET', | ||
url: '/documentation/yaml', | ||
headers: { authorization: basicAuthEncode('admin', 'admin') } | ||
}) | ||
t.equal(res.statusCode, 200) | ||
t.equal(res.headers['content-type'], 'application/x-yaml') | ||
try { | ||
yaml.load(res.payload) | ||
t.pass('valid swagger yaml') | ||
} catch (err) { | ||
t.fail(err) | ||
} | ||
|
||
res = await fastify.inject('/documentation/json') | ||
t.equal(res.statusCode, 401, 'auth required json') | ||
res = await fastify.inject({ | ||
method: 'GET', | ||
url: '/documentation/json', | ||
headers: { authorization: basicAuthEncode('admin', 'admin') } | ||
}) | ||
t.equal(typeof res.payload, 'string') | ||
t.equal(res.headers['content-type'], 'application/json; charset=utf-8') | ||
try { | ||
yaml.load(res.payload) | ||
t.pass('valid swagger json') | ||
} catch (err) { | ||
t.fail(err) | ||
} | ||
}) | ||
|
||
test('hooks on dynamic swagger', async t => { | ||
const fastify = Fastify() | ||
await fastify.register(require('fastify-basic-auth'), authOptions) | ||
|
||
fastify.register(fastifySwagger, { | ||
...swaggerOption, | ||
exposeRoute: true, | ||
uiHooks: { | ||
onRequest: fastify.basicAuth | ||
} | ||
}) | ||
|
||
fastify.get('/fooBar123', schemaBody, () => {}) | ||
|
||
let res = await fastify.inject('/documentation') | ||
t.equal(res.statusCode, 401, 'root auth required') | ||
|
||
res = await fastify.inject('/documentation/yaml') | ||
t.equal(res.statusCode, 401, 'auth required yaml') | ||
|
||
res = await fastify.inject('/documentation/json') | ||
t.equal(res.statusCode, 401, 'auth required json') | ||
res = await fastify.inject({ | ||
method: 'GET', | ||
url: '/documentation/json', | ||
headers: { authorization: basicAuthEncode('admin', 'admin') } | ||
}) | ||
t.equal(typeof res.payload, 'string') | ||
t.equal(res.headers['content-type'], 'application/json; charset=utf-8') | ||
|
||
const swaggerObject = res.json() | ||
t.ok(swaggerObject.paths) | ||
t.ok(swaggerObject.paths['/fooBar123']) | ||
}) |