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
17 changes: 14 additions & 3 deletions src/jwt-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,25 @@ export function scopeAllowsRequest(scope: string | undefined, request: express.R
const access = parts[1];
const readwrite = access === Constants.READWRITE;
path = parts[0];

if (requestPath === Constants.THINGS_PATH) {
const allowedCollectionRead =
path === Constants.THINGS_PATH || path.startsWith(Constants.THINGS_PATH);
if (!allowedCollectionRead) {
continue;
}
if (request.method === 'GET' || request.method === 'OPTIONS') {
return true;
}
return path === Constants.THINGS_PATH && readwrite;
}

const allowedDirect = requestPath.startsWith(path);
const allowedThings =
requestPath === Constants.THINGS_PATH && path.startsWith(Constants.THINGS_PATH);
// Allow access to media only if scope covers all things
const allowedMedia =
requestPath.startsWith(Constants.MEDIA_PATH) && path === Constants.THINGS_PATH;

if (allowedDirect || allowedThings || allowedMedia) {
if (allowedDirect || allowedMedia) {
if (!readwrite && request.method !== 'GET' && request.method !== 'OPTIONS') {
return false;
}
Expand Down
72 changes: 72 additions & 0 deletions src/test/integration/oauth-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,78 @@ describe('oauth/', function () {
expect(err.get('www-authenticate')).toEqual(expect.stringContaining('insufficient_scope'));
});

it('rejects collection writes for a scoped thing token', async () => {
setupOAuth();
await addDevice();
await addDevice({
...TEST_THING,
id: 'test-2',
title: 'test-2',
});

const res = await chai
.request(server)
.get(
`${Constants.OAUTH_PATH}/allow?${qs.stringify({
response_type: 'code',
client_id: CLIENT_ID,
scope: '/things/test-1:readwrite',
state: REQUEST_STATE,
})}`
)
.set('Accept', 'application/json')
.set(...headerAuth(userJWT));

const jwt = res.body.token.access_token;

const listRes = await chai
.request(server)
.get(Constants.THINGS_PATH)
.set('Accept', 'application/json')
.set(...headerAuth(jwt));
expect(listRes.status).toEqual(200);
expect(listRes.body.length).toEqual(1);
expect(listRes.body[0].href).toEqual('/things/test-1');

const pinErr = await chai
.request(server)
.patch(Constants.THINGS_PATH)
.set('Accept', 'application/json')
.set(...headerAuth(jwt))
.send({ thingId: 'test-2', pin: '1234' });
expect(pinErr.status).toEqual(403);
expect(pinErr.header).toHaveProperty('www-authenticate');
expect(pinErr.get('www-authenticate')).toEqual(expect.stringContaining('insufficient_scope'));

const credentialsErr = await chai
.request(server)
.patch(Constants.THINGS_PATH)
.set('Accept', 'application/json')
.set(...headerAuth(jwt))
.send({ thingId: 'test-2', username: 'user', password: 'pass' });
expect(credentialsErr.status).toEqual(403);

const createErr = await chai
.request(server)
.post(Constants.THINGS_PATH)
.set('Accept', 'application/json')
.set(...headerAuth(jwt))
.send({
id: 'test-3',
title: 'test-3',
'@context': ['https://www.w3.org/2022/wot/td/v1.1', 'https://webthings.io/schemas'],
'@type': ['OnOffSwitch'],
properties: {
power: {
'@type': 'OnOffProperty',
type: 'boolean',
value: false,
},
},
});
expect(createErr.status).toEqual(403);
});

it('rejects use of authorization code as access token', async () => {
setupOAuth({}, function customCallbackHandler(req: express.Request, res: express.Response) {
const code = req.query.code;
Expand Down
Loading