Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compliance/fix scope #267

Merged
merged 7 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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/token-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ class TokenHandler {
updateSuccessResponse (response, tokenType) {
response.body = tokenType.valueOf();

// for compliance reasons we rebuild the internal scope to be a string
// https://datatracker.ietf.org/doc/html/rfc6749.html#section-5.1
if (response.body.scope) {
response.body.scope = response.body.scope.join(' ');
}

response.set('Cache-Control', 'no-store');
response.set('Pragma', 'no-cache');
}
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/scope-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ module.exports = {
return undefined;
}

return requestedScope.split(' ');
return Array.isArray(requestedScope) ? requestedScope : requestedScope.split(' ');
jankapunkt marked this conversation as resolved.
Show resolved Hide resolved
}
};
90 changes: 0 additions & 90 deletions lib/validator/is.js

This file was deleted.

4 changes: 3 additions & 1 deletion test/compliance/client-authentication_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ const client = db.saveClient({ id: 'a', secret: 'b', grants: ['password'] });
const scope = 'read write';

function createDefaultRequest () {
const dice = Math.random() > 0.5;
const currentScope = dice ? scope : scope.split(' ');
jankapunkt marked this conversation as resolved.
Show resolved Hide resolved
return createRequest({
body: {
grant_type: 'password',
username: user.username,
password: user.password,
scope
scope: currentScope
},
headers: {
'authorization': 'Basic ' + Buffer.from(client.id + ':' + client.secret).toString('base64'),
Expand Down
2 changes: 1 addition & 1 deletion test/compliance/client-credential-workflow_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('ClientCredentials Workflow Compliance (4.4)', function () {
response.body.token_type.should.equal('Bearer');
response.body.access_token.should.equal(token.accessToken);
response.body.expires_in.should.be.a('number');
response.body.scope.should.eql(['read', 'write']);
response.body.scope.should.eql('read write');
('refresh_token' in response.body).should.equal(false);

token.accessToken.should.be.a('string');
Expand Down
2 changes: 1 addition & 1 deletion test/compliance/password-grant-type_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe('PasswordGrantType Compliance', function () {
response.body.access_token.should.equal(token.accessToken);
response.body.refresh_token.should.equal(token.refreshToken);
response.body.expires_in.should.be.a('number');
response.body.scope.should.eql(['read', 'write']);
response.body.scope.should.eql('read write');

token.accessToken.should.be.a('string');
token.refreshToken.should.be.a('string');
Expand Down
4 changes: 2 additions & 2 deletions test/compliance/refresh-token-grant-type_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ describe('RefreshTokenGrantType Compliance', function () {
refreshResponse.body.access_token.should.equal(token.accessToken);
refreshResponse.body.refresh_token.should.equal(token.refreshToken);
refreshResponse.body.expires_in.should.be.a('number');
refreshResponse.body.scope.should.eql(['read', 'write']);
refreshResponse.body.scope.should.eql('read write');

token.accessToken.should.be.a('string');
token.refreshToken.should.be.a('string');
Expand Down Expand Up @@ -223,7 +223,7 @@ describe('RefreshTokenGrantType Compliance', function () {
refreshResponse.body.access_token.should.equal(token.accessToken);
refreshResponse.body.refresh_token.should.equal(token.refreshToken);
refreshResponse.body.expires_in.should.be.a('number');
refreshResponse.body.scope.should.eql(['read']);
refreshResponse.body.scope.should.eql('read');
});
});
});
14 changes: 12 additions & 2 deletions test/helpers/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ function createModel (db) {
}

async function saveToken (token, client, user) {
if (token.scope && !Array.isArray(token.scope)) {
throw new Error('Scope should internally be an array');
jankapunkt marked this conversation as resolved.
Show resolved Hide resolved
}
const meta = {
clientId: client.id,
userId: user.id,
Expand Down Expand Up @@ -38,7 +41,9 @@ function createModel (db) {
if (!meta) {
return false;
}

if (meta.scope && !Array.isArray(meta.scope)) {
throw new Error('Scope should internally be an array');
}
return {
accessToken,
accessTokenExpiresAt: meta.accessTokenExpiresAt,
Expand All @@ -54,7 +59,9 @@ function createModel (db) {
if (!meta) {
return false;
}

if (meta.scope && !Array.isArray(meta.scope)) {
throw new Error('Scope should internally be an array');
}
return {
refreshToken,
refreshTokenExpiresAt: meta.refreshTokenExpiresAt,
Expand All @@ -71,6 +78,9 @@ function createModel (db) {
}

async function verifyScope (token, scope) {
if (!Array.isArray(scope)) {
throw new Error('Scope should internally be an array');
}
return scope.every(s => scopes.includes(s));
}

Expand Down
10 changes: 5 additions & 5 deletions test/integration/handlers/token-handler_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ describe('TokenHandler integration', function() {
});

it('should not return custom attributes in a bearer token if the allowExtendedTokenAttributes is not set', function() {
const token = { accessToken: 'foo', client: {}, refreshToken: 'bar', scope: ['foobar'], user: {}, foo: 'bar' };
const token = { accessToken: 'foo', client: {}, refreshToken: 'bar', scope: ['baz'], user: {}, foo: 'bar' };
const model = {
getClient: function() { return { grants: ['password'] }; },
getUser: function() { return {}; },
Expand All @@ -344,7 +344,7 @@ describe('TokenHandler integration', function() {
username: 'foo',
password: 'bar',
grant_type: 'password',
scope: 'baz'
scope: ['baz']
jankapunkt marked this conversation as resolved.
Show resolved Hide resolved
},
headers: { 'content-type': 'application/x-www-form-urlencoded', 'transfer-encoding': 'chunked' },
method: 'POST',
Expand All @@ -357,14 +357,14 @@ describe('TokenHandler integration', function() {
should.exist(response.body.access_token);
should.exist(response.body.refresh_token);
should.exist(response.body.token_type);
should.exist(response.body.scope);
response.body.scope.should.eql('baz');
should.not.exist(response.body.foo);
})
.catch(should.fail);
});

it('should return custom attributes in a bearer token if the allowExtendedTokenAttributes is set', function() {
const token = { accessToken: 'foo', client: {}, refreshToken: 'bar', scope: ['foobar'], user: {}, foo: 'bar' };
const token = { accessToken: 'foo', client: {}, refreshToken: 'bar', scope: ['baz'], user: {}, foo: 'bar' };
const model = {
getClient: function() { return { grants: ['password'] }; },
getUser: function() { return {}; },
Expand Down Expand Up @@ -392,7 +392,7 @@ describe('TokenHandler integration', function() {
should.exist(response.body.access_token);
should.exist(response.body.refresh_token);
should.exist(response.body.token_type);
should.exist(response.body.scope);
response.body.scope.should.eql('baz');
should.exist(response.body.foo);
})
.catch(should.fail);
Expand Down
Loading