Skip to content

Commit

Permalink
Merge pull request #36 from florianheinemann/number-token
Browse files Browse the repository at this point in the history
ADD options.numberToken
  • Loading branch information
florianheinemann committed Nov 8, 2014
2 parents b6cbba9 + e6d02ea commit a1154a2
Show file tree
Hide file tree
Showing 8 changed files with 334 additions and 15 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,14 @@
# 1.0.5 (2014-11-07)

Bugfixes:
- N/A

Features:
- ADD addDelivery options.numberToken to generate number-based tokens

Documentation:
- Small update to include numberToken

# 1.0.4 (2014-10-29)

Bugfixes:
Expand Down
215 changes: 209 additions & 6 deletions docs/Passwordless.html
Expand Up @@ -129,6 +129,174 @@ <h3 class="subsection-title">Methods</h3>

<dl>

<dt>
<h4 class="name" id="_generateNumberToken"><span class="type-signature">&lt;private> </span>_generateNumberToken<span class="signature">(max)</span><span class="type-signature"> &rarr; {Number}</span></h4>


</dt>
<dd>


<div class="description">
Generates a strong random number between 0 and a maximum value. The
maximum value cannot exceed 2^32
</div>







<h5>Parameters:</h5>


<table class="params">
<thead>
<tr>

<th>Name</th>


<th>Type</th>





<th class="last">Description</th>
</tr>
</thead>

<tbody>


<tr>

<td class="name"><code>max</code></td>


<td class="type">


<span class="param-type">Number</span>



</td>





<td class="description last">Maximum number to be generated</td>
</tr>


</tbody>
</table>



<dl class="details">



















<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="passwordless.js.html">passwordless.js</a>, <a href="passwordless.js.html#line615">line 615</a>
</li></ul></dd>







</dl>









<h5>Throws:</h5>



<dl>
<dt>
<div class="param-desc">
Will throw an error if there is no sufficient
entropy accumulated
</div>
</dt>
<dt>
<dl>
<dt>
Type
</dt>
<dd>

<span class="param-type">Error</span>


</dd>
</dl>
</dt>
</dl>





<h5>Returns:</h5>


<div class="param-desc">
Random number between 0 and max
</div>



<dl>
<dt>
Type
</dt>
<dd>

<span class="param-type">Number</span>


</dd>
</dl>




</dd>



<dt>
<h4 class="name" id="_generateToken"><span class="type-signature">&lt;private> </span>_generateToken<span class="signature">(randomBytes)</span><span class="type-signature"> &rarr; {function}</span></h4>

Expand Down Expand Up @@ -219,7 +387,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="passwordless.js.html">passwordless.js</a>, <a href="passwordless.js.html#line588">line 588</a>
<a href="passwordless.js.html">passwordless.js</a>, <a href="passwordless.js.html#line598">line 598</a>
</li></ul></dd>


Expand Down Expand Up @@ -456,7 +624,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="passwordless.js.html">passwordless.js</a>, <a href="passwordless.js.html#line566">line 566</a>
<a href="passwordless.js.html">passwordless.js</a>, <a href="passwordless.js.html#line576">line 576</a>
</li></ul></dd>


Expand Down Expand Up @@ -573,7 +741,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="passwordless.js.html">passwordless.js</a>, <a href="passwordless.js.html#line549">line 549</a>
<a href="passwordless.js.html">passwordless.js</a>, <a href="passwordless.js.html#line559">line 559</a>
</li></ul></dd>


Expand Down Expand Up @@ -1301,7 +1469,42 @@ <h6>Properties</h6>


<td class="description last">The algorithm used to generate a token.
Should simply return the token in sync mode (default: UUID v4)</td>
Function shall return the token in sync mode (default: Base58 token)</td>
</tr>



<tr>

<td class="name"><code>numberToken.max</code></td>


<td class="type">


<span class="param-type">Number</span>



</td>


<td class="attributes">

&lt;optional><br>





</td>




<td class="description last">Overwrites the default token generator
by a random number generator which generates numbers between 0 and max. Cannot be used
together with options.tokenAlgorithm</td>
</tr>


Expand Down Expand Up @@ -1338,7 +1541,7 @@ <h6>Properties</h6>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="passwordless.js.html">passwordless.js</a>, <a href="passwordless.js.html#line506">line 506</a>
<a href="passwordless.js.html">passwordless.js</a>, <a href="passwordless.js.html#line513">line 513</a>
</li></ul></dd>


Expand Down Expand Up @@ -2914,7 +3117,7 @@ <h2><a href="index.html">Index</a></h2><h3>Classes</h3><ul><li><a href="Password
<br clear="both">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a> on Wed Oct 29 2014 22:42:11 GMT-0400 (EDT)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a> on Fri Nov 07 2014 20:58:44 GMT-0500 (EST)
</footer>

<script> prettyPrint(); </script>
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Expand Up @@ -54,7 +54,7 @@ <h2><a href="index.html">Index</a></h2><h3>Classes</h3><ul><li><a href="Password
<br clear="both">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a> on Wed Oct 29 2014 22:42:10 GMT-0400 (EDT)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a> on Fri Nov 07 2014 20:58:44 GMT-0500 (EST)
</footer>

<script> prettyPrint(); </script>
Expand Down
32 changes: 28 additions & 4 deletions docs/passwordless.js.html
Expand Up @@ -459,7 +459,11 @@ <h1 class="page-title">Source: passwordless.js</h1>
} else if(uid) {
var token;
try {
token = (deliveryMethod.options.tokenAlgorithm || self._generateToken())();
if(deliveryMethod.options.numberToken && deliveryMethod.options.numberToken.max) {
token = self._generateNumberToken(deliveryMethod.options.numberToken.max);
} else {
token = (deliveryMethod.options.tokenAlgorithm || self._generateToken())();
}
} catch(err) {
next(new Error('Error while generating a token: ' + err));
}
Expand Down Expand Up @@ -528,7 +532,10 @@ <h1 class="page-title">Source: passwordless.js</h1>
* @param {Number} [options.ttl] - Duration in ms that the token shall be valid
* (example: 1000*60*30, default: 1 hour)
* @param {function()} [options.tokenAlgorithm] - The algorithm used to generate a token.
* Should simply return the token in sync mode (default: UUID v4)
* Function shall return the token in sync mode (default: Base58 token)
* @param {Number} [options.numberToken.max] - Overwrites the default token generator
* by a random number generator which generates numbers between 0 and max. Cannot be used
* together with options.tokenAlgorithm
*/
Passwordless.prototype.addDelivery = function(name, sendToken, options) {
// So that add can be called with (sendToken [, options])
Expand All @@ -545,8 +552,11 @@ <h1 class="page-title">Source: passwordless.js</h1>
|| (name && typeof name !== 'string')) {
throw new Error('Passwordless.addDelivery called with wrong parameters');
} else if((options.ttl && typeof options.ttl !== 'number') ||
(options.tokenAlgorithm && typeof options.tokenAlgorithm !== 'function')) {
(options.tokenAlgorithm && typeof options.tokenAlgorithm !== 'function') ||
(options.numberToken && (!options.numberToken.max || typeof options.numberToken.max !== 'number'))) {
throw new Error('One of the provided options is of the wrong format');
} else if(options.tokenAlgorithm && options.numberToken) {
throw new Error('options.tokenAlgorithm cannot be used together with options.numberToken');
} else if(this._defaultDelivery) {
throw new Error('Only one default delivery method shall be defined and not be mixed up with named methods. Use named delivery methods instead')
} else if(defaultUsage && Object.keys(this._deliveryMethods).length > 0) {
Expand Down Expand Up @@ -620,6 +630,20 @@ <h1 class="page-title">Source: passwordless.js</h1>
}
};

/**
* Generates a strong random number between 0 and a maximum value. The
* maximum value cannot exceed 2^32
* @param {Number} max - Maximum number to be generated
* @return {Number} Random number between 0 and max
* @throws {Error} Will throw an error if there is no sufficient
* entropy accumulated
* @private
*/
Passwordless.prototype._generateNumberToken = function(max) {
var buf = crypto.randomBytes(4);
return Math.floor(buf.readUInt32BE(0)%max);
};

module.exports = Passwordless;
</code></pre>
</article>
Expand All @@ -637,7 +661,7 @@ <h2><a href="index.html">Index</a></h2><h3>Classes</h3><ul><li><a href="Password
<br clear="both">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a> on Wed Oct 29 2014 22:42:10 GMT-0400 (EDT)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a> on Fri Nov 07 2014 20:58:44 GMT-0500 (EST)
</footer>

<script> prettyPrint(); </script>
Expand Down

0 comments on commit a1154a2

Please sign in to comment.