From 8ac655075929eb3c0c5292a75504d9a40e671366 Mon Sep 17 00:00:00 2001 From: toonvanstrijp Date: Tue, 24 Jul 2018 00:38:24 +0200 Subject: [PATCH] Issues fixed with plugin dependencies --- index.js | 15 ++------------- package-lock.json | 12 +++++++++++- package.json | 1 + test/integration/index_test.js | 26 ++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index fad7e9c..bae5063 100755 --- a/index.js +++ b/index.js @@ -9,14 +9,6 @@ function plugin (fastify, options, next) { fastify.decorateRequest('oauth', oauth); - if(!fastify.hasContentTypeParser('application/x-www-form-urlencoded')){ - fastify.addContentTypeParser( - 'application/x-www-form-urlencoded', - {parseAs: 'buffer', bodyLimit: opts.bodyLimit}, - contentParser - ); - } - function oauth(request, reply, next){ if(next === undefined){ oauthServer.authenticate()(request.raw, reply.res, () => { @@ -27,10 +19,6 @@ function plugin (fastify, options, next) { } } - function contentParser (req, body, done) { - done(null, qs.parse(body.toString())); - } - fastify.post(prefix+'/authorize', (req, reply) => { req.raw.query = req.query; req.raw.body = req.body; @@ -53,5 +41,6 @@ function plugin (fastify, options, next) { module.exports = fp(plugin, { fastify: '>= 0.39.0', - name: 'fastify-oauth-server' + name: 'fastify-oauth-server', + dependencies: ['fastify-formbody'] }); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 927b2e2..e0819e7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "fastify-oauth-server", - "version": "2.0.0", + "version": "1.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -472,6 +472,16 @@ "tiny-lru": "^1.6.1" } }, + "fastify-formbody": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/fastify-formbody/-/fastify-formbody-2.0.1.tgz", + "integrity": "sha512-Gk591NV3FuIhSL9hGQncqnTVngPQlu3pgT2ccBuoI4fhS5JaCcDoQ1ck6k8wwWrG62TGLYru4Oj8pMtXXcflUQ==", + "dev": true, + "requires": { + "fastify-plugin": "^1.0.0", + "qs": "^6.5.1" + } + }, "fastify-plugin": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/fastify-plugin/-/fastify-plugin-1.2.0.tgz", diff --git a/package.json b/package.json index d6f953f..2a767ae 100755 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "co-mocha": "^1.1.0", "co-supertest": "0.0.8", "fastify": "^1.8.0", + "fastify-formbody": "^2.0.1", "jshint": "^2.8.0", "mocha": "^2.0.1", "request": "^2.87.0", diff --git a/test/integration/index_test.js b/test/integration/index_test.js index 2bc6a13..daec666 100755 --- a/test/integration/index_test.js +++ b/test/integration/index_test.js @@ -26,6 +26,7 @@ describe('FastifyOAuthServer', function() { beforeEach(function() { app = fastify(); + app.register(require('fastify-formbody')) app.register(require('../../'), {prefix: '/oauth', model: {}}); }); @@ -33,6 +34,8 @@ describe('FastifyOAuthServer', function() { it('should return an error if `model` is empty', function(done) { app = fastify(); + app.register(require('fastify-formbody')) + app.register(require('../../'), {prefix: '/oauth', model: {}}); app.get('/test', (req, res) => { @@ -48,6 +51,9 @@ describe('FastifyOAuthServer', function() { it('should authenticate the request', function(done) { app = fastify(); + + app.register(require('fastify-formbody')) + var tokenExpires = new Date(); tokenExpires.setDate(tokenExpires.getDate() + 1); @@ -73,6 +79,9 @@ describe('FastifyOAuthServer', function() { it('should authenticate the request with middleware', function(done) { app = fastify(); + + app.register(require('fastify-formbody')) + var tokenExpires = new Date(); tokenExpires.setDate(tokenExpires.getDate() + 1); @@ -100,6 +109,9 @@ describe('FastifyOAuthServer', function() { it('should unauthorized the request with middleware', function(done) { app = fastify(); + + app.register(require('fastify-formbody')) + var tokenExpires = new Date(); tokenExpires.setDate(tokenExpires.getDate() - 60000); @@ -125,6 +137,9 @@ describe('FastifyOAuthServer', function() { it('should unauthorized the request with middleware custom response', function(done) { app = fastify(); + + app.register(require('fastify-formbody')) + var tokenExpires = new Date(); tokenExpires.setDate(tokenExpires.getDate() + 60000); @@ -155,6 +170,9 @@ describe('FastifyOAuthServer', function() { describe('authorize()', function() { it('should return an error', function(done) { app = fastify(); + + app.register(require('fastify-formbody')); + var model = { getAccessToken: function() { return { user: {}, accessTokenExpiresAt: new Date() }; @@ -183,6 +201,9 @@ describe('FastifyOAuthServer', function() { it('should return a `location` header with the code', function(done) { app = fastify(); + + app.register(require('fastify-formbody')) + var model = { getAccessToken: function() { return { user: {}, accessTokenExpiresAt: new Date().setDate(new Date().getTime() + 60 * 60 * 1000) }; @@ -208,6 +229,8 @@ describe('FastifyOAuthServer', function() { it('should return an error if `model` is empty', function(done) { app = fastify(); + app.register(require('fastify-formbody')) + app.register(require('../../'), {prefix: '/oauth', model: {}}); request(listen()) @@ -220,6 +243,9 @@ describe('FastifyOAuthServer', function() { describe('token()', function() { it('should return an `access_token`', function(done) { app = fastify(); + + app.register(require('fastify-formbody')) + var model = { getClient: function() { return { grants: ['password'] };