diff --git a/lib/grant/token.js b/lib/grant/token.js index aaf2a2fe..217197be 100644 --- a/lib/grant/token.js +++ b/lib/grant/token.js @@ -84,7 +84,8 @@ module.exports = function token(options, issue) { var clientID = req.query.client_id , redirectURI = req.query.redirect_uri , scope = req.query.scope - , state = req.query.state; + , state = req.query.state + , immediate = req.query.immediate === 'true'; if (!clientID) { throw new AuthorizationError('Missing required parameter: client_id', 'invalid_request'); } @@ -106,7 +107,8 @@ module.exports = function token(options, issue) { clientID: clientID, redirectURI: redirectURI, scope: scope, - state: state + state: state, + immediate: immediate }; } diff --git a/lib/middleware/authorization.js b/lib/middleware/authorization.js index 540aa539..d3b36dd9 100644 --- a/lib/middleware/authorization.js +++ b/lib/middleware/authorization.js @@ -136,6 +136,11 @@ module.exports = function(server, options, validate, immediate) { req.oauth2.req = areq; req.oauth2.user = req[userProperty]; + if (areq.immediate && !req.oauth2.user) { + next(new AuthorizationError('', 'immediate_unsuccessful')); + return; + } + function immediated(err, allow, ares) { if (err) { return next(err); } if (allow) { @@ -146,6 +151,8 @@ module.exports = function(server, options, validate, immediate) { if (err) { return next(err); } return next(new AuthorizationError('Unsupported response type: ' + req.oauth2.req.type, 'unsupported_response_type')); }); + } else if (areq.immediate) { + next(new AuthorizationError('', 'immediate_unsuccessful')); } else { // A dialog needs to be conducted to obtain the user's approval. // Serialize a transaction to the session. The transaction will be diff --git a/test/middleware/authorization.immediate.test.js b/test/middleware/authorization.immediate.test.js index 65b4d23e..975e8a87 100644 --- a/test/middleware/authorization.immediate.test.js +++ b/test/middleware/authorization.immediate.test.js @@ -3,6 +3,7 @@ var chai = require('chai') , authorization = require('../../lib/middleware/authorization') + , AuthorizationError = require('../../lib/errors/authorizationerror') , Server = require('../../lib/server'); @@ -26,7 +27,22 @@ describe('authorization', function() { } return next(new Error('something went wrong while sending response')); }); - + + server.grant('token', function (req) { + return { + clientID: req.query['client_id'], + redirectURI: req.query['redirect_uri'], + scope: req.query['scope'], + immediate: req.query['immediate'] === 'true' + }; + }); + server.grant('token', 'response', function(txn, res, next) { + if ((txn.client.id == '1234' || txn.client.id == '2234') && txn.user.id == 'u123' && txn.res.allow === true && txn.res.scope === 'read') { + return res.redirect(txn.redirectURI); + } + return next(new Error('something went wrong while sending response')); + }); + server.grant('foo', function(req) { return { clientID: req.query['client_id'], @@ -296,5 +312,61 @@ describe('authorization', function() { }); }); }); - + + describe('with immediate query flag and no user', function() { + var err; + + function immediate(client, user, scope, done) { + done(null, false); + } + + before(function(done) { + chai.connect.use(authorization(server, validate, immediate)) + .req(function(req) { + req.query = { response_type: 'token', client_id: '1234', redirect_uri: 'http://example.com/auth/callback', immediate: 'true' }; + req.session = {}; + }) + .next(function(e) { + err = e; + done(); + }) + .dispatch(); + }); + + it('should error immediate_unsuccessful', function() { + expect(err).to.be.instanceOf(AuthorizationError); + expect(err.code).to.equal('immediate_unsuccessful'); + }); + + }); + + describe('with immediate query flag and with an user', function() { + var err; + + function immediate(client, user, scope, done) { + done(null, false); + } + + before(function(done) { + chai.connect.use(authorization(server, validate, immediate)) + .req(function(req) { + req.query = { response_type: 'token', client_id: '1234', redirect_uri: 'http://example.com/auth/callback', immediate: 'true' }; + req.session = {}; + req.user = { id: 'u123' }; + }) + .next(function(e) { + err = e; + done(); + }) + .dispatch(); + }); + + it('should error immediate_unsuccessful', function() { + expect(err).to.be.instanceOf(AuthorizationError); + expect(err.code).to.equal('immediate_unsuccessful'); + }); + + }); + + });