Skip to content

Commit

Permalink
Merge branch 'hotfix/v0.0.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
genaromadrid committed May 14, 2016
2 parents bc5e584 + 459165f commit 2173016
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 72 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Build Status][travis-image]][travis-url]
[![Coverage Status][coveralls-image]][coveralls-url]

Version: 0.0.3
Version: 0.0.4

Electronic signed document XML Protocol for Node & Browser

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xml-fiesta",
"version": "0.0.3",
"version": "0.0.4",
"authors": [
"Genaro Madrid <genmadrid@gmail.com>"
],
Expand Down
63 changes: 26 additions & 37 deletions dist/xml-fiesta.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,21 +244,17 @@
parseString = require('xml2js').parseString;

Document = (function() {
var VERSION, _pdf, _signers, add_signer, signer_exist;
var VERSION;

VERSION = '0.0.1';

_pdf = null;

_signers = null;

function Document(pdf, options) {
var defaultOpts, hash;
var defaultOpts, doc, hash;
if (!pdf) {
throw new Error('pdf is required');
}
_signers = [];
_pdf = pdf;
this.pdf_content = pdf;
this.signers = [];
defaultOpts = {
version: VERSION,
signers: []
Expand All @@ -272,51 +268,48 @@
});
this.originalHash = hash.digestHex(this.pdf('hex'));
if (options.signers.length > 0) {
doc = this;
options.signers.forEach(function(el) {
return add_signer(el);
return doc.add_signer(el);
});
}
}

Document.prototype.pdfBuffer = function() {
if (!_pdf) {
if (!this.pdf_content) {
return null;
}
return new Buffer(_pdf, 'base64');
return new Buffer(this.pdf_content, 'base64');
};

Document.prototype.pdf = function(format) {
if (!_pdf) {
if (!this.pdf_content) {
return null;
}
if (!format) {
return common.b64toAscii(_pdf);
return common.b64toAscii(this.pdf_content);
}
if (format === 'hex') {
return common.b64toHex(_pdf);
return common.b64toHex(this.pdf_content);
}
if (format === 'base64') {
return _pdf;
return this.pdf_content;
}
throw new errors.ArgumentError("unknown format " + format);
};

Document.prototype.signers = function() {
return _signers;
};

add_signer = function(signer) {
Document.prototype.add_signer = function(signer) {
if (!signer.cer || !signer.signature || !signer.signedAt) {
throw new errors.InvalidSignerError('signer must contain cer, signature and signedAt');
}
if (signer_exist(signer)) {
if (this.signer_exist(signer)) {
throw new errors.DuplicateSignersError('signer already exists');
}
return _signers.push(signer);
return this.signers.push(signer);
};

Document.prototype.signatures = function() {
return _signers.map(function(signer) {
return this.signers.map(function(signer) {
return new Signature(signer.cer, signer.signature, signer.signedAt);
});
};
Expand All @@ -336,9 +329,9 @@
return valid;
};

signer_exist = function(signer) {
Document.prototype.signer_exist = function(signer) {
var selected;
selected = _signers.filter(function(s) {
selected = this.signers.filter(function(s) {
return s.email === signer.email || s.cer === signer.cer || s.signature === signer.signature;
});
return selected.length > 0;
Expand All @@ -360,9 +353,8 @@
pdfAttrs = result.electronicDocument.pdf[0].$;
signers = result.electronicDocument.signers;
parsedSigners = [];
signers.forEach(function(signer) {
signers[0].signer.forEach(function(signer) {
var attrs;
signer = signer.signer[0];
attrs = signer.$;
return parsedSigners.push({
email: attrs.email,
Expand Down Expand Up @@ -450,11 +442,8 @@
common = require('./common');

Signature = (function() {
var _signature;

_signature = null;

function Signature(cer, signature, signedAt, email) {
this.signature = signature;
this.signedAt = signedAt;
this.email = email;
if (!this.signedAt) {
Expand All @@ -463,7 +452,6 @@
if (!cer) {
throw new errors.ArgumentError('Signature must have cer');
}
_signature = signature;
this.certificate = new Certificate(false, cer);
if (this.email == null) {
this.email = this.certificate.email();
Expand All @@ -477,10 +465,10 @@

Signature.prototype.sig = function(format) {
if (format === 'hex' || !format) {
return _signature;
return this.signature;
}
if (format === 'base64') {
return common.hextoB64(_signature);
return common.hextoB64(this.signature);
}
throw new errors.ArgumentError("unknown format " + format);
};
Expand All @@ -489,7 +477,7 @@
if (!hash) {
throw new errors.ArgumentError('hash is required');
}
return this.certificate.verifyString(hash, _signature);
return this.certificate.verifyString(hash, this.signature);
};

return Signature;
Expand All @@ -505,12 +493,13 @@
module.exports = {
Certificate: require('./document'),
Document: require('./document'),
Signature: require('./document')
Signature: require('./document'),
errors: require('./errors')
};

}).call(this);

},{"./document":3}],7:[function(require,module,exports){
},{"./document":3,"./errors":4}],7:[function(require,module,exports){

},{}],8:[function(require,module,exports){
arguments[4][7][0].apply(exports,arguments)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xml-fiesta",
"version": "0.0.3",
"version": "0.0.4",
"description": "Electronic signed document XML Protocol for Node & Browser",
"main": "lib",
"scripts": {
Expand Down
8 changes: 4 additions & 4 deletions spec/documentSpec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe 'Document', ->
describe 'without signers', ->
it 'should be OK', ->
doc = new Document('cGRmLWJhc2U2NC1jb250ZW50')
expect(doc.signers()).to.be.empty()
expect(doc.signers).to.be.empty()

describe 'without cer', ->
it 'should raise error', (done) ->
Expand Down Expand Up @@ -94,10 +94,10 @@ describe 'Document', ->

describe '.signers', ->
it 'should be defined', ->
expect(doc.signers).to.be.a('function')
expect(doc.signers).to.be.an('array')

it 'should have signers', ->
expect(doc.signers()[0].email).to.be signers[0].email
expect(doc.signers[0].email).to.be signers[0].email

describe '.signatures', ->
it 'should be defined', ->
Expand Down Expand Up @@ -126,7 +126,7 @@ describe 'Document', ->
done()

it 'should parse the xml', ->
xmlSigners = doc.signers()
xmlSigners = doc.signers
signer = xmlSigners[0]

expect(doc).to.be.a Document
Expand Down
38 changes: 17 additions & 21 deletions src/document.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ parseString = require('xml2js').parseString

class Document
VERSION = '0.0.1'
_pdf = null
_signers = null

constructor: (pdf, options) ->
throw new Error('pdf is required') unless pdf
_signers = []
_pdf = pdf
@pdf_content = pdf
@signers = []
defaultOpts =
version: VERSION
signers: []
Expand All @@ -28,35 +26,34 @@ class Document
@originalHash = hash.digestHex(@pdf('hex'))

if options.signers.length > 0
doc = this
options.signers.forEach (el) ->
add_signer(el)
doc.add_signer(el)

pdfBuffer: ->
return null unless _pdf
new Buffer(_pdf, 'base64')
return null unless @pdf_content
new Buffer(@pdf_content, 'base64')

pdf: (format) ->
return null unless _pdf
return common.b64toAscii(_pdf) unless format
return common.b64toHex(_pdf) if format is 'hex'
return _pdf if format is 'base64'
return null unless @pdf_content
return common.b64toAscii(@pdf_content) unless format
return common.b64toHex(@pdf_content) if format is 'hex'
return @pdf_content if format is 'base64'
throw new errors.ArgumentError "unknown format #{format}"

signers: -> _signers

add_signer = (signer) ->
add_signer: (signer) ->
if !signer.cer || !signer.signature || !signer.signedAt
throw new errors.InvalidSignerError(
'signer must contain cer, signature and signedAt'
)
if signer_exist(signer)
if @signer_exist(signer)
throw new errors.DuplicateSignersError(
'signer already exists'
)
_signers.push(signer)
@signers.push(signer)

signatures: ->
_signers.map (signer) ->
@signers.map (signer) ->
new Signature(
signer.cer,
signer.signature,
Expand All @@ -71,8 +68,8 @@ class Document
valid = false if valid && !signature.valid(oHash)
valid

signer_exist = (signer) ->
selected = _signers.filter (s) ->
signer_exist: (signer) ->
selected = @signers.filter (s) ->
s.email == signer.email ||
s.cer == signer.cer ||
s.signature == signer.signature
Expand All @@ -88,8 +85,7 @@ class Document
pdfAttrs = result.electronicDocument.pdf[0].$
signers = result.electronicDocument.signers
parsedSigners = []
signers.forEach (signer) ->
signer = signer.signer[0]
signers[0].signer.forEach (signer) ->
attrs = signer.$
parsedSigners.push({
email: attrs.email
Expand Down
11 changes: 4 additions & 7 deletions src/signature.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ errors = require './errors'
common = require './common'

class Signature
_signature = null

constructor: (cer, signature, @signedAt, @email) ->
constructor: (cer, @signature, @signedAt, @email) ->
throw new errors.ArgumentError(
'Signature must have signedAt'
) unless @signedAt
throw new errors.ArgumentError(
'Signature must have cer'
) unless cer

_signature = signature
@certificate = new Certificate(false, cer)
@email ?= @certificate.email()

Expand All @@ -24,12 +21,12 @@ class Signature
}

sig: (format) ->
return _signature if format is 'hex' or !format
return common.hextoB64(_signature) if format is 'base64'
return @signature if format is 'hex' or !format
return common.hextoB64(@signature) if format is 'base64'
throw new errors.ArgumentError "unknown format #{format}"

valid: (hash) ->
throw new errors.ArgumentError 'hash is required' unless hash
@certificate.verifyString(hash, _signature)
@certificate.verifyString(hash, @signature)

module.exports = Signature
1 change: 1 addition & 0 deletions src/xml-fiesta.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ module.exports =
Certificate: require './document'
Document: require './document'
Signature: require './document'
errors: require './errors'

0 comments on commit 2173016

Please sign in to comment.