Skip to content

Commit f0a7010

Browse files
committed
Properly set the status code for errors according to Section 4.2.2.1 of RFC 6749 and Section 3.1 of RFC 6750. Fixes for oauthjs#553.
1 parent 7df03c5 commit f0a7010

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

dist/lib/handlers/authenticate-handler.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/lib/handlers/authenticate-handler.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/handlers/authenticate-handler.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,27 +91,28 @@ export class AuthenticateHandler {
9191
// @see https://tools.ietf.org/html/rfc6750#section-3.1
9292
if (e instanceof UnauthorizedRequestError) {
9393
response.set('WWW-Authenticate', 'Bearer realm="Service"');
94+
response.status = 401;
9495
} else if (e instanceof InvalidRequestError) {
9596
if (e.message) {
9697
response.set('WWW-Authenticate', `Bearer realm="Service",error="invalid_request",error_description="${e.message}"`);
97-
}
98-
else {
98+
} else {
9999
response.set('WWW-Authenticate', `Bearer realm="Service",error="invalid_request"`);
100100
}
101+
response.status = 400;
101102
} else if (e instanceof InvalidTokenError) {
102103
if (e.message) {
103-
response.set('WWW-Authenticate', `Bearer realm="Service",error="invalid_token",error_description="${e.message}"`)
104-
}
105-
else {
104+
response.set('WWW-Authenticate', `Bearer realm="Service",error="invalid_token",error_description="${e.message}"`);
105+
} else {
106106
response.set('WWW-Authenticate', `Bearer realm="Service",error="invalid_token"`);
107107
}
108+
response.status = 401;
108109
} else if (e instanceof InsufficientScopeError) {
109110
if (e.message) {
110111
response.set('WWW-Authenticate', `Bearer realm="Service",error="insufficient_scope",error_description="${e.message}"`);
111-
}
112-
else {
112+
} else {
113113
response.set('WWW-Authenticate', `Bearer realm="Service",error="insufficient_scope"`);
114114
}
115+
response.status = 403;
115116
}
116117

117118
if (!(e instanceof OAuthError)) {

test/integration/handlers/authenticate-handler.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ describe('AuthenticateHandler integration', () => {
156156
response
157157
.get('WWW-Authenticate')
158158
.should.equal('Bearer realm="Service"');
159+
response.status.should.equal(401);
159160
});
160161
});
161162

@@ -175,6 +176,7 @@ describe('AuthenticateHandler integration', () => {
175176
})
176177
.catch(() => {
177178
response.get('WWW-Authenticate').should.equal('Bearer realm="Service",error="invalid_request",error_description="Bad Request"');
179+
response.status.should.equal(400);
178180
});
179181
});
180182

@@ -195,6 +197,7 @@ describe('AuthenticateHandler integration', () => {
195197
})
196198
.catch(() => {
197199
response.get('WWW-Authenticate').should.equal(`Bearer realm="Service",error="invalid_request",error_description="${errorDescription}"`);
200+
response.status.should.equal(400);
198201
});
199202
});
200203

@@ -214,6 +217,7 @@ describe('AuthenticateHandler integration', () => {
214217
})
215218
.catch(() => {
216219
response.get('WWW-Authenticate').should.equal('Bearer realm="Service",error="invalid_token",error_description="Unauthorized"');
220+
response.status.should.equal(401);
217221
});
218222
});
219223

@@ -234,6 +238,7 @@ describe('AuthenticateHandler integration', () => {
234238
})
235239
.catch(() => {
236240
response.get('WWW-Authenticate').should.equal(`Bearer realm="Service",error="invalid_token",error_description="${errorDescription}"`);
241+
response.status.should.equal(401);
237242
});
238243
});
239244

@@ -253,6 +258,7 @@ describe('AuthenticateHandler integration', () => {
253258
})
254259
.catch(() => {
255260
response.get('WWW-Authenticate').should.equal('Bearer realm="Service",error="insufficient_scope",error_description="Forbidden"');
261+
response.status.should.equal(403);
256262
});
257263
});
258264

@@ -273,6 +279,7 @@ describe('AuthenticateHandler integration', () => {
273279
})
274280
.catch(() => {
275281
response.get('WWW-Authenticate').should.equal(`Bearer realm="Service",error="insufficient_scope",error_description="${errorDescription}"`);
282+
response.status.should.equal(403);
276283
});
277284
});
278285

0 commit comments

Comments
 (0)