Skip to content

Commit 8ccc556

Browse files
committed
Merge pull request oauthjs#148 from thomseddon/fix/set-cache-control-headers
Set Cache-Control and Pragma Headers
2 parents 57a402e + c19719e commit 8ccc556

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

lib/error.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,14 @@ function OAuth2Error (error, description, err) {
4040
Error.captureStackTrace(this, this.constructor);
4141
}
4242

43+
this.headers = {
44+
'Cache-Control': 'no-store',
45+
'Pragma': 'no-cache'
46+
};
47+
4348
switch (error) {
4449
case 'invalid_client':
45-
this.headers = {
46-
'WWW-Authenticate': 'Basic realm="Service"'
47-
};
50+
this.headers['WWW-Authenticate'] = 'Basic realm="Service"';
4851
/* falls through */
4952
case 'invalid_grant':
5053
case 'invalid_request':

lib/grant.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,10 @@ function sendResponse (done) {
466466

467467
if (this.refreshToken) response.refresh_token = this.refreshToken;
468468

469-
this.res.jsonp(response);
469+
this.res
470+
.set('Cache-Control', 'no-store')
471+
.set('Pragma', 'no-cache')
472+
.jsonp(response);
470473

471474
if (this.config.continueAfterResponse)
472475
done();

test/error.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,23 @@ describe('OAuth2Error', function() {
2828
error.name.should.equal('OAuth2Error');
2929
});
3030

31-
it('should expose `headers` if error is `invalid_client`', function () {
31+
it('should set cache `headers`', function () {
32+
var error = new OAuth2Error('invalid_request');
33+
34+
error.headers.should.eql({
35+
'Cache-Control': 'no-store',
36+
'Pragma': 'no-cache'
37+
});
38+
});
39+
40+
it('should include WWW-Authenticate `header` if error is `invalid_client`', function () {
3241
var error = new OAuth2Error('invalid_client');
3342

34-
error.headers.should.eql({ 'WWW-Authenticate': 'Basic realm="Service"' });
43+
error.headers.should.eql({
44+
'Cache-Control': 'no-store',
45+
'Pragma': 'no-cache',
46+
'WWW-Authenticate': 'Basic realm="Service"'
47+
});
3548
});
3649

3750
it('should expose a status `code`', function () {

test/grant.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,8 @@ describe('Grant', function() {
411411
.set('Content-Type', 'application/x-www-form-urlencoded')
412412
.send(validBody)
413413
.expect(200)
414+
.expect('Cache-Control', 'no-store')
415+
.expect('Pragma', 'no-cache')
414416
.end(function (err, res) {
415417
if (err) return done(err);
416418

@@ -452,6 +454,8 @@ describe('Grant', function() {
452454
.set('Content-Type', 'application/x-www-form-urlencoded')
453455
.send(validBody)
454456
.expect(200)
457+
.expect('Cache-Control', 'no-store')
458+
.expect('Pragma', 'no-cache')
455459
.end(function (err, res) {
456460
if (err) return done(err);
457461

0 commit comments

Comments
 (0)