Skip to content
Merged
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
5 changes: 3 additions & 2 deletions lib/handlers/token-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function TokenHandler(options) {
this.grantTypes = _.assign({}, grantTypes, options.extendedGrantTypes);
this.model = options.model;
this.refreshTokenLifetime = options.refreshTokenLifetime;
this.allowExtendedTokenAttributes = options.allowExtendedTokenAttributes;
}

/**
Expand Down Expand Up @@ -90,7 +91,7 @@ TokenHandler.prototype.handle = function(request, response) {
return this.handleGrantType(request, client);
})
.tap(function(data) {
var model = new TokenModel(data);
var model = new TokenModel(data, {allowExtendedTokenAttributes: this.allowExtendedTokenAttributes});
var tokenType = this.getTokenType(model);

this.updateSuccessResponse(response, tokenType);
Expand Down Expand Up @@ -240,7 +241,7 @@ TokenHandler.prototype.getRefreshTokenLifetime = function(client) {
*/

TokenHandler.prototype.getTokenType = function(model) {
return new BearerTokenType(model.accessToken, model.accessTokenLifetime, model.refreshToken, model.scope);
return new BearerTokenType(model.accessToken, model.accessTokenLifetime, model.refreshToken, model.scope, model.customAttributes);
};

/**
Expand Down
14 changes: 13 additions & 1 deletion lib/models/token-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ var InvalidArgumentError = require('../errors/invalid-argument-error');
* Constructor.
*/

function TokenModel(data) {
var modelAttributes = ['accessToken', 'accessTokenExpiresAt', 'refreshToken', 'refreshTokenExpiresAt', 'scope', 'client', 'user'];

function TokenModel(data, options) {
data = data || {};

if (!data.accessToken) {
Expand Down Expand Up @@ -41,6 +43,16 @@ function TokenModel(data) {
this.scope = data.scope;
this.user = data.user;

if (options && options.allowExtendedTokenAttributes) {
this.customAttributes = {};

for (var key in data) {
if (data.hasOwnProperty(key) && (modelAttributes.indexOf(key) < 0)) {
this.customAttributes[key] = data[key];
}
}
}

if(this.accessTokenExpiresAt) {
this.accessTokenLifetime = Math.floor((this.accessTokenExpiresAt - new Date()) / 1000);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ OAuth2Server.prototype.authorize = function(request, response, options, callback
OAuth2Server.prototype.token = function(request, response, options, callback) {
options = _.assign({
accessTokenLifetime: 60 * 60, // 1 hour.
refreshTokenLifetime: 60 * 60 * 24 * 14 // 2 weeks.
refreshTokenLifetime: 60 * 60 * 24 * 14, // 2 weeks.
allowExtendedTokenAttributes: false
}, this.options, options);

return new TokenHandler(options)
Expand Down
11 changes: 10 additions & 1 deletion lib/token-types/bearer-token-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var InvalidArgumentError = require('../errors/invalid-argument-error');
* Constructor.
*/

function BearerTokenType(accessToken, accessTokenLifetime, refreshToken, scope) {
function BearerTokenType(accessToken, accessTokenLifetime, refreshToken, scope, customAttributes) {
if (!accessToken) {
throw new InvalidArgumentError('Missing parameter: `accessToken`');
}
Expand All @@ -19,6 +19,10 @@ function BearerTokenType(accessToken, accessTokenLifetime, refreshToken, scope)
this.accessTokenLifetime = accessTokenLifetime;
this.refreshToken = refreshToken;
this.scope = scope;

if (customAttributes) {
this.customAttributes = customAttributes;
}
}

/**
Expand All @@ -43,6 +47,11 @@ BearerTokenType.prototype.valueOf = function() {
object.scope = this.scope;
}

for (var key in this.customAttributes) {
if (this.customAttributes.hasOwnProperty(key)) {
object[key] = this.customAttributes[key];
}
}
return object;
};

Expand Down
32 changes: 0 additions & 32 deletions test/integration/grant-types/refresh-token-grant-type_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,38 +449,6 @@ describe('RefreshTokenGrantType integration', function() {
});
});

it('should throw an error if the `token.refreshTokenExpiresAt` is invalid', function() {
var model = {
getRefreshToken: function() {},
revokeToken: function() { return { refreshTokenExpiresAt: [] }; },
saveToken: function() {}
};
var grantType = new RefreshTokenGrantType({ accessTokenLifetime: 120, model: model });

grantType.revokeToken({})
.then(should.fail)
.catch(function (e) {
e.should.be.an.instanceOf(ServerError);
e.message.should.equal('Server error: `refreshTokenExpiresAt` must be a Date instance');
});
});

it('should throw an error if the `token.refreshTokenExpiresAt` is not expired', function() {
var model = {
getRefreshToken: function() {},
revokeToken: function() { return { refreshTokenExpiresAt: new Date(new Date() * 2) }; },
saveToken: function() {}
};
var grantType = new RefreshTokenGrantType({ accessTokenLifetime: 120, model: model });

grantType.revokeToken({})
.then(should.fail)
.catch(function (e) {
e.should.be.an.instanceOf(ServerError);
e.message.should.equal('Server error: refresh token should be expired');
});
});

it('should revoke the token', function() {
var token = { accessToken: 'foo', client: {}, refreshTokenExpiresAt: new Date(new Date() / 2), user: {} };
var model = {
Expand Down
71 changes: 71 additions & 0 deletions test/integration/handlers/token-handler_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,79 @@ describe('TokenHandler integration', function() {
})
.catch(should.fail);
});

it('should not return custom attributes in a bearer token if the allowExtendedTokenAttributes is not set', function() {
var token = { accessToken: 'foo', client: {}, refreshToken: 'bar', scope: 'foobar', user: {}, foo: 'bar' };
var model = {
getClient: function() { return { grants: ['password'] }; },
getUser: function() { return {}; },
saveToken: function() { return token; },
validateScope: function() { return 'baz'; }
};
var handler = new TokenHandler({ accessTokenLifetime: 120, model: model, refreshTokenLifetime: 120 });
var request = new Request({
body: {
client_id: 12345,
client_secret: 'secret',
username: 'foo',
password: 'bar',
grant_type: 'password',
scope: 'baz'
},
headers: { 'content-type': 'application/x-www-form-urlencoded', 'transfer-encoding': 'chunked' },
method: 'POST',
query: {}
});
var response = new Response({ body: {}, headers: {} });

return handler.handle(request, response)
.then(function() {
should.exist(response.body.access_token);
should.exist(response.body.refresh_token);
should.exist(response.body.token_type);
should.exist(response.body.scope);
should.not.exist(response.body.foo);
})
.catch(should.fail);
});

it('should return custom attributes in a bearer token if the allowExtendedTokenAttributes is set', function() {
var token = { accessToken: 'foo', client: {}, refreshToken: 'bar', scope: 'foobar', user: {}, foo: 'bar' };
var model = {
getClient: function() { return { grants: ['password'] }; },
getUser: function() { return {}; },
saveToken: function() { return token; },
validateScope: function() { return 'baz'; }
};
var handler = new TokenHandler({ accessTokenLifetime: 120, model: model, refreshTokenLifetime: 120, allowExtendedTokenAttributes: true });
var request = new Request({
body: {
client_id: 12345,
client_secret: 'secret',
username: 'foo',
password: 'bar',
grant_type: 'password',
scope: 'baz'
},
headers: { 'content-type': 'application/x-www-form-urlencoded', 'transfer-encoding': 'chunked' },
method: 'POST',
query: {}
});
var response = new Response({ body: {}, headers: {} });

return handler.handle(request, response)
.then(function() {
should.exist(response.body.access_token);
should.exist(response.body.refresh_token);
should.exist(response.body.token_type);
should.exist(response.body.scope);
should.exist(response.body.foo);
})
.catch(should.fail);
});
});


describe('getClient()', function() {
it('should throw an error if `clientId` is invalid', function() {
var model = {
Expand Down