-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pulled functionality out into unique files
- Loading branch information
Showing
5 changed files
with
237 additions
and
211 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/*global module, process*/ | ||
const Buffer = require('buffer').Buffer; | ||
const Stream = require('stream'); | ||
const util = require('util'); | ||
|
||
function DataStream(data) { | ||
this.buffer = Buffer(data||0); | ||
this.writable = true; | ||
this.readable = true; | ||
if (!data) | ||
return this; | ||
if (typeof data.pipe === 'function') | ||
data.pipe(this); | ||
else if (data.length) { | ||
this.writable = false; | ||
process.nextTick(function () { | ||
this.buffer = data; | ||
this.emit('end', data); | ||
this.readable = false; | ||
this.emit('close'); | ||
}.bind(this)); | ||
} | ||
} | ||
util.inherits(DataStream, Stream); | ||
|
||
DataStream.prototype.write = function write(data) { | ||
this.buffer = Buffer.concat([this.buffer, Buffer(data)]); | ||
this.emit('data', data); | ||
}; | ||
|
||
DataStream.prototype.end = function end(data) { | ||
if (data) | ||
this.write(data); | ||
this.emit('end', data); | ||
this.emit('close'); | ||
this.writable = false; | ||
this.readable = false; | ||
}; | ||
|
||
module.exports = DataStream; |
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,59 @@ | ||
/*global module*/ | ||
const base64url = require('base64url'); | ||
const DataStream = require('./data-stream'); | ||
const jwa = require('jwa'); | ||
const Stream = require('stream'); | ||
const toString = require('./tostring'); | ||
const util = require('util'); | ||
|
||
function jwsSecuredInput(header, payload) { | ||
const encodedHeader = base64url(toString(header), 'binary'); | ||
const encodedPayload = base64url(toString(payload), 'binary'); | ||
return util.format('%s.%s', encodedHeader, encodedPayload); | ||
} | ||
|
||
function jwsSign(opts) { | ||
const header = opts.header; | ||
const payload = opts.payload; | ||
const secretOrKey = opts.secret || opts.privateKey; | ||
const algo = jwa(header.alg); | ||
const securedInput = jwsSecuredInput(header, payload); | ||
const signature = algo.sign(securedInput, secretOrKey); | ||
return util.format('%s.%s', securedInput, signature); | ||
} | ||
|
||
function SignStream(opts) { | ||
const secret = opts.secret||opts.privateKey||opts.key; | ||
const secretStream = new DataStream(secret); | ||
this.readable = true; | ||
this.header = opts.header; | ||
this.secret = this.privateKey = this.key = secretStream; | ||
this.payload = new DataStream(opts.payload); | ||
this.secret.once('close', function () { | ||
if (!this.payload.writable && this.readable) | ||
this.sign(); | ||
}.bind(this)); | ||
|
||
this.payload.once('close', function () { | ||
if (!this.secret.writable && this.readable) | ||
this.sign(); | ||
}.bind(this)); | ||
} | ||
util.inherits(SignStream, Stream); | ||
|
||
SignStream.prototype.sign = function sign() { | ||
const signature = jwsSign({ | ||
header: this.header, | ||
payload: this.payload.buffer, | ||
secret: this.secret.buffer | ||
}); | ||
this.emit('done', signature); | ||
this.emit('data', signature); | ||
this.emit('end'); | ||
this.readable = false; | ||
return signature; | ||
}; | ||
|
||
SignStream.sign = jwsSign; | ||
|
||
module.exports = SignStream; |
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,10 @@ | ||
/*global module*/ | ||
const Buffer = require('buffer').Buffer; | ||
|
||
module.exports = function toString(obj) { | ||
if (typeof obj === 'string') | ||
return obj; | ||
if (typeof obj === 'number' || Buffer.isBuffer(obj)) | ||
return obj.toString(); | ||
return JSON.stringify(obj); | ||
}; |
Oops, something went wrong.