Skip to content

Commit

Permalink
fix: add scope to implicit responses when different from request
Browse files Browse the repository at this point in the history
Co-authored-by: ayazismailhakki <ismail.ayaz@ekinokssoftware.com>
Co-authored-by: Filip Skokan <panva.ip@gmail.com>
  • Loading branch information
3 people committed May 24, 2019
1 parent a3d078f commit 71b2e7e
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 21 deletions.
8 changes: 7 additions & 1 deletion lib/actions/authorization/process_response_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@ async function tokenHandler(ctx) {

ctx.oidc.entity('AccessToken', token);

return {
const result = {
access_token: await token.save(),
expires_in: token.expiration,
token_type: 'Bearer',
};

if (token.scope !== ctx.oidc.params.scope) {
result.scope = token.scope;
}

return result;
}

async function codeHandler(ctx) {
Expand Down
24 changes: 24 additions & 0 deletions test/core/hybrid/code+id_token+token.authorization.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,30 @@ describe('HYBRID code+id_token+token', () => {
});
});

describe(`${verb} ${route} with the scope not being fulfilled`, () => {
before(function () {
return this.login({
scope: 'openid profile email',
rejectedScopes: ['email'],
});
});

it('responds with an extra parameter scope', async function () {
const auth = new this.AuthorizationRequest({
response_type,
scope: 'openid profile email',
});

await this.wrap({ route, verb, auth })
.expect(302)
.expect(auth.validateFragment)
.expect(auth.validatePresence(['code', 'id_token', 'state', 'access_token', 'expires_in', 'token_type', 'scope']))
.expect(auth.validateState)
.expect(auth.validateClientLocation)
.expect(auth.validateResponseParameter('scope', 'openid profile'));
});
});

describe(`${verb} ${route} errors`, () => {
it('disallowed response mode', function () {
const spy = sinon.spy();
Expand Down
24 changes: 24 additions & 0 deletions test/core/hybrid/code+token.authorization.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,30 @@ describe('HYBRID code+token', () => {
});
});

describe(`${verb} ${route} with the scope not being fulfilled`, () => {
before(function () {
return this.login({
scope: 'openid profile email',
rejectedScopes: ['email'],
});
});

it('responds with an extra parameter scope', async function () {
const auth = new this.AuthorizationRequest({
response_type,
scope: 'openid profile email',
});

await this.wrap({ route, verb, auth })
.expect(302)
.expect(auth.validateFragment)
.expect(auth.validatePresence(['code', 'state', 'access_token', 'expires_in', 'token_type', 'scope']))
.expect(auth.validateState)
.expect(auth.validateClientLocation)
.expect(auth.validateResponseParameter('scope', 'openid profile'));
});
});

describe(`${verb} ${route} errors`, () => {
it('disallowed response mode', function () {
const spy = sinon.spy();
Expand Down
24 changes: 24 additions & 0 deletions test/core/implicit/id_token+token.authorization.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,30 @@ describe('IMPLICIT id_token+token', () => {
});
});

describe(`${verb} ${route} with the scope not being fulfilled`, () => {
before(function () {
return this.login({
scope: 'openid profile email',
rejectedScopes: ['email'],
});
});

it('responds with an extra parameter scope', async function () {
const auth = new this.AuthorizationRequest({
response_type,
scope: 'openid profile email',
});

await this.wrap({ route, verb, auth })
.expect(302)
.expect(auth.validateFragment)
.expect(auth.validatePresence(['id_token', 'state', 'access_token', 'expires_in', 'token_type', 'scope']))
.expect(auth.validateState)
.expect(auth.validateClientLocation)
.expect(auth.validateResponseParameter('scope', 'openid profile'));
});
});

describe(`${verb} ${route} errors`, () => {
it('disallowed response mode', function () {
const spy = sinon.spy();
Expand Down
30 changes: 10 additions & 20 deletions test/test_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,37 +233,27 @@ module.exports = function testHelper(dir, { config: base = path.basename(dir), m
};
};

AuthorizationRequest.prototype.validateError = function (expected) {
AuthorizationRequest.prototype.validateResponseParameter = function (parameter, expected) {
return (response) => {
const { query: { error } } = parse(response.headers.location, true);
const { query: { [parameter]: value } } = parse(response.headers.location, true);
if (expected.exec) {
expect(error).to.match(expected);
expect(value).to.match(expected);
} else {
expect(error).to.equal(expected);
expect(value).to.equal(expected);
}
};
};

AuthorizationRequest.prototype.validateError = function (expected) {
return this.validateResponseParameter('error', expected);
};

AuthorizationRequest.prototype.validateScope = function (expected) {
return (response) => {
const { query: { scope } } = parse(response.headers.location, true);
if (expected.exec) {
expect(scope).to.match(expected);
} else {
expect(scope).to.equal(expected);
}
};
return this.validateResponseParameter('scope', expected);
};

AuthorizationRequest.prototype.validateErrorDescription = function (expected) {
return (response) => {
const { query: { error_description } } = parse(response.headers.location, true);
if (expected.exec) {
expect(error_description).to.match(expected);
} else {
expect(error_description).to.equal(expected);
}
};
return this.validateResponseParameter('error_description', expected);
};

function getSession({ instantiate } = { instantiate: false }) {
Expand Down

0 comments on commit 71b2e7e

Please sign in to comment.