-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
160 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict'; | ||
|
||
const randomatic = require('randomatic'); | ||
const getRandomToken = () => randomatic('Aa0', 32); | ||
|
||
const CouchError = require('./CouchError'); | ||
const nanoPromise = require('./nanoPromise'); | ||
|
||
exports.createEntryToken = async function createEntryToken(db, user, uuid, rights) { | ||
if (typeof rights === 'string') { | ||
rights = [rights]; | ||
} else if (!Array.isArray(rights)) { | ||
throw new CouchError('rights must be an array'); | ||
} | ||
const token = { | ||
$type: 'token', | ||
$kind: 'entry', | ||
$id: getRandomToken(), | ||
$owner: user, | ||
$creationDate: Date.now(), | ||
uuid, | ||
rights | ||
}; | ||
await nanoPromise.insertDocument(db, token); | ||
return token; | ||
}; | ||
|
||
exports.getToken = async function getToken(db, tokenId) { | ||
const result = await nanoPromise.queryView(db, 'tokenById', {key: tokenId, include_docs: true}, {onlyDoc: true}); | ||
if (result.length === 0) { | ||
return null; | ||
} else if (result.length === 1) { | ||
return result[0]; | ||
} else { | ||
throw new CouchError('multiple tokens with the same ID', 'fatal'); | ||
} | ||
}; | ||
|
||
exports.getTokens = async function getTokens(db, user) { | ||
return await nanoPromise.queryView(db, 'tokenByOwner', {key: user, include_docs: true}, {onlyDoc: true}); | ||
}; | ||
|
||
exports.destroyToken = async function destroyToken(db, tokenId, rev) { | ||
return await nanoPromise.destroyDocument(db, tokenId, rev); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict'; | ||
|
||
const data = require('./data/data'); | ||
|
||
describe.only('token methods', function () { | ||
before(data); | ||
it('user should be able to create and get tokens', function () { | ||
return Promise.all([couch.createEntryToken('a@a.com', 'A'), couch.createEntryToken('a@a.com', 'B')]) | ||
.then(tokens => { | ||
tokens[0].$id.should.not.equal(tokens[1].$id); | ||
const token = tokens[0]; | ||
token.$type.should.equal('token'); | ||
token.$kind.should.equal('entry'); | ||
token.$id.length.should.equal(32); | ||
token.$owner.should.equal('a@a.com'); | ||
token.uuid.should.equal('A'); | ||
token.rights.should.eql(['read']); | ||
return couch.getToken(token.$id).then(gotToken => { | ||
gotToken.$id.should.equal(token.$id); | ||
return couch.getTokens().then(tokens => { | ||
tokens.length.should.equal(2); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
it('user should be able to create and destroy tokens', function () { | ||
return couch.createEntryToken('a@a.com', 'A') | ||
.then(token => { | ||
return couch.getToken(token.$id).then(gotToken => { | ||
gotToken.$id.should.equal(token.$id); | ||
return couch.deleteToken('a@a.com', token.$id).then(() => { | ||
return couch.getToken(token.$id).should.be.rejected(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
it('user should not be able to create a token without write right', function () { | ||
return couch.createEntryToken('a@a.com', 'C').should.be.rejected(); | ||
}); | ||
|
||
|
||
}); |