Skip to content
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

implement immediate query flag #98

Open
wants to merge 1 commit into
base: master
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
6 changes: 4 additions & 2 deletions lib/grant/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'); }

Expand All @@ -106,7 +107,8 @@ module.exports = function token(options, issue) {
clientID: clientID,
redirectURI: redirectURI,
scope: scope,
state: state
state: state,
immediate: immediate
};
}

Expand Down
7 changes: 7 additions & 0 deletions lib/middleware/authorization.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
76 changes: 74 additions & 2 deletions test/middleware/authorization.immediate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

var chai = require('chai')
, authorization = require('../../lib/middleware/authorization')
, AuthorizationError = require('../../lib/errors/authorizationerror')
, Server = require('../../lib/server');


Expand All @@ -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'],
Expand Down Expand Up @@ -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');
});

});


});