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
6 changes: 6 additions & 0 deletions lib/handlers/authenticate-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ AuthenticateHandler.prototype.handle = function(request, response) {
// @see https://tools.ietf.org/html/rfc6750#section-3.1
if (e instanceof UnauthorizedRequestError) {
response.set('WWW-Authenticate', 'Bearer realm="Service"');
} else if (e instanceof InvalidRequestError) {
response.set('WWW-Authenticate', 'Bearer realm="Service",error="invalid_request"');
} else if (e instanceof InvalidTokenError) {
response.set('WWW-Authenticate', 'Bearer realm="Service",error="invalid_token"');
} else if (e instanceof InsufficientScopeError) {
response.set('WWW-Authenticate', 'Bearer realm="Service",error="insufficient_scope"');
}

if (!(e instanceof OAuthError)) {
Expand Down
51 changes: 51 additions & 0 deletions test/integration/handlers/authenticate-handler_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,57 @@ describe('AuthenticateHandler integration', function() {
});
});

it('should set the `WWW-Authenticate` header if an InvalidRequestError is thrown', function() {
const model = {
getAccessToken: function() {
throw new InvalidRequestError();
}
};
const handler = new AuthenticateHandler({ model: model });
const request = new Request({ body: {}, headers: { 'Authorization': 'Bearer foo' }, method: {}, query: {} });
const response = new Response({ body: {}, headers: {} });

return handler.handle(request, response)
.then(should.fail)
.catch(function() {
response.get('WWW-Authenticate').should.equal('Bearer realm="Service",error="invalid_request"');
});
});

it('should set the `WWW-Authenticate` header if an InvalidTokenError is thrown', function() {
const model = {
getAccessToken: function() {
throw new InvalidTokenError();
}
};
const handler = new AuthenticateHandler({ model: model });
const request = new Request({ body: {}, headers: { 'Authorization': 'Bearer foo' }, method: {}, query: {} });
const response = new Response({ body: {}, headers: {} });

return handler.handle(request, response)
.then(should.fail)
.catch(function() {
response.get('WWW-Authenticate').should.equal('Bearer realm="Service",error="invalid_token"');
});
});

it('should set the `WWW-Authenticate` header if an InsufficientScopeError is thrown', function() {
const model = {
getAccessToken: function() {
throw new InsufficientScopeError();
}
};
const handler = new AuthenticateHandler({ model: model });
const request = new Request({ body: {}, headers: { 'Authorization': 'Bearer foo' }, method: {}, query: {} });
const response = new Response({ body: {}, headers: {} });

return handler.handle(request, response)
.then(should.fail)
.catch(function() {
response.get('WWW-Authenticate').should.equal('Bearer realm="Service",error="insufficient_scope"');
});
});

it('should throw the error if an oauth error is thrown', function() {
const model = {
getAccessToken: function() {
Expand Down