Skip to content

2.x support: oauth.token promise, returns token object instead of sending response #424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,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');
});
Expand Down
28 changes: 27 additions & 1 deletion lib/grant.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,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);
}

Expand Down Expand Up @@ -472,3 +476,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);
}
24 changes: 24 additions & 0 deletions lib/oauth2server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
4 changes: 2 additions & 2 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down