Skip to content

Commit 6a41841

Browse files
authored
Merge pull request #363 from andrewmaillet/master
Remove standard-http-error to remove AGPL license.
2 parents 5039737 + 9249509 commit 6a41841

File tree

5 files changed

+90
-64
lines changed

5 files changed

+90
-64
lines changed

lib/errors/invalid-argument-error.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
var _ = require('lodash');
8-
var StandardHttpError = require('standard-http-error');
8+
var OAuthError = require('./oauth-error');
99
var util = require('util');
1010

1111
/**
@@ -18,14 +18,14 @@ function InvalidArgumentError(message, properties) {
1818
name: 'invalid_argument'
1919
}, properties);
2020

21-
StandardHttpError.call(this, properties.code, message, properties);
21+
OAuthError.call(this, message, properties);
2222
}
2323

2424
/**
2525
* Inherit prototype.
2626
*/
2727

28-
util.inherits(InvalidArgumentError, StandardHttpError);
28+
util.inherits(InvalidArgumentError, OAuthError);
2929

3030
/**
3131
* Export constructor.

lib/errors/oauth-error.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,40 @@
33
/**
44
* Module dependencies.
55
*/
6-
7-
var StandardHttpError = require('standard-http-error');
6+
var _ = require('lodash');
87
var util = require('util');
9-
8+
var statuses = require('statuses');
109
/**
1110
* Constructor.
1211
*/
1312

1413
function OAuthError(messageOrError, properties) {
1514
var message = messageOrError instanceof Error ? messageOrError.message : messageOrError;
1615
var error = messageOrError instanceof Error ? messageOrError : null;
16+
if (_.isEmpty(properties))
17+
{
18+
properties = {};
19+
}
20+
21+
_.defaults(properties, { code: 500 });
1722

1823
if (error) {
1924
properties.inner = error;
2025
}
21-
22-
StandardHttpError.call(this, properties.code, message, properties);
26+
if (_.isEmpty(message)) {
27+
message = statuses[properties.code];
28+
}
29+
this.code = this.status = this.statusCode = properties.code;
30+
this.message = message;
31+
for (var key in properties) {
32+
if (key !== 'code') {
33+
this[key] = properties[key];
34+
}
35+
}
36+
Error.captureStackTrace(this, OAuthError);
2337
}
2438

25-
/**
26-
* Inherit prototype.
27-
*/
28-
29-
util.inherits(OAuthError, StandardHttpError);
39+
util.inherits(OAuthError, Error);
3040

3141
/**
3242
* Export constructor.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"camel-case": "^1.1.1",
2323
"lodash": "^3.3.1",
2424
"promisify-any": "2.0.1",
25-
"standard-http-error": "^1.1.0",
25+
"statuses": "^1.3.1",
2626
"type-is": "^1.6.0",
2727
"validator.js": "^1.1.1"
2828
},

test/integration/grant-types/authorization-code-grant-type_test.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ describe('AuthorizationCodeGrantType integration', function() {
9191
e.message.should.equal('Missing parameter: `request`');
9292
}
9393
});
94-
95-
it('should throw an error if `client` is missing', function() {
94+
95+
it('should throw an error if `client` is invalid', function() {
9696
var client = {};
9797
var model = {
9898
getAuthorizationCode: function() { return { authorizationCode: 12345, expiresAt: new Date(new Date() * 2), user: {} }; },
@@ -110,6 +110,25 @@ describe('AuthorizationCodeGrantType integration', function() {
110110
});
111111
});
112112

113+
it('should throw an error if `client` is missing', function() {
114+
115+
var model = {
116+
getAuthorizationCode: function() { return { authorizationCode: 12345, expiresAt: new Date(new Date() * 2), user: {} }; },
117+
revokeAuthorizationCode: function() {},
118+
saveToken: function() {}
119+
};
120+
var grantType = new AuthorizationCodeGrantType({ accessTokenLifetime: 123, model: model });
121+
var request = new Request({ body: { code: 12345 }, headers: {}, method: {}, query: {} });
122+
123+
try {
124+
grantType.handle(request, null);
125+
}
126+
catch (e) {
127+
e.should.be.an.instanceOf(InvalidArgumentError);
128+
e.message.should.equal('Missing parameter: `client`');
129+
}
130+
});
131+
113132
it('should return a token', function() {
114133
var client = { id: 'foobar' };
115134
var token = {};
@@ -462,6 +481,25 @@ describe('AuthorizationCodeGrantType integration', function() {
462481
.catch(should.fail);
463482
});
464483

484+
it('should throw an error when the auth code is invalid', function() {
485+
var authorizationCode = { authorizationCode: 12345, client: {}, expiresAt: new Date(new Date() / 2), user: {} };
486+
var model = {
487+
getAuthorizationCode: function() {},
488+
revokeAuthorizationCode: function() { return false; },
489+
saveToken: function() {}
490+
};
491+
var grantType = new AuthorizationCodeGrantType({ accessTokenLifetime: 123, model: model });
492+
493+
return grantType.revokeAuthorizationCode(authorizationCode)
494+
.then(function(data) {
495+
data.should.equal(authorizationCode);
496+
})
497+
.catch(function(e) {
498+
e.should.be.an.instanceOf(InvalidGrantError);
499+
e.message.should.equal('Invalid grant: authorization code is invalid');
500+
});
501+
});
502+
465503
it('should support promises', function() {
466504
var authorizationCode = { authorizationCode: 12345, client: {}, expiresAt: new Date(new Date() / 2), user: {} };
467505
var model = {

test/integration/grant-types/refresh-token-grant-type_test.js

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ describe('RefreshTokenGrantType integration', function() {
170170
});
171171

172172
describe('getRefreshToken()', function() {
173-
it('should throw an error if the requested `refreshToken` is missing', function() {
173+
it('should throw an error if the `refreshToken` parameter is missing from the request body', function() {
174174
var client = {};
175175
var model = {
176176
getRefreshToken: function() {},
@@ -190,35 +190,15 @@ describe('RefreshTokenGrantType integration', function() {
190190
}
191191
});
192192

193-
it('should throw an error if the requested `refreshToken` is invalid', function() {
194-
var client = {};
195-
var model = {
196-
getRefreshToken: function() {},
197-
revokeToken: function() {},
198-
saveToken: function() {}
199-
};
200-
var grantType = new RefreshTokenGrantType({ accessTokenLifetime: 120, model: model });
201-
var request = new Request({ body: { refresh_token: [] }, headers: {}, method: {}, query: {} });
202-
203-
try {
204-
grantType.getRefreshToken(request, client);
205-
206-
should.fail();
207-
} catch (e) {
208-
e.should.be.an.instanceOf(InvalidRequestError);
209-
e.message.should.equal('Invalid parameter: `refresh_token`');
210-
}
211-
});
212-
213-
it('should throw an error if `refreshToken` is missing', function() {
214-
var client = {};
193+
it('should throw an error if `refreshToken` is not found', function() {
194+
var client = { id: 123 };
215195
var model = {
216-
getRefreshToken: function() { return { accessToken: 'foo', client: { id: 123 }, user: {} }; },
196+
getRefreshToken: function() { return; },
217197
revokeToken: function() {},
218198
saveToken: function() {}
219199
};
220200
var grantType = new RefreshTokenGrantType({ accessTokenLifetime: 120, model: model });
221-
var request = new Request({ body: { refresh_token: 12345 }, headers: {}, method: {}, query: {} });
201+
var request = new Request({ body: { refresh_token: '12345' }, headers: {}, method: {}, query: {} });
222202

223203
return grantType.getRefreshToken(request, client)
224204
.then(should.fail)
@@ -286,29 +266,7 @@ describe('RefreshTokenGrantType integration', function() {
286266
});
287267
});
288268

289-
it('should throw an error if the request body does not contain `refresh_token`', function() {
290-
var client = {};
291-
var model = {
292-
getRefreshToken: function() {
293-
return { client: { id: 456 }, user: {} };
294-
},
295-
revokeToken: function() {},
296-
saveToken: function() {}
297-
};
298-
var grantType = new RefreshTokenGrantType({ accessTokenLifetime: 120, model: model });
299-
var request = new Request({ body: {}, headers: {}, method: {}, query: {} });
300-
301-
try {
302-
grantType.getRefreshToken(request, client);
303-
304-
should.fail();
305-
} catch (e) {
306-
e.should.be.an.instanceOf(InvalidRequestError);
307-
e.message.should.equal('Missing parameter: `refresh_token`');
308-
}
309-
});
310-
311-
it('should throw an error if `refresh_token` is invalid', function() {
269+
it('should throw an error if `refresh_token` contains invalid characters', function() {
312270
var client = {};
313271
var model = {
314272
getRefreshToken: function() {
@@ -371,6 +329,26 @@ describe('RefreshTokenGrantType integration', function() {
371329
});
372330
});
373331

332+
it('should throw an error if `refreshTokenExpiresAt` is not a date value', function() {
333+
var client = { id: 123 };
334+
var model = {
335+
getRefreshToken: function() {
336+
return { accessToken: 'foo', client: { id: 123 }, refreshTokenExpiresAt: 'stringvalue', user: {} };
337+
},
338+
revokeToken: function() {},
339+
saveToken: function() {}
340+
};
341+
var grantType = new RefreshTokenGrantType({ accessTokenLifetime: 120, model: model });
342+
var request = new Request({ body: { refresh_token: 12345 }, headers: {}, method: {}, query: {} });
343+
344+
return grantType.getRefreshToken(request, client)
345+
.then(should.fail)
346+
.catch(function(e) {
347+
e.should.be.an.instanceOf(ServerError);
348+
e.message.should.equal('Server error: `refreshTokenExpiresAt` must be a Date instance');
349+
});
350+
});
351+
374352
it('should return a token', function() {
375353
var client = { id: 123 };
376354
var token = { accessToken: 'foo', client: { id: 123 }, user: {} };

0 commit comments

Comments
 (0)