Skip to content

Commit

Permalink
feat(api): allow to specify rights for user token (#81)
Browse files Browse the repository at this point in the history
* feat(api): allow to specify rights for user token

* fix: add "delete" to global right types
  • Loading branch information
targos authored Dec 1, 2017
1 parent 95ae5a7 commit 2383c09
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// super administrators have all these rights
const globalRightTypes = [
'delete',
'read',
'write',
'create',
Expand Down
3 changes: 2 additions & 1 deletion src/couch/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const CouchError = require('../util/CouchError');
const debug = require('../util/debug')('main:token');
const token = require('../util/token');
const {isValidUsername} = require('./util');
const {isValidUsername, ensureRightsArray} = require('./util');

const methods = {
async createEntryToken(user, uuid) {
Expand All @@ -22,6 +22,7 @@ const methods = {
if (!isValidUsername(user)) {
throw new CouchError('only a user can create a token', 'unauthorized');
}
ensureRightsArray(rights);
await this.open();
return token.createUserToken(this._db, user, rights);
},
Expand Down
9 changes: 5 additions & 4 deletions src/couch/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,21 @@ async function validateTokenOrRights(ctx, uuid, owners, rights, user, token, typ

if (token && token.$kind === type && token.uuid === uuid) {
for (var i = 0; i < rights.length; i++) {
if (token.rights.includes(rights[i])) {
return true;
if (!token.rights.includes(rights[i])) {
return false;
}
}
return true;
}
const ok = await validateRights(ctx, owners, user, rights, type);
return ok[0];
}

function areRightsInToken(rights, token) {
const tokenRights = new Set(token.rights);
if (rights.length !== tokenRights.size) {
if (rights.length > token.rights.length) {
return false;
}
const tokenRights = new Set(token.rights);
for (const right of rights) {
if (!tokenRights.has(right)) {
return false;
Expand Down
3 changes: 2 additions & 1 deletion src/server/middleware/couch.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ exports.createEntryToken = composeWithError(async (ctx) => {
});

exports.createUserToken = composeWithError(async (ctx) => {
const token = await ctx.state.couch.createUserToken(ctx.state.userEmail);
const rights = ctx.query.rights ? ctx.query.rights.split(',') : undefined;
const token = await ctx.state.couch.createUserToken(ctx.state.userEmail, rights);
ctx.status = 201;
ctx.body = token;
});
Expand Down
5 changes: 5 additions & 0 deletions test/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ describe('token methods', function () {
await couch.createEntryToken('anonymous', 'A').should.be.rejectedWith('only a user can create a token');
await couch.createUserToken('anonymous').should.be.rejectedWith('only a user can create a token');
});

it('token should not accept invalid right', async () => {
await couch.createUserToken('a@a.com', 'test1').should.be.rejectedWith('invalid right: test1');
await couch.createUserToken('a@a.com', ['read', 'test2']).should.be.rejectedWith('invalid right: test2');
});
});

0 comments on commit 2383c09

Please sign in to comment.