Skip to content

Commit d1ba63c

Browse files
authored
fix(request): set WWW-Authenticate header for invalid requests oauthjs#646
Merge pull request #96 from FStefanni/issue_89_18_646 Set WWW-Authenticate header for invalid requests Related: oauthjs#646 Fixes issue #89, point 18. Thanks to @FStefanni
2 parents 4921a1c + b56afcd commit d1ba63c

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

lib/handlers/authenticate-handler.js

+6
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ AuthenticateHandler.prototype.handle = function(request, response) {
9090
// @see https://tools.ietf.org/html/rfc6750#section-3.1
9191
if (e instanceof UnauthorizedRequestError) {
9292
response.set('WWW-Authenticate', 'Bearer realm="Service"');
93+
} else if (e instanceof InvalidRequestError) {
94+
response.set('WWW-Authenticate', 'Bearer realm="Service",error="invalid_request"');
95+
} else if (e instanceof InvalidTokenError) {
96+
response.set('WWW-Authenticate', 'Bearer realm="Service",error="invalid_token"');
97+
} else if (e instanceof InsufficientScopeError) {
98+
response.set('WWW-Authenticate', 'Bearer realm="Service",error="insufficient_scope"');
9399
}
94100

95101
if (!(e instanceof OAuthError)) {

test/integration/handlers/authenticate-handler_test.js

+51
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,57 @@ describe('AuthenticateHandler integration', function() {
132132
});
133133
});
134134

135+
it('should set the `WWW-Authenticate` header if an InvalidRequestError is thrown', function() {
136+
const model = {
137+
getAccessToken: function() {
138+
throw new InvalidRequestError();
139+
}
140+
};
141+
const handler = new AuthenticateHandler({ model: model });
142+
const request = new Request({ body: {}, headers: { 'Authorization': 'Bearer foo' }, method: {}, query: {} });
143+
const response = new Response({ body: {}, headers: {} });
144+
145+
return handler.handle(request, response)
146+
.then(should.fail)
147+
.catch(function() {
148+
response.get('WWW-Authenticate').should.equal('Bearer realm="Service",error="invalid_request"');
149+
});
150+
});
151+
152+
it('should set the `WWW-Authenticate` header if an InvalidTokenError is thrown', function() {
153+
const model = {
154+
getAccessToken: function() {
155+
throw new InvalidTokenError();
156+
}
157+
};
158+
const handler = new AuthenticateHandler({ model: model });
159+
const request = new Request({ body: {}, headers: { 'Authorization': 'Bearer foo' }, method: {}, query: {} });
160+
const response = new Response({ body: {}, headers: {} });
161+
162+
return handler.handle(request, response)
163+
.then(should.fail)
164+
.catch(function() {
165+
response.get('WWW-Authenticate').should.equal('Bearer realm="Service",error="invalid_token"');
166+
});
167+
});
168+
169+
it('should set the `WWW-Authenticate` header if an InsufficientScopeError is thrown', function() {
170+
const model = {
171+
getAccessToken: function() {
172+
throw new InsufficientScopeError();
173+
}
174+
};
175+
const handler = new AuthenticateHandler({ model: model });
176+
const request = new Request({ body: {}, headers: { 'Authorization': 'Bearer foo' }, method: {}, query: {} });
177+
const response = new Response({ body: {}, headers: {} });
178+
179+
return handler.handle(request, response)
180+
.then(should.fail)
181+
.catch(function() {
182+
response.get('WWW-Authenticate').should.equal('Bearer realm="Service",error="insufficient_scope"');
183+
});
184+
});
185+
135186
it('should throw the error if an oauth error is thrown', function() {
136187
const model = {
137188
getAccessToken: function() {

0 commit comments

Comments
 (0)