Skip to content

Commit e17cae2

Browse files
committed
Fix user denial and missing state parameter on error redirect oauthjs#649
1 parent 8aae2b0 commit e17cae2

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

lib/handlers/authorize-handler.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,6 @@ export class AuthorizeHandler {
7777
);
7878
}
7979

80-
if (request.query.allowed === 'false') {
81-
throw new AccessDeniedError(
82-
'Access denied: user denied access to application',
83-
);
84-
}
85-
8680
// Extend model object with request
8781
this.model.request = request;
8882

@@ -95,11 +89,17 @@ export class AuthorizeHandler {
9589
let responseType: any;
9690
const uri: string = this.getRedirectUri(request, client);
9791
try {
98-
const requestedScope = this.getScope(request);
92+
state = this.getState(request);
93+
if (request.query.allowed === 'false') {
94+
throw new AccessDeniedError(
95+
'Access denied: user denied access to application',
96+
);
97+
}
9998

99+
const requestedScope = this.getScope(request);
100100
const validScope = await this.validateScope(user, client, requestedScope);
101101
scope = validScope;
102-
state = this.getState(request);
102+
103103
RequestedResponseType = this.getResponseType(request, client);
104104
responseType = new RequestedResponseType(this.options);
105105
const codeOrAccessToken = await responseType.handle(

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

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,19 +191,35 @@ describe('AuthorizeHandler integration', () => {
191191

192192
it('should throw an error if `allowed` is `false`', () => {
193193
const model = {
194-
getAccessToken: () => {},
195-
getClient: () => {},
196-
saveAuthorizationCode: () => {},
194+
getAccessToken: function() {
195+
return {
196+
user: {},
197+
accessTokenExpiresAt: new Date(new Date().getTime() + 10000)
198+
};
199+
},
200+
getClient: function() {
201+
return { grants: ['authorization_code'], redirectUris: ['http://example.com/cb'] };
202+
},
203+
saveAuthorizationCode: function() {
204+
throw new Error('Unhandled exception');
205+
}
197206
};
198207
const handler = new AuthorizeHandler({
199208
authorizationCodeLifetime: 120,
200209
model,
201210
});
202211
const request = new Request({
203-
body: {},
204-
headers: {},
212+
body: {
213+
client_id: 'test'
214+
},
215+
headers: {
216+
'Authorization': 'Bearer foo'
217+
},
205218
method: 'ANY',
206-
query: { allowed: 'false' },
219+
query: {
220+
allowed: 'false',
221+
state: 'foobar'
222+
}
207223
});
208224
const response = new Response({ body: {}, headers: {} });
209225

@@ -217,6 +233,11 @@ describe('AuthorizeHandler integration', () => {
217233
e.message.should.equal(
218234
'Access denied: user denied access to application',
219235
);
236+
response
237+
.get('location')
238+
.should.equal(
239+
'http://example.com/cb?error=access_denied&error_description=Access%20denied%3A%20user%20denied%20access%20to%20application&state=foobar',
240+
);
220241
});
221242
});
222243

@@ -419,7 +440,7 @@ describe('AuthorizeHandler integration', () => {
419440
response
420441
.get('location')
421442
.should.equal(
422-
'http://example.com/cb?error=invalid_scope&error_description=Invalid%20parameter%3A%20%60scope%60',
443+
'http://example.com/cb?error=invalid_scope&error_description=Invalid%20parameter%3A%20%60scope%60&state=foobar',
423444
);
424445
});
425446
});
@@ -509,7 +530,7 @@ describe('AuthorizeHandler integration', () => {
509530
should.fail('should.fail', '');
510531
})
511532
.catch(function() {
512-
response.get('location').should.equal('http://example.com/cb?error=invalid_scope&error_description=Invalid%20scope%3A%20Requested%20scope%20is%20invalid');
533+
response.get('location').should.equal('http://example.com/cb?error=invalid_scope&error_description=Invalid%20scope%3A%20Requested%20scope%20is%20invalid&state=foobar');
513534
});
514535
});
515536

0 commit comments

Comments
 (0)