From 2378c18e6a116b20a33824a15fd82a81b271e9b9 Mon Sep 17 00:00:00 2001 From: Frane Cagalj Date: Wed, 15 Mar 2017 00:51:34 +0100 Subject: [PATCH] oauth.token promise, returns token object instead of sending response --- Readme.md | 8 ++++++++ lib/grant.js | 28 +++++++++++++++++++++++++++- lib/oauth2server.js | 24 ++++++++++++++++++++++++ lib/runner.js | 4 ++-- 4 files changed, 61 insertions(+), 3 deletions(-) diff --git a/Readme.md b/Readme.md index 14db76c43..07a17b0c8 100644 --- a/Readme.md +++ b/Readme.md @@ -29,6 +29,14 @@ app.oauth = oauthserver({ app.all('/oauth/token', app.oauth.grant()); +app.get('/custom/token', function (req, res) { + + app.oauth.token(req) + .then(function(tokenRes) { + res.send(tokenRes); + }) +}); + app.get('/', app.oauth.authorise(), function (req, res) { res.send('Secret area'); }); diff --git a/lib/grant.js b/lib/grant.js index 2ebbcd817..ba0c09203 100644 --- a/lib/grant.js +++ b/lib/grant.js @@ -46,13 +46,17 @@ var fns = [ * @param {Object} res * @param {Function} next */ -function Grant (config, req, res, next) { +function Grant(config, req, res, next, returnPromise) { this.config = config; this.model = config.model; this.now = new Date(); this.req = req; this.res = res; + if (returnPromise) { + fns[fns.length - 1] = returnToken; + } + runner(fns, this, next); } @@ -453,3 +457,25 @@ function sendResponse (done) { if (this.config.continueAfterResponse) done(); } + +/** + * Create an access token and call callback done with token response. + * + * @param {Function} done + * @this OAuth + */ +function returnToken(done) { + + var response = { + token_type: 'bearer', + access_token: this.accessToken + }; + + if (this.config.accessTokenLifetime !== null) { + response.expires_in = this.config.accessTokenLifetime; + } + + if (this.refreshToken) response.refresh_token = this.refreshToken; + + done(null, response); +} \ No newline at end of file diff --git a/lib/oauth2server.js b/lib/oauth2server.js index fc6b9c1dc..dd5f3ab03 100644 --- a/lib/oauth2server.js +++ b/lib/oauth2server.js @@ -89,6 +89,30 @@ OAuth2Server.prototype.grant = function () { }; }; +/** + * Token Promise + * + * Returns promise that will grant tokens to valid requests, + * and return object with access token, expire period and token type + * + * @return {Function} middleware + */ +OAuth2Server.prototype.token = function (req) { + var self = this; + + return new Promise((resolve, reject) => { + + function cb(err, response) { + if(err){ + return reject(err); + } + return resolve(response); + } + + new Grant(self, req, {}, cb, true); + }); +}; + /** * Code Auth Grant Middleware * diff --git a/lib/runner.js b/lib/runner.js index 9b25e6213..1212e2e4b 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -12,8 +12,8 @@ function runner (fns, context, next) { var last = fns.length - 1; (function run(pos) { - fns[pos].call(context, function (err) { - if (err || pos === last) return next(err); + fns[pos].call(context, function (err, res) { + if (err || pos === last) return next(err, res); run(++pos); }); })(0);