Skip to content
This repository has been archived by the owner on Sep 12, 2019. It is now read-only.

Commit

Permalink
Updated generateTotpUri(), tests, gulpfile
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekn committed Oct 6, 2014
1 parent becc449 commit 7225ffc
Show file tree
Hide file tree
Showing 8 changed files with 19,786 additions and 18 deletions.
10 changes: 9 additions & 1 deletion README.md
@@ -1,3 +1,6 @@
> # :warning: Alpha version. Don't use in production.

stellar-wallet-js-sdk
=====================

Expand Down Expand Up @@ -101,7 +104,12 @@ a user.

```js
var key = StellarWallet.util.generateTotpKey();
var uri = StellarWallet.util.generateTotpUri(key);
var uri = StellarWallet.util.generateTotpUri(key, {
// Your organization name
issuer: 'Stellar Development Foundation',
// Account name
accountName: 'bob@stellar.org'
});
```

### Wallet object
Expand Down
8 changes: 7 additions & 1 deletion bower.json
Expand Up @@ -4,5 +4,11 @@
"private": false,
"main": "build/stellar-wallet.js",
"dependencies": {},
"devDependencies": {}
"devDependencies": {},
"ignore": [
"/lib",
"/test",
"/index.js",
"/gulpfile.js"
]
}
19,719 changes: 19,711 additions & 8 deletions build/stellar-wallet.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions build/stellar-wallet.min.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion gulpfile.js
Expand Up @@ -17,8 +17,10 @@ gulp.task('build-browser', [], function() {
library: 'StellarWallet'
}
}))
.pipe(plugins.uglify())
.pipe(plugins.rename('stellar-wallet.js'))
.pipe(gulp.dest('build'))
.pipe(plugins.uglify())
.pipe(plugins.rename('stellar-wallet.min.js'))
.pipe(gulp.dest('build'));
});

Expand Down
23 changes: 18 additions & 5 deletions lib/util/totp.js
@@ -1,17 +1,30 @@
var _ = require('lodash');
var errors = require('../errors')
var nacl = require('tweetnacl');
var base32 = require('thirty-two');

function generateRandomTotpKey() {
var key = nacl.randomBytes(10);
// Google Authenticator doesn't like ='s in the end
return base32.encode(key).toString().replace(/=/g,'');

}

function generateTotpUri(key) {
return 'otpauth://totp/Stellar:'+
'username'+
'/stellar-client?secret='+key+'&issuer=Stellar+Development+Foundation';
function generateTotpUri(key, meta) {
var throwMissingField = function(field) {
var e = new errors.MissingField();
e.field = field;
throw e;
};

var fields = ['issuer', 'accountName'];
_.each(fields, function(field) {
if (!_.isString(meta[field])) {
throwMissingField(field);
}
});

meta = _.mapValues(meta, encodeURI);
return 'otpauth://totp/'+meta.issuer+':'+meta.accountName+'?secret='+key+'&issuer='+meta.issuer;
}

module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions test/util/crypto.js
Expand Up @@ -4,7 +4,7 @@ var expect = require('chai').expect;
var crypto = require('../../lib/util/crypto');
var sjcl = require('../../lib/util/sjcl');

describe('crypto', function () {
describe('util/crypto', function () {
it('should correctly encrypt/decrypt data', function (done) {
var key = sjcl.random.randomWords(8);
var secret = 'this is secret data';
Expand All @@ -13,4 +13,4 @@ describe('crypto', function () {
expect(decrypted).to.be.equal(secret);
done();
});
});
});
28 changes: 28 additions & 0 deletions test/util/totp.js
@@ -0,0 +1,28 @@
'use strict';

var expect = require('chai').expect;
var totp = require('../../lib/util/totp');
var base32 = require('thirty-two');

describe('util/totp', function () {
it('should generate 10 bytes base32 encoded without padding', function (done) {
var randomKey = totp.generateRandomTotpKey();
expect(randomKey.slice(-1)).to.not.be.equal('='); // no padding
var decodedKey = base32.decode(randomKey);
expect(decodedKey.length).to.be.equal(10);
done();
});
});

describe('util/totp', function () {
it('should generate otpauth uri', function (done) {
var key = totp.generateRandomTotpKey();
var expected = 'otpauth://totp/Stellar%20Development%20Foundation:bob@stellar.org?secret='+key+'&issuer=Stellar%20Development%20Foundation';
var uri = totp.generateTotpUri(key, {
issuer: 'Stellar Development Foundation',
accountName: 'bob@stellar.org'
});
expect(uri).to.be.equal(expected);
done();
});
});

0 comments on commit 7225ffc

Please sign in to comment.